Skip to content

Commit

Permalink
Fix CSS Asset Compiler to work with files started with data and http
Browse files Browse the repository at this point in the history
  • Loading branch information
brenogazzola committed Nov 28, 2024
1 parent 6e80584 commit f17761d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/propshaft/compiler/css_asset_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/propshaft/compiler/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f17761d

Please sign in to comment.