csspool 0.2.6 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. data/.autotest +16 -0
  2. data/{CHANGELOG.txt → CHANGELOG.rdoc} +10 -0
  3. data/Manifest.txt +60 -72
  4. data/README.rdoc +78 -0
  5. data/Rakefile +9 -52
  6. data/lib/csspool/collection.rb +50 -0
  7. data/lib/csspool/css/charset.rb +7 -0
  8. data/lib/csspool/css/declaration.rb +8 -0
  9. data/lib/csspool/css/document.rb +34 -0
  10. data/lib/csspool/css/document_handler.rb +51 -0
  11. data/lib/csspool/css/import_rule.rb +15 -0
  12. data/lib/csspool/css/media.rb +7 -0
  13. data/lib/csspool/css/rule_set.rb +12 -0
  14. data/lib/csspool/css.rb +7 -0
  15. data/lib/csspool/lib_croco/cr_additional_sel.rb +46 -0
  16. data/lib/csspool/lib_croco/cr_attr_sel.rb +16 -0
  17. data/lib/csspool/lib_croco/cr_doc_handler.rb +24 -0
  18. data/lib/csspool/lib_croco/cr_num.rb +13 -0
  19. data/lib/csspool/lib_croco/cr_parser.rb +11 -0
  20. data/lib/csspool/lib_croco/cr_parsing_location.rb +17 -0
  21. data/lib/csspool/lib_croco/cr_pseudo.rb +14 -0
  22. data/lib/csspool/lib_croco/cr_rgb.rb +18 -0
  23. data/lib/csspool/lib_croco/cr_selector.rb +34 -0
  24. data/lib/csspool/lib_croco/cr_simple_sel.rb +54 -0
  25. data/lib/csspool/lib_croco/cr_term.rb +97 -0
  26. data/lib/csspool/lib_croco/glist.rb +21 -0
  27. data/lib/csspool/lib_croco.rb +78 -0
  28. data/lib/csspool/node.rb +5 -0
  29. data/lib/csspool/sac/document.rb +35 -0
  30. data/lib/csspool/sac/parser.rb +122 -0
  31. data/lib/csspool/sac.rb +2 -0
  32. data/lib/csspool/selector.rb +32 -0
  33. data/lib/csspool/selectors/additional.rb +6 -0
  34. data/lib/csspool/selectors/attribute.rb +21 -0
  35. data/lib/csspool/selectors/class.rb +11 -0
  36. data/lib/csspool/selectors/id.rb +11 -0
  37. data/lib/csspool/selectors/pseudo_class.rb +13 -0
  38. data/lib/csspool/selectors/simple.rb +22 -0
  39. data/lib/csspool/selectors/type.rb +6 -0
  40. data/lib/csspool/selectors/universal.rb +6 -0
  41. data/lib/csspool/selectors.rb +8 -0
  42. data/lib/csspool/terms/function.rb +17 -0
  43. data/lib/csspool/terms/hash.rb +6 -0
  44. data/lib/csspool/terms/ident.rb +15 -0
  45. data/lib/csspool/terms/number.rb +14 -0
  46. data/lib/csspool/terms/rgb.rb +23 -0
  47. data/lib/csspool/terms/string.rb +6 -0
  48. data/lib/csspool/terms/uri.rb +6 -0
  49. data/lib/csspool/terms.rb +7 -0
  50. data/lib/csspool/visitable.rb +18 -0
  51. data/lib/csspool/visitors/comparable.rb +78 -0
  52. data/lib/csspool/visitors/to_css.rb +201 -0
  53. data/lib/csspool/visitors/visitor.rb +17 -0
  54. data/lib/csspool/visitors.rb +3 -0
  55. data/lib/csspool.rb +18 -1
  56. data/test/css/test_document.rb +13 -0
  57. data/test/css/test_import_rule.rb +42 -0
  58. data/test/helper.rb +62 -6
  59. data/test/sac/test_parser.rb +115 -0
  60. data/test/sac/test_properties.rb +43 -0
  61. data/test/sac/test_terms.rb +134 -0
  62. data/test/test_collection.rb +81 -0
  63. data/test/test_parser.rb +75 -241
  64. data/test/test_selector.rb +45 -34
  65. data/test/visitors/test_comparable.rb +72 -0
  66. data/test/visitors/test_to_css.rb +141 -0
  67. metadata +98 -85
  68. data/README.txt +0 -79
  69. data/lib/css/sac/conditions/attribute_condition.rb +0 -56
  70. data/lib/css/sac/conditions/begin_hyphen_condition.rb +0 -22
  71. data/lib/css/sac/conditions/class_condition.rb +0 -22
  72. data/lib/css/sac/conditions/combinator_condition.rb +0 -40
  73. data/lib/css/sac/conditions/condition.rb +0 -35
  74. data/lib/css/sac/conditions/id_condition.rb +0 -27
  75. data/lib/css/sac/conditions/one_of_condition.rb +0 -22
  76. data/lib/css/sac/conditions/pseudo_class_condition.rb +0 -24
  77. data/lib/css/sac/conditions.rb +0 -5
  78. data/lib/css/sac/document_handler.rb +0 -67
  79. data/lib/css/sac/error_handler.rb +0 -14
  80. data/lib/css/sac/generated_parser.rb +0 -999
  81. data/lib/css/sac/generated_property_parser.rb +0 -9214
  82. data/lib/css/sac/lexeme.rb +0 -29
  83. data/lib/css/sac/lexical_unit.rb +0 -203
  84. data/lib/css/sac/parse_exception.rb +0 -6
  85. data/lib/css/sac/parser.rb +0 -115
  86. data/lib/css/sac/property_parser.rb +0 -47
  87. data/lib/css/sac/selectors/child_selector.rb +0 -40
  88. data/lib/css/sac/selectors/conditional_selector.rb +0 -49
  89. data/lib/css/sac/selectors/descendant_selector.rb +0 -40
  90. data/lib/css/sac/selectors/element_selector.rb +0 -39
  91. data/lib/css/sac/selectors/selector.rb +0 -31
  92. data/lib/css/sac/selectors/sibling_selector.rb +0 -39
  93. data/lib/css/sac/selectors/simple_selector.rb +0 -25
  94. data/lib/css/sac/selectors.rb +0 -5
  95. data/lib/css/sac/token.rb +0 -27
  96. data/lib/css/sac/tokenizer.rb +0 -187
  97. data/lib/css/sac/visitable.rb +0 -106
  98. data/lib/css/sac.rb +0 -14
  99. data/lib/css/stylesheet/rule.rb +0 -20
  100. data/lib/css/stylesheet/stylesheet.rb +0 -96
  101. data/lib/css/stylesheet.rb +0 -3
  102. data/lib/parser.y +0 -308
  103. data/lib/property_parser.y +0 -2346
  104. data/lib/property_parser.y.erb +0 -1321
  105. data/test/condition_test_case.rb +0 -6
  106. data/test/selector_test_case.rb +0 -20
  107. data/test/test_all.rb +0 -4
  108. data/test/test_attribute_condition.rb +0 -39
  109. data/test/test_begin_hyphen_condition.rb +0 -15
  110. data/test/test_child_selector.rb +0 -60
  111. data/test/test_class_condition.rb +0 -15
  112. data/test/test_combinator_condition.rb +0 -38
  113. data/test/test_condition.rb +0 -31
  114. data/test/test_conditional_selector.rb +0 -44
  115. data/test/test_descendant_selector.rb +0 -64
  116. data/test/test_element_selector.rb +0 -31
  117. data/test/test_error_recovery.rb +0 -28
  118. data/test/test_id_condition.rb +0 -16
  119. data/test/test_lexeme.rb +0 -39
  120. data/test/test_lexical_color.rb +0 -49
  121. data/test/test_lexical_function.rb +0 -31
  122. data/test/test_lexical_ident.rb +0 -30
  123. data/test/test_lexical_number.rb +0 -68
  124. data/test/test_lexical_string.rb +0 -23
  125. data/test/test_lexical_unit.rb +0 -106
  126. data/test/test_lexical_uri.rb +0 -28
  127. data/test/test_one_of_condition.rb +0 -15
  128. data/test/test_parse_error.rb +0 -199
  129. data/test/test_property_parser.rb +0 -593
  130. data/test/test_selector_as_string.rb +0 -94
  131. data/test/test_selector_parser.rb +0 -178
  132. data/test/test_sibling_selector.rb +0 -42
  133. data/test/test_simple_selector.rb +0 -9
  134. data/test/test_specificity.rb +0 -76
  135. data/test/test_stylesheet.rb +0 -95
  136. data/test/test_token.rb +0 -24
  137. data/test/test_tokenizer.rb +0 -127
  138. data/test/test_xpath.rb +0 -105
@@ -0,0 +1,134 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module SAC
5
+ class TestTerms < CSSPool::TestCase
6
+ def setup
7
+ @doc = MyDoc.new
8
+ @parser = CSSPool::SAC::Parser.new(@doc)
9
+ end
10
+
11
+ def test_hash_range
12
+ @parser.parse <<-eocss
13
+ div { border: #123; }
14
+ eocss
15
+ hash = @doc.properties.first[1].first
16
+ assert_equal '123', hash.value
17
+ end
18
+
19
+ def test_rgb
20
+ @parser.parse <<-eocss
21
+ div { border: rgb(1,2,3); }
22
+ eocss
23
+ color = @doc.properties.first[1].first
24
+ assert_equal 1, color.red
25
+ assert_equal 2, color.green
26
+ assert_equal 3, color.blue
27
+ assert !color.percentage?
28
+ assert_match('rgb(1,2,3)', color.to_css)
29
+ end
30
+
31
+ def test_rgb_with_percentage
32
+ @parser.parse <<-eocss
33
+ div { border: rgb(100%,2%,3%); }
34
+ eocss
35
+ color = @doc.properties.first[1].first
36
+ assert_equal 100, color.red
37
+ assert_equal 2, color.green
38
+ assert_equal 3, color.blue
39
+ assert color.percentage?
40
+ assert_match('rgb(100%,2%,3%)', color.to_css)
41
+ end
42
+
43
+ def test_negative_number
44
+ @parser.parse <<-eocss
45
+ div { border: -1px; }
46
+ eocss
47
+ assert_equal 1, @doc.properties.length
48
+ size = @doc.properties.first[1].first
49
+ assert_equal :minus, size.unary_operator
50
+ assert_equal 1, size.value
51
+ assert_equal '-1.0px', size.to_s
52
+ assert_equal '-1.0px', size.to_css
53
+ end
54
+
55
+ def test_positive_number
56
+ @parser.parse <<-eocss
57
+ div { border: 1px; }
58
+ eocss
59
+ assert_equal 1, @doc.properties.length
60
+ size = @doc.properties.first[1].first
61
+ assert_equal 1, size.value
62
+ assert_equal '1.0px', size.to_s
63
+ end
64
+
65
+ %w{
66
+ 1 1em 1ex 1px 1in 1cm 1mm 1pt 1pc 1% 1deg 1rad 1ms 1s 1Hz 1kHz
67
+ }.each do |num|
68
+ expected = num.sub(/1/, '1.0')
69
+ define_method(:"test_num_#{num}") do
70
+ @parser.parse <<-eocss
71
+ div { border: #{num}; }
72
+ eocss
73
+ assert_equal 1, @doc.properties.length
74
+ size = @doc.properties.first[1].first
75
+ assert_equal 1, size.value
76
+ assert_equal expected, size.to_s
77
+ assert_equal expected, size.to_css
78
+ end
79
+ end
80
+
81
+ def test_string_term
82
+ @parser.parse <<-eocss
83
+ div { border: "hello"; }
84
+ eocss
85
+ assert_equal 1, @doc.properties.length
86
+ string = @doc.properties.first[1].first
87
+ assert_equal 'hello', string.value
88
+ assert_equal '"hello"', string.to_css
89
+ end
90
+
91
+ def test_inherit
92
+ @parser.parse <<-eocss
93
+ div { color: inherit; }
94
+ eocss
95
+ assert_equal 1, @doc.properties.length
96
+ string = @doc.properties.first[1].first
97
+ assert_equal 'inherit', string.value
98
+ assert_equal 'inherit', string.to_css
99
+ end
100
+
101
+ def test_important
102
+ @parser.parse <<-eocss
103
+ div { color: inherit !important; }
104
+ eocss
105
+ assert_equal 1, @doc.properties.length
106
+ string = @doc.properties.first[1].first
107
+ assert_equal 'inherit', string.value
108
+ assert_equal 'inherit', string.to_css
109
+ end
110
+
111
+ def test_function
112
+ @parser.parse <<-eocss
113
+ div { border: foo("hello"); }
114
+ eocss
115
+ assert_equal 1, @doc.properties.length
116
+ func = @doc.properties.first[1].first
117
+ assert_equal 'foo', func.name
118
+ assert_equal 1, func.params.length
119
+ assert_equal 'hello', func.params.first.value
120
+ assert_match 'foo("hello")', func.to_css
121
+ end
122
+
123
+ def test_uri
124
+ @parser.parse <<-eocss
125
+ div { border: url(http://tenderlovemaking.com/); }
126
+ eocss
127
+ assert_equal 1, @doc.properties.length
128
+ url = @doc.properties.first[1].first
129
+ assert_equal 'http://tenderlovemaking.com/', url.value
130
+ assert_match 'url(http://tenderlovemaking.com/)', url.to_css
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ class TestCollection < CSSPool::TestCase
5
+ def test_new
6
+ assert CSSPool::Collection.new
7
+ assert CSSPool::Collection.new { |url| }
8
+ end
9
+
10
+ def test_append
11
+ collection = CSSPool::Collection.new
12
+ collection << "div { background: green; }"
13
+ assert_equal 1, collection.length
14
+ end
15
+
16
+ def test_collection_imports_stuff
17
+ called = false
18
+ collection = CSSPool::Collection.new do |url|
19
+ called = true
20
+ assert_equal 'hello.css', url
21
+ "div { background: red; }"
22
+ end
23
+
24
+ collection << '@import url(hello.css);'
25
+ assert called, "block was not called"
26
+ assert_equal 2, collection.length
27
+ end
28
+
29
+ def test_collection_imports_imports_imports
30
+ css = {
31
+ 'foo.css' => '@import url("bar.css");',
32
+ 'bar.css' => '@import url("baz.css");',
33
+ 'baz.css' => 'div { background: red; }',
34
+ }
35
+
36
+ collection = CSSPool::Collection.new do |url|
37
+ css[url] || raise
38
+ end
39
+
40
+ collection << '@import url(foo.css);'
41
+ assert_equal 4, collection.length
42
+ assert_nil collection.last.parent
43
+ assert_equal collection[-2].parent, collection.last
44
+ assert_equal collection[-3].parent, collection[-2]
45
+ assert_equal collection[-4].parent, collection[-3]
46
+ end
47
+
48
+ def test_load_only_once
49
+ css = {
50
+ 'foo.css' => '@import url("foo.css");',
51
+ }
52
+
53
+ collection = CSSPool::Collection.new do |url|
54
+ css[url] || raise
55
+ end
56
+
57
+ collection << '@import url(foo.css);'
58
+
59
+ assert_equal 2, collection.length
60
+ end
61
+
62
+ def test_each
63
+ css = {
64
+ 'foo.css' => '@import url("foo.css");',
65
+ }
66
+
67
+ collection = CSSPool::Collection.new do |url|
68
+ css[url] || raise
69
+ end
70
+
71
+ collection << '@import url(foo.css);'
72
+
73
+ list = []
74
+ collection.each do |thing|
75
+ list << thing
76
+ end
77
+
78
+ assert_equal 2, list.length
79
+ end
80
+ end
81
+ end
data/test/test_parser.rb CHANGED
@@ -1,257 +1,91 @@
1
- require File.dirname(__FILE__) + "/helper"
1
+ require 'helper'
2
2
 
3
- class ParserTest < Test::Unit::TestCase
4
- def setup
5
- @sac = CSS::SAC::Parser.new()
6
- end
7
-
8
- def teardown
9
- flexmock_verify
10
- flexmock_close
11
- end
12
-
13
- def test_parse_rule
14
- class << @sac.document_handler
15
- attr_accessor :selector
16
- alias :start_selector :selector=
3
+ module CSSPool
4
+ class TestParser < CSSPool::TestCase
5
+ def test_empty_doc_on_blank
6
+ assert CSSPool.CSS(nil)
7
+ assert CSSPool.CSS('')
17
8
  end
18
- @sac.parse('h1 > div { }')
19
-
20
- expected_rule = @sac.document_handler.selector
21
- assert expected_rule
22
-
23
- rule = @sac.parse_rule('h1 > div')
24
- assert rule
25
- assert_equal 1, rule.length
26
- assert_equal expected_rule, rule
27
- end
28
-
29
- def test_page
30
- flexmock(@sac.document_handler).
31
- should_receive(:start_page).ordered.
32
- with('foo', 'print').once
33
- flexmock(@sac.document_handler).
34
- should_receive(:property).ordered.with('color', on { |list|
35
- list.length == 1 && list.first.dimension_unit_text.nil? &&
36
- list.first.lexical_unit_type == :SAC_IDENT &&
37
- list.first.string_value == "black" &&
38
- list.first.integer_value.nil?
39
- }, false).once
40
- flexmock(@sac.document_handler).
41
- should_receive(:end_page).ordered.
42
- with('foo', 'print').once
43
- @sac.parse('@page foo:print { color: black }')
44
- end
45
-
46
- def test_empty
47
- assert_nothing_raised {
48
- @sac.parse('')
49
- }
50
- end
51
-
52
- def test_at_import
53
- flexmock(@sac.document_handler).
54
- should_receive(:import_style).ordered.
55
- with('"subs.css"', []).once
56
- @sac.parse('@import "subs.css";')
57
- flexmock_verify
58
- end
59
-
60
- def test_at_import_many_media
61
- flexmock(@sac.document_handler).
62
- should_receive(:import_style).ordered.
63
- with('"subs.css"', ['paper', 'boobs']).once
64
- @sac.parse('@import "subs.css" paper, boobs;')
65
- flexmock_verify
66
- end
67
-
68
- def test_multi_import
69
- flexmock(@sac.document_handler).
70
- should_receive(:import_style).ordered.
71
- with('"subs.css"', []).once
72
- flexmock(@sac.document_handler).
73
- should_receive(:import_style).ordered.
74
- with('"subs1.css"', []).once
75
- @sac.parse('@import "subs.css"; @import "subs1.css";')
76
- flexmock_verify
77
- end
78
-
79
- def test_media
80
- flexmock(@sac.document_handler).
81
- should_receive(:start_media).ordered.
82
- with(['print', 'tv']).once
83
- flexmock(@sac.document_handler).
84
- should_receive(:start_selector).ordered.once
85
- flexmock(@sac.document_handler).
86
- should_receive(:property).ordered.with('color', on { |list|
87
- list.length == 1 && list.first.dimension_unit_text.nil? &&
88
- list.first.lexical_unit_type == :SAC_IDENT &&
89
- list.first.string_value == "black" &&
90
- list.first.integer_value.nil?
91
- }, false).once
92
- flexmock(@sac.document_handler).
93
- should_receive(:end_selector).ordered.once
94
- flexmock(@sac.document_handler).
95
- should_receive(:end_media).ordered.
96
- with(['print', 'tv']).once
97
- @sac.parse('@media print, tv { h1 { color: black } }')
98
- end
99
-
100
- def test_media_single
101
- flexmock(@sac.document_handler).
102
- should_receive(:start_media).ordered.
103
- with(['print']).once
104
- flexmock(@sac.document_handler).
105
- should_receive(:end_media).ordered.
106
- with(['print']).once
107
- @sac.parse('@media print { h1 { color: black } }')
108
- end
109
-
110
- def test_comment
111
- flexmock(@sac.document_handler).
112
- should_receive(:comment).ordered.
113
- with("/* sup bro */").once
114
- @sac.parse('/* sup bro */')
115
- end
116
-
117
- def test_weird
118
- @sac.parse('#topnav legend { display: none; }')
119
- end
120
-
121
- def test_ignorable_at_rule
122
- flexmock(@sac.document_handler).
123
- should_receive(:start_document).ordered.once
124
- flexmock(@sac.document_handler).
125
- should_receive(:ignorable_at_rule).ordered.
126
- with("blah").once
127
- flexmock(@sac.document_handler).
128
- should_receive(:end_document).ordered.once
129
- @sac.parse('@blah "style.css";')
130
- end
131
-
132
- def test_selector
133
- flexmock(@sac.document_handler).
134
- should_receive(:start_document).ordered.once
135
- flexmock(@sac.document_handler).
136
- should_receive(:start_selector).ordered.once
137
- flexmock(@sac.document_handler).
138
- should_receive(:property).ordered.with('color', on { |list|
139
- list.length == 1 && list.first.dimension_unit_text.nil? &&
140
- list.first.lexical_unit_type == :SAC_IDENT &&
141
- list.first.string_value == "black" &&
142
- list.first.integer_value.nil?
143
- }, false).once
144
- flexmock(@sac.document_handler).
145
- should_receive(:end_selector).ordered.once
146
- flexmock(@sac.document_handler).
147
- should_receive(:end_document).ordered.once
148
9
 
149
- @sac.parse('div h1 { color: black; }')
150
- flexmock_verify
151
- end
10
+ def test_doc_charset
11
+ doc = CSSPool.CSS <<-eocss
12
+ @charset "UTF-8";
13
+ @import url("foo.css") screen;
14
+ div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
15
+ eocss
16
+ assert_equal 'UTF-8', doc.charsets.first.name
17
+ end
152
18
 
153
- def test_properties
154
- flexmock(@sac.document_handler).
155
- should_receive(:property).with('color', on { |list|
156
- list.length == 1 && list.first.dimension_unit_text.nil? &&
157
- list.first.lexical_unit_type == :SAC_IDENT &&
158
- list.first.string_value == "black" &&
159
- list.first.integer_value.nil?
160
- }, false).once
161
- @sac.parse('div h1 { color: black; }')
162
- flexmock_verify
19
+ def test_doc_parser
20
+ doc = CSSPool.CSS <<-eocss
21
+ @charset "UTF-8";
22
+ div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
23
+ eocss
24
+
25
+ assert_equal 1, doc.rule_sets.length
26
+ rule_set = doc.rule_sets.first
27
+ assert_equal 4, rule_set.selectors.length
28
+ assert_equal 1, rule_set.declarations.length
29
+ assert_equal 'background', rule_set.declarations.first.property
30
+ end
163
31
 
164
- flexmock(@sac.document_handler).
165
- should_receive(:property).never
166
- @sac.parse('div h1 { }')
167
- flexmock_verify
168
- end
32
+ def test_media
33
+ doc = CSSPool.CSS <<-eocss
34
+ @media print {
35
+ div { background: red, blue; }
36
+ }
37
+ eocss
38
+ assert_equal 1, doc.rule_sets.first.media.length
39
+ end
169
40
 
170
- def test_properties_unary_op
171
- flexmock(@sac.document_handler).
172
- should_receive(:property).with('border-spacing', on { |list|
173
- list.length == 1 && list.first.dimension_unit_text == 'em' &&
174
- list.first.lexical_unit_type == :SAC_EM &&
175
- list.first.integer_value == -10
176
- }, false).once
177
- @sac.parse('div h1 { border-spacing: -10em; }')
178
- @sac.parse('div h1 { border: dashed black dotted; }')
179
- flexmock_verify
180
- end
41
+ def test_universal_to_css
42
+ doc = CSSPool.CSS <<-eocss
43
+ * { background: red, blue; }
44
+ eocss
45
+ assert_match '*', doc.to_css
46
+ end
181
47
 
182
- def test_properties_function
183
- flexmock(@sac.document_handler).
184
- should_receive(:property).with('clip', on { |list|
185
- list.length == 1 && list.first.dimension_unit_text.nil? &&
186
- list.first.lexical_unit_type == :SAC_RECT_FUNCTION &&
187
- list.first.string_value == "rect(1in, 1in, 1in, 2in)" &&
188
- list.first.parameters.length == 4 &&
189
- list.first.parameters.map { |x| x.to_s } == %w{ 1in 1in 1in 2in } &&
190
- list.first.integer_value.nil?
191
- }, false).once
192
- @sac.parse('div h1 { clip: rect(1in, 1in, 1in, 2in); }')
193
- flexmock_verify
194
- end
48
+ def test_doc_to_css
49
+ doc = CSSPool.CSS <<-eocss
50
+ div#a, a.foo, a:hover, a[href][int="10"]{ background: red, blue; }
51
+ eocss
52
+ assert_match 'div#a, a.foo, a:hover, a[href][int="10"]', doc.to_css
53
+ assert_match 'background: red, blue;', doc.to_css
54
+ end
195
55
 
196
- def test_important_properties
197
- flexmock(@sac.document_handler).
198
- should_receive(:property).with('color', on { |list|
199
- list.length == 1 && list.first.dimension_unit_text.nil? &&
200
- list.first.lexical_unit_type == :SAC_IDENT &&
201
- list.first.string_value == "black" &&
202
- list.first.integer_value.nil?
203
- }, true).once
204
- @sac.parse('h1 { color: black !important; }')
205
- flexmock_verify
206
- end
56
+ def test_doc_desc_to_css
57
+ doc = CSSPool.CSS <<-eocss
58
+ div > a { background: #123; }
59
+ eocss
60
+ assert_match 'div > a', doc.to_css
61
+ end
207
62
 
208
- def test_pseudo_function
209
- flexmock(@sac.document_handler).
210
- should_receive(:start_selector).ordered.once
211
- flexmock(@sac.document_handler).
212
- should_receive(:property).with('color', on { |list|
213
- list.length == 1 && list.first.dimension_unit_text.nil? &&
214
- list.first.lexical_unit_type == :SAC_IDENT &&
215
- list.first.string_value == "black" &&
216
- list.first.integer_value.nil?
217
- }, true).ordered.once
218
- flexmock(@sac.document_handler).
219
- should_receive(:end_selector).ordered.once
220
- @sac.parse('h1:nth(1) { color: black !important; }')
221
- flexmock_verify
222
- end
63
+ def test_doc_pseudo_to_css
64
+ doc = CSSPool.CSS <<-eocss
65
+ :hover { background: #123; }
66
+ eocss
67
+ assert_match ':hover', doc.to_css
68
+ end
223
69
 
224
- def test_pseudo_class
225
- flexmock(@sac.document_handler).
226
- should_receive(:start_selector).ordered.once
227
- flexmock(@sac.document_handler).
228
- should_receive(:property).with('color', on { |list|
229
- list.length == 1 && list.first.dimension_unit_text.nil? &&
230
- list.first.lexical_unit_type == :SAC_IDENT &&
231
- list.first.string_value == "black" &&
232
- list.first.integer_value.nil?
233
- }, true).ordered.once
234
- flexmock(@sac.document_handler).
235
- should_receive(:end_selector).ordered.once
236
- @sac.parse('a:hover { color: black !important; }')
237
- flexmock_verify
238
- end
70
+ def test_doc_id_to_css
71
+ doc = CSSPool.CSS <<-eocss
72
+ #hover { background: #123; }
73
+ eocss
74
+ assert_match '#hover', doc.to_css
75
+ end
239
76
 
240
- def test_pseudo_class_to_css
241
- class << @sac.document_handler
242
- attr_accessor :selectors
243
- alias :start_selector :selectors=
77
+ def test_important
78
+ doc = CSSPool.CSS <<-eocss
79
+ div > a { background: #123 !important; }
80
+ eocss
81
+ assert_match '!important', doc.to_css
244
82
  end
245
- @sac.parse('a:hover { color: black !important; }')
246
- assert_equal 'a:hover', @sac.document_handler.selectors.first.to_css
247
- end
248
83
 
249
- def test_pseudo_function_to_css
250
- class << @sac.document_handler
251
- attr_accessor :selectors
252
- alias :start_selector :selectors=
84
+ def test_doc_func_to_css
85
+ doc = CSSPool.CSS <<-eocss
86
+ div { border: foo(1, 2); }
87
+ eocss
88
+ assert_match('foo(1.0, 2.0)', doc.to_css)
253
89
  end
254
- @sac.parse('a:nth(3) { color: black !important; }')
255
- assert_equal 'a:nth(3)', @sac.document_handler.selectors.first.to_css
256
90
  end
257
91
  end
@@ -1,36 +1,47 @@
1
- require File.dirname(__FILE__) + "/helper"
2
-
3
- class SelectorTest < SelectorTestCase
4
- class MySelector < Selector; end
5
-
6
- def test_hash
7
- first = Selector.new(1)
8
- second = Selector.new(1)
9
- assert_equal first.hash, second.hash
10
-
11
- third = Selector.new(2)
12
- assert_not_equal first.hash, third.hash
13
- end
14
-
15
- def test_equals2
16
- first = Selector.new(1)
17
- second = Selector.new(1)
18
- assert_equal first, second
19
-
20
- third = Selector.new(2)
21
- assert_not_equal first, third
22
-
23
- fourth = MySelector.new(1)
24
- assert_equal first, fourth
25
- assert_not_equal first, 1
26
- end
27
-
28
- def test_eql?
29
- first = Selector.new(1)
30
- second = Selector.new(1)
31
- assert first.eql?(second)
32
-
33
- third = Selector.new(2)
34
- assert !first.eql?(third)
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ class TestSelector < CSSPool::TestCase
5
+ def test_specificity
6
+ doc = CSSPool.CSS <<-eocss
7
+ *, foo > bar, #hover, :hover, div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
8
+ eocss
9
+ selectors = doc.rule_sets.first.selectors
10
+ specs = selectors.map do |sel|
11
+ sel.specificity
12
+ end
13
+ assert_equal [[0, 0, 1],
14
+ [0, 0, 2],
15
+ [1, 0, 1],
16
+ [0, 1, 1],
17
+ [1, 0, 1],
18
+ [0, 1, 1],
19
+ [0, 1, 1],
20
+ [0, 2, 1]], specs
21
+ end
22
+
23
+ def test_selector_knows_its_ruleset
24
+ doc = CSSPool.CSS <<-eocss
25
+ a[href][int="10"]{ background: red; }
26
+ eocss
27
+ rs = doc.rule_sets.first
28
+ assert_equal rs, rs.selectors.first.rule_set
29
+ end
30
+
31
+ def test_selector_gets_declarations
32
+ doc = CSSPool.CSS <<-eocss
33
+ a[href][int="10"]{ background: red; }
34
+ eocss
35
+ rs = doc.rule_sets.first
36
+ assert_equal rs.declarations, rs.selectors.first.declarations
37
+ end
38
+
39
+ def test_declaration_should_know_ruleset
40
+ doc = CSSPool.CSS <<-eocss
41
+ a[href][int="10"]{ background: red; }
42
+ eocss
43
+ rs = doc.rule_sets.first
44
+ rs.declarations.each { |del| assert_equal rs, del.rule_set }
45
+ end
35
46
  end
36
47
  end