assets_booster 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/assets_booster/merger/css.rb +5 -32
- data/lib/assets_booster/mixin/css.rb +38 -0
- data/lib/assets_booster/mixin/url.rb +21 -0
- data/lib/assets_booster/package/stylesheet.rb +16 -1
- data/lib/assets_booster/version.rb +1 -1
- data/spec/merger/css_spec.rb +0 -82
- data/spec/mixin/css_spec.rb +81 -0
- data/spec/mixin/url_spec.rb +60 -0
- data/spec/package/stylesheet_spec.rb +32 -0
- metadata +6 -2
@@ -1,7 +1,10 @@
|
|
1
|
+
require 'assets_booster/mixin/css'
|
1
2
|
require 'assets_booster/merger/base'
|
2
3
|
module AssetsBooster
|
3
4
|
module Merger
|
4
5
|
class CSS < Base
|
6
|
+
include AssetsBooster::Mixin::Css
|
7
|
+
|
5
8
|
def name
|
6
9
|
"CSS Merger"
|
7
10
|
end
|
@@ -10,7 +13,7 @@ module AssetsBooster
|
|
10
13
|
target_folder = dirname(target)
|
11
14
|
code = assets.inject("") do |code, asset|
|
12
15
|
source_folder = dirname(asset[:source])
|
13
|
-
asset[:css]=
|
16
|
+
asset[:css]= adjust_relative_urls(asset[:css], source_folder, target_folder)
|
14
17
|
code << asset[:css]
|
15
18
|
code << "\n"
|
16
19
|
end.strip
|
@@ -43,7 +46,7 @@ module AssetsBooster
|
|
43
46
|
url, quotes = unquote(url.strip)
|
44
47
|
|
45
48
|
# we don't want to statically import external stylesheets
|
46
|
-
next import if
|
49
|
+
next import if external_url?(url)
|
47
50
|
|
48
51
|
# recursively process the imported css
|
49
52
|
load_source(source_folder+url)
|
@@ -51,40 +54,10 @@ module AssetsBooster
|
|
51
54
|
end
|
52
55
|
assets << asset
|
53
56
|
end
|
54
|
-
|
55
|
-
def rewrite_urls(css, source_folder, target_folder)
|
56
|
-
url_prepend = path_difference(source_folder, target_folder)
|
57
|
-
return css if url_prepend == ""
|
58
57
|
|
59
|
-
css.gsub(/url\(([^)]+)\)/i) do |match|
|
60
|
-
url, quotes = unquote($1.strip)
|
61
|
-
|
62
|
-
# we don't want to change references to external assets
|
63
|
-
next match if absolute_url?(url)
|
64
|
-
|
65
|
-
"url(#{quotes}#{url_prepend}/#{url}#{quotes})"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def unquote(quoted)
|
70
|
-
(quoted[0].chr =~ /["']/) ? [quoted.slice(1, quoted.length-2), quoted[0].chr] : [quoted, ""]
|
71
|
-
end
|
72
|
-
|
73
|
-
def absolute_url?(url)
|
74
|
-
!!(url =~ /^(\/|https?:\/\/)/i)
|
75
|
-
end
|
76
|
-
|
77
58
|
def dirname(path)
|
78
59
|
path.include?("/") ? File.dirname(path) : ""
|
79
60
|
end
|
80
|
-
|
81
|
-
def path_difference(source, target)
|
82
|
-
return source if target == ""
|
83
|
-
if source[0..target.length-1] != target
|
84
|
-
raise ArgumentError, "source and target to not share a common base path [#{source}, #{target}]"
|
85
|
-
end
|
86
|
-
source[target.length+1..-1] || ""
|
87
|
-
end
|
88
61
|
end
|
89
62
|
end
|
90
63
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'assets_booster/mixin/url'
|
2
|
+
module AssetsBooster
|
3
|
+
module Mixin
|
4
|
+
module Css
|
5
|
+
include Url
|
6
|
+
|
7
|
+
def unquote(quoted)
|
8
|
+
(quoted[0].chr =~ /["']/) ? [quoted.slice(1, quoted.length-2), quoted[0].chr] : [quoted, ""]
|
9
|
+
end
|
10
|
+
|
11
|
+
def adjust_relative_urls(css, source_folder, target_folder)
|
12
|
+
url_prepend = path_difference(source_folder, target_folder)
|
13
|
+
return css if url_prepend == ""
|
14
|
+
|
15
|
+
css.gsub(/url\(([^)]+)\)/i) do |match|
|
16
|
+
url, quotes = unquote($1.strip)
|
17
|
+
|
18
|
+
# we don't want to change references to absolute & external assets
|
19
|
+
next match if absolute_url?(url) || external_url?(url)
|
20
|
+
|
21
|
+
"url(#{quotes}#{url_prepend}/#{url}#{quotes})"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def hostify_urls(base_url, css)
|
26
|
+
css.gsub(/url\(([^)]+)\)/i) do |match|
|
27
|
+
url, quotes = unquote($1.strip)
|
28
|
+
|
29
|
+
# we don't want to change references to external assets
|
30
|
+
next match if external_url?(url)
|
31
|
+
|
32
|
+
url = url[1..-1] if url[0] == "/"
|
33
|
+
"url(#{quotes}#{base_url}/#{url}#{quotes})"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AssetsBooster
|
2
|
+
module Mixin
|
3
|
+
module Url
|
4
|
+
def absolute_url?(url)
|
5
|
+
!!(url =~ %r{^/}i)
|
6
|
+
end
|
7
|
+
|
8
|
+
def external_url?(url)
|
9
|
+
!!(url =~ %r{^https?://}i)
|
10
|
+
end
|
11
|
+
|
12
|
+
def path_difference(source, target)
|
13
|
+
return source if target == ""
|
14
|
+
if source[0..target.length-1] != target
|
15
|
+
raise ArgumentError, "source and target to not share a common base path [#{source}, #{target}]"
|
16
|
+
end
|
17
|
+
source[target.length+1..-1] || ""
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,6 +1,9 @@
|
|
1
|
+
require 'assets_booster/mixin/css'
|
1
2
|
module AssetsBooster
|
2
3
|
module Package
|
3
4
|
class Stylesheet < Base
|
5
|
+
include AssetsBooster::Mixin::Css
|
6
|
+
|
4
7
|
def merger_class
|
5
8
|
require "assets_booster/merger/css"
|
6
9
|
AssetsBooster::Merger::CSS
|
@@ -11,9 +14,21 @@ module AssetsBooster
|
|
11
14
|
path = File.join(path, name+".css") if name
|
12
15
|
end
|
13
16
|
|
17
|
+
def default_asset_host
|
18
|
+
Rails.configuration.action_controller.asset_host
|
19
|
+
end
|
20
|
+
|
14
21
|
def view_helper(view, options)
|
15
22
|
if options[:inline]
|
16
|
-
|
23
|
+
code = read
|
24
|
+
inline = options[:inline]
|
25
|
+
if inline.is_a?(Hash)
|
26
|
+
if inline[:hostify_urls]
|
27
|
+
base_url = (inline[:hostify_urls] == true) ? default_asset_host : inline[:hostify_urls]
|
28
|
+
code = hostify_urls(base_url, code)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
view.style_tag(code, options.except(:inline))
|
17
32
|
else
|
18
33
|
view.stylesheet_link_tag(view_helper_sources, options)
|
19
34
|
end
|
data/spec/merger/css_spec.rb
CHANGED
@@ -71,88 +71,6 @@ module AssetsBooster
|
|
71
71
|
lambda{ subject.merge("target.css") }.should raise_error(ArgumentError)
|
72
72
|
end
|
73
73
|
end
|
74
|
-
|
75
|
-
describe "absolute_url?" do
|
76
|
-
it "should detect absolute urls" do
|
77
|
-
[
|
78
|
-
["http://www.example.com", true],
|
79
|
-
["HTTP://www.example.com", true],
|
80
|
-
["https://www.example.com", true],
|
81
|
-
["/absolute.css", true],
|
82
|
-
["relative.css", false],
|
83
|
-
["another/relative.css", false],
|
84
|
-
].each do |url, result|
|
85
|
-
subject.absolute_url?(url).should == result
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "path_difference" do
|
91
|
-
it "should return the difference" do
|
92
|
-
[
|
93
|
-
["", "", ""],
|
94
|
-
["test", "", "test"],
|
95
|
-
["test", "test", ""],
|
96
|
-
["home/test", "", "home/test"],
|
97
|
-
["home/test", "home", "test"],
|
98
|
-
["/home/test", "/home", "test"],
|
99
|
-
].each do |source, target, result|
|
100
|
-
subject.path_difference(source, target).should == result
|
101
|
-
end
|
102
|
-
end
|
103
|
-
it "should raise if source and target dont share a common base path" do
|
104
|
-
lambda{ subject.path_difference("/home/gucki/test", "/home/peter") }.should raise_error(ArgumentError)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "rewrite_urls" do
|
109
|
-
it "should rewrite url properties" do
|
110
|
-
subject.rewrite_urls("{color:#f00}", "/home/test", "/home/test").should == "{color:#f00}"
|
111
|
-
subject.rewrite_urls("{color:#f00; background:url(test.png)}", "nested", "").should == "{color:#f00; background:url(nested/test.png)}"
|
112
|
-
source_folder = "/home/test/nested"
|
113
|
-
target_folder = "/home/test"
|
114
|
-
[
|
115
|
-
[
|
116
|
-
"{color:#f00}",
|
117
|
-
"{color:#f00}",
|
118
|
-
],
|
119
|
-
[
|
120
|
-
"{color:#f00; background:url(test.png)}",
|
121
|
-
"{color:#f00; background:url(nested/test.png)}",
|
122
|
-
],
|
123
|
-
[
|
124
|
-
"{color:#f00; background:url(\"test.png\")}",
|
125
|
-
"{color:#f00; background:url(\"nested/test.png\")}",
|
126
|
-
],
|
127
|
-
[
|
128
|
-
"{color:#f00; background:url('test.png')}",
|
129
|
-
"{color:#f00; background:url('nested/test.png')}",
|
130
|
-
],
|
131
|
-
[
|
132
|
-
"{color:#f00; background:url('test file.png')}",
|
133
|
-
"{color:#f00; background:url('nested/test file.png')}",
|
134
|
-
],
|
135
|
-
[
|
136
|
-
"{color:#f00; background:url(../test.png)}",
|
137
|
-
"{color:#f00; background:url(nested/../test.png)}",
|
138
|
-
],
|
139
|
-
].each do |input, output|
|
140
|
-
subject.rewrite_urls(input, source_folder, target_folder).should == output
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
describe "unquote" do
|
146
|
-
it "should return unquoted string and quotes" do
|
147
|
-
[
|
148
|
-
["'test.png'", ["test.png", "'"]],
|
149
|
-
['"test.png"', ["test.png", '"']],
|
150
|
-
["test.png", ["test.png", ""]],
|
151
|
-
].each do |input, output|
|
152
|
-
subject.unquote(input).should == output
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
74
|
|
157
75
|
describe "dirname" do
|
158
76
|
it "should return the folder of the given path" do
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'assets_booster/mixin/css'
|
2
|
+
module AssetsBooster
|
3
|
+
module Mixin
|
4
|
+
describe Css do
|
5
|
+
subject do
|
6
|
+
dummy = Class.new
|
7
|
+
dummy.extend(described_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "adjust_relative_urls" do
|
11
|
+
it "should adjust relative urls according to path changes of containing css file" do
|
12
|
+
subject.adjust_relative_urls("{color:#f00}", "/home/test", "/home/test").should == "{color:#f00}"
|
13
|
+
subject.adjust_relative_urls("{color:#f00; background:url(test.png)}", "nested", "").should == "{color:#f00; background:url(nested/test.png)}"
|
14
|
+
source_folder = "/home/test/nested"
|
15
|
+
target_folder = "/home/test"
|
16
|
+
[
|
17
|
+
[
|
18
|
+
"{color:#f00}",
|
19
|
+
"{color:#f00}",
|
20
|
+
],
|
21
|
+
[
|
22
|
+
"{color:#f00; background:url(test.png)}",
|
23
|
+
"{color:#f00; background:url(nested/test.png)}",
|
24
|
+
],
|
25
|
+
[
|
26
|
+
"{color:#f00; background:url(\"test.png\")}",
|
27
|
+
"{color:#f00; background:url(\"nested/test.png\")}",
|
28
|
+
],
|
29
|
+
[
|
30
|
+
"{color:#f00; background:url('test.png')}",
|
31
|
+
"{color:#f00; background:url('nested/test.png')}",
|
32
|
+
],
|
33
|
+
[
|
34
|
+
"{color:#f00; background:url('test file.png')}",
|
35
|
+
"{color:#f00; background:url('nested/test file.png')}",
|
36
|
+
],
|
37
|
+
[
|
38
|
+
"{color:#f00; background:url(../test.png)}",
|
39
|
+
"{color:#f00; background:url(nested/../test.png)}",
|
40
|
+
],
|
41
|
+
].each do |input, output|
|
42
|
+
subject.adjust_relative_urls(input, source_folder, target_folder).should == output
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not change absolute urls" do
|
47
|
+
subject.adjust_relative_urls("{color:#f00; background:url(/test.png)}", "nested", "").should == "{color:#f00; background:url(/test.png)}"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should not change external urls" do
|
51
|
+
subject.adjust_relative_urls("{color:#f00; background:url(http://external.com/test.png)}", "nested", "").should == "{color:#f00; background:url(http://external.com/test.png)}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "hostify_urls" do
|
56
|
+
it "should transform relative urls into absolute urls using the given base url" do
|
57
|
+
subject.hostify_urls("http://webcache.eu", "{color:#f00; background:url(test.png)}").should == "{color:#f00; background:url(http://webcache.eu/test.png)}"
|
58
|
+
subject.hostify_urls("http://webcache.eu", "{color:#f00; background:url(/test.png)}").should == "{color:#f00; background:url(http://webcache.eu/test.png)}"
|
59
|
+
subject.hostify_urls("http://webcache.eu", "{color:#f00; background:url(nested/test.png)}").should == "{color:#f00; background:url(http://webcache.eu/nested/test.png)}"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not change externals urls" do
|
63
|
+
subject.hostify_urls("http://webcache.eu", "{color:#f00; background:url(http://external.com/test.png)}").should == "{color:#f00; background:url(http://external.com/test.png)}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "unquote" do
|
68
|
+
it "should return unquoted string and quotes" do
|
69
|
+
[
|
70
|
+
["'test.png'", ["test.png", "'"]],
|
71
|
+
['"test.png"', ["test.png", '"']],
|
72
|
+
["test.png", ["test.png", ""]],
|
73
|
+
].each do |input, output|
|
74
|
+
subject.unquote(input).should == output
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'assets_booster/mixin/url'
|
2
|
+
module AssetsBooster
|
3
|
+
module Mixin
|
4
|
+
describe Url do
|
5
|
+
subject do
|
6
|
+
dummy = Class.new
|
7
|
+
dummy.extend(described_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "absolute_url?" do
|
11
|
+
it "should only detect absolute urls" do
|
12
|
+
[
|
13
|
+
["http://www.example.com", false],
|
14
|
+
["HTTP://www.example.com", false],
|
15
|
+
["https://www.example.com", false],
|
16
|
+
["/absolute.css", true],
|
17
|
+
["relative.css", false],
|
18
|
+
["another/relative.css", false],
|
19
|
+
].each do |url, result|
|
20
|
+
subject.absolute_url?(url).should == result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "external_url?" do
|
26
|
+
it "should only detect absolute urls" do
|
27
|
+
[
|
28
|
+
["http://www.example.com", true],
|
29
|
+
["HTTP://www.example.com", true],
|
30
|
+
["https://www.example.com", true],
|
31
|
+
["/absolute.css", false],
|
32
|
+
["relative.css", false],
|
33
|
+
["another/relative.css", false],
|
34
|
+
].each do |url, result|
|
35
|
+
subject.external_url?(url).should == result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "path_difference" do
|
41
|
+
it "should return the difference" do
|
42
|
+
[
|
43
|
+
["", "", ""],
|
44
|
+
["test", "", "test"],
|
45
|
+
["test", "test", ""],
|
46
|
+
["home/test", "", "home/test"],
|
47
|
+
["home/test", "home", "test"],
|
48
|
+
["/home/test", "/home", "test"],
|
49
|
+
].each do |source, target, result|
|
50
|
+
subject.path_difference(source, target).should == result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
it "should raise if source and target dont share a common base path" do
|
54
|
+
lambda{ subject.path_difference("/home/gucki/test", "/home/peter") }.should raise_error(ArgumentError)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -10,6 +10,13 @@ module AssetsBooster
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
describe "default_asset_host" do
|
14
|
+
it "should return Rails.configuratrion.action_controller.asset_host" do
|
15
|
+
Rails.stub_chain(:configuration, :action_controller, :asset_host).and_return(:test)
|
16
|
+
subject.default_asset_host.should == :test
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
13
20
|
describe "view_helper" do
|
14
21
|
before do
|
15
22
|
@view = double("View")
|
@@ -31,6 +38,31 @@ module AssetsBooster
|
|
31
38
|
@view.should_receive(:style_tag).with("css code", @options.except(:inline))
|
32
39
|
subject.view_helper(@view, @options)
|
33
40
|
end
|
41
|
+
|
42
|
+
describe "and the hositfy_urls option set to true" do
|
43
|
+
before do
|
44
|
+
@options[:inline] = {:hostify_urls => true}
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return a style tag with inline css whose urls have been hostified with the default asset host" do
|
48
|
+
subject.should_receive(:default_asset_host).and_return("http://webcache.eu")
|
49
|
+
subject.should_receive(:read).and_return("<style>body{background-image:url(test.png)}</style>")
|
50
|
+
@view.should_receive(:style_tag).with("<style>body{background-image:url(http://webcache.eu/test.png)}</style>", @options.except(:inline))
|
51
|
+
subject.view_helper(@view, @options)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "and the hositfy_urls option set to a string" do
|
56
|
+
before do
|
57
|
+
@options[:inline] = {:hostify_urls => "http://myhost.eu"}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return a style tag with inline css whose urls have been hostified with the specified host" do
|
61
|
+
subject.should_receive(:read).and_return("<style>body{background-image:url(test.png)}</style>")
|
62
|
+
@view.should_receive(:style_tag).with("<style>body{background-image:url(http://myhost.eu/test.png)}</style>", @options.except(:inline))
|
63
|
+
subject.view_helper(@view, @options)
|
64
|
+
end
|
65
|
+
end
|
34
66
|
end
|
35
67
|
|
36
68
|
describe "with no special options" do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: assets_booster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.11
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Corin Langosch
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-14 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -98,6 +98,8 @@ files:
|
|
98
98
|
- lib/assets_booster/merger/base.rb
|
99
99
|
- lib/assets_booster/merger/css.rb
|
100
100
|
- lib/assets_booster/merger/simple.rb
|
101
|
+
- lib/assets_booster/mixin/css.rb
|
102
|
+
- lib/assets_booster/mixin/url.rb
|
101
103
|
- lib/assets_booster/package/base.rb
|
102
104
|
- lib/assets_booster/package/javascript.rb
|
103
105
|
- lib/assets_booster/package/stylesheet.rb
|
@@ -117,6 +119,8 @@ files:
|
|
117
119
|
- spec/merger/base.rb
|
118
120
|
- spec/merger/css_spec.rb
|
119
121
|
- spec/merger/simple_spec.rb
|
122
|
+
- spec/mixin/css_spec.rb
|
123
|
+
- spec/mixin/url_spec.rb
|
120
124
|
- spec/package/base.rb
|
121
125
|
- spec/package/javascript_spec.rb
|
122
126
|
- spec/package/stylesheet_spec.rb
|