csspool-st 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.autotest +16 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.rdoc +87 -0
  4. data/Manifest.txt +62 -0
  5. data/README.rdoc +65 -0
  6. data/Rakefile +50 -0
  7. data/lib/csspool/collection.rb +50 -0
  8. data/lib/csspool/css/charset.rb +13 -0
  9. data/lib/csspool/css/declaration.rb +19 -0
  10. data/lib/csspool/css/document.rb +34 -0
  11. data/lib/csspool/css/document_handler.rb +51 -0
  12. data/lib/csspool/css/import_rule.rb +27 -0
  13. data/lib/csspool/css/media.rb +13 -0
  14. data/lib/csspool/css/parser.rb +1298 -0
  15. data/lib/csspool/css/parser.y +398 -0
  16. data/lib/csspool/css/rule_set.rb +17 -0
  17. data/lib/csspool/css/tokenizer.rb +231 -0
  18. data/lib/csspool/css/tokenizer.rex +97 -0
  19. data/lib/csspool/css.rb +8 -0
  20. data/lib/csspool/node.rb +41 -0
  21. data/lib/csspool/sac/document.rb +35 -0
  22. data/lib/csspool/sac/parser.rb +16 -0
  23. data/lib/csspool/sac.rb +2 -0
  24. data/lib/csspool/selector.rb +36 -0
  25. data/lib/csspool/selectors/additional.rb +6 -0
  26. data/lib/csspool/selectors/attribute.rb +21 -0
  27. data/lib/csspool/selectors/class.rb +11 -0
  28. data/lib/csspool/selectors/id.rb +11 -0
  29. data/lib/csspool/selectors/pseudo_class.rb +13 -0
  30. data/lib/csspool/selectors/simple.rb +22 -0
  31. data/lib/csspool/selectors/type.rb +6 -0
  32. data/lib/csspool/selectors/universal.rb +6 -0
  33. data/lib/csspool/selectors.rb +9 -0
  34. data/lib/csspool/terms/function.rb +17 -0
  35. data/lib/csspool/terms/hash.rb +6 -0
  36. data/lib/csspool/terms/ident.rb +15 -0
  37. data/lib/csspool/terms/number.rb +14 -0
  38. data/lib/csspool/terms/rgb.rb +20 -0
  39. data/lib/csspool/terms/string.rb +6 -0
  40. data/lib/csspool/terms/uri.rb +6 -0
  41. data/lib/csspool/terms.rb +7 -0
  42. data/lib/csspool/visitors/children.rb +50 -0
  43. data/lib/csspool/visitors/comparable.rb +84 -0
  44. data/lib/csspool/visitors/iterator.rb +80 -0
  45. data/lib/csspool/visitors/to_css.rb +248 -0
  46. data/lib/csspool/visitors/visitor.rb +17 -0
  47. data/lib/csspool/visitors.rb +5 -0
  48. data/lib/csspool.rb +21 -0
  49. data/test/css/test_document.rb +13 -0
  50. data/test/css/test_import_rule.rb +42 -0
  51. data/test/css/test_parser.rb +462 -0
  52. data/test/css/test_tokenizer.rb +320 -0
  53. data/test/helper.rb +65 -0
  54. data/test/sac/test_parser.rb +123 -0
  55. data/test/sac/test_properties.rb +43 -0
  56. data/test/sac/test_terms.rb +228 -0
  57. data/test/test_collection.rb +81 -0
  58. data/test/test_parser.rb +91 -0
  59. data/test/test_selector.rb +51 -0
  60. data/test/visitors/test_children.rb +20 -0
  61. data/test/visitors/test_comparable.rb +102 -0
  62. data/test/visitors/test_each.rb +19 -0
  63. data/test/visitors/test_to_css.rb +309 -0
  64. metadata +214 -0
@@ -0,0 +1,309 @@
1
+ # encoding: utf-8
2
+ require 'helper'
3
+
4
+ module CSSPool
5
+ module Visitors
6
+ class TestToCSS < CSSPool::TestCase
7
+ def test_font_serialization
8
+ doc = CSSPool.CSS 'p { font: 14px/30px Tahoma; }'
9
+
10
+ assert_equal 3, doc.rule_sets.first.declarations[0].expressions.length
11
+
12
+ doc = CSSPool.CSS(doc.to_css)
13
+ assert_equal 3, doc.rule_sets.first.declarations[0].expressions.length
14
+
15
+ assert_equal 'font: 14px / 30px Tahoma;',
16
+ doc.rule_sets.first.declarations.first.to_css.strip
17
+ end
18
+
19
+ def test_combinators
20
+
21
+ selectors = %w{* p #id .cl :hover ::selection [href]}
22
+ combinators = ['', ' ', ' > ', ' + ']
23
+
24
+ combinations = selectors.product(combinators).map do |s,c|
25
+ if s != '*' && c != '' then
26
+ s + c
27
+ end
28
+ end.compact
29
+ combinations = combinations.product(selectors).map { |s,c| s + c}
30
+
31
+ combinations.each do |s|
32
+ doc = CSSPool.CSS s + ' { }'
33
+ assert_equal s, doc.rule_sets.first.selectors.first.to_css
34
+ end
35
+
36
+ end
37
+
38
+ def test_hash_operator
39
+ doc = CSSPool.CSS 'p { font: foo, #666; }'
40
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
41
+
42
+ doc = CSSPool.CSS(doc.to_css)
43
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
44
+ end
45
+
46
+ def test_uri_operator
47
+ doc = CSSPool.CSS 'p { font: foo, url(http://example.com/); }'
48
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
49
+
50
+ doc = CSSPool.CSS(doc.to_css)
51
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
52
+ end
53
+
54
+ def test_string_operator
55
+ doc = CSSPool.CSS 'p { font: foo, "foo"; }'
56
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
57
+
58
+ doc = CSSPool.CSS(doc.to_css)
59
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
60
+ end
61
+
62
+ def test_function_operator
63
+ doc = CSSPool.CSS 'p { font: foo, foo(1); }'
64
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
65
+
66
+ doc = CSSPool.CSS(doc.to_css)
67
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
68
+ end
69
+
70
+ def test_rgb_operator
71
+ doc = CSSPool.CSS 'p { font: foo, rgb(1,2,3); }'
72
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
73
+
74
+ doc = CSSPool.CSS(doc.to_css)
75
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
76
+ end
77
+
78
+ def test_includes
79
+ doc = CSSPool.CSS <<-eocss
80
+ div[bar ~= 'adsf'] { background: red, blue; }
81
+ eocss
82
+
83
+ assert_equal 1, doc.rule_sets.first.selectors.first.simple_selectors.first.additional_selectors.length
84
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
85
+
86
+ doc = CSSPool.CSS(doc.to_css)
87
+ assert_equal 1, doc.rule_sets.first.selectors.first.simple_selectors.first.additional_selectors.length
88
+ assert_equal 2, doc.rule_sets.first.declarations[0].expressions.length
89
+ end
90
+
91
+ def test_dashmatch
92
+ doc = CSSPool.CSS <<-eocss
93
+ div[bar |= 'adsf'] { background: red, blue; }
94
+ eocss
95
+
96
+ assert_equal 1, doc.rule_sets.first.selectors.first.simple_selectors.first.additional_selectors.length
97
+
98
+ doc = CSSPool.CSS(doc.to_css)
99
+ assert_equal 1, doc.rule_sets.first.selectors.first.simple_selectors.first.additional_selectors.length
100
+ end
101
+
102
+ def test_media
103
+ css = <<-eocss
104
+ @media only screen and (min-width: 480px) {
105
+ div { background: red, blue; }
106
+ }
107
+ eocss
108
+ doc = CSSPool.CSS css
109
+ assert_equal css.lines.first.strip, doc.to_css.lines.first.strip
110
+ assert_equal 1, doc.rule_sets.first.media.length
111
+
112
+ doc = CSSPool.CSS(doc.to_css)
113
+ assert_equal 1, doc.rule_sets.first.media.length
114
+ end
115
+
116
+ def test_multiple_media
117
+ css = <<-eocss
118
+ @media only screen and (min-width: 480px), print {
119
+ div { background: red, blue; }
120
+ }
121
+
122
+ @media all {
123
+ div { background: red, blue; }
124
+ }
125
+ eocss
126
+ doc = CSSPool.CSS css
127
+ assert_equal css.lines.first.strip, doc.to_css.lines.first.strip
128
+ assert_equal 2, doc.rule_sets.first.media.length
129
+ assert_equal 1, doc.rule_sets[1].media.length
130
+
131
+ doc = CSSPool.CSS(doc.to_css)
132
+ assert_equal 2, doc.rule_sets.first.media.length
133
+ assert_equal 1, doc.rule_sets[1].media.length
134
+ end
135
+
136
+ def test_import
137
+ doc = CSSPool.CSS <<-eocss
138
+ @import "test.css";
139
+ @import url("test.css");
140
+ @import url("test.css") print, screen;
141
+ eocss
142
+
143
+ assert_equal 3, doc.import_rules.length
144
+ assert_equal 2, doc.import_rules.last.media.length
145
+
146
+ doc = CSSPool.CSS(doc.to_css)
147
+
148
+ assert_equal 3, doc.import_rules.length
149
+ assert_equal 2, doc.import_rules.last.media.length
150
+ end
151
+
152
+ def test_charsets
153
+ doc = CSSPool.CSS <<-eocss
154
+ @charset "UTF-8";
155
+ eocss
156
+
157
+ assert_equal 1, doc.charsets.length
158
+
159
+ doc = CSSPool.CSS(doc.to_css)
160
+
161
+ assert_equal 1, doc.charsets.length
162
+ end
163
+
164
+ def test_selector_attribute
165
+ input_output = {
166
+ "\"quotes\"" => "[title=\"\\\"quotes\\\"\"]",
167
+ }
168
+ input_output.each_pair do |input, output|
169
+ node = Selectors::Attribute.new 'title', input, Selectors::Attribute::EQUALS
170
+ assert_equal output, node.to_css
171
+ end
172
+
173
+ input_output = {
174
+ " space" => "[\\ space=\"value\"]",
175
+ "equal=" => "[equal\\==\"value\"]",
176
+ "new\nline" => "[new\\00000aline=\"value\"]"
177
+ }
178
+ input_output.each_pair do |input, output|
179
+ node = Selectors::Attribute.new input, 'value', Selectors::Attribute::EQUALS
180
+ assert_equal output, node.to_css
181
+ end
182
+ end
183
+
184
+ def test_selector_psuedo
185
+ input_output = {
186
+ "pseudo" => ":pseudo",
187
+ "\"quotes\"" => ":\\000022quotes\\000022",
188
+ "paren(" => ":paren\\(",
189
+ }
190
+ input_output.each_pair do |input, output|
191
+ node = Selectors::PseudoClass.new input
192
+ assert_equal output, node.to_css
193
+ end
194
+
195
+ input_output = {
196
+ "" => ":pseudo()",
197
+ "ident" => ":pseudo(ident)",
198
+ " " => ":pseudo(\\ )",
199
+ "\"quoted\"" => ":pseudo(\\000022quoted\\000022)"
200
+ }
201
+ input_output.each_pair do |input, output|
202
+ node = Selectors::PseudoClass.new "pseudo", input
203
+ assert_equal output, node.to_css
204
+ end
205
+
206
+ assert_equal "::before", Selectors::PseudoElement.new("before").to_css
207
+ assert_equal ":before", Selectors::PseudoElement.new("before", true).to_css
208
+ end
209
+
210
+ def test_selector_other
211
+ input_output = {
212
+ "pseudo" => "pseudo",
213
+ "\"quotes\"" => "\\000022quotes\\000022",
214
+ "space " => "space\\ ",
215
+ "new\nline" => "new\\00000aline"
216
+ }
217
+ input_output.each_pair do |input, output|
218
+ node = Selectors::Type.new input
219
+ assert_equal "#{output}", node.to_css
220
+ end
221
+ input_output.each_pair do |input, output|
222
+ node = Selectors::Id.new input
223
+ assert_equal "##{output}", node.to_css
224
+ end
225
+ input_output.each_pair do |input, output|
226
+ node = Selectors::Class.new input
227
+ assert_equal ".#{output}", node.to_css
228
+ end
229
+ end
230
+
231
+ def test_property
232
+ input_output = {
233
+ "property" => " property: value;",
234
+ "colon:" => " colon\\:: value;",
235
+ "space " => " space\\ : value;"
236
+ }
237
+ input_output.each_pair do |input, output|
238
+ node = CSS::Declaration.new input, [Terms::Ident.new("value")], false, nil
239
+ assert_equal output, node.to_css
240
+ end
241
+ end
242
+
243
+ def test_function_term
244
+ input_output = {
245
+ "attr" => "attr(\"string\", ident)",
246
+ "0" => "\\000030(\"string\", ident)",
247
+ "a function" => "a\\ function(\"string\", ident)",
248
+ "a(" => "a\\((\"string\", ident)",
249
+ }
250
+ input_output.each_pair do |input, output|
251
+ node = Terms::Function.new input, [Terms::String.new("string"), Terms::Ident.new("ident", ',')]
252
+ assert_equal output, node.to_css
253
+ end
254
+ end
255
+
256
+ def test_uri_term
257
+ input_output = {
258
+ "http://example.com" => "url(\"http://example.com\")",
259
+ }
260
+ input_output.each_pair do |input, output|
261
+ node = Terms::URI.new input
262
+ assert_equal output, node.to_css
263
+ end
264
+ end
265
+
266
+ def test_string_term
267
+ input_output = {
268
+ "basic" => "\"basic\"",
269
+ "\"quotes\"" => "\"\\\"quotes\\\"\"",
270
+ "…" => "\"…\"",
271
+ "\n\r\f" => "\"\\a \\\r\\\f\""
272
+ }
273
+ input_output.each_pair do |input, output|
274
+ node = Terms::String.new input
275
+ assert_equal output, node.to_css
276
+ end
277
+ end
278
+
279
+ def test_minification
280
+ rawCSS = <<-eocss
281
+ p {
282
+ font: foo;
283
+ color: #f00;
284
+ }
285
+ eocss
286
+
287
+ doc = CSSPool.CSS rawCSS
288
+ parsed_doc = doc.to_minified_css
289
+
290
+ assert_equal "p { font: foo; color: #f00; }", parsed_doc
291
+ end
292
+
293
+ def test_minification_alt
294
+ rawCSS = <<-eocss
295
+ p {
296
+ font: foo;
297
+ color: #f00;
298
+ }
299
+ eocss
300
+
301
+ doc = CSSPool.CSS rawCSS
302
+ parsed_doc = doc.to_css :minify => true
303
+
304
+ assert_equal "p { font: foo; color: #f00; }", parsed_doc
305
+ end
306
+
307
+ end
308
+ end
309
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csspool-st
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Patterson
9
+ - John Barnette
10
+ - stereobooster
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-04-27 00:00:00.000000000Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rdoc
18
+ requirement: &28296624 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '3.10'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *28296624
27
+ - !ruby/object:Gem::Dependency
28
+ name: racc
29
+ requirement: &28296192 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *28296192
38
+ - !ruby/object:Gem::Dependency
39
+ name: rexical
40
+ requirement: &28295796 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *28295796
49
+ - !ruby/object:Gem::Dependency
50
+ name: hoe-git
51
+ requirement: &28295400 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *28295400
60
+ - !ruby/object:Gem::Dependency
61
+ name: hoe-gemspec
62
+ requirement: &28295112 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *28295112
71
+ - !ruby/object:Gem::Dependency
72
+ name: hoe-bundler
73
+ requirement: &28294788 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *28294788
82
+ - !ruby/object:Gem::Dependency
83
+ name: hoe
84
+ requirement: &28294500 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *28294500
93
+ description: ! 'CSSPool is a CSS parser. CSSPool provides a SAC interface for parsing
94
+ CSS as
95
+
96
+ well as a document oriented interface for parsing CSS.'
97
+ email:
98
+ - aaronp@rubyforge.org
99
+ - jbarnette@rubyforge.org
100
+ - stereobooster@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files:
104
+ - CHANGELOG.rdoc
105
+ - Manifest.txt
106
+ - README.rdoc
107
+ files:
108
+ - .autotest
109
+ - CHANGELOG.rdoc
110
+ - Manifest.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - lib/csspool.rb
114
+ - lib/csspool/collection.rb
115
+ - lib/csspool/css.rb
116
+ - lib/csspool/css/charset.rb
117
+ - lib/csspool/css/declaration.rb
118
+ - lib/csspool/css/document.rb
119
+ - lib/csspool/css/document_handler.rb
120
+ - lib/csspool/css/import_rule.rb
121
+ - lib/csspool/css/media.rb
122
+ - lib/csspool/css/parser.rb
123
+ - lib/csspool/css/parser.y
124
+ - lib/csspool/css/rule_set.rb
125
+ - lib/csspool/css/tokenizer.rb
126
+ - lib/csspool/css/tokenizer.rex
127
+ - lib/csspool/node.rb
128
+ - lib/csspool/sac.rb
129
+ - lib/csspool/sac/document.rb
130
+ - lib/csspool/sac/parser.rb
131
+ - lib/csspool/selector.rb
132
+ - lib/csspool/selectors.rb
133
+ - lib/csspool/selectors/additional.rb
134
+ - lib/csspool/selectors/attribute.rb
135
+ - lib/csspool/selectors/class.rb
136
+ - lib/csspool/selectors/id.rb
137
+ - lib/csspool/selectors/pseudo_class.rb
138
+ - lib/csspool/selectors/simple.rb
139
+ - lib/csspool/selectors/type.rb
140
+ - lib/csspool/selectors/universal.rb
141
+ - lib/csspool/terms.rb
142
+ - lib/csspool/terms/function.rb
143
+ - lib/csspool/terms/hash.rb
144
+ - lib/csspool/terms/ident.rb
145
+ - lib/csspool/terms/number.rb
146
+ - lib/csspool/terms/rgb.rb
147
+ - lib/csspool/terms/string.rb
148
+ - lib/csspool/terms/uri.rb
149
+ - lib/csspool/visitors.rb
150
+ - lib/csspool/visitors/children.rb
151
+ - lib/csspool/visitors/comparable.rb
152
+ - lib/csspool/visitors/iterator.rb
153
+ - lib/csspool/visitors/to_css.rb
154
+ - lib/csspool/visitors/visitor.rb
155
+ - test/css/test_document.rb
156
+ - test/css/test_import_rule.rb
157
+ - test/css/test_parser.rb
158
+ - test/css/test_tokenizer.rb
159
+ - test/helper.rb
160
+ - test/sac/test_parser.rb
161
+ - test/sac/test_properties.rb
162
+ - test/sac/test_terms.rb
163
+ - test/test_collection.rb
164
+ - test/test_parser.rb
165
+ - test/test_selector.rb
166
+ - test/visitors/test_children.rb
167
+ - test/visitors/test_comparable.rb
168
+ - test/visitors/test_each.rb
169
+ - test/visitors/test_to_css.rb
170
+ - .gemtest
171
+ homepage: http://csspool.rubyforge.org
172
+ licenses: []
173
+ post_install_message:
174
+ rdoc_options:
175
+ - --main
176
+ - README.rdoc
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ! '>='
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ segments:
186
+ - 0
187
+ hash: -863683469
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ none: false
190
+ requirements:
191
+ - - ! '>='
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project: csspool-st
196
+ rubygems_version: 1.8.15
197
+ signing_key:
198
+ specification_version: 3
199
+ summary: CSSPool is a CSS parser
200
+ test_files:
201
+ - test/css/test_document.rb
202
+ - test/css/test_import_rule.rb
203
+ - test/css/test_parser.rb
204
+ - test/css/test_tokenizer.rb
205
+ - test/sac/test_parser.rb
206
+ - test/sac/test_properties.rb
207
+ - test/sac/test_terms.rb
208
+ - test/test_collection.rb
209
+ - test/test_parser.rb
210
+ - test/test_selector.rb
211
+ - test/visitors/test_children.rb
212
+ - test/visitors/test_comparable.rb
213
+ - test/visitors/test_each.rb
214
+ - test/visitors/test_to_css.rb