From f17761df637984004d17cab178639c14d8c737ee Mon Sep 17 00:00:00 2001 From: Breno Gazzola Date: Thu, 28 Nov 2024 16:13:58 -0300 Subject: [PATCH] Fix CSS Asset Compiler to work with files started with data and http --- lib/propshaft/compiler/css_asset_urls.rb | 2 +- .../assets/vendor/foobar/source/database.jpg | Bin 0 -> 375 bytes .../assets/vendor/foobar/source/http-diagram.jpg | Bin 0 -> 375 bytes test/propshaft/compiler/css_asset_urls_test.rb | 6 ++++++ 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/assets/vendor/foobar/source/database.jpg create mode 100644 test/fixtures/assets/vendor/foobar/source/http-diagram.jpg diff --git a/lib/propshaft/compiler/css_asset_urls.rb b/lib/propshaft/compiler/css_asset_urls.rb index 5e9a737..c2c68c0 100644 --- a/lib/propshaft/compiler/css_asset_urls.rb +++ b/lib/propshaft/compiler/css_asset_urls.rb @@ -3,7 +3,7 @@ require "propshaft/compiler" class Propshaft::Compiler::CssAssetUrls < Propshaft::Compiler - ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/ + ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data:|http:|https:|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/ def compile(asset, input) input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(asset.logical_path.dirname, $1), asset.logical_path, $2, $1 } diff --git a/test/fixtures/assets/vendor/foobar/source/database.jpg b/test/fixtures/assets/vendor/foobar/source/database.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5aa9c1eea3024b71bdcb955401d3e6c4a766afa3 GIT binary patch literal 375 zcmex=`F8!xadCO+zw40cr=MJ%c>R X1VMBK3=k_AfeOG@;84H}QE(Fgm_9V% literal 0 HcmV?d00001 diff --git a/test/fixtures/assets/vendor/foobar/source/http-diagram.jpg b/test/fixtures/assets/vendor/foobar/source/http-diagram.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5aa9c1eea3024b71bdcb955401d3e6c4a766afa3 GIT binary patch literal 375 zcmex=`F8!xadCO+zw40cr=MJ%c>R X1VMBK3=k_AfeOG@;84H}QE(Fgm_9V% literal 0 HcmV?d00001 diff --git a/test/propshaft/compiler/css_asset_urls_test.rb b/test/propshaft/compiler/css_asset_urls_test.rb index 3b672ad..41e6980 100644 --- a/test/propshaft/compiler/css_asset_urls_test.rb +++ b/test/propshaft/compiler/css_asset_urls_test.rb @@ -76,6 +76,9 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase test "url" do compiled = compile_asset_with_content(%({ background: url('https://rubyonrails.org/images/rails-logo.svg'); })) assert_match "{ background: url('https://rubyonrails.org/images/rails-logo.svg'); }", compiled + + compiled = compile_asset_with_content(%({ background: url(http-diagram.jpg); })) + assert_match(/{ background: url\("\/assets\/foobar\/source\/http-diagram-[a-z0-9]{8}.jpg"\); }/, compiled) end test "relative protocol url" do @@ -86,6 +89,9 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase test "data" do compiled = compile_asset_with_content(%({ background: url(data:image/png;base64,iRxVB0); })) assert_match "{ background: url(data:image/png;base64,iRxVB0); }", compiled + + compiled = compile_asset_with_content(%({ background: url(database.jpg); })) + assert_match(/{ background: url\("\/assets\/foobar\/source\/database-[a-z0-9]{8}.jpg"\); }/, compiled) end test "anchor" do