Skip to content

Commit

Permalink
Merge pull request #202 from Fuseit/fix-css-paths-with-params
Browse files Browse the repository at this point in the history
Put url parameters outside of erb on paths resolve
  • Loading branch information
SergeyKishenin authored Sep 14, 2016
2 parents 874142c + 98451ad commit 15a9d87
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
9 changes: 5 additions & 4 deletions lib/bower-rails/performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def resolve_asset_paths(root_directory = components_directory)
Dir["#{components_directory}/**/*.css"].each do |filename|
contents = File.read(filename) if FileTest.file?(filename)
# http://www.w3.org/TR/CSS2/syndata.html#uri
url_regex = /url\((?!\#)\s*['"]?(?![a-z]+:)([^'"\)]*)['"]?\s*\)/
url_regex = /url\((?!\#)\s*['"]?((?![a-z]+:)([^'"\)]*?)([?#][^'"\)]*)?)['"]?\s*\)/

# Resolve paths in CSS file if it contains a url
if contents =~ url_regex
Expand All @@ -127,11 +127,12 @@ def resolve_asset_paths(root_directory = components_directory)

# Replace relative paths in URLs with Rails asset_path helper
new_contents = contents.gsub(url_regex) do |match|
relative_path = $1
relative_path = $2
params = $3
image_path = directory_path.join(relative_path).cleanpath
puts "#{match} => #{image_path}"
puts "#{match} => #{image_path} #{params}"

"url(<%= asset_path '#{image_path}' %>)"
"url(<%= asset_path '#{image_path}' %>#{params})"
end

# Replace CSS with ERB CSS file with resolved asset paths
Expand Down
91 changes: 66 additions & 25 deletions spec/bower-rails/performer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
require 'pry'

describe BowerRails::Performer do

let(:performer) { BowerRails::Performer.new }
let(:main_files) { {} }
let(:exempt_list) { nil }
let(:root) { File.expand_path('../../..', __FILE__) }
let(:tmp_dir) { File.join root, "tmp" }
let(:files_dir) { File.join root, "spec", "files" }
let(:create_bower_file) { true }

context "remove_extra_files" do
let(:root) { File.expand_path('../../..', __FILE__) }
let(:create_bower_file) { true }
before do
# `rm -rf ./tmp` remove temporary directory
FileUtils.rm_rf("#{root}/tmp")

before do
# `rm -rf ./tmp` remove temporary directory
FileUtils.rm_rf("#{root}/tmp")
# setup temporary directory
FileUtils.mkdir("#{root}/tmp")
FileUtils.cp("#{root}/spec/files/Bowerfile", "#{root}/tmp/Bowerfile") if create_bower_file
FileUtils.cp("#{root}/spec/files/bower.json", "#{root}/tmp/bower.json")

# points `root_path` to temporary directory
allow(performer).to receive(:root_path) { "#{root}/tmp" }

# setup temporary directory
FileUtils.mkdir("#{root}/tmp")
FileUtils.cp("#{root}/spec/files/Bowerfile", "#{root}/tmp/Bowerfile") if create_bower_file
FileUtils.cp("#{root}/spec/files/bower.json", "#{root}/tmp/bower.json")
# trick BowerRails that system has bower installed
allow(performer).to receive(:find_command) { "bower" }

# sets main_files in DSL
allow_any_instance_of(BowerRails::Dsl).to receive(:main_files){ main_files }

Dir.chdir("#{root}/tmp")

# Stub exclude from clean setting
BowerRails.exclude_from_clean = exempt_list
end

describe "remove_extra_files" do
before do
FileUtils.mkdir_p("#{root}/tmp/vendor/assets/bower_components")

# creates bower library
Expand All @@ -38,20 +53,6 @@
f.write(%q({"name":"moment","main":["./moment.js", "./fonts/*"]}))
end

# points `root_path` to temporary directory
allow(performer).to receive(:root_path) { "#{root}/tmp" }

# trick BowerRails that system has bower installed
allow(performer).to receive(:find_command) { "bower" }

# sets main_files in DSL
allow_any_instance_of(BowerRails::Dsl).to receive(:main_files){ main_files }

Dir.chdir("#{root}/tmp")

# Stub exclude from clean setting
BowerRails.exclude_from_clean = exempt_list

performer.perform false do
remove_extra_files
end
Expand Down Expand Up @@ -138,4 +139,44 @@
end
end

describe "resolve_asset_paths" do
let(:target_path) { "#{tmp_dir}/vendor/assets/bower_components/foobar/style.css" }

before do
FileUtils.mkdir_p(File.dirname(target_path))
FileUtils.cp("#{files_dir}/style.css", target_path)

performer.perform false do
resolve_asset_paths
end
end

it "removes .css file" do
expect(File).to_not exist(target_path)
end

it "creates .css.erb file" do
expect(File).to exist("#{target_path}.erb")
end

describe "created .erb file" do
subject { File.read "#{target_path}.erb" }

it "includes proper asset paths" do
expect(subject).to include "<%= asset_path 'foobar.png' %>"
end

it "does not include improper asset paths" do
expect(subject).not_to match(/<%= asset_path 'foobar.png[?#]/)
end

it "pushes url params after the erb block" do
expect(subject).to include "<%= asset_path 'foobar.png' %>?"
end

it "pushes url anchor after the erb block" do
expect(subject).to include "<%= asset_path 'foobar.png' %>#"
end
end
end
end
16 changes: 16 additions & 0 deletions spec/files/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
body {
background: url('../foobar.png')
}

.param {
background: url("../foobar.png?foo=true")
}

.anchor {
background: url(../foobar.png#foo)
}

.param.anchor {
background: url("../foobar.png#foo?foo=true#foo")
}

0 comments on commit 15a9d87

Please sign in to comment.