-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process css files so that they get digested paths for asset files (#476)
* Process css files so that they get digested paths for asset files This is required so that we can use cssbundling-rails and reference images that will receive digested paths * Style * $1 is calculated before calling #gsub Co-authored-by: David Heinemeier Hansson <david@basecamp.com>
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Sprockets | ||
module Rails | ||
# Rewrites urls in CSS files with the digested paths | ||
class AssetUrlProcessor | ||
REGEX = /url\(\s*["']?(?!(?:\#|data|http))([^"'\s)]+)\s*["']?\)/ | ||
|
||
def self.call(input) | ||
context = input[:environment].context_class.new(input) | ||
data = input[:data].gsub(REGEX) { |_match| "url(#{context.asset_path($1)})" } | ||
|
||
{ data: data } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'minitest/autorun' | ||
require 'sprockets/railtie' | ||
|
||
|
||
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | ||
class TestAssetUrlProcessor < Minitest::Test | ||
def setup | ||
@env = Sprockets::Environment.new | ||
@env.context_class.class_eval do | ||
def asset_path(path, options = {}) | ||
'image-hexcodegoeshere.png' | ||
end | ||
end | ||
end | ||
|
||
def test_basic | ||
input = { environment: @env, data: 'background: url(image.png);', filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "background: url(image-hexcodegoeshere.png);" }, output) | ||
end | ||
|
||
def test_spaces | ||
input = { environment: @env, data: 'background: url( image.png );', filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "background: url(image-hexcodegoeshere.png);" }, output) | ||
end | ||
|
||
def test_single_quote | ||
input = { environment: @env, data: "background: url('image.png');", filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "background: url(image-hexcodegoeshere.png);" }, output) | ||
end | ||
|
||
def test_double_quote | ||
input = { environment: @env, data: 'background: url("image.png");', filename: 'url2.css', metadata: {} } | ||
output = Sprockets::Rails::AssetUrlProcessor.call(input) | ||
assert_equal({ data: "background: url(image-hexcodegoeshere.png);" }, output) | ||
end | ||
end |
ccf8411
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This broke relative URLs. See #478