css_parser 1.2.2 → 3.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.
@@ -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