css_parser 1.2.6 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require "set"
3
-
4
- # Test cases for parsing CSS blocks
5
- class RuleSetTests < Test::Unit::TestCase
6
- include CssParser
7
-
8
- def setup
9
- @cp = Parser.new
10
- end
11
-
12
- def test_setting_property_values
13
- rs = RuleSet.new(nil, nil)
14
-
15
- rs['background-color'] = 'red'
16
- assert_equal('red;', rs['background-color'])
17
-
18
- rs['background-color'] = 'blue !important;'
19
- assert_equal('blue !important;', rs['background-color'])
20
- end
21
-
22
- def test_getting_property_values
23
- rs = RuleSet.new('#content p, a', 'color: #fff;')
24
- assert_equal('#fff;', rs['color'])
25
- end
26
-
27
- def test_getting_property_value_ignoring_case
28
- rs = RuleSet.new('#content p, a', 'color: #fff;')
29
- assert_equal('#fff;', rs[' ColoR '])
30
- end
31
-
32
- def test_each_selector
33
- expected = [
34
- {:selector => "#content p", :declarations => "color: #fff;", :specificity => 101},
35
- {:selector => "a", :declarations => "color: #fff;", :specificity => 1}
36
- ]
37
-
38
- actual = []
39
- rs = RuleSet.new('#content p, a', 'color: #fff;')
40
- rs.each_selector do |sel, decs, spec|
41
- actual << {:selector => sel, :declarations => decs, :specificity => spec}
42
- end
43
-
44
- assert_equal(expected, actual)
45
- end
46
-
47
- def test_each_declaration
48
- expected = Set.new([
49
- {:property => 'margin', :value => '1px -0.25em', :is_important => false},
50
- {:property => 'background', :value => 'white none no-repeat', :is_important => true},
51
- {:property => 'color', :value => '#fff', :is_important => false}
52
- ])
53
-
54
- actual = Set.new
55
- rs = RuleSet.new(nil, 'color: #fff; Background: white none no-repeat !important; margin: 1px -0.25em;')
56
- rs.each_declaration do |prop, val, imp|
57
- actual << {:property => prop, :value => val, :is_important => imp}
58
- end
59
-
60
- assert_equal(expected, actual)
61
- end
62
-
63
- def test_each_declaration_respects_order
64
- css_fragment = "margin: 0; padding: 20px; margin-bottom: 28px;"
65
- rs = RuleSet.new(nil, css_fragment)
66
- expected = %w(margin padding margin-bottom)
67
- actual = []
68
- rs.each_declaration { |prop, val, imp| actual << prop }
69
- assert_equal(expected, actual)
70
- end
71
-
72
- def test_declarations_to_s
73
- declarations = 'color: #fff; font-weight: bold;'
74
- rs = RuleSet.new('#content p, a', declarations)
75
- assert_equal(declarations.split(' ').sort, rs.declarations_to_s.split(' ').sort)
76
- end
77
-
78
- def test_important_declarations_to_s
79
- declarations = 'color: #fff; font-weight: bold !important;'
80
- rs = RuleSet.new('#content p, a', declarations)
81
- assert_equal(declarations.split(' ').sort, rs.declarations_to_s.split(' ').sort)
82
- end
83
-
84
- def test_overriding_specificity
85
- rs = RuleSet.new('#content p, a', 'color: white', 1000)
86
- rs.each_selector do |sel, decs, spec|
87
- assert_equal 1000, spec
88
- end
89
- end
90
- end
@@ -1,143 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- # Test cases for reading and generating CSS shorthand properties
4
- class RuleSetCreatingShorthandTests < Test::Unit::TestCase
5
- include CssParser
6
-
7
- def setup
8
- @cp = CssParser::Parser.new
9
- end
10
-
11
- # ==== Border shorthand
12
- def test_combining_borders_into_shorthand
13
- properties = {'border-top-width' => 'auto', 'border-right-width' => 'thin', 'border-bottom-width' => 'auto', 'border-left-width' => '0px'}
14
-
15
- combined = create_shorthand(properties)
16
-
17
- assert_equal('', combined['border'])
18
- assert_equal('auto thin auto 0px;', combined['border-width'])
19
-
20
- # after creating shorthand, all long-hand properties should be deleted
21
- assert_properties_are_deleted(combined, properties)
22
-
23
- # should not combine if any properties are missing
24
- properties.delete('border-top-width')
25
-
26
- combined = create_shorthand(properties)
27
-
28
- assert_equal '', combined['border-width']
29
-
30
- properties = {'border-width' => '22%', 'border-color' => 'rgba(255, 0, 0)'}
31
- combined = create_shorthand(properties)
32
- assert_equal '22% rgba(255, 0, 0);', combined['border']
33
- assert_equal '', combined['border-width']
34
-
35
- properties = {'border-top-style' => 'none', 'border-right-style' => 'none', 'border-bottom-style' => 'none', 'border-left-style' => 'none'}
36
- combined = create_shorthand(properties)
37
- assert_equal 'none;', combined['border']
38
- end
39
-
40
- # ==== Dimensions shorthand
41
- def test_combining_dimensions_into_shorthand
42
- properties = {'margin-right' => 'auto', 'margin-bottom' => '0px', 'margin-left' => 'auto', 'margin-top' => '0px',
43
- 'padding-right' => '1.25em', 'padding-bottom' => '11%', 'padding-left' => '3pc', 'padding-top' => '11.25ex'}
44
-
45
- combined = create_shorthand(properties)
46
-
47
- assert_equal('0px auto;', combined['margin'])
48
- assert_equal('11.25ex 1.25em 11% 3pc;', combined['padding'])
49
-
50
- # after creating shorthand, all long-hand properties should be deleted
51
- assert_properties_are_deleted(combined, properties)
52
-
53
- # should not combine if any properties are missing
54
- properties.delete('margin-right')
55
- properties.delete('padding-right')
56
-
57
- combined = create_shorthand(properties)
58
-
59
- assert_equal '', combined['margin']
60
- assert_equal '', combined['padding']
61
- end
62
-
63
- # ==== Dimensions shorthand, auto property
64
- def test_combining_dimensions_into_shorthand_with_auto
65
- rs = RuleSet.new('#page', "margin: 0; margin-left: auto; margin-right: auto;")
66
- rs.expand_shorthand!
67
- assert_equal('auto;', rs['margin-left'])
68
- rs.create_shorthand!
69
- assert_equal('0 auto;', rs['margin'])
70
- end
71
-
72
- # ==== Font shorthand
73
- def test_combining_font_into_shorthand
74
- # should combine if all font properties are present
75
- properties = {"font-weight" => "300", "font-size" => "12pt",
76
- "font-family" => "sans-serif", "line-height" => "18px",
77
- "font-style" => "oblique", "font-variant" => "small-caps"}
78
-
79
- combined = create_shorthand(properties)
80
- assert_equal('oblique small-caps 300 12pt/18px sans-serif;', combined['font'])
81
-
82
- # after creating shorthand, all long-hand properties should be deleted
83
- assert_properties_are_deleted(combined, properties)
84
-
85
- # should not combine if any properties are missing
86
- properties.delete('font-weight')
87
- combined = create_shorthand(properties)
88
- assert_equal '', combined['font']
89
- end
90
-
91
- # ==== Background shorthand
92
- def test_combining_background_into_shorthand
93
- properties = {'background-image' => 'url(\'chess.png\')', 'background-color' => 'gray',
94
- 'background-position' => 'center -10.2%', 'background-attachment' => 'fixed',
95
- 'background-repeat' => 'no-repeat'}
96
-
97
- combined = create_shorthand(properties)
98
-
99
- assert_equal('gray url(\'chess.png\') no-repeat center -10.2% fixed;', combined['background'])
100
-
101
- # after creating shorthand, all long-hand properties should be deleted
102
- assert_properties_are_deleted(combined, properties)
103
- end
104
-
105
-
106
- # ==== List-style shorthand
107
- def test_combining_list_style_into_shorthand
108
- properties = {'list-style-image' => 'url(\'chess.png\')', 'list-style-type' => 'katakana',
109
- 'list-style-position' => 'inside'}
110
-
111
- combined = create_shorthand(properties)
112
-
113
- assert_equal('katakana inside url(\'chess.png\');', combined['list-style'])
114
-
115
- # after creating shorthand, all long-hand properties should be deleted
116
- assert_properties_are_deleted(combined, properties)
117
- end
118
-
119
-
120
- def test_property_values_in_url
121
- rs = RuleSet.new('#header', "background:url(http://example.com/1528/www/top-logo.jpg) no-repeat top right; padding: 79px 0 10px 0; text-align:left;")
122
- rs.expand_shorthand!
123
- assert_equal('top right;', rs['background-position'])
124
- rs.create_shorthand!
125
- assert_equal('url(http://example.com/1528/www/top-logo.jpg) no-repeat top right;', rs['background'])
126
- end
127
-
128
- protected
129
- def assert_properties_are_deleted(ruleset, properties)
130
- properties.each do |property, value|
131
- assert_equal '', ruleset[property]
132
- end
133
- end
134
-
135
- def create_shorthand(properties)
136
- ruleset = RuleSet.new(nil, nil)
137
- properties.each do |property, value|
138
- ruleset[property] = value
139
- end
140
- ruleset.create_shorthand!
141
- ruleset
142
- end
143
- end
@@ -1,223 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class RuleSetExpandingShorthandTests < Test::Unit::TestCase
4
- include CssParser
5
-
6
- def setup
7
- @cp = CssParser::Parser.new
8
- end
9
-
10
- # ==== Dimensions shorthand
11
- def test_expanding_border_shorthand
12
- declarations = expand_declarations('border: none')
13
- assert_equal 'none', declarations['border-right-style']
14
-
15
- declarations = expand_declarations('border: 1px solid red')
16
- assert_equal '1px', declarations['border-top-width']
17
- assert_equal 'solid', declarations['border-bottom-style']
18
-
19
- declarations = expand_declarations('border-color: red hsla(255, 0, 0, 5) rgb(2% ,2%,2%)')
20
- assert_equal 'red', declarations['border-top-color']
21
- assert_equal 'rgb(2%,2%,2%)', declarations['border-bottom-color']
22
- assert_equal 'hsla(255,0,0,5)', declarations['border-left-color']
23
-
24
- declarations = expand_declarations('border: thin dot-dot-dash')
25
- assert_equal 'dot-dot-dash', declarations['border-left-style']
26
- assert_equal 'thin', declarations['border-left-width']
27
- assert_nil declarations['border-left-color']
28
- end
29
-
30
- # ==== Dimensions shorthand
31
- def test_getting_dimensions_from_shorthand
32
- # test various shorthand forms
33
- ['margin: 0px auto', 'margin: 0px auto 0px', 'margin: 0px auto 0px'].each do |shorthand|
34
- declarations = expand_declarations(shorthand)
35
- assert_equal({"margin-right" => "auto", "margin-bottom" => "0px", "margin-left" => "auto", "margin-top" => "0px"}, declarations)
36
- end
37
-
38
- # test various units
39
- ['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
40
- shorthand = "margin: 0% -0.123#{unit} 9px -.9pc"
41
- declarations = expand_declarations(shorthand)
42
- assert_equal({"margin-right" => "-0.123#{unit}", "margin-bottom" => "9px", "margin-left" => "-.9pc", "margin-top" => "0%"}, declarations)
43
- end
44
- end
45
-
46
-
47
- # ==== Font shorthand
48
- def test_getting_font_size_from_shorthand
49
- ['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
50
- shorthand = "font: 300 italic 11.25#{unit}/14px verdana, helvetica, sans-serif;"
51
- declarations = expand_declarations(shorthand)
52
- assert_equal("11.25#{unit}", declarations['font-size'])
53
- end
54
-
55
- ['smaller', 'small', 'medium', 'large', 'x-large', 'auto'].each do |unit|
56
- shorthand = "font: 300 italic #{unit}/14px verdana, helvetica, sans-serif;"
57
- declarations = expand_declarations(shorthand)
58
- assert_equal(unit, declarations['font-size'])
59
- end
60
- end
61
-
62
- def test_getting_font_families_from_shorthand
63
- shorthand = "font: 300 italic 12px/14px \"Helvetica-Neue-Light 45\", 'verdana', helvetica, sans-serif;"
64
- declarations = expand_declarations(shorthand)
65
- assert_equal("\"Helvetica-Neue-Light 45\", 'verdana', helvetica, sans-serif", declarations['font-family'])
66
- end
67
-
68
- def test_getting_font_weight_from_shorthand
69
- ['300', 'bold', 'bolder', 'lighter', 'normal'].each do |unit|
70
- shorthand = "font: #{unit} italic 12px sans-serif;"
71
- declarations = expand_declarations(shorthand)
72
- assert_equal(unit, declarations['font-weight'])
73
- end
74
-
75
- # ensure normal is the default state
76
- ['font: normal italic 12px sans-serif;', 'font: italic 12px sans-serif;',
77
- 'font: small-caps normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
78
- declarations = expand_declarations(shorthand)
79
- assert_equal('normal', declarations['font-weight'], shorthand)
80
- end
81
- end
82
-
83
- def test_getting_font_variant_from_shorthand
84
- shorthand = "font: small-caps italic 12px sans-serif;"
85
- declarations = expand_declarations(shorthand)
86
- assert_equal('small-caps', declarations['font-variant'])
87
-
88
- # ensure normal is the default state
89
- ['font: normal italic 12px sans-serif;', 'font: italic 12px sans-serif;',
90
- 'font: normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
91
- declarations = expand_declarations(shorthand)
92
- assert_equal('normal', declarations['font-variant'], shorthand)
93
- end
94
- end
95
-
96
- def test_getting_font_style_from_shorthand
97
- ['italic', 'oblique'].each do |unit|
98
- shorthand = "font: normal #{unit} bold 12px sans-serif;"
99
- declarations = expand_declarations(shorthand)
100
- assert_equal(unit, declarations['font-style'])
101
- end
102
-
103
- # ensure normal is the default state
104
- ['font: normal bold 12px sans-serif;', 'font: small-caps 12px sans-serif;',
105
- 'font: normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
106
- declarations = expand_declarations(shorthand)
107
- assert_equal('normal', declarations['font-style'], shorthand)
108
- end
109
- end
110
-
111
- def test_getting_line_height_from_shorthand
112
- ['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
113
- shorthand = "font: 300 italic 12px/0.25#{unit} verdana, helvetica, sans-serif;"
114
- declarations = expand_declarations(shorthand)
115
- assert_equal("0.25#{unit}", declarations['line-height'])
116
- end
117
-
118
- # ensure normal is the default state
119
- ['font: normal bold 12px sans-serif;', 'font: small-caps 12px sans-serif;',
120
- 'font: normal 12px sans-serif;', 'font: 12px sans-serif;'].each do |shorthand|
121
- declarations = expand_declarations(shorthand)
122
- assert_equal('normal', declarations['line-height'], shorthand)
123
- end
124
- end
125
-
126
-
127
- # ==== Background shorthand
128
- def test_getting_background_properties_from_shorthand
129
- expected = {"background-image" => "url('chess.png')", "background-color" => "gray", "background-repeat" => "repeat",
130
- "background-attachment" => "fixed", "background-position" => "50%"}
131
-
132
- shorthand = "background: url('chess.png') gray 50% repeat fixed;"
133
- declarations = expand_declarations(shorthand)
134
- assert_equal expected, declarations
135
- end
136
-
137
- def test_getting_background_position_from_shorthand
138
- ['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
139
- shorthand = "background: url('chess.png') gray 30% -0.15#{unit} repeat fixed;"
140
- declarations = expand_declarations(shorthand)
141
- assert_equal("30% -0.15#{unit}", declarations['background-position'])
142
- end
143
-
144
- ['left', 'center', 'right', 'top', 'bottom', 'inherit'].each do |position|
145
- shorthand = "background: url('chess.png') #000fff #{position} no-repeat fixed;"
146
- declarations = expand_declarations(shorthand)
147
- assert_equal(position, declarations['background-position'])
148
- end
149
- end
150
-
151
- def test_getting_background_colour_from_shorthand
152
- ['blue', 'lime', 'rgb(10,10,10)', 'rgb ( -10%, 99, 300)', '#ffa0a0', '#03c', 'trAnsparEnt', 'inherit'].each do |colour|
153
- shorthand = "background:#{colour} url('chess.png') center repeat fixed ;"
154
- declarations = expand_declarations(shorthand)
155
- assert_equal(colour, declarations['background-color'])
156
- end
157
- end
158
-
159
- def test_getting_background_attachment_from_shorthand
160
- ['scroll', 'fixed', 'inherit'].each do |attachment|
161
- shorthand = "background:#0f0f0f url('chess.png') center repeat #{attachment};"
162
- declarations = expand_declarations(shorthand)
163
- assert_equal(attachment, declarations['background-attachment'])
164
- end
165
- end
166
-
167
- def test_getting_background_repeat_from_shorthand
168
- ['repeat-x', 'repeat-y', 'no-repeat', 'inherit'].each do |repeat|
169
- shorthand = "background:#0f0f0f none #{repeat};"
170
- declarations = expand_declarations(shorthand)
171
- assert_equal(repeat, declarations['background-repeat'])
172
- end
173
- end
174
-
175
- def test_getting_background_image_from_shorthand
176
- ['url("chess.png")', 'url("https://example.org:80/~files/chess.png?123=abc&test#5")',
177
- 'url(https://example.org:80/~files/chess.png?123=abc&test#5)',
178
- "url('https://example.org:80/~files/chess.png?123=abc&test#5')", 'none', 'inherit'].each do |image|
179
-
180
- shorthand = "background: #0f0f0f #{image} ;"
181
- declarations = expand_declarations(shorthand)
182
- assert_equal(image, declarations['background-image'])
183
- end
184
- end
185
-
186
- # ==== List-style shorthand
187
- def test_getting_list_style_properties_from_shorthand
188
- expected = {'list-style-image' => 'url(\'chess.png\')', 'list-style-type' => 'katakana',
189
- 'list-style-position' => 'inside'}
190
-
191
- shorthand = "list-style: katakana inside url(\'chess.png\');"
192
- declarations = expand_declarations(shorthand)
193
- assert_equal expected, declarations
194
- end
195
-
196
- def test_getting_list_style_position_from_shorthand
197
- ['inside', 'outside'].each do |position|
198
- shorthand = "list-style: katakana #{position} url('chess.png');"
199
- declarations = expand_declarations(shorthand)
200
- assert_equal(position, declarations['list-style-position'])
201
- end
202
- end
203
-
204
- def test_getting_list_style_type_from_shorthand
205
- ['disc', 'circle', 'square', 'decimal', 'decimal-leading-zero', 'lower-roman', 'upper-roman', 'lower-greek', 'lower-alpha', 'lower-latin', 'upper-alpha', 'upper-latin', 'hebrew', 'armenian', 'georgian', 'cjk-ideographic', 'hiragana', 'katakana', 'hira-gana-iroha', 'katakana-iroha', 'none'].each do |type|
206
- shorthand = "list-style: #{type} inside url('chess.png');"
207
- declarations = expand_declarations(shorthand)
208
- assert_equal(type, declarations['list-style-type'])
209
- end
210
- end
211
-
212
- protected
213
- def expand_declarations(declarations)
214
- ruleset = RuleSet.new(nil, declarations)
215
- ruleset.expand_shorthand!
216
-
217
- collected = {}
218
- ruleset.each_declaration do |prop, val, imp|
219
- collected[prop.to_s] = val.to_s
220
- end
221
- collected
222
- end
223
- end