diff --git a/lib/sprockets/rails/asset_url_processor.rb b/lib/sprockets/rails/asset_url_processor.rb index 7adcb9e9..87d82818 100644 --- a/lib/sprockets/rails/asset_url_processor.rb +++ b/lib/sprockets/rails/asset_url_processor.rb @@ -2,7 +2,7 @@ module Sprockets module Rails # Resolve assets referenced in CSS `url()` calls and replace them with the digested paths class AssetUrlProcessor - REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?\.\/)?(?[^"'\s)]+)\s*["']?\)/ + REGEX = /url\(\s*["']?(?!(?:\#|data:|http[s]?:))(?\.\/)?(?[^"'\s)]+)\s*["']?\)/ def self.call(input) context = input[:environment].context_class.new(input) data = input[:data].gsub(REGEX) do |_match| diff --git a/test/fixtures/data/logo.png b/test/fixtures/data/logo.png new file mode 100644 index 00000000..1ab162d4 Binary files /dev/null and b/test/fixtures/data/logo.png differ diff --git a/test/test_asset_url_processor.rb b/test/test_asset_url_processor.rb index 7b1bdac2..03d87aa1 100644 --- a/test/test_asset_url_processor.rb +++ b/test/test_asset_url_processor.rb @@ -62,6 +62,18 @@ def test_subdirectory assert_equal("background: url(/jquery/jquery-#{jquery_digest}.js);", output[:data]) end + def test_data_subdirectory + input = { environment: @env, data: "background: url('data/logo.png');", filename: 'url2.css', metadata: {} } + output = Sprockets::Rails::AssetUrlProcessor.call(input) + assert_equal("background: url(/data/logo-#{@logo_digest}.png);", output[:data]) + end + + def test_protocol_data + input = { environment: @env, data: "background: url('data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjMwMCIgd2lkdGg9IjMwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBkYXRhLW5hbWU9IkxheWVyIDEiIHZpZXdCb3g9IjAgMCA1MCA1MCI+PHBhdGggZD0iTTQ4LjcxIDQyLjkxTDM0LjA4IDI4LjI5IDQ0LjMzIDE4YTEgMSAwIDAwLS4zMy0xLjYxTDIuMzUgMS4wNmExIDEgMCAwMC0xLjI5IDEuMjlMMTYuMzkgNDRhMSAxIDAgMDAxLjY1LjM2bDEwLjI1LTEwLjI4IDE0LjYyIDE0LjYzYTEgMSAwIDAwMS40MSAwbDQuMzgtNC4zOGExIDEgMCAwMC4wMS0xLjQyem0tNS4wOSAzLjY3TDI5IDMyYTEgMSAwIDAwLTEuNDEgMGwtOS44NSA5Ljg1TDMuNjkgMy42OWwzOC4xMiAxNEwzMiAyNy41OEExIDEgMCAwMDMyIDI5bDE0LjU5IDE0LjYyeiIvPjwvc3ZnPg==');", filename: 'url2.css', metadata: {} } + output = Sprockets::Rails::AssetUrlProcessor.call(input) + assert_equal(input[:data], output[:data]) + end + def test_protocol_relative_paths input = { environment: @env, data: "background: url(//assets.example.com/assets/fontawesome-webfont-82ff0fe46a6f60e0ab3c4a9891a0ae0a1f7b7e84c625f55358379177a2dcb202.eot);", filename: 'url2.css', metadata: {} } output = Sprockets::Rails::AssetUrlProcessor.call(input)