css_parser 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +33 -0
- data/LICENSE +42 -0
- data/README +60 -0
- data/lib/css_parser/parser.rb +345 -0
- data/lib/css_parser/regexps.rb +46 -0
- data/lib/css_parser/rule_set.rb +381 -0
- data/lib/css_parser.rb +149 -0
- data/test/fixtures/import-circular-reference.css +4 -0
- data/test/fixtures/import-with-media-types.css +3 -0
- data/test/fixtures/import1.css +3 -0
- data/test/fixtures/simple.css +6 -0
- data/test/fixtures/subdir/import2.css +3 -0
- data/test/test_css_parser_basic.rb +56 -0
- data/test/test_css_parser_downloading.rb +81 -0
- data/test/test_css_parser_media_types.rb +71 -0
- data/test/test_css_parser_misc.rb +143 -0
- data/test/test_css_parser_regexps.rb +68 -0
- data/test/test_helper.rb +8 -0
- data/test/test_merging.rb +88 -0
- data/test/test_rule_set.rb +74 -0
- data/test/test_rule_set_creating_shorthand.rb +90 -0
- data/test/test_rule_set_expanding_shorthand.rb +178 -0
- metadata +82 -0
@@ -0,0 +1,178 @@
|
|
1
|
+
require 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_getting_dimensions_from_shorthand
|
12
|
+
# test various shorthand forms
|
13
|
+
['margin: 0px auto', 'margin: 0px auto 0px', 'margin: 0px auto 0px'].each do |shorthand|
|
14
|
+
declarations = expand_declarations(shorthand)
|
15
|
+
assert_equal({"margin-right" => "auto", "margin-bottom" => "0px", "margin-left" => "auto", "margin-top" => "0px"}, declarations)
|
16
|
+
end
|
17
|
+
|
18
|
+
# test various units
|
19
|
+
['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
|
20
|
+
shorthand = "margin: 0% -0.123#{unit} 9px -.9pc"
|
21
|
+
declarations = expand_declarations(shorthand)
|
22
|
+
assert_equal({"margin-right" => "-0.123#{unit}", "margin-bottom" => "9px", "margin-left" => "-.9pc", "margin-top" => "0%"}, declarations)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# ==== Font shorthand
|
28
|
+
def test_getting_font_size_from_shorthand
|
29
|
+
['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
|
30
|
+
shorthand = "font: 300 italic 11.25#{unit}/14px verdana, helvetica, sans-serif;"
|
31
|
+
declarations = expand_declarations(shorthand)
|
32
|
+
assert_equal("11.25#{unit}", declarations['font-size'])
|
33
|
+
end
|
34
|
+
|
35
|
+
['smaller', 'small', 'medium', 'large', 'x-large', 'auto'].each do |unit|
|
36
|
+
shorthand = "font: 300 italic #{unit}/14px verdana, helvetica, sans-serif;"
|
37
|
+
declarations = expand_declarations(shorthand)
|
38
|
+
assert_equal(unit, declarations['font-size'])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_getting_font_families_from_shorthand
|
43
|
+
shorthand = "font: 300 italic 12px/14px \"Helvetica-Neue-Light 45\", 'verdana', helvetica, sans-serif;"
|
44
|
+
declarations = expand_declarations(shorthand)
|
45
|
+
assert_equal("\"Helvetica-Neue-Light 45\", 'verdana', helvetica, sans-serif", declarations['font-family'])
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_getting_font_weight_from_shorthand
|
49
|
+
['300', 'bold', 'bolder', 'lighter', 'normal'].each do |unit|
|
50
|
+
shorthand = "font: #{unit} italic 12px sans-serif;"
|
51
|
+
declarations = expand_declarations(shorthand)
|
52
|
+
assert_equal(unit, declarations['font-weight'])
|
53
|
+
end
|
54
|
+
|
55
|
+
# ensure normal is the default state
|
56
|
+
['font: normal italic 12px sans-serif;', 'font: italic 12px sans-serif;',
|
57
|
+
'font: small-caps normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
|
58
|
+
declarations = expand_declarations(shorthand)
|
59
|
+
assert_equal('normal', declarations['font-weight'], shorthand)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_getting_font_variant_from_shorthand
|
64
|
+
shorthand = "font: small-caps italic 12px sans-serif;"
|
65
|
+
declarations = expand_declarations(shorthand)
|
66
|
+
assert_equal('small-caps', declarations['font-variant'])
|
67
|
+
|
68
|
+
# ensure normal is the default state
|
69
|
+
['font: normal italic 12px sans-serif;', 'font: italic 12px sans-serif;',
|
70
|
+
'font: normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
|
71
|
+
declarations = expand_declarations(shorthand)
|
72
|
+
assert_equal('normal', declarations['font-variant'], shorthand)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_getting_font_style_from_shorthand
|
77
|
+
['italic', 'oblique'].each do |unit|
|
78
|
+
shorthand = "font: normal #{unit} bold 12px sans-serif;"
|
79
|
+
declarations = expand_declarations(shorthand)
|
80
|
+
assert_equal(unit, declarations['font-style'])
|
81
|
+
end
|
82
|
+
|
83
|
+
# ensure normal is the default state
|
84
|
+
['font: normal bold 12px sans-serif;', 'font: small-caps 12px sans-serif;',
|
85
|
+
'font: normal 12px sans-serif;', 'font: 12px/16px sans-serif;'].each do |shorthand|
|
86
|
+
declarations = expand_declarations(shorthand)
|
87
|
+
assert_equal('normal', declarations['font-style'], shorthand)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_getting_line_height_from_shorthand
|
92
|
+
['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
|
93
|
+
shorthand = "font: 300 italic 12px/0.25#{unit} verdana, helvetica, sans-serif;"
|
94
|
+
declarations = expand_declarations(shorthand)
|
95
|
+
assert_equal("0.25#{unit}", declarations['line-height'])
|
96
|
+
end
|
97
|
+
|
98
|
+
# ensure normal is the default state
|
99
|
+
['font: normal bold 12px sans-serif;', 'font: small-caps 12px sans-serif;',
|
100
|
+
'font: normal 12px sans-serif;', 'font: 12px sans-serif;'].each do |shorthand|
|
101
|
+
declarations = expand_declarations(shorthand)
|
102
|
+
assert_equal('normal', declarations['line-height'], shorthand)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
# ==== Background shorthand
|
108
|
+
def test_getting_background_properties_from_shorthand
|
109
|
+
expected = {"background-image" => "url('chess.png')", "background-color" => "gray", "background-repeat" => "repeat",
|
110
|
+
"background-attachment" => "fixed", "background-position" => "50%"}
|
111
|
+
|
112
|
+
shorthand = "background: url('chess.png') gray 50% repeat fixed;"
|
113
|
+
declarations = expand_declarations(shorthand)
|
114
|
+
assert_equal expected, declarations
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_getting_background_position_from_shorthand
|
118
|
+
['em', 'ex', 'in', 'px', 'pt', 'pc', '%'].each do |unit|
|
119
|
+
shorthand = "background: url('chess.png') gray 30% -0.15#{unit} repeat fixed;"
|
120
|
+
declarations = expand_declarations(shorthand)
|
121
|
+
assert_equal("30% -0.15#{unit}", declarations['background-position'])
|
122
|
+
end
|
123
|
+
|
124
|
+
['left', 'center', 'right', 'top', 'bottom', 'inherit'].each do |position|
|
125
|
+
shorthand = "background: url('chess.png') #000fff #{position} no-repeat fixed;"
|
126
|
+
declarations = expand_declarations(shorthand)
|
127
|
+
assert_equal(position, declarations['background-position'])
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_getting_background_colour_from_shorthand
|
132
|
+
['blue', 'lime', 'rgb(10,10,10)', 'rgb ( -10%, 99, 300)', '#ffa0a0', '#03c', 'trAnsparEnt', 'inherit'].each do |colour|
|
133
|
+
shorthand = "background:#{colour} url('chess.png') center repeat fixed ;"
|
134
|
+
declarations = expand_declarations(shorthand)
|
135
|
+
assert_equal(colour, declarations['background-color'])
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_getting_background_attachment_from_shorthand
|
140
|
+
['scroll', 'fixed', 'inherit'].each do |attachment|
|
141
|
+
shorthand = "background:#0f0f0f url('chess.png') center repeat #{attachment};"
|
142
|
+
declarations = expand_declarations(shorthand)
|
143
|
+
assert_equal(attachment, declarations['background-attachment'])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_getting_background_repeat_from_shorthand
|
148
|
+
['repeat-x', 'repeat-y', 'no-repeat', 'inherit'].each do |repeat|
|
149
|
+
shorthand = "background:#0f0f0f none #{repeat};"
|
150
|
+
declarations = expand_declarations(shorthand)
|
151
|
+
assert_equal(repeat, declarations['background-repeat'])
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_getting_background_image_from_shorthand
|
156
|
+
['url("chess.png")', 'url("https://example.org:80/~files/chess.png?123=abc&test#5")',
|
157
|
+
'url(https://example.org:80/~files/chess.png?123=abc&test#5)',
|
158
|
+
"url('https://example.org:80/~files/chess.png?123=abc&test#5')", 'none', 'inherit'].each do |image|
|
159
|
+
|
160
|
+
shorthand = "background: #0f0f0f #{image} ;"
|
161
|
+
declarations = expand_declarations(shorthand)
|
162
|
+
assert_equal(image, declarations['background-image'])
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
protected
|
168
|
+
def expand_declarations(declarations)
|
169
|
+
ruleset = RuleSet.new(nil, declarations)
|
170
|
+
ruleset.expand_shorthand!
|
171
|
+
|
172
|
+
collected = {}
|
173
|
+
ruleset.each_declaration do |prop, val, imp|
|
174
|
+
collected[prop.to_s] = val.to_s
|
175
|
+
end
|
176
|
+
collected
|
177
|
+
end
|
178
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: css_parser
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.9.0
|
7
|
+
date: 2007-11-19 00:00:00 -08:00
|
8
|
+
summary: A set of classes for parsing CSS.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage: http://code.dunae.ca/css_parser
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Alex Dunae
|
31
|
+
files:
|
32
|
+
- lib/css_parser.rb
|
33
|
+
- lib/css_parser/parser.rb
|
34
|
+
- lib/css_parser/regexps.rb
|
35
|
+
- lib/css_parser/rule_set.rb
|
36
|
+
- test/fixtures
|
37
|
+
- test/fixtures/import-circular-reference.css
|
38
|
+
- test/fixtures/import-with-media-types.css
|
39
|
+
- test/fixtures/import1.css
|
40
|
+
- test/fixtures/simple.css
|
41
|
+
- test/fixtures/subdir
|
42
|
+
- test/fixtures/subdir/import2.css
|
43
|
+
- test/test_css_parser_basic.rb
|
44
|
+
- test/test_css_parser_downloading.rb
|
45
|
+
- test/test_css_parser_media_types.rb
|
46
|
+
- test/test_css_parser_misc.rb
|
47
|
+
- test/test_css_parser_regexps.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
- test/test_merging.rb
|
50
|
+
- test/test_rule_set.rb
|
51
|
+
- test/test_rule_set_creating_shorthand.rb
|
52
|
+
- test/test_rule_set_expanding_shorthand.rb
|
53
|
+
- README
|
54
|
+
- CHANGELOG
|
55
|
+
- LICENSE
|
56
|
+
test_files:
|
57
|
+
- test/test_css_parser_basic.rb
|
58
|
+
- test/test_css_parser_downloading.rb
|
59
|
+
- test/test_css_parser_media_types.rb
|
60
|
+
- test/test_css_parser_misc.rb
|
61
|
+
- test/test_css_parser_regexps.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
- test/test_merging.rb
|
64
|
+
- test/test_rule_set.rb
|
65
|
+
- test/test_rule_set_creating_shorthand.rb
|
66
|
+
- test/test_rule_set_expanding_shorthand.rb
|
67
|
+
rdoc_options:
|
68
|
+
- --all
|
69
|
+
- --inline-source
|
70
|
+
- --line-numbers
|
71
|
+
extra_rdoc_files:
|
72
|
+
- README
|
73
|
+
- CHANGELOG
|
74
|
+
- LICENSE
|
75
|
+
executables: []
|
76
|
+
|
77
|
+
extensions: []
|
78
|
+
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
dependencies: []
|
82
|
+
|