maca-fork-csspool 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/CHANGELOG.rdoc +72 -0
  2. data/History.txt +4 -0
  3. data/Manifest.txt +70 -0
  4. data/README.rdoc +78 -0
  5. data/Rakefile +35 -0
  6. data/lib/csspool.rb +20 -0
  7. data/lib/csspool/collection.rb +50 -0
  8. data/lib/csspool/css.rb +7 -0
  9. data/lib/csspool/css/charset.rb +7 -0
  10. data/lib/csspool/css/declaration.rb +8 -0
  11. data/lib/csspool/css/document.rb +34 -0
  12. data/lib/csspool/css/document_handler.rb +51 -0
  13. data/lib/csspool/css/import_rule.rb +15 -0
  14. data/lib/csspool/css/media.rb +7 -0
  15. data/lib/csspool/css/rule_set.rb +12 -0
  16. data/lib/csspool/lib_croco.rb +78 -0
  17. data/lib/csspool/lib_croco/cr_additional_sel.rb +46 -0
  18. data/lib/csspool/lib_croco/cr_attr_sel.rb +16 -0
  19. data/lib/csspool/lib_croco/cr_doc_handler.rb +24 -0
  20. data/lib/csspool/lib_croco/cr_num.rb +13 -0
  21. data/lib/csspool/lib_croco/cr_parser.rb +11 -0
  22. data/lib/csspool/lib_croco/cr_parsing_location.rb +17 -0
  23. data/lib/csspool/lib_croco/cr_pseudo.rb +14 -0
  24. data/lib/csspool/lib_croco/cr_rgb.rb +18 -0
  25. data/lib/csspool/lib_croco/cr_selector.rb +33 -0
  26. data/lib/csspool/lib_croco/cr_simple_sel.rb +54 -0
  27. data/lib/csspool/lib_croco/cr_term.rb +97 -0
  28. data/lib/csspool/lib_croco/glist.rb +21 -0
  29. data/lib/csspool/node.rb +5 -0
  30. data/lib/csspool/sac.rb +2 -0
  31. data/lib/csspool/sac/document.rb +35 -0
  32. data/lib/csspool/sac/parser.rb +122 -0
  33. data/lib/csspool/selector.rb +35 -0
  34. data/lib/csspool/selectors.rb +8 -0
  35. data/lib/csspool/selectors/additional.rb +6 -0
  36. data/lib/csspool/selectors/attribute.rb +21 -0
  37. data/lib/csspool/selectors/class.rb +11 -0
  38. data/lib/csspool/selectors/id.rb +11 -0
  39. data/lib/csspool/selectors/pseudo_class.rb +13 -0
  40. data/lib/csspool/selectors/simple.rb +22 -0
  41. data/lib/csspool/selectors/type.rb +6 -0
  42. data/lib/csspool/selectors/universal.rb +6 -0
  43. data/lib/csspool/terms.rb +7 -0
  44. data/lib/csspool/terms/function.rb +17 -0
  45. data/lib/csspool/terms/hash.rb +6 -0
  46. data/lib/csspool/terms/ident.rb +15 -0
  47. data/lib/csspool/terms/number.rb +14 -0
  48. data/lib/csspool/terms/rgb.rb +23 -0
  49. data/lib/csspool/terms/string.rb +6 -0
  50. data/lib/csspool/terms/uri.rb +6 -0
  51. data/lib/csspool/visitable.rb +30 -0
  52. data/lib/csspool/visitors.rb +5 -0
  53. data/lib/csspool/visitors/children.rb +50 -0
  54. data/lib/csspool/visitors/comparable.rb +85 -0
  55. data/lib/csspool/visitors/iterator.rb +80 -0
  56. data/lib/csspool/visitors/to_css.rb +188 -0
  57. data/lib/csspool/visitors/visitor.rb +17 -0
  58. data/test/css/test_document.rb +13 -0
  59. data/test/css/test_import_rule.rb +42 -0
  60. data/test/helper.rb +67 -0
  61. data/test/sac/test_parser.rb +115 -0
  62. data/test/sac/test_properties.rb +43 -0
  63. data/test/sac/test_terms.rb +134 -0
  64. data/test/test_collection.rb +81 -0
  65. data/test/test_parser.rb +91 -0
  66. data/test/test_selector.rb +47 -0
  67. data/test/visitors/test_children.rb +20 -0
  68. data/test/visitors/test_comparable.rb +103 -0
  69. data/test/visitors/test_each.rb +19 -0
  70. data/test/visitors/test_to_css.rb +200 -0
  71. metadata +173 -0
@@ -0,0 +1,17 @@
1
+ module CSSPool
2
+ module Visitors
3
+ class Visitor
4
+ def self.visitor_for *klasses, &block
5
+ klasses.each do |klass|
6
+ method_name = klass.name.split('::').join('_')
7
+ define_method(:"visit_#{method_name}", block)
8
+ end
9
+ end
10
+
11
+ def accept target
12
+ method_name = target.class.name.split('::').join('_')
13
+ send(:"visit_#{method_name}", target)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module CSS
5
+ class TestDocument < CSSPool::TestCase
6
+ def test_search
7
+ doc = CSSPool.CSS('div > p { background: red; }')
8
+ assert_equal 1, doc['div > p'].length
9
+ assert_equal 1, doc['div > p'].first.declarations.length
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module CSS
5
+ class TestImportRule < CSSPool::TestCase
6
+ def test_import
7
+ doc = CSSPool.CSS <<-eocss
8
+ @import "foo.css";
9
+ eocss
10
+
11
+ assert_equal 1, doc.import_rules.length
12
+
13
+ doc.import_rules.each do |ir|
14
+ new_doc = ir.load do |url|
15
+ assert_equal "foo.css", url
16
+ "div { background: red; }"
17
+ end
18
+ assert new_doc
19
+ assert_equal 1, new_doc.rule_sets.length
20
+ assert_equal ir, new_doc.parent_import_rule
21
+ assert_equal doc, new_doc.parent
22
+ end
23
+ end
24
+
25
+ def test_import_with_media
26
+ doc = CSSPool.CSS <<-eocss
27
+ @import "foo.css" screen, print;
28
+ eocss
29
+
30
+ assert_equal 1, doc.import_rules.length
31
+ doc.import_rules.each do |ir|
32
+ new_doc = ir.load do |url|
33
+ "div { background: red; }"
34
+ end
35
+ new_doc.rule_sets.each do |rs|
36
+ assert_equal ir.media, rs.media
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,67 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "csspool"
4
+
5
+
6
+ module CSSPool
7
+ class TestCase < Test::Unit::TestCase
8
+ unless RUBY_VERSION >= '1.9'
9
+ undef :default_test
10
+ end
11
+
12
+ ASSET_DIR = File.join(File.dirname(__FILE__), 'files')
13
+
14
+ class MyDoc < CSSPool::SAC::Document
15
+ attr_accessor :start_documents, :end_documents
16
+ attr_accessor :charsets, :import_styles, :comments, :start_selectors
17
+ attr_accessor :end_selectors, :properties
18
+
19
+ def initialize
20
+ @start_documents = []
21
+ @end_documents = []
22
+ @charsets = []
23
+ @import_styles = []
24
+ @comments = []
25
+ @start_selectors = []
26
+ @end_selectors = []
27
+ @properties = []
28
+ end
29
+
30
+ def property name, expression, important
31
+ @properties << [name, expression]
32
+ end
33
+
34
+ def start_document
35
+ @start_documents << true
36
+ end
37
+
38
+ def end_document
39
+ @end_documents << true
40
+ end
41
+
42
+ def charset name, location
43
+ @charsets << [name, location]
44
+ end
45
+
46
+ def import_style media_list, uri, default_ns, location
47
+ @import_styles << [media_list, uri, default_ns, location]
48
+ end
49
+
50
+ def namespace_declaration prefix, uri, location
51
+ @import_styles << [prefix, uri, location]
52
+ end
53
+
54
+ def comment string
55
+ @comments << string
56
+ end
57
+
58
+ def start_selector selectors
59
+ @start_selectors << selectors
60
+ end
61
+
62
+ def end_selector selectors
63
+ @end_selectors << selectors
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,115 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module SAC
5
+ class TestParser < CSSPool::TestCase
6
+ def setup
7
+ super
8
+ @doc = MyDoc.new
9
+ @css = <<-eocss
10
+ @charset "UTF-8";
11
+ @import url("foo.css") screen;
12
+ /* This is a comment */
13
+ div a.foo, #bar, * { background: red; }
14
+ div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
15
+ eocss
16
+ @parser = CSSPool::SAC::Parser.new(@doc)
17
+ @parser.parse(@css)
18
+ end
19
+
20
+ def test_start_and_end_called_with_the_same
21
+ assert_equal @doc.start_selectors, @doc.end_selectors
22
+ end
23
+
24
+ def test_parse_no_doc
25
+ parser = CSSPool::SAC::Parser.new
26
+ parser.parse(@css)
27
+ end
28
+
29
+ def test_start_document
30
+ assert_equal [true], @doc.start_documents
31
+ end
32
+
33
+ def test_end_document
34
+ assert_equal [true], @doc.end_documents
35
+ end
36
+
37
+ def test_charset
38
+ assert_equal(
39
+ [["UTF-8", { :line => 1, :byte_offset => 10, :column => 11}]],
40
+ @doc.charsets)
41
+ end
42
+
43
+ def test_import_style
44
+ styles = @doc.import_styles.first
45
+ assert_equal ["screen"], styles.first
46
+ assert_equal "foo.css", styles[1]
47
+ assert_nil styles[2]
48
+ end
49
+
50
+ def test_start_selector
51
+ selectors_for_rule = @doc.start_selectors.first
52
+ assert selectors_for_rule
53
+ assert_equal 3, selectors_for_rule.length
54
+ end
55
+
56
+ def test_simple_selector
57
+ selectors_for_rule = @doc.start_selectors.first
58
+ selector = selectors_for_rule.first # => div a.foo
59
+ assert_equal 2, selector.simple_selectors.length
60
+ selector.simple_selectors.each do |ss|
61
+ assert ss.parse_location
62
+ end
63
+ end
64
+
65
+ def test_additional_selector_list
66
+ selectors_for_rule = @doc.start_selectors.first
67
+ selector = selectors_for_rule.first # => div a.foo
68
+ simple_selector = selector.simple_selectors[1] # => a.foo
69
+ assert additional_selectors = simple_selector.additional_selectors
70
+ assert_equal 1, additional_selectors.length
71
+ end
72
+
73
+ def test_additional_selector_class_name
74
+ selectors_for_rule = @doc.start_selectors.first
75
+ selector = selectors_for_rule.first # => div a.foo
76
+ simple_selector = selector.simple_selectors[1] # => a.foo
77
+ assert additional_selectors = simple_selector.additional_selectors
78
+ foo_class = additional_selectors.first
79
+ assert_equal 'foo', foo_class.name
80
+ end
81
+
82
+ # div#a, a.foo, a:hover, a[href='watever'] { background: red; }
83
+ def test_id_additional_selector
84
+ selectors_for_rule = @doc.start_selectors[1]
85
+ selector = selectors_for_rule.first # => div#a
86
+ simple_selector = selector.simple_selectors.first # => div#a
87
+ assert_equal 'a', simple_selector.additional_selectors.first.name
88
+ end
89
+
90
+ # div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
91
+ def test_pseudo_additional_selector
92
+ selectors_for_rule = @doc.start_selectors[1]
93
+ selector = selectors_for_rule[2] # => 'a:hover'
94
+ simple_selector = selector.simple_selectors.first # => a:hover
95
+ assert_equal 'hover', simple_selector.additional_selectors.first.name
96
+ assert_nil simple_selector.additional_selectors.first.extra
97
+ end
98
+
99
+ # div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
100
+ def test_attribute_selector
101
+ selectors_for_rule = @doc.start_selectors[1]
102
+ selector = selectors_for_rule[3] # => a[href][int="10"]
103
+ simple_selector = selector.simple_selectors.first
104
+
105
+ assert_equal 'href', simple_selector.additional_selectors.first.name
106
+ assert_nil simple_selector.additional_selectors.first.value
107
+ assert_equal 1, simple_selector.additional_selectors.first.match_way
108
+
109
+ assert_equal 'int', simple_selector.additional_selectors[1].name
110
+ assert_equal '10', simple_selector.additional_selectors[1].value
111
+ assert_equal 2, simple_selector.additional_selectors[1].match_way
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,43 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module SAC
5
+ class TestProperties < CSSPool::TestCase
6
+ def setup
7
+ super
8
+ @doc = MyDoc.new
9
+ @css = <<-eocss
10
+ @charset "UTF-8";
11
+ @import url("foo.css") screen;
12
+ /* This is a comment */
13
+ div a.foo, #bar, * { background: red; }
14
+ div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
15
+ eocss
16
+ @parser = CSSPool::SAC::Parser.new(@doc)
17
+ @parser.parse(@css)
18
+ end
19
+
20
+ def test_properties
21
+ assert_equal ['background'], @doc.properties.map { |x| x.first }.uniq
22
+ @doc.properties.each do |property|
23
+ assert_equal 1, property[1].length
24
+ end
25
+ assert_equal ['red'], @doc.properties.map { |x| x[1].first.value }.uniq
26
+ end
27
+
28
+ def test_ident_with_comma
29
+ doc = MyDoc.new
30
+ parser = CSSPool::SAC::Parser.new(doc)
31
+ parser.parse <<-eocss
32
+ h1 { font-family: Verdana, sans-serif, monospace; }
33
+ eocss
34
+ assert_equal 1, doc.properties.length
35
+ values = doc.properties.first[1]
36
+ assert_equal 3, values.length
37
+ assert_equal [nil, ',', ','],
38
+ values.map { |value| value.operator }
39
+ values.each { |value| assert value.parse_location }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,134 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ module SAC
5
+ class TestTerms < CSSPool::TestCase
6
+ def setup
7
+ @doc = MyDoc.new
8
+ @parser = CSSPool::SAC::Parser.new(@doc)
9
+ end
10
+
11
+ def test_hash_range
12
+ @parser.parse <<-eocss
13
+ div { border: #123; }
14
+ eocss
15
+ hash = @doc.properties.first[1].first
16
+ assert_equal '123', hash.value
17
+ end
18
+
19
+ def test_rgb
20
+ @parser.parse <<-eocss
21
+ div { border: rgb(1,2,3); }
22
+ eocss
23
+ color = @doc.properties.first[1].first
24
+ assert_equal 1, color.red
25
+ assert_equal 2, color.green
26
+ assert_equal 3, color.blue
27
+ assert !color.percentage?
28
+ assert_match('rgb(1,2,3)', color.to_css)
29
+ end
30
+
31
+ def test_rgb_with_percentage
32
+ @parser.parse <<-eocss
33
+ div { border: rgb(100%,2%,3%); }
34
+ eocss
35
+ color = @doc.properties.first[1].first
36
+ assert_equal 100, color.red
37
+ assert_equal 2, color.green
38
+ assert_equal 3, color.blue
39
+ assert color.percentage?
40
+ assert_match('rgb(100%,2%,3%)', color.to_css)
41
+ end
42
+
43
+ def test_negative_number
44
+ @parser.parse <<-eocss
45
+ div { border: -1px; }
46
+ eocss
47
+ assert_equal 1, @doc.properties.length
48
+ size = @doc.properties.first[1].first
49
+ assert_equal :minus, size.unary_operator
50
+ assert_equal 1, size.value
51
+ assert_equal '-1.0px', size.to_s
52
+ assert_equal '-1.0px', size.to_css
53
+ end
54
+
55
+ def test_positive_number
56
+ @parser.parse <<-eocss
57
+ div { border: 1px; }
58
+ eocss
59
+ assert_equal 1, @doc.properties.length
60
+ size = @doc.properties.first[1].first
61
+ assert_equal 1, size.value
62
+ assert_equal '1.0px', size.to_s
63
+ end
64
+
65
+ %w{
66
+ 1 1em 1ex 1px 1in 1cm 1mm 1pt 1pc 1% 1deg 1rad 1ms 1s 1Hz 1kHz
67
+ }.each do |num|
68
+ expected = num.sub(/1/, '1.0')
69
+ define_method(:"test_num_#{num}") do
70
+ @parser.parse <<-eocss
71
+ div { border: #{num}; }
72
+ eocss
73
+ assert_equal 1, @doc.properties.length
74
+ size = @doc.properties.first[1].first
75
+ assert_equal 1, size.value
76
+ assert_equal expected, size.to_s
77
+ assert_equal expected, size.to_css
78
+ end
79
+ end
80
+
81
+ def test_string_term
82
+ @parser.parse <<-eocss
83
+ div { border: "hello"; }
84
+ eocss
85
+ assert_equal 1, @doc.properties.length
86
+ string = @doc.properties.first[1].first
87
+ assert_equal 'hello', string.value
88
+ assert_equal '"hello"', string.to_css
89
+ end
90
+
91
+ def test_inherit
92
+ @parser.parse <<-eocss
93
+ div { color: inherit; }
94
+ eocss
95
+ assert_equal 1, @doc.properties.length
96
+ string = @doc.properties.first[1].first
97
+ assert_equal 'inherit', string.value
98
+ assert_equal 'inherit', string.to_css
99
+ end
100
+
101
+ def test_important
102
+ @parser.parse <<-eocss
103
+ div { color: inherit !important; }
104
+ eocss
105
+ assert_equal 1, @doc.properties.length
106
+ string = @doc.properties.first[1].first
107
+ assert_equal 'inherit', string.value
108
+ assert_equal 'inherit', string.to_css
109
+ end
110
+
111
+ def test_function
112
+ @parser.parse <<-eocss
113
+ div { border: foo("hello"); }
114
+ eocss
115
+ assert_equal 1, @doc.properties.length
116
+ func = @doc.properties.first[1].first
117
+ assert_equal 'foo', func.name
118
+ assert_equal 1, func.params.length
119
+ assert_equal 'hello', func.params.first.value
120
+ assert_match 'foo("hello")', func.to_css
121
+ end
122
+
123
+ def test_uri
124
+ @parser.parse <<-eocss
125
+ div { border: url(http://tenderlovemaking.com/); }
126
+ eocss
127
+ assert_equal 1, @doc.properties.length
128
+ url = @doc.properties.first[1].first
129
+ assert_equal 'http://tenderlovemaking.com/', url.value
130
+ assert_match 'url(http://tenderlovemaking.com/)', url.to_css
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ module CSSPool
4
+ class TestCollection < CSSPool::TestCase
5
+ def test_new
6
+ assert CSSPool::Collection.new
7
+ assert CSSPool::Collection.new { |url| }
8
+ end
9
+
10
+ def test_append
11
+ collection = CSSPool::Collection.new
12
+ collection << "div { background: green; }"
13
+ assert_equal 1, collection.length
14
+ end
15
+
16
+ def test_collection_imports_stuff
17
+ called = false
18
+ collection = CSSPool::Collection.new do |url|
19
+ called = true
20
+ assert_equal 'hello.css', url
21
+ "div { background: red; }"
22
+ end
23
+
24
+ collection << '@import url(hello.css);'
25
+ assert called, "block was not called"
26
+ assert_equal 2, collection.length
27
+ end
28
+
29
+ def test_collection_imports_imports_imports
30
+ css = {
31
+ 'foo.css' => '@import url("bar.css");',
32
+ 'bar.css' => '@import url("baz.css");',
33
+ 'baz.css' => 'div { background: red; }',
34
+ }
35
+
36
+ collection = CSSPool::Collection.new do |url|
37
+ css[url] || raise
38
+ end
39
+
40
+ collection << '@import url(foo.css);'
41
+ assert_equal 4, collection.length
42
+ assert_nil collection.last.parent
43
+ assert_equal collection[-2].parent, collection.last
44
+ assert_equal collection[-3].parent, collection[-2]
45
+ assert_equal collection[-4].parent, collection[-3]
46
+ end
47
+
48
+ def test_load_only_once
49
+ css = {
50
+ 'foo.css' => '@import url("foo.css");',
51
+ }
52
+
53
+ collection = CSSPool::Collection.new do |url|
54
+ css[url] || raise
55
+ end
56
+
57
+ collection << '@import url(foo.css);'
58
+
59
+ assert_equal 2, collection.length
60
+ end
61
+
62
+ def test_each
63
+ css = {
64
+ 'foo.css' => '@import url("foo.css");',
65
+ }
66
+
67
+ collection = CSSPool::Collection.new do |url|
68
+ css[url] || raise
69
+ end
70
+
71
+ collection << '@import url(foo.css);'
72
+
73
+ list = []
74
+ collection.each do |thing|
75
+ list << thing
76
+ end
77
+
78
+ assert_equal 2, list.length
79
+ end
80
+ end
81
+ end