assets_booster 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,12 +8,29 @@ module AssetsBooster
8
8
 
9
9
  def merge(target)
10
10
  target_folder = dirname(target)
11
- assets.inject("") do |code, asset|
11
+ code = assets.inject("") do |code, asset|
12
12
  source_folder = dirname(asset[:source])
13
13
  asset[:css]= rewrite_urls(asset[:css], source_folder, target_folder)
14
14
  code << asset[:css]
15
15
  code << "\n"
16
16
  end.strip
17
+
18
+ charset = nil
19
+ code.gsub!(/@charset\s+([^;\n]+)[;\n]*/).each do
20
+ current_charset, quotes = unquote($1)
21
+ current_charset.downcase!
22
+ if charset && charset != current_charset
23
+ raise ArgumentError, "source files have conflicting charsets (#{charset} != #{current_charset})"
24
+ end
25
+ charset = current_charset
26
+ ""
27
+ end
28
+
29
+ if charset
30
+ code = "@charset \"#{charset}\";\n"+code
31
+ end
32
+
33
+ code
17
34
  end
18
35
 
19
36
  def load_source(source)
@@ -23,8 +40,8 @@ module AssetsBooster
23
40
  source_folder << "/" unless source_folder == ""
24
41
  asset[:css].gsub!(/@import\s+([^;\n]+)[;\n]*/).each do |import|
25
42
  url = $1.gsub(/^url\((.+)\)/i, '\1')
26
- url, quotes = extract_url(url.strip)
27
-
43
+ url, quotes = unquote(url.strip)
44
+
28
45
  # we don't want to statically import external stylesheets
29
46
  next import if absolute_url?(url)
30
47
 
@@ -40,7 +57,7 @@ module AssetsBooster
40
57
  return css if url_prepend == ""
41
58
 
42
59
  css.gsub(/url\(([^)]+)\)/i) do |match|
43
- url, quotes = extract_url($1.strip)
60
+ url, quotes = unquote($1.strip)
44
61
 
45
62
  # we don't want to change references to external assets
46
63
  next match if absolute_url?(url)
@@ -49,8 +66,8 @@ module AssetsBooster
49
66
  end
50
67
  end
51
68
 
52
- def extract_url(quoted_url)
53
- (quoted_url[0].chr =~ /["']/) ? [quoted_url.slice(1, quoted_url.length-2), quoted_url[0].chr] : [quoted_url, ""]
69
+ def unquote(quoted)
70
+ (quoted[0].chr =~ /["']/) ? [quoted.slice(1, quoted.length-2), quoted[0].chr] : [quoted, ""]
54
71
  end
55
72
 
56
73
  def absolute_url?(url)
@@ -1,3 +1,3 @@
1
1
  module AssetsBooster
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -38,6 +38,38 @@ module AssetsBooster
38
38
  end
39
39
  subject.merge("target.css").should == "{color:#f00;}\n{color:#00f;}\n\n@import url(http://www.example.com/test.css)\n{color:#fff;}"
40
40
  end
41
+
42
+ it "should merge sources, combine charset directives and move the remaining one at top" do
43
+ File.stub(:read) do |source|
44
+ case source
45
+ when 'a.css'
46
+ "{color:#fff;}"
47
+ when 'nested/b.css'
48
+ "@charset 'UTF-8';"
49
+ when 'c.css'
50
+ "@import d.css;@import e.css"
51
+ when 'd.css'
52
+ "{color:#00f;}"
53
+ when 'e.css'
54
+ "@charset 'utf-8'"
55
+ end
56
+ end
57
+ subject.merge("target.css").should == "@charset \"utf-8\";\n{color:#fff;}\n{color:#00f;}\n"
58
+ end
59
+
60
+ it "should merge sources, and complain if sources have conflicting charset directives" do
61
+ File.stub(:read) do |source|
62
+ case source
63
+ when 'a.css'
64
+ "{color:#fff;}"
65
+ when 'nested/b.css'
66
+ "@charset 'UTF-8';"
67
+ when 'c.css'
68
+ "@charset 'iso-8859-1'"
69
+ end
70
+ end
71
+ lambda{ subject.merge("target.css") }.should raise_error(ArgumentError)
72
+ end
41
73
  end
42
74
 
43
75
  describe "absolute_url?" do
@@ -110,14 +142,14 @@ module AssetsBooster
110
142
  end
111
143
  end
112
144
 
113
- describe "extract_url" do
114
- it "should extract urls" do
145
+ describe "unquote" do
146
+ it "should return unquoted string and quotes" do
115
147
  [
116
148
  ["'test.png'", ["test.png", "'"]],
117
149
  ['"test.png"', ["test.png", '"']],
118
150
  ["test.png", ["test.png", ""]],
119
151
  ].each do |input, output|
120
- subject.extract_url(input).should == output
152
+ subject.unquote(input).should == output
121
153
  end
122
154
  end
123
155
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assets_booster
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Corin Langosch