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,3 @@
1
+ @import "../simple.css";
2
+
3
+ a { text-decoration: none; }
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for reading and generating CSS shorthand properties
4
+ class CssParserBasicTests < Test::Unit::TestCase
5
+ include CssParser
6
+
7
+ def setup
8
+ @cp = CssParser::Parser.new
9
+ @css = <<-EOT
10
+ html, body, p { margin: 0px; }
11
+ p { padding: 0px; }
12
+ #content { font: 12px/normal sans-serif; }
13
+ EOT
14
+ end
15
+
16
+ def test_finding_by_selector
17
+ @cp.add_block!(@css)
18
+ assert_equal 'margin: 0px;', @cp.find_by_selector('body').join(' ')
19
+ assert_equal 'margin: 0px; padding: 0px;', @cp.find_by_selector('p').join(' ')
20
+ end
21
+
22
+ def test_adding_block
23
+ @cp.add_block!(@css)
24
+ assert_equal 'margin: 0px;', @cp.find_by_selector('body').join
25
+ end
26
+
27
+ def test_adding_a_rule
28
+ @cp.add_rule!('div', 'color: blue;')
29
+ assert_equal 'color: blue;', @cp.find_by_selector('div').join(' ')
30
+ end
31
+
32
+ def test_adding_a_rule_set
33
+ rs = CssParser::RuleSet.new('div', 'color: blue;')
34
+ @cp.add_rule_set!(rs)
35
+ assert_equal 'color: blue;', @cp.find_by_selector('div').join(' ')
36
+ end
37
+
38
+ def test_toggling_uri_conversion
39
+ # with conversion
40
+ cp_with_conversion = Parser.new(:absolute_paths => true)
41
+ cp_with_conversion.add_block!("body { background: url('../style/yellow.png?abc=123') };",
42
+ :base_uri => 'http://example.org/style/basic.css')
43
+
44
+ assert_equal "background: url('http://example.org/style/yellow.png?abc=123');",
45
+ cp_with_conversion['body'].join(' ')
46
+
47
+ # without conversion
48
+ cp_without_conversion = Parser.new(:absolute_paths => false)
49
+ cp_without_conversion.add_block!("body { background: url('../style/yellow.png?abc=123') };",
50
+ :base_uri => 'http://example.org/style/basic.css')
51
+
52
+ assert_equal "background: url('../style/yellow.png?abc=123');",
53
+ cp_without_conversion['body'].join(' ')
54
+ end
55
+
56
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for the CssParser's downloading functions.
4
+ class CssParserDownloadingTests < Test::Unit::TestCase
5
+ include CssParser
6
+ include WEBrick
7
+
8
+ def setup
9
+ # from http://nullref.se/blog/2006/5/17/testing-with-webrick
10
+ @cp = Parser.new
11
+
12
+ @uri_base = 'http://localhost:12000'
13
+
14
+ www_root = File.dirname(__FILE__) + '/fixtures/'
15
+
16
+ @server_thread = Thread.new do
17
+ s = WEBrick::HTTPServer.new(:Port => 12000, :DocumentRoot => www_root, :Logger => Log.new(nil, BasicLog::ERROR), :AccessLog => [])
18
+ @port = s.config[:Port]
19
+ begin
20
+ s.start
21
+ ensure
22
+ s.shutdown
23
+ end
24
+ end
25
+
26
+ sleep 1 # ensure the server has time to load
27
+ end
28
+
29
+ def teardown
30
+ @server_thread.kill
31
+ @server_thread.join(5)
32
+ @server_thread = nil
33
+ end
34
+
35
+ def test_loading_a_remote_file
36
+ @cp.load_uri!("#{@uri_base}/simple.css")
37
+ assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
38
+ end
39
+
40
+ def test_following_at_import_rules
41
+ @cp.load_uri!("#{@uri_base}/import1.css")
42
+
43
+ # from '/import1.css'
44
+ assert_equal 'color: lime;', @cp.find_by_selector('div').join(' ')
45
+
46
+ # from '/subdir/import2.css'
47
+ assert_equal 'text-decoration: none;', @cp.find_by_selector('a').join(' ')
48
+
49
+ # from '/subdir/../simple.css'
50
+ assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
51
+ end
52
+
53
+ def test_importing_with_media_types
54
+ @cp.load_uri!("#{@uri_base}/import-with-media-types.css")
55
+
56
+ # from simple.css with :screen media type
57
+ assert_equal 'margin: 0px;', @cp.find_by_selector('p', :screen).join(' ')
58
+ assert_equal '', @cp.find_by_selector('p', :tty).join(' ')
59
+ end
60
+
61
+ def test_throwing_circular_reference_exception
62
+ assert_raise CircularReferenceError do
63
+ @cp.load_uri!("#{@uri_base}/import-circular-reference.css")
64
+ end
65
+ end
66
+
67
+ def test_toggling_not_found_exceptions
68
+ cp_with_exceptions = Parser.new(:io_exceptions => true)
69
+
70
+ assert_raise RemoteFileError do
71
+ cp_with_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
72
+ end
73
+
74
+ cp_without_exceptions = Parser.new(:io_exceptions => false)
75
+
76
+ assert_nothing_raised RemoteFileError do
77
+ cp_without_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for the handling of media types
4
+ class CssParserMediaTypesTests < Test::Unit::TestCase
5
+ include CssParser
6
+
7
+ def setup
8
+ @cp = Parser.new
9
+ end
10
+
11
+ def test_finding_by_media_type
12
+ # from http://www.w3.org/TR/CSS21/media.html#at-media-rule
13
+ css = <<-EOT
14
+ @media print {
15
+ body { font-size: 10pt }
16
+ }
17
+ @media screen {
18
+ body { font-size: 13px }
19
+ }
20
+ @media screen, print {
21
+ body { line-height: 1.2 }
22
+ }
23
+ EOT
24
+
25
+ @cp.add_block!(css)
26
+
27
+ assert_equal 'font-size: 10pt; line-height: 1.2;', @cp.find_by_selector('body', :print).join(' ')
28
+ assert_equal 'font-size: 13px; line-height: 1.2;', @cp.find_by_selector('body', :screen).join(' ')
29
+ end
30
+
31
+ def atest_finding_by_multiple_media_types
32
+ css = <<-EOT
33
+ @media print {
34
+ body { font-size: 10pt }
35
+ }
36
+ @media handheld {
37
+ body { font-size: 13px }
38
+ }
39
+ @media screen, print {
40
+ body { line-height: 1.2 }
41
+ }
42
+ EOT
43
+ @cp.add_block!(css)
44
+
45
+ assert_equal 'font-size: 13px; line-height: 1.2;', @cp.find_by_selector('body', [:screen,:handheld]).join(' ')
46
+ end
47
+
48
+ def test_adding_block_with_media_types
49
+ css = <<-EOT
50
+ body { font-size: 10pt }
51
+ EOT
52
+
53
+ @cp.add_block!(css, :media_types => [:screen])
54
+
55
+ assert_equal 'font-size: 10pt;', @cp.find_by_selector('body', :screen).join(' ')
56
+ assert @cp.find_by_selector('body', :handheld).empty?
57
+ end
58
+
59
+ def atest_adding_rule_set_with_media_type
60
+ @cp.add_rule!('body', 'color: black;', [:handheld,:tty])
61
+ @cp.add_rule!('body', 'color: blue;', :screen)
62
+ assert_equal 'color: black;', @cp.find_by_selector('body', :handheld).join(' ')
63
+ end
64
+
65
+ def atest_selecting_with_all_meda_type
66
+ @cp.add_rule!('body', 'color: black;', [:handheld,:tty])
67
+ assert_equal 'color: black;', @cp.find_by_selector('body', :all).join(' ')
68
+ end
69
+
70
+
71
+ end
@@ -0,0 +1,143 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for the CssParser.
4
+ class CssParserTests < Test::Unit::TestCase
5
+ include CssParser
6
+
7
+ def setup
8
+ @cp = Parser.new
9
+ end
10
+
11
+ def test_at_page_rule
12
+ # from http://www.w3.org/TR/CSS21/page.html#page-selectors
13
+ css = <<-EOT
14
+ @page { margin: 2cm }
15
+
16
+ @page :first {
17
+ margin-top: 10cm
18
+ }
19
+ EOT
20
+
21
+ @cp.add_block!(css)
22
+
23
+ assert_equal 'margin: 2cm;', @cp.find_by_selector('@page').join(' ')
24
+ assert_equal 'margin-top: 10cm;', @cp.find_by_selector('@page :first').join(' ')
25
+ end
26
+
27
+ def test_should_ignore_comments
28
+ # see http://www.w3.org/Style/CSS/Test/CSS2.1/current/html4/t040109-c17-comments-00-b.htm
29
+ css =<<-EOT
30
+ /* This is a CSS comment. */
31
+ .one {color: green;} /* Another comment */
32
+ /* The following should not be used:
33
+ .one {color: red;} */
34
+ .two {color: green; /* color: yellow; */}
35
+ /**
36
+ .three {color: red;} */
37
+ .three {color: green;}
38
+ /**/
39
+ .four {color: green;}
40
+ /*********/
41
+ .five {color: green;}
42
+ /* a comment **/
43
+ .six {color: green;}
44
+ EOT
45
+
46
+ @cp.add_block!(css)
47
+ @cp.each_selector do |sel, decs, spec|
48
+ assert_equal 'color: green;', decs
49
+ end
50
+ end
51
+
52
+ def test_parsing_blocks
53
+ # dervived from http://www.w3.org/TR/CSS21/syndata.html#rule-sets
54
+ css = <<-EOT
55
+ div[name='test'] {
56
+
57
+ color:
58
+
59
+ red;
60
+
61
+ }div:hover{coloR:red;
62
+ }div:first-letter{color:red;/*color:blue;}"commented out"*/}
63
+
64
+ p[example="public class foo\
65
+ {\
66
+ private string x;\
67
+ \
68
+ foo(int x) {\
69
+ this.x = 'test';\
70
+ this.x = \"test\";\
71
+ }\
72
+ \
73
+ }"] { color: red }
74
+
75
+ p { color:red}
76
+ EOT
77
+
78
+ @cp.add_block!(css)
79
+
80
+ @cp.each_selector do |sel, decs, spec|
81
+ assert_equal 'color: red;', decs
82
+ end
83
+ end
84
+
85
+ def test_ignoring_malformed_declarations
86
+ flunk
87
+ # dervived from http://www.w3.org/TR/CSS21/syndata.html#parsing-errors
88
+ css = <<-EOT
89
+ p { color:green }
90
+ p { color:green; color } /* malformed declaration missing ':', value */
91
+ p { color:red; color; color:green } /* same with expected recovery */
92
+ p { color:green; color: } /* malformed declaration missing value */
93
+ p { color:red; color:; color:green } /* same with expected recovery */
94
+ p { color:green; color{;color:maroon} } /* unexpected tokens { } */
95
+ p { color:red; color{;color:maroon}; color:green } /* same with recovery */
96
+ EOT
97
+
98
+ @cp.add_block!(css)
99
+
100
+ @cp.each_selector do |sel, decs, spec|
101
+ assert_equal 'color: green;', decs
102
+ end
103
+ end
104
+
105
+ def test_calculating_specificity
106
+ # from http://www.w3.org/TR/CSS21/cascade.html#specificity
107
+ assert_equal 0, CssParser.calculate_specificity('*')
108
+ assert_equal 1, CssParser.calculate_specificity('li')
109
+ assert_equal 2, CssParser.calculate_specificity('li:first-line')
110
+ assert_equal 2, CssParser.calculate_specificity('ul li')
111
+ assert_equal 3, CssParser.calculate_specificity('ul ol+li')
112
+ assert_equal 11, CssParser.calculate_specificity('h1 + *[rel=up]')
113
+ assert_equal 13, CssParser.calculate_specificity('ul ol li.red')
114
+ assert_equal 21, CssParser.calculate_specificity('li.red.level')
115
+ assert_equal 100, CssParser.calculate_specificity('#x34y')
116
+
117
+ # from http://www.hixie.ch/tests/adhoc/css/cascade/specificity/003.html
118
+ assert_equal CssParser.calculate_specificity('div *'), CssParser.calculate_specificity('p')
119
+ assert CssParser.calculate_specificity('body div *') > CssParser.calculate_specificity('div *')
120
+
121
+ # other tests
122
+ assert_equal 11, CssParser.calculate_specificity('h1[id|=123]')
123
+ end
124
+
125
+ def test_converting_uris
126
+ base_uri = 'http://www.example.org/style/basic.css'
127
+ ["body { background: url(yellow) };", "body { background: url('yellow') };",
128
+ "body { background: url('/style/yellow') };",
129
+ "body { background: url(\"../style/yellow\") };",
130
+ "body { background: url(\"lib/../../style/yellow\") };"].each do |css|
131
+ converted_css = CssParser.convert_uris(css, base_uri)
132
+ assert_equal "body { background: url('http://www.example.org/style/yellow') };", converted_css
133
+ end
134
+
135
+ converted_css = CssParser.convert_uris("body { background: url(../style/yellow-dot_symbol$.png?abc=123&amp;def=456&ghi=789#1011) };", base_uri)
136
+ assert_equal "body { background: url('http://www.example.org/style/yellow-dot_symbol$.png?abc=123&amp;def=456&ghi=789#1011') };", converted_css
137
+
138
+ # taken from error log: 2007-10-23 04:37:41#2399
139
+ converted_css = CssParser.convert_uris('.specs {font-family:Helvetica;font-weight:bold;font-style:italic;color:#008CA8;font-size:1.4em;list-style-image:url("images/bullet.gif");}', 'http://www.example.org/directory/file.html')
140
+ assert_equal ".specs {font-family:Helvetica;font-weight:bold;font-style:italic;color:#008CA8;font-size:1.4em;list-style-image:url('http://www.example.org/directory/images/bullet.gif');}", converted_css
141
+ end
142
+
143
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Test cases for CSS regular expressions
4
+ #
5
+ # see http://www.w3.org/TR/CSS21/syndata.html and
6
+ # http://www.w3.org/TR/CSS21/grammar.html
7
+ class CssParserRegexpTests < Test::Unit::TestCase
8
+ def test_strings
9
+ # complete matches
10
+ ['"abcd"', '" A sd s�drcv \'dsf\' asd rfg asd"', '"A\ d??ef 123!"',
11
+ "\"this is\\\n a test\"", '"back\67round"', '"r\000065 ed"',
12
+ "'abcd'", "' A sd sedrcv \"dsf\" asd rf�&23$%#%$g asd'", "'A\\\n def 123!'",
13
+ "'this is\\\n a test'", "'back\\67round'", "'r\\000065 ed'"
14
+ ].each do |str|
15
+ assert_equal str, str.match(CssParser::RE_STRING).to_s
16
+ end
17
+
18
+ test_string = "p { background: red url(\"url\\.'p'ng\"); }"
19
+ assert_equal "\"url\\.'p'ng\"", test_string.match(CssParser::RE_STRING).to_s
20
+
21
+ end
22
+
23
+ def test_unicode
24
+ ['back\67round', 'r\000065 ed', '\00006C'].each do |str|
25
+ assert_match(Regexp.new(CssParser::RE_UNICODE), str)
26
+ end
27
+ end
28
+
29
+ def test_colour
30
+ ['color: #fff', 'color:#f0a09c;', 'color: #04A', 'color: #04a9CE',
31
+ 'color: rgb(100, -10%, 300);', 'color: rgb(10,10,10)', 'color:rgb(12.7253%, -12%,0)',
32
+ 'color: black', 'color:Red;', 'color: AqUa;', 'color: blue ', 'color: transparent'
33
+ ].each do |colour|
34
+ assert_match(CssParser::RE_COLOUR, colour)
35
+ end
36
+
37
+ ['color: #fa', 'color:#f009c;', 'color: #04G', 'color: #04a9Cq',
38
+ 'color: rgb 100, -10%, 300;', 'color: rgb 10,10,10', 'color:rgb(12px, -12%,0)',
39
+ 'color:fuscia;', 'color: thick'
40
+ ].each do |colour|
41
+ assert_no_match(CssParser::RE_COLOUR, colour)
42
+ end
43
+ end
44
+
45
+ def test_uris
46
+ crazy_uri = 'http://www.example.com:80/~/redb%20all.png?test=test&test;test+test#test!'
47
+
48
+ assert_equal "url('#{crazy_uri}')",
49
+ "li { list-style: url('#{crazy_uri}') disc }".match(CssParser::RE_URI).to_s
50
+
51
+ assert_equal "url(#{crazy_uri})",
52
+ "li { list-style: url(#{crazy_uri}) disc }".match(CssParser::RE_URI).to_s
53
+
54
+ assert_equal "url(\"#{crazy_uri}\")",
55
+ "li { list-style: url(\"#{crazy_uri}\") disc }".match(CssParser::RE_URI).to_s
56
+ end
57
+
58
+
59
+ protected
60
+ def load_test_file(filename)
61
+ fh = File.new("fixtures/#{filename}", 'r')
62
+ test_file = fh.read
63
+ fh.close
64
+
65
+ return test_file
66
+ end
67
+
68
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../'))
2
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/'))
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'css_parser'
6
+ require 'net/http'
7
+ require 'open-uri'
8
+ require 'WEBrick'
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class MergingTests < Test::Unit::TestCase
4
+ include CssParser
5
+
6
+ def setup
7
+ @cp = CssParser::Parser.new
8
+ end
9
+
10
+ def test_simple_merge
11
+ rs1 = RuleSet.new(nil, 'color: black;')
12
+ rs2 = RuleSet.new(nil, 'margin: 0px;')
13
+ merged = CssParser.merge(rs1, rs2)
14
+ assert_equal '0px;', merged['margin']
15
+ assert_equal 'black;', merged['color']
16
+ end
17
+
18
+ def test_merging_array
19
+ rs1 = RuleSet.new(nil, 'color: black;')
20
+ rs2 = RuleSet.new(nil, 'margin: 0px;')
21
+ merged = CssParser.merge([rs1, rs2])
22
+ assert_equal '0px;', merged['margin']
23
+ assert_equal 'black;', merged['color']
24
+ end
25
+
26
+
27
+ def test_merging_multiple
28
+ rs1 = RuleSet.new(nil, 'color: black;')
29
+ rs2 = RuleSet.new(nil, 'margin: 0px;')
30
+ rs3 = RuleSet.new(nil, 'margin: 5px;')
31
+ merged = CssParser.merge(rs1, rs2, rs3)
32
+ assert_equal '5px;', merged['margin']
33
+ end
34
+
35
+ def test_multiple_selectors_should_have_zero_specificity
36
+ rs1 = RuleSet.new('p, a[rel="external"]', 'color: black;')
37
+ rs2 = RuleSet.new('a', 'color: blue;')
38
+ merged = CssParser.merge(rs1, rs2)
39
+ assert_equal 'blue;', merged['color']
40
+ end
41
+
42
+ def test_setting_specificity
43
+ rs1 = RuleSet.new(nil, 'color: red;', 20)
44
+ rs2 = RuleSet.new(nil, 'color: blue;', 10)
45
+ merged = CssParser.merge(rs1, rs2)
46
+ assert_equal 'red;', merged['color']
47
+ end
48
+
49
+ def test_properties_should_be_case_insensitive
50
+ rs1 = RuleSet.new(nil, ' CoLor : red ;', 20)
51
+ rs2 = RuleSet.new(nil, 'color: blue;', 10)
52
+ merged = CssParser.merge(rs1, rs2)
53
+ assert_equal 'red;', merged['color']
54
+ end
55
+
56
+ def test_merging_backgrounds
57
+ rs1 = RuleSet.new(nil, 'background-color: black;')
58
+ rs2 = RuleSet.new(nil, 'background-image: none;')
59
+ merged = CssParser.merge(rs1, rs2)
60
+ assert_equal 'black none;', merged['background']
61
+ end
62
+
63
+ def test_merging_dimensions
64
+ rs1 = RuleSet.new(nil, 'margin: 3em;')
65
+ rs2 = RuleSet.new(nil, 'margin-left: 1em;')
66
+ merged = CssParser.merge(rs1, rs2)
67
+ assert_equal '3em 3em 3em 1em;', merged['margin']
68
+ end
69
+
70
+ def test_merging_fonts
71
+ rs1 = RuleSet.new(nil, 'font: 11px Arial;')
72
+ rs2 = RuleSet.new(nil, 'font-weight: bold;')
73
+ merged = CssParser.merge(rs1, rs2)
74
+ assert_equal 'bold 11px Arial;', merged['font']
75
+ end
76
+
77
+ def test_raising_error_on_bad_type
78
+ assert_raise ArgumentError do
79
+ CssParser.merge([1,2,3])
80
+ end
81
+ end
82
+
83
+ def test_returning_early_with_only_one_params
84
+ rs = RuleSet.new(nil, 'font-weight: bold;')
85
+ merged = CssParser.merge(rs)
86
+ assert_equal rs.object_id, merged.object_id
87
+ end
88
+ end