css_inliner 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -71,16 +71,6 @@ Note that this is <b>not stable at all</b>.
71
71
  CSSInliner.process html, '.', 'article' # => The first article element
72
72
  CSSInliner.process html, 'http://example.net/stylesheets' # Passed URI will be used as base directory when resolving relative URI of CSS
73
73
 
74
- == History
75
- === 0.3.1 / 2012-07-17
76
- * [BUG FIX]Fix an error which occurs when parsing non-XHTML documents
77
- * Change homepage: Gitorious -> GitHub
78
- * Reimplement some tests which have pended
79
-
80
- === 0.3.0 / 2012-05-28
81
- * Group external CSS files refered by <link> element by "title" attribute.
82
- * Work on progress: CSSPool::CSS::Declaration#expand_dimension
83
-
84
74
  == Note
85
75
  Note that
86
76
  <link rel="stylesheet" href="...">
@@ -88,6 +78,19 @@ and
88
78
  <style type="text/css"> ... </style>
89
79
  will be removed in process.
90
80
 
81
+ = History
82
+ == 0.3.2 / 2013-01-01
83
+ * Expand dimensions(top, right, bottom, left) and properties of borders(style, width, color) when inlining
84
+
85
+ == 0.3.1 / 2012-07-17
86
+ * [BUG FIX]Fix an error which occurs when parsing non-XHTML documents
87
+ * Change homepage: Gitorious -> GitHub
88
+ * Reimplement some tests which have pended
89
+
90
+ == 0.3.0 / 2012-05-28
91
+ * Group external CSS files refered by <link> element by "title" attribute.
92
+ * Work on progress: CSSPool::CSS::Declaration#expand_dimension
93
+
91
94
  = LICENSE
92
- CSS Inliner is copyright (c) 2011, 2012 KITAITI Makoto.
95
+ CSS Inliner is copyright (c) 2011, 2012, 2013 KITAITI Makoto.
93
96
  Distribued under the MIT license, see LISENCE file for the term.
@@ -27,4 +27,5 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency "yard"
28
28
  s.add_development_dependency "pry"
29
29
  s.add_development_dependency "pry-doc"
30
+ s.add_development_dependency "guard-test"
30
31
  end
@@ -3,13 +3,15 @@ require 'csspool'
3
3
  module CSSPool
4
4
  module CSS
5
5
  class << self
6
- # Update declarations in base with ones in other
6
+ # Update declarations in +base+ with ones in +other+
7
7
  # @param [Array<Declaration>] base updated array of declarations
8
8
  # @param [Array<Declaration>] other array of declarations
9
- # @return [Array<Declaration>] base itself
9
+ # @return [Array<Declaration>] +base+ itself
10
10
  def update_declarations(base, other)
11
- other.each do |other_decl|
11
+ other_decls = other.map {|decl| decl.expand_border.map(&:expand_dimension)}.flatten
12
+ other_decls.each do |other_decl|
12
13
  base_decls = base.find_all {|base_decl| base_decl.property == other_decl.property}
14
+ base_decls = base_decls.map {|decl| decl.expand_border.map(&:expand_dimension)}.flatten
13
15
  if base_decls.empty?
14
16
  base << other_decl
15
17
  else
@@ -33,18 +35,29 @@ module CSSPool
33
35
  end
34
36
 
35
37
  class Declaration
38
+ class InvalidExpressionError < StandardError; end
36
39
  class InvalidExpressionCountError < StandardError; end
37
40
 
41
+ COLOR_NAMES = %w[aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen]
42
+
43
+ BORDER_STYLES = %w[none hidden solid double groove ridge inset outset dashed dotted]
44
+ BORDER_WIDTH_KEYWORDS = %w[thin medium thick]
45
+ BORDER_COLOR_KEYWORDS = %w[transparent]
46
+
38
47
  DIMENSIONS = %w[top right bottom left]
39
- # property => property of [top, right, bottom, left]
48
+
40
49
  PROPERTY_EXPANSION = {}
50
+ PROPERTY_EXPANSION['border'] = %w[border-style border-width border-color]
51
+ DIMENSIONS.each do |dim|
52
+ PROPERTY_EXPANSION["border-#{dim}"] = %w[style width color].map {|subprop| "border-#{dim}-#{subprop}"}
53
+ end
41
54
  %w[margin padding].each do |prop|
42
55
  PROPERTY_EXPANSION[prop] = DIMENSIONS.map {|dim| "#{prop}-#{dim}"}
43
56
  end
44
- %w[color style width].each do |subprop|
57
+ %w[style width color].each do |subprop|
45
58
  PROPERTY_EXPANSION["border-#{subprop}"] = DIMENSIONS.map {|dim| "border-#{dim}-#{subprop}"}
46
59
  end
47
- # count of expressions => index of [top, right, bottom, left]
60
+
48
61
  EXPANSION_INDICES = {
49
62
  1 => [0, 0, 0, 0],
50
63
  2 => [0, 1, 0, 1],
@@ -67,9 +80,29 @@ module CSSPool
67
80
  dup.update other
68
81
  end
69
82
 
83
+ # @todo consider transparent and so on
84
+ # @return [Array<Declaration>] array of declaration expanded to style, width and color
85
+ def expand_border
86
+ expanded_properties = PROPERTY_EXPANSION[property]
87
+ return [self] unless expanded_properties
88
+ raise InvalidExpressionCountError, "has #{expressions.length} expressions" if expressions.length > expanded_properties.length
89
+
90
+ decls = []
91
+ expanded_properties.each do |prop|
92
+ expressions.each do |exp|
93
+ if prop.end_with? find_property(exp)
94
+ decls << Declaration.new(prop, [exp], important, rule_set)
95
+ end
96
+ end
97
+ end
98
+
99
+ decls
100
+ end
101
+
70
102
  # @return [Array<Declaration>] array of declaration indicating four dimensions
71
103
  def expand_dimension
72
104
  expanded_properties = PROPERTY_EXPANSION[property]
105
+
73
106
  return [self] unless expanded_properties
74
107
 
75
108
  expansion_map = EXPANSION_INDICES[expressions.length]
@@ -77,9 +110,28 @@ module CSSPool
77
110
 
78
111
  expanded_properties.map.with_index {|prop, i|
79
112
  expression = expressions[expansion_map[i]]
80
- Declaration.new(prop, expression, important, rule_set)
113
+ Declaration.new(prop, [expression], important, rule_set)
81
114
  }
82
115
  end
116
+
117
+ private
118
+
119
+ def find_property(expression)
120
+ case expression
121
+ when Terms::Number
122
+ 'width'
123
+ when Terms::Hash, Terms::Rgb
124
+ 'color'
125
+ when Terms::Ident
126
+ if BORDER_STYLES.include? expression.to_s
127
+ 'style'
128
+ elsif BORDER_WIDTH_KEYWORDS.include? expression.to_s
129
+ 'width'
130
+ elsif (COLOR_NAMES + BORDER_COLOR_KEYWORDS).include? expression.to_s
131
+ 'color'
132
+ end
133
+ end
134
+ end
83
135
  end
84
136
  end
85
137
 
@@ -1,3 +1,3 @@
1
1
  module CSSInliner
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -60,7 +60,7 @@ EOC
60
60
  <title>With Style Attribute</title>
61
61
  <link rel="stylesheet" type="text/css" href="./style.css">
62
62
  <style type="text/css">
63
- p {font-style: oblique;}
63
+ p {font-style: oblique; border: dotted;}
64
64
  </style>
65
65
  </head>
66
66
  <body>
@@ -76,7 +76,7 @@ EOC
76
76
  inlined = process(html, css)
77
77
  doc = Nokogiri.HTML(inlined)
78
78
  style = doc.search('p').first['style'].strip.gsub(/ +/, '')
79
- assert_equal 'color:blue;font-style:oblique;', style
79
+ assert_equal 'color:blue;font-style:oblique;border-top-style:dotted;border-right-style:dotted;border-bottom-style:dotted;border-left-style:dotted;', style
80
80
  end
81
81
 
82
82
  def test_process_with_inline_style_and_style_elem
@@ -36,7 +36,192 @@ EOC
36
36
  assert_equal 'color: blue !important;', base.to_css.strip
37
37
  end
38
38
 
39
- def test_non_dimension_expandable
39
+ def test_complex_border
40
+ complex_border = CSSPool.CSS <<EOC
41
+ p {
42
+ border: red 1px;
43
+ }
44
+ div {
45
+ border-top: blue solid thin;
46
+ }
47
+ EOC
48
+ expected = CSSPool.CSS <<EOC
49
+ p {
50
+ border-top-width: 1px;
51
+ border-right-width: 1px;
52
+ border-bottom-width: 1px;
53
+ border-left-width: 1px;
54
+ border-top-color: red;
55
+ border-right-color: red;
56
+ border-bottom-color: red;
57
+ border-left-color: red;
58
+ }
59
+ div {
60
+ border-top-width: thin;
61
+ border-top-color: blue;
62
+ border-top-style: solid;
63
+ }
64
+ EOC
65
+ assert_equal expected.rule_sets.map {|rs| rs.declarations}.flatten.map(&:to_s).sort,
66
+ complex_border.rule_sets.map {|rs| rs.declarations.map {|decl| decl.expand_border.map(&:expand_dimension)}}.flatten.map(&:to_s).sort
67
+ end
68
+
69
+ def test_non_expandable_border
70
+ non_expandable = CSSPool.CSS <<EOC
71
+ p {
72
+ border-top-width: 12em;
73
+ }
74
+ EOC
75
+ expected = CSSPool.CSS <<EOC
76
+ p {
77
+ border-top-width: 12em;
78
+ }
79
+ EOC
80
+ assert_equal expected.rule_sets.first.declarations.to_s,
81
+ non_expandable.rule_sets.first.declarations.first.expand_border.to_s
82
+ end
83
+
84
+ def test_expand_border_with_three_expressions
85
+ non_expanded = CSSPool.CSS <<EOC
86
+ p {
87
+ border: black dotted 1px;
88
+ }
89
+ EOC
90
+ expected = CSSPool.CSS <<EOC
91
+ p {
92
+ border-color: black;
93
+ border-style: dotted;
94
+ border-width: 1px;
95
+ }
96
+ EOC
97
+ assert_equal expected.rule_sets.map {|rs| rs.declarations}.flatten.map(&:to_s).sort,
98
+ non_expanded.rule_sets.map {|rs| rs.declarations.map(&:expand_border)}.flatten.map(&:to_s).sort
99
+ end
100
+
101
+ def test_expand_border_with_two_expressions
102
+ non_expanded = CSSPool.CSS <<EOC
103
+ p {
104
+ border: #FFFFFF thin;
105
+ }
106
+ EOC
107
+ expected = CSSPool.CSS <<EOC
108
+ p {
109
+ border-color: #FFFFFF;
110
+ border-width: thin;
111
+ }
112
+ EOC
113
+ assert_equal expected.rule_sets.map {|rs| rs.declarations}.flatten.map(&:to_s).sort,
114
+ non_expanded.rule_sets.map {|rs| rs.declarations.map(&:expand_border)}.flatten.map(&:to_s).sort
115
+ end
116
+
117
+ def test_expand_border_with_one_expression
118
+ non_expanded = CSSPool.CSS <<EOC
119
+ p {
120
+ border: rgb(100%, 0%, 0%);
121
+ }
122
+ EOC
123
+ expected = CSSPool.CSS <<EOC
124
+ p {
125
+ border-color: rgb(100%, 0%, 0%);
126
+ }
127
+ EOC
128
+ assert_equal expected.rule_sets.map {|rs| rs.declarations}.flatten.map(&:to_s).sort,
129
+ non_expanded.rule_sets.map {|rs| rs.declarations.map(&:expand_border)}.flatten.map(&:to_s).sort
130
+ end
131
+
132
+ def test_expand_five_dimension_border_width
133
+ five_border_dimensions = CSSPool.CSS <<EOC
134
+ p {
135
+ border-width: 12em 3em 1em 5em 3em;
136
+ }
137
+ EOC
138
+ assert_raise CSSPool::CSS::Declaration::InvalidExpressionCountError do
139
+ five_border_dimensions.rule_sets.first.declarations.first.expand_border
140
+ end
141
+ end
142
+
143
+ def test_expand_four_dimension_border_width
144
+ four_dimension_border_width = CSSPool.CSS <<EOC
145
+ p {
146
+ border-width: 1em 2em 3em 4em
147
+ }
148
+ EOC
149
+ expected = CSSPool.CSS <<EOC
150
+ p {
151
+ border-top-width: 1em;
152
+ border-right-width: 2em;
153
+ border-bottom-width: 3em;
154
+ border-left-width: 4em;
155
+ }
156
+ EOC
157
+ assert_equal expected.rule_sets.first.declarations.to_s,
158
+ four_dimension_border_width.rule_sets.first.declarations.first.expand_dimension.to_s
159
+ end
160
+
161
+ def test_expand_three_dimension_border_width
162
+ three_dimension_border_width = CSSPool.CSS <<EOC
163
+ p {
164
+ border-width: 1em 2em 3em;
165
+ }
166
+ EOC
167
+ expected = CSSPool.CSS <<EOC
168
+ p {
169
+ border-top-width: 1em;
170
+ border-right-width: 2em;
171
+ border-bottom-width: 3em;
172
+ border-left-width: 2em;
173
+ }
174
+ EOC
175
+ assert_equal expected.rule_sets.first.declarations.to_s,
176
+ three_dimension_border_width.rule_sets.first.declarations.first.expand_dimension.to_s
177
+ end
178
+
179
+ def test_expand_two_dimension_border_width
180
+ two_dimension_border_width = CSSPool.CSS <<EOC
181
+ p {
182
+ border-width: 1em 2em;
183
+ }
184
+ EOC
185
+ expected = CSSPool.CSS <<EOC
186
+ p {
187
+ border-top-width: 1em;
188
+ border-right-width: 2em;
189
+ border-bottom-width: 1em;
190
+ border-left-width: 2em;
191
+ }
192
+ EOC
193
+ assert_equal expected.rule_sets.first.declarations.to_s,
194
+ two_dimension_border_width.rule_sets.first.declarations.first.expand_dimension.to_s
195
+ end
196
+
197
+ def test_expand_one_dimension_border_width
198
+ one_dimension_border_width = CSSPool.CSS <<EOC
199
+ p {
200
+ border-width: 1em;
201
+ }
202
+ EOC
203
+ expected = CSSPool.CSS <<EOC
204
+ p {
205
+ border-top-width: 1em;
206
+ border-right-width: 1em;
207
+ border-bottom-width: 1em;
208
+ border-left-width: 1em;
209
+ }
210
+ EOC
211
+ assert_equal expected.rule_sets.first.declarations.to_s,
212
+ one_dimension_border_width.rule_sets.first.declarations.first.expand_border.to_s
213
+ end
214
+
215
+ def test_expand_border_with_invalid_property
216
+ no_dimension_border_width = CSSPool.CSS <<EOC
217
+ p {
218
+ border: zero;
219
+ }
220
+ EOC
221
+ assert_empty no_dimension_border_width.rule_sets.first.declarations.first.expand_border
222
+ end
223
+
224
+ def test_non_expandable_dimension
40
225
  no_dimension = CSSPool.CSS <<EOC
41
226
  p {
42
227
  color: blue;
@@ -47,7 +232,6 @@ p {
47
232
  color: blue;
48
233
  }
49
234
  EOC
50
-
51
235
  assert_equal expected.rule_sets.first.declarations.to_s,
52
236
  no_dimension.rule_sets.first.declarations.first.expand_dimension.to_s
53
237
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: css_inliner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000Z
12
+ date: 2012-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &7998240 !ruby/object:Gem::Requirement
16
+ requirement: &6872660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7998240
24
+ version_requirements: *6872660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: csspool
27
- requirement: &7997400 !ruby/object:Gem::Requirement
27
+ requirement: &6871840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *7997400
35
+ version_requirements: *6871840
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bsearch
38
- requirement: &7996520 !ruby/object:Gem::Requirement
38
+ requirement: &6871340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *7996520
46
+ version_requirements: *6871340
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: test-unit-full
49
- requirement: &7442420 !ruby/object:Gem::Requirement
49
+ requirement: &6870620 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *7442420
57
+ version_requirements: *6870620
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &7439740 !ruby/object:Gem::Requirement
60
+ requirement: &6870040 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *7439740
68
+ version_requirements: *6870040
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry
71
- requirement: &7438740 !ruby/object:Gem::Requirement
71
+ requirement: &6869360 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *7438740
79
+ version_requirements: *6869360
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: pry-doc
82
- requirement: &7437580 !ruby/object:Gem::Requirement
82
+ requirement: &6868680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,18 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *7437580
90
+ version_requirements: *6868680
91
+ - !ruby/object:Gem::Dependency
92
+ name: guard-test
93
+ requirement: &6868120 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *6868120
91
102
  description: ! "\n inline CSS from external file(s) and/or style elment(s) in head
92
103
  element\n into style attibute of HTML elements\n "
93
104
  email:
@@ -171,9 +182,7 @@ files:
171
182
  - test/fixtures/sample2/images/wrench_orange.png
172
183
  - test/fixtures/sample2/images/zoom.png
173
184
  - test/fixtures/sample2/index.html
174
- - test/fixtures/sample2/index.html~
175
185
  - test/fixtures/sample2/index.inlined.html
176
- - test/fixtures/sample2/index.inlined.html~
177
186
  - test/fixtures/sample2/js/darkfish.js
178
187
  - test/fixtures/sample2/js/jquery.js
179
188
  - test/fixtures/sample2/js/quicksearch.js
@@ -261,7 +270,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
261
270
  version: '0'
262
271
  segments:
263
272
  - 0
264
- hash: -3751475647972594840
273
+ hash: -4258511309440831495
265
274
  required_rubygems_version: !ruby/object:Gem::Requirement
266
275
  none: false
267
276
  requirements:
@@ -270,7 +279,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
270
279
  version: '0'
271
280
  segments:
272
281
  - 0
273
- hash: -3751475647972594840
282
+ hash: -4258511309440831495
274
283
  requirements: []
275
284
  rubyforge_project:
276
285
  rubygems_version: 1.8.8