DanaDanger-css_parser 0.9.1

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.
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for parsing CSS blocks
4
+ class RuleSetTests < Test::Unit::TestCase
5
+ include CssParser
6
+
7
+ def setup
8
+ @cp = Parser.new
9
+ end
10
+
11
+ def test_setting_property_values
12
+ rs = RuleSet.new(nil, nil)
13
+
14
+ rs['background-color'] = 'red'
15
+ assert_equal('red;', rs['background-color'])
16
+
17
+ rs['background-color'] = 'blue !important;'
18
+ assert_equal('blue !important;', rs['background-color'])
19
+ end
20
+
21
+ def test_getting_property_values
22
+ rs = RuleSet.new('#content p, a', 'color: #fff;')
23
+ assert_equal('#fff;', rs['color'])
24
+ end
25
+
26
+ def test_getting_property_value_ignoring_case
27
+ rs = RuleSet.new('#content p, a', 'color: #fff;')
28
+ assert_equal('#fff;', rs[' ColoR '])
29
+ end
30
+
31
+ def test_each_selector
32
+ expected = [
33
+ {:selector => "#content p", :declarations => "color: #fff;", :specificity => 101},
34
+ {:selector => "a", :declarations => "color: #fff;", :specificity => 1}
35
+ ]
36
+
37
+ actual = []
38
+ rs = RuleSet.new('#content p, a', 'color: #fff;')
39
+ rs.each_selector do |sel, decs, spec|
40
+ actual << {:selector => sel, :declarations => decs, :specificity => spec}
41
+ end
42
+
43
+ assert_equal(expected, actual)
44
+ end
45
+
46
+ def test_each_declaration
47
+ expected = [
48
+ {:property => 'margin', :value => '1px -0.25em', :is_important => false},
49
+ {:property => 'background', :value => 'white none no-repeat', :is_important => true},
50
+ {:property => 'color', :value => '#fff', :is_important => false}
51
+ ]
52
+
53
+ actual = []
54
+ rs = RuleSet.new(nil, 'color: #fff; Background: white none no-repeat !important; margin: 1px -0.25em;')
55
+ rs.each_declaration do |prop, val, imp|
56
+ actual << {:property => prop, :value => val, :is_important => imp}
57
+ end
58
+
59
+ assert_equal(expected, actual)
60
+ end
61
+
62
+ def test_declarations_to_s
63
+ declarations = 'color: #fff; font-weight: bold;'
64
+ rs = RuleSet.new('#content p, a', declarations)
65
+ assert_equal(declarations.split(' ').sort, rs.declarations_to_s.split(' ').sort)
66
+ end
67
+
68
+ def test_overriding_specificity
69
+ rs = RuleSet.new('#content p, a', 'color: white', 1000)
70
+ rs.each_selector do |sel, decs, spec|
71
+ assert_equal 1000, spec
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,90 @@
1
+ require 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
+ # ==== Dimensions shorthand
12
+ def test_combining_dimensions_into_shorthand
13
+ properties = {'margin-right' => 'auto', 'margin-bottom' => '0px', 'margin-left' => 'auto', 'margin-top' => '0px',
14
+ 'padding-right' => '1.25em', 'padding-bottom' => '11%', 'padding-left' => '3pc', 'padding-top' => '11.25ex'}
15
+
16
+ combined = create_shorthand(properties)
17
+
18
+ assert_equal('0px auto;', combined['margin'])
19
+ assert_equal('11.25ex 1.25em 11% 3pc;', combined['padding'])
20
+
21
+ # after creating shorthand, all long-hand properties should be deleted
22
+ assert_properties_are_deleted(combined, properties)
23
+
24
+ # should not combine if any properties are missing
25
+ properties.delete('margin-right')
26
+ properties.delete('padding-right')
27
+
28
+ combined = create_shorthand(properties)
29
+
30
+ assert_equal '', combined['margin']
31
+ assert_equal '', combined['padding']
32
+ end
33
+
34
+ # ==== Font shorthand
35
+ def test_combining_font_into_shorthand
36
+ # should combine if all font properties are present
37
+ properties = {"font-weight" => "300", "font-size" => "12pt",
38
+ "font-family" => "sans-serif", "line-height" => "18px",
39
+ "font-style" => "oblique", "font-variant" => "small-caps"}
40
+
41
+ combined = create_shorthand(properties)
42
+ assert_equal('oblique small-caps 300 12pt/18px sans-serif;', combined['font'])
43
+
44
+ # after creating shorthand, all long-hand properties should be deleted
45
+ assert_properties_are_deleted(combined, properties)
46
+
47
+ # should not combine if any properties are missing
48
+ properties.delete('font-weight')
49
+ combined = create_shorthand(properties)
50
+ assert_equal '', combined['font']
51
+ end
52
+
53
+ # ==== Background shorthand
54
+ def test_combining_background_into_shorthand
55
+ properties = {'background-image' => 'url(\'chess.png\')', 'background-color' => 'gray',
56
+ 'background-position' => 'center -10.2%', 'background-attachment' => 'fixed',
57
+ 'background-repeat' => 'no-repeat'}
58
+
59
+ combined = create_shorthand(properties)
60
+
61
+ assert_equal('gray url(\'chess.png\') no-repeat center -10.2% fixed;', combined['background'])
62
+
63
+ # after creating shorthand, all long-hand properties should be deleted
64
+ assert_properties_are_deleted(combined, properties)
65
+ end
66
+
67
+ def test_property_values_in_url
68
+ 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;")
69
+ rs.expand_shorthand!
70
+ assert_equal('top right;', rs['background-position'])
71
+ rs.create_shorthand!
72
+ assert_equal('url(http://example.com/1528/www/top-logo.jpg) no-repeat top right;', rs['background'])
73
+ end
74
+
75
+ protected
76
+ def assert_properties_are_deleted(ruleset, properties)
77
+ properties.each do |property, value|
78
+ assert_equal '', ruleset[property]
79
+ end
80
+ end
81
+
82
+ def create_shorthand(properties)
83
+ ruleset = RuleSet.new(nil, nil)
84
+ properties.each do |property, value|
85
+ ruleset[property] = value
86
+ end
87
+ ruleset.create_shorthand!
88
+ ruleset
89
+ end
90
+ end
@@ -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,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DanaDanger-css_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Dunae
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - CHANGELOG
25
+ - LICENSE
26
+ files:
27
+ - lib/css_parser.rb
28
+ - lib/css_parser/rule_set.rb
29
+ - lib/css_parser/parser.rb
30
+ - lib/css_parser/regexps.rb
31
+ - test/fixtures/import-circular-reference.css
32
+ - test/fixtures/import-with-media-types.css
33
+ - test/fixtures/import1.css
34
+ - test/fixtures/simple.css
35
+ - test/fixtures/subdir/import2.css
36
+ - test/test_css_parser_basic.rb
37
+ - test/test_css_parser_downloading.rb
38
+ - test/test_css_parser_media_types.rb
39
+ - test/test_css_parser_misc.rb
40
+ - test/test_css_parser_regexps.rb
41
+ - test/test_helper.rb
42
+ - test/test_merging.rb
43
+ - test/test_rule_set.rb
44
+ - test/test_rule_set_creating_shorthand.rb
45
+ - test/test_rule_set_expanding_shorthand.rb
46
+ - README
47
+ - CHANGELOG
48
+ - LICENSE
49
+ has_rdoc: true
50
+ homepage: http://code.dunae.ca/css_parser
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --all
54
+ - --inline-source
55
+ - --line-numbers
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: A set of classes for parsing CSS.
77
+ test_files:
78
+ - test/test_css_parser_basic.rb
79
+ - test/test_css_parser_downloading.rb
80
+ - test/test_css_parser_media_types.rb
81
+ - test/test_css_parser_misc.rb
82
+ - test/test_css_parser_regexps.rb
83
+ - test/test_helper.rb
84
+ - test/test_merging.rb
85
+ - test/test_rule_set.rb
86
+ - test/test_rule_set_creating_shorthand.rb
87
+ - test/test_rule_set_expanding_shorthand.rb