Skip to content

Commit

Permalink
Handle SVG mask URL with encoded anchor (#98)
Browse files Browse the repository at this point in the history
This patch ensures that CSS assets with inlined SVG containing a mask
URL with an HTML encoded anchor are properly handled by the
`CssAssetUrls` compiler.

This avoids printing warning message like:
```
Unable to resolve '%23c' for missing asset '%23c' in XYZ.css
```
  • Loading branch information
thibaudgg authored Jun 22, 2022
1 parent f01be80 commit 467f2a1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/propshaft/compilers/css_asset_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Propshaft::Compilers::CssAssetUrls
attr_reader :assembly

ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/
ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/

def initialize(assembly)
@assembly = assembly
Expand Down
5 changes: 5 additions & 0 deletions test/propshaft/compilers/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#rails"\); }/, compiled)
end

test "svg mask encoded anchor" do
compiled = compile_asset_with_content(%({ background: url("data:image/svg+xml;charset=utf-8,%3Csvg mask='url(%23MyMask)'%3E%3C/svg%3E"); }))
assert_match "{ background: url(\"data:image/svg+xml;charset=utf-8,%3Csvg mask='url(%23MyMask)'%3E%3C/svg%3E\"); }", compiled
end

test "non greedy anchors" do
compiled = compile_asset_with_content(%({ content: url(file.svg#demo) url(file.svg#demo); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\); }/, compiled)
Expand Down

0 comments on commit 467f2a1

Please sign in to comment.