Skip to content

Commit

Permalink
Merge pull request #485 from PikachuEXE/fix/protocol-relative-urls
Browse files Browse the repository at this point in the history
Fix protocol relative URLs amended accidentally
  • Loading branch information
rafaelfranca authored Nov 26, 2021
2 parents e7482e0 + 70e107b commit 4e0f168
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sprockets/rails/asset_url_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Sprockets
module Rails
# Rewrites urls in CSS files with the digested paths
class AssetUrlProcessor
REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>\.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
def self.call(input)
context = input[:environment].context_class.new(input)
data = input[:data].gsub(REGEX) do |_match|
Expand Down
6 changes: 6 additions & 0 deletions test/test_asset_url_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ def test_subdirectory
jquery_digest = 'c6910e1db4a5ed4905be728ab786471e81565f4a9d544734b199f3790de9f9a3'
assert_equal("background: url(/jquery/jquery-#{jquery_digest}.js);", 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)
assert_equal("background: url(//assets.example.com/assets/fontawesome-webfont-82ff0fe46a6f60e0ab3c4a9891a0ae0a1f7b7e84c625f55358379177a2dcb202.eot);", output[:data])
end
end

0 comments on commit 4e0f168

Please sign in to comment.