csspool 0.2.6 → 2.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.
Files changed (138) hide show
  1. data/.autotest +16 -0
  2. data/{CHANGELOG.txt → CHANGELOG.rdoc} +10 -0
  3. data/Manifest.txt +60 -72
  4. data/README.rdoc +78 -0
  5. data/Rakefile +9 -52
  6. data/lib/csspool/collection.rb +50 -0
  7. data/lib/csspool/css/charset.rb +7 -0
  8. data/lib/csspool/css/declaration.rb +8 -0
  9. data/lib/csspool/css/document.rb +34 -0
  10. data/lib/csspool/css/document_handler.rb +51 -0
  11. data/lib/csspool/css/import_rule.rb +15 -0
  12. data/lib/csspool/css/media.rb +7 -0
  13. data/lib/csspool/css/rule_set.rb +12 -0
  14. data/lib/csspool/css.rb +7 -0
  15. data/lib/csspool/lib_croco/cr_additional_sel.rb +46 -0
  16. data/lib/csspool/lib_croco/cr_attr_sel.rb +16 -0
  17. data/lib/csspool/lib_croco/cr_doc_handler.rb +24 -0
  18. data/lib/csspool/lib_croco/cr_num.rb +13 -0
  19. data/lib/csspool/lib_croco/cr_parser.rb +11 -0
  20. data/lib/csspool/lib_croco/cr_parsing_location.rb +17 -0
  21. data/lib/csspool/lib_croco/cr_pseudo.rb +14 -0
  22. data/lib/csspool/lib_croco/cr_rgb.rb +18 -0
  23. data/lib/csspool/lib_croco/cr_selector.rb +34 -0
  24. data/lib/csspool/lib_croco/cr_simple_sel.rb +54 -0
  25. data/lib/csspool/lib_croco/cr_term.rb +97 -0
  26. data/lib/csspool/lib_croco/glist.rb +21 -0
  27. data/lib/csspool/lib_croco.rb +78 -0
  28. data/lib/csspool/node.rb +5 -0
  29. data/lib/csspool/sac/document.rb +35 -0
  30. data/lib/csspool/sac/parser.rb +122 -0
  31. data/lib/csspool/sac.rb +2 -0
  32. data/lib/csspool/selector.rb +32 -0
  33. data/lib/csspool/selectors/additional.rb +6 -0
  34. data/lib/csspool/selectors/attribute.rb +21 -0
  35. data/lib/csspool/selectors/class.rb +11 -0
  36. data/lib/csspool/selectors/id.rb +11 -0
  37. data/lib/csspool/selectors/pseudo_class.rb +13 -0
  38. data/lib/csspool/selectors/simple.rb +22 -0
  39. data/lib/csspool/selectors/type.rb +6 -0
  40. data/lib/csspool/selectors/universal.rb +6 -0
  41. data/lib/csspool/selectors.rb +8 -0
  42. data/lib/csspool/terms/function.rb +17 -0
  43. data/lib/csspool/terms/hash.rb +6 -0
  44. data/lib/csspool/terms/ident.rb +15 -0
  45. data/lib/csspool/terms/number.rb +14 -0
  46. data/lib/csspool/terms/rgb.rb +23 -0
  47. data/lib/csspool/terms/string.rb +6 -0
  48. data/lib/csspool/terms/uri.rb +6 -0
  49. data/lib/csspool/terms.rb +7 -0
  50. data/lib/csspool/visitable.rb +18 -0
  51. data/lib/csspool/visitors/comparable.rb +78 -0
  52. data/lib/csspool/visitors/to_css.rb +201 -0
  53. data/lib/csspool/visitors/visitor.rb +17 -0
  54. data/lib/csspool/visitors.rb +3 -0
  55. data/lib/csspool.rb +18 -1
  56. data/test/css/test_document.rb +13 -0
  57. data/test/css/test_import_rule.rb +42 -0
  58. data/test/helper.rb +62 -6
  59. data/test/sac/test_parser.rb +115 -0
  60. data/test/sac/test_properties.rb +43 -0
  61. data/test/sac/test_terms.rb +134 -0
  62. data/test/test_collection.rb +81 -0
  63. data/test/test_parser.rb +75 -241
  64. data/test/test_selector.rb +45 -34
  65. data/test/visitors/test_comparable.rb +72 -0
  66. data/test/visitors/test_to_css.rb +141 -0
  67. metadata +98 -85
  68. data/README.txt +0 -79
  69. data/lib/css/sac/conditions/attribute_condition.rb +0 -56
  70. data/lib/css/sac/conditions/begin_hyphen_condition.rb +0 -22
  71. data/lib/css/sac/conditions/class_condition.rb +0 -22
  72. data/lib/css/sac/conditions/combinator_condition.rb +0 -40
  73. data/lib/css/sac/conditions/condition.rb +0 -35
  74. data/lib/css/sac/conditions/id_condition.rb +0 -27
  75. data/lib/css/sac/conditions/one_of_condition.rb +0 -22
  76. data/lib/css/sac/conditions/pseudo_class_condition.rb +0 -24
  77. data/lib/css/sac/conditions.rb +0 -5
  78. data/lib/css/sac/document_handler.rb +0 -67
  79. data/lib/css/sac/error_handler.rb +0 -14
  80. data/lib/css/sac/generated_parser.rb +0 -999
  81. data/lib/css/sac/generated_property_parser.rb +0 -9214
  82. data/lib/css/sac/lexeme.rb +0 -29
  83. data/lib/css/sac/lexical_unit.rb +0 -203
  84. data/lib/css/sac/parse_exception.rb +0 -6
  85. data/lib/css/sac/parser.rb +0 -115
  86. data/lib/css/sac/property_parser.rb +0 -47
  87. data/lib/css/sac/selectors/child_selector.rb +0 -40
  88. data/lib/css/sac/selectors/conditional_selector.rb +0 -49
  89. data/lib/css/sac/selectors/descendant_selector.rb +0 -40
  90. data/lib/css/sac/selectors/element_selector.rb +0 -39
  91. data/lib/css/sac/selectors/selector.rb +0 -31
  92. data/lib/css/sac/selectors/sibling_selector.rb +0 -39
  93. data/lib/css/sac/selectors/simple_selector.rb +0 -25
  94. data/lib/css/sac/selectors.rb +0 -5
  95. data/lib/css/sac/token.rb +0 -27
  96. data/lib/css/sac/tokenizer.rb +0 -187
  97. data/lib/css/sac/visitable.rb +0 -106
  98. data/lib/css/sac.rb +0 -14
  99. data/lib/css/stylesheet/rule.rb +0 -20
  100. data/lib/css/stylesheet/stylesheet.rb +0 -96
  101. data/lib/css/stylesheet.rb +0 -3
  102. data/lib/parser.y +0 -308
  103. data/lib/property_parser.y +0 -2346
  104. data/lib/property_parser.y.erb +0 -1321
  105. data/test/condition_test_case.rb +0 -6
  106. data/test/selector_test_case.rb +0 -20
  107. data/test/test_all.rb +0 -4
  108. data/test/test_attribute_condition.rb +0 -39
  109. data/test/test_begin_hyphen_condition.rb +0 -15
  110. data/test/test_child_selector.rb +0 -60
  111. data/test/test_class_condition.rb +0 -15
  112. data/test/test_combinator_condition.rb +0 -38
  113. data/test/test_condition.rb +0 -31
  114. data/test/test_conditional_selector.rb +0 -44
  115. data/test/test_descendant_selector.rb +0 -64
  116. data/test/test_element_selector.rb +0 -31
  117. data/test/test_error_recovery.rb +0 -28
  118. data/test/test_id_condition.rb +0 -16
  119. data/test/test_lexeme.rb +0 -39
  120. data/test/test_lexical_color.rb +0 -49
  121. data/test/test_lexical_function.rb +0 -31
  122. data/test/test_lexical_ident.rb +0 -30
  123. data/test/test_lexical_number.rb +0 -68
  124. data/test/test_lexical_string.rb +0 -23
  125. data/test/test_lexical_unit.rb +0 -106
  126. data/test/test_lexical_uri.rb +0 -28
  127. data/test/test_one_of_condition.rb +0 -15
  128. data/test/test_parse_error.rb +0 -199
  129. data/test/test_property_parser.rb +0 -593
  130. data/test/test_selector_as_string.rb +0 -94
  131. data/test/test_selector_parser.rb +0 -178
  132. data/test/test_sibling_selector.rb +0 -42
  133. data/test/test_simple_selector.rb +0 -9
  134. data/test/test_specificity.rb +0 -76
  135. data/test/test_stylesheet.rb +0 -95
  136. data/test/test_token.rb +0 -24
  137. data/test/test_tokenizer.rb +0 -127
  138. data/test/test_xpath.rb +0 -105
data/.autotest ADDED
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ begin
4
+ require 'autotest/fsevent'
5
+ rescue LoadError
6
+ end
7
+
8
+ Autotest.add_hook :run_command do |at|
9
+ at.unit_diff = 'cat'
10
+ end
11
+
12
+ Autotest.add_hook :ran_command do |at|
13
+ File.open('/tmp/autotest.txt', 'wb') { |f|
14
+ f.write(at.results.join)
15
+ }
16
+ end
@@ -1,3 +1,13 @@
1
+ == 2.0.0
2
+
3
+ * Bugfixes
4
+
5
+ * Uh... Many.
6
+
7
+ * New Features
8
+
9
+ * Now wraps libcroco via FFI
10
+
1
11
  == 0.2.6
2
12
 
3
13
  * Fix comment greediness. [Seth Rasmussen]
data/Manifest.txt CHANGED
@@ -1,78 +1,66 @@
1
- CHANGELOG.txt
1
+ .autotest
2
+ CHANGELOG.rdoc
2
3
  Manifest.txt
3
- README.txt
4
+ README.rdoc
4
5
  Rakefile
5
- lib/css/sac.rb
6
- lib/css/sac/conditions.rb
7
- lib/css/sac/conditions/attribute_condition.rb
8
- lib/css/sac/conditions/begin_hyphen_condition.rb
9
- lib/css/sac/conditions/class_condition.rb
10
- lib/css/sac/conditions/combinator_condition.rb
11
- lib/css/sac/conditions/condition.rb
12
- lib/css/sac/conditions/id_condition.rb
13
- lib/css/sac/conditions/one_of_condition.rb
14
- lib/css/sac/conditions/pseudo_class_condition.rb
15
- lib/css/sac/document_handler.rb
16
- lib/css/sac/error_handler.rb
17
- lib/css/sac/generated_parser.rb
18
- lib/css/sac/generated_property_parser.rb
19
- lib/css/sac/lexeme.rb
20
- lib/css/sac/lexical_unit.rb
21
- lib/css/sac/parse_exception.rb
22
- lib/css/sac/parser.rb
23
- lib/css/sac/property_parser.rb
24
- lib/css/sac/selectors.rb
25
- lib/css/sac/selectors/child_selector.rb
26
- lib/css/sac/selectors/conditional_selector.rb
27
- lib/css/sac/selectors/descendant_selector.rb
28
- lib/css/sac/selectors/element_selector.rb
29
- lib/css/sac/selectors/selector.rb
30
- lib/css/sac/selectors/sibling_selector.rb
31
- lib/css/sac/selectors/simple_selector.rb
32
- lib/css/sac/token.rb
33
- lib/css/sac/tokenizer.rb
34
- lib/css/sac/visitable.rb
35
- lib/css/stylesheet.rb
36
- lib/css/stylesheet/rule.rb
37
- lib/css/stylesheet/stylesheet.rb
38
6
  lib/csspool.rb
39
- lib/parser.y
40
- lib/property_parser.y
41
- lib/property_parser.y.erb
42
- test/condition_test_case.rb
7
+ lib/csspool/collection.rb
8
+ lib/csspool/css.rb
9
+ lib/csspool/css/charset.rb
10
+ lib/csspool/css/declaration.rb
11
+ lib/csspool/css/document.rb
12
+ lib/csspool/css/document_handler.rb
13
+ lib/csspool/css/import_rule.rb
14
+ lib/csspool/css/media.rb
15
+ lib/csspool/css/rule_set.rb
16
+ lib/csspool/lib_croco.rb
17
+ lib/csspool/lib_croco/cr_additional_sel.rb
18
+ lib/csspool/lib_croco/cr_attr_sel.rb
19
+ lib/csspool/lib_croco/cr_doc_handler.rb
20
+ lib/csspool/lib_croco/cr_num.rb
21
+ lib/csspool/lib_croco/cr_parser.rb
22
+ lib/csspool/lib_croco/cr_parsing_location.rb
23
+ lib/csspool/lib_croco/cr_pseudo.rb
24
+ lib/csspool/lib_croco/cr_rgb.rb
25
+ lib/csspool/lib_croco/cr_selector.rb
26
+ lib/csspool/lib_croco/cr_simple_sel.rb
27
+ lib/csspool/lib_croco/cr_term.rb
28
+ lib/csspool/lib_croco/glist.rb
29
+ lib/csspool/node.rb
30
+ lib/csspool/sac.rb
31
+ lib/csspool/sac/document.rb
32
+ lib/csspool/sac/parser.rb
33
+ lib/csspool/selector.rb
34
+ lib/csspool/selectors.rb
35
+ lib/csspool/selectors/additional.rb
36
+ lib/csspool/selectors/attribute.rb
37
+ lib/csspool/selectors/class.rb
38
+ lib/csspool/selectors/id.rb
39
+ lib/csspool/selectors/pseudo_class.rb
40
+ lib/csspool/selectors/simple.rb
41
+ lib/csspool/selectors/type.rb
42
+ lib/csspool/selectors/universal.rb
43
+ lib/csspool/terms.rb
44
+ lib/csspool/terms/function.rb
45
+ lib/csspool/terms/hash.rb
46
+ lib/csspool/terms/ident.rb
47
+ lib/csspool/terms/number.rb
48
+ lib/csspool/terms/rgb.rb
49
+ lib/csspool/terms/string.rb
50
+ lib/csspool/terms/uri.rb
51
+ lib/csspool/visitable.rb
52
+ lib/csspool/visitors.rb
53
+ lib/csspool/visitors/comparable.rb
54
+ lib/csspool/visitors/to_css.rb
55
+ lib/csspool/visitors/visitor.rb
56
+ test/css/test_document.rb
57
+ test/css/test_import_rule.rb
43
58
  test/helper.rb
44
- test/selector_test_case.rb
45
- test/test_all.rb
46
- test/test_attribute_condition.rb
47
- test/test_begin_hyphen_condition.rb
48
- test/test_child_selector.rb
49
- test/test_class_condition.rb
50
- test/test_combinator_condition.rb
51
- test/test_condition.rb
52
- test/test_conditional_selector.rb
53
- test/test_descendant_selector.rb
54
- test/test_element_selector.rb
55
- test/test_error_recovery.rb
56
- test/test_id_condition.rb
57
- test/test_lexeme.rb
58
- test/test_lexical_color.rb
59
- test/test_lexical_function.rb
60
- test/test_lexical_ident.rb
61
- test/test_lexical_number.rb
62
- test/test_lexical_string.rb
63
- test/test_lexical_unit.rb
64
- test/test_lexical_uri.rb
65
- test/test_one_of_condition.rb
66
- test/test_parse_error.rb
59
+ test/sac/test_parser.rb
60
+ test/sac/test_properties.rb
61
+ test/sac/test_terms.rb
62
+ test/test_collection.rb
67
63
  test/test_parser.rb
68
- test/test_property_parser.rb
69
64
  test/test_selector.rb
70
- test/test_selector_as_string.rb
71
- test/test_selector_parser.rb
72
- test/test_sibling_selector.rb
73
- test/test_simple_selector.rb
74
- test/test_specificity.rb
75
- test/test_stylesheet.rb
76
- test/test_token.rb
77
- test/test_tokenizer.rb
78
- test/test_xpath.rb
65
+ test/visitors/test_comparable.rb
66
+ test/visitors/test_to_css.rb
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = CSSPool
2
+
3
+ * http://csspool.rubyforge.org
4
+ * http://github.com/tenderlove/csspool
5
+
6
+ == DESCRIPTION:
7
+
8
+ CSSPool is a CSS parser. CSSPool provides a SAC interface for parsing CSS as
9
+ well as a document oriented interface for parsing CSS.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ CSSPool now depends on libcroco[http://www.freespiders.org/projects/libcroco/]
14
+ and interfaces with libcroco via FFI. This means that if libcroco isn't
15
+ installed in one of your default library paths, you'll need to tell CSSPool
16
+ where to find the libcroco shared library. This is typically the case for
17
+ people using OS X and installing libcroco via macports.
18
+
19
+ You can tell CSSPool where to find the libcroco shared library in a couple ways.
20
+ The first way is to set LD_LIBRARY_PATH to point at the correct directory. Just
21
+ do this:
22
+
23
+ $ export LD_LIBRARY_PATH=/opt/local/lib
24
+
25
+ Then run your script.
26
+
27
+ The second way is to tell CSSPool specifically where to find the dynamic
28
+ library. To do that, just set the LIBCROCO environment variable. On OS X,
29
+ I would do this:
30
+
31
+ $ export LIBCROCO=/opt/local/lib/libcroco-0.6.dylib
32
+
33
+ Then run my script.
34
+
35
+ == SYNOPSIS:
36
+
37
+ doc = CSSPool.CSS open('/path/to/css.css')
38
+ doc.rule_sets.each do |rs|
39
+ puts rs.to_css
40
+ end
41
+
42
+ puts doc.to_css
43
+
44
+ == REQUIREMENTS:
45
+
46
+ * libcroco (on OS X do "sudo port install libcroco")
47
+
48
+ == INSTALL:
49
+
50
+ * sudo gem install csspool
51
+
52
+ == LICENSE:
53
+
54
+ (The MIT License)
55
+
56
+ Copyright (c) 2007-2009
57
+
58
+ * {Aaron Patterson}[http://tenderlovemaking.com]
59
+ * {John Barnette}[http://www.jbarnette.com/]
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,57 +1,14 @@
1
+ # -*- ruby -*-
2
+
1
3
  require 'rubygems'
2
- require 'erb'
3
4
  require 'hoe'
4
5
 
5
- $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "lib")
6
-
7
- GENERATED_PARSER = "lib/css/sac/generated_parser.rb"
8
- GENERATED_PROPERTY_PARSER = "lib/css/sac/generated_property_parser.rb"
9
-
10
- Hoe.new('csspool', "0.2.6") do |p|
11
- p.rubyforge_name = 'csspool'
12
- p.author = 'Aaron Patterson'
13
- p.email = 'aaronp@rubyforge.org'
14
- p.summary = "Parses CSS"
15
- p.description = p.paragraphs_of('README.txt', 3).join("\n\n")
16
- p.url = p.paragraphs_of('README.txt', 1).first.strip
17
- p.changes = p.paragraphs_of('CHANGELOG.txt', 0..1).join("\n\n")
18
- p.clean_globs = [GENERATED_PARSER, GENERATED_PROPERTY_PARSER]
19
- end
20
-
21
- class Array
22
- def permutations
23
- return [self] if size < 2
24
- perm = []
25
- each { |e| (self - [e]).permutations.each { |p| perm << ([e] + p) } }
26
- perm
27
- end
28
-
29
- def permute_all_combinations
30
- list = []
31
- permutations.each do |perm|
32
- while perm.length > 0
33
- list << perm.dup
34
- perm.shift
35
- end
36
- end
37
- list.uniq.sort_by { |x| x.length }.reverse
38
- end
6
+ Hoe.spec('csspool') do
7
+ developer('Aaron Patterson', 'aaronp@rubyforge.org')
8
+ developer('John Barnette', 'jabarnette@rubyforge.org')
9
+ self.readme_file = 'README.rdoc'
10
+ self.history_file = 'CHANGELOG.rdoc'
11
+ self.extra_deps = ['ffi']
39
12
  end
40
13
 
41
- file GENERATED_PARSER => "lib/parser.y" do |t|
42
- sh "racc -o #{t.name} #{t.prerequisites.first}"
43
- end
44
-
45
- file GENERATED_PROPERTY_PARSER => "lib/property_parser.y.erb" do |t|
46
- template = ERB.new(File.open(t.prerequisites.first, 'rb') { |x| x.read })
47
- File.open("lib/property_parser.y", 'wb') { |f|
48
- f.write template.result(binding)
49
- }
50
- sh "racc -o #{t.name} lib/property_parser.y"
51
- end
52
-
53
- task :parser => [GENERATED_PARSER, GENERATED_PROPERTY_PARSER]
54
-
55
- # make sure the parser's up-to-date when we test
56
- Rake::Task[:test].prerequisites << :parser
57
- Rake::Task[:check_manifest].prerequisites << :parser
14
+ # vim: syntax=Ruby
@@ -0,0 +1,50 @@
1
+ module CSSPool
2
+ class Collection
3
+ include Enumerable
4
+
5
+ def initialize &block
6
+ @docs = []
7
+ @block = block
8
+ end
9
+
10
+ def << string
11
+ doc = CSSPool.CSS string
12
+
13
+ import_tree = [[doc]]
14
+
15
+ imported_urls = {}
16
+
17
+ until import_tree.last.all? { |x| x.import_rules.length == 0 }
18
+ level = import_tree.last
19
+ import_tree << []
20
+ level.each do |doc|
21
+ doc.import_rules.each do |ir|
22
+ next if imported_urls.key? ir.uri
23
+
24
+ new_doc = ir.load(&@block)
25
+
26
+ imported_urls[ir.uri] = ir.load(&@block)
27
+ import_tree.last << new_doc
28
+ end
29
+ end
30
+ end
31
+
32
+ @docs += import_tree.flatten.reverse
33
+ self
34
+ end
35
+
36
+ def length
37
+ @docs.length
38
+ end
39
+
40
+ def [] idx
41
+ @docs[idx]
42
+ end
43
+
44
+ def each &block
45
+ @docs.each(&block)
46
+ end
47
+
48
+ def last; @docs.last; end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Charset < Struct.new(:name, :parse_location)
4
+ include CSSPool::Visitable
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Declaration < Struct.new(:property, :expressions, :important, :rule_set)
4
+ include CSSPool::Visitable
5
+ alias :important? :important
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,34 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Document < Node
4
+ def self.parse string
5
+ unless string && string.length > 0
6
+ return CSSPool::CSS::Document.new
7
+ end
8
+ handler = CSSPool::CSS::DocumentHandler.new
9
+ parser = CSSPool::SAC::Parser.new(handler)
10
+ parser.parse(string)
11
+ handler.document
12
+ end
13
+
14
+ attr_accessor :rule_sets
15
+ attr_accessor :charsets
16
+ attr_accessor :import_rules
17
+ attr_accessor :parent
18
+ attr_accessor :parent_import_rule
19
+
20
+ def initialize
21
+ @rule_sets = []
22
+ @charsets = []
23
+ @import_rules = []
24
+ @parent = nil
25
+ @parent_import_rule = nil
26
+ end
27
+
28
+ def [] selector
29
+ selectors = CSSPool.CSS("#{selector} {}").rule_sets.first.selectors
30
+ rule_sets.find_all { |rs| rs.selectors == selectors}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ module CSSPool
2
+ module CSS
3
+ class DocumentHandler < CSSPool::SAC::Document
4
+ attr_accessor :document
5
+
6
+ def initialize
7
+ @document = nil
8
+ @media_stack = []
9
+ end
10
+
11
+ def start_document
12
+ @document = CSSPool::CSS::Document.new
13
+ end
14
+
15
+ def charset name, location
16
+ @document.charsets << CSS::Charset.new(name, location)
17
+ end
18
+
19
+ def import_style media_list, uri, ns, loc
20
+ @document.import_rules << CSS::ImportRule.new(
21
+ uri,
22
+ ns,
23
+ media_list.map { |x| CSS::Media.new(x, loc) },
24
+ @document,
25
+ loc
26
+ )
27
+ end
28
+
29
+ def start_selector selector_list
30
+ @document.rule_sets << RuleSet.new(
31
+ selector_list,
32
+ [],
33
+ @media_stack.last || []
34
+ )
35
+ end
36
+
37
+ def property name, exp, important
38
+ rs = @document.rule_sets.last
39
+ rs.declarations << Declaration.new(name, exp, important, rs)
40
+ end
41
+
42
+ def start_media media_list, parse_location
43
+ @media_stack << media_list.map { |x| CSS::Media.new(x, parse_location) }
44
+ end
45
+
46
+ def end_media media_list, parse_location
47
+ @media_stack.pop
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ module CSSPool
2
+ module CSS
3
+ class ImportRule < Struct.new(:uri, :namespace, :media, :document, :parse_location)
4
+ include CSSPool::Visitable
5
+
6
+ def load
7
+ new_doc = CSSPool.CSS(yield uri)
8
+ new_doc.parent_import_rule = self
9
+ new_doc.parent = document
10
+ new_doc.rule_sets.each { |rs| rs.media = media }
11
+ new_doc
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module CSSPool
2
+ module CSS
3
+ class Media < Struct.new(:name, :parse_location)
4
+ include CSSPool::Visitable
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module CSSPool
2
+ module CSS
3
+ class RuleSet < Struct.new(:selectors, :declarations, :media)
4
+ include CSSPool::Visitable
5
+
6
+ def initialize selectors, declarations = [], media = []
7
+ selectors.each { |sel| sel.rule_set = self }
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'csspool/css/charset'
2
+ require 'csspool/css/import_rule'
3
+ require 'csspool/css/media'
4
+ require 'csspool/css/rule_set'
5
+ require 'csspool/css/declaration'
6
+ require 'csspool/css/document'
7
+ require 'csspool/css/document_handler'
@@ -0,0 +1,46 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRAdditionalSel < FFI::Struct
4
+ layout(
5
+ :sel_type, :int,
6
+ :content, :pointer,
7
+ :next, :pointer,
8
+ :prev, :pointer,
9
+ :line, :int,
10
+ :column, :int,
11
+ :byte_offset, :int
12
+ )
13
+
14
+ def to_additional_selector
15
+ case self[:sel_type]
16
+ when 1 # CLASS_ADD_SELECTOR
17
+ CSSPool::Selectors::Class.new(
18
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string
19
+ )
20
+ when 1 << 1 # PSEUDO_CLASS_ADD_SELECTOR
21
+ pseudo = CRPseudo.new(self[:content])
22
+ CSSPool::Selectors::PseudoClass.new(
23
+ pseudo[:name].null? ? nil :
24
+ LibCroco.cr_string_peek_raw_str(pseudo[:name]).read_string,
25
+ pseudo[:extra].null? ? nil :
26
+ LibCroco.cr_string_peek_raw_str(pseudo[:extra]).read_string
27
+ )
28
+ when 1 << 3 # ID_ADD_SELECTOR
29
+ CSSPool::Selectors::Id.new(
30
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string
31
+ )
32
+ when 1 << 4 # ATTRIBUTE_ADD_SELECTOR
33
+ attr_sel = CRAttrSel.new(self[:content])
34
+ raise "FIXME: additional add selectors" unless attr_sel[:next].null?
35
+ CSSPool::Selectors::Attribute.new(
36
+ attr_sel[:name].null? ? nil :
37
+ LibCroco.cr_string_peek_raw_str(attr_sel[:name]).read_string,
38
+ attr_sel[:value].null? ? nil :
39
+ LibCroco.cr_string_peek_raw_str(attr_sel[:value]).read_string,
40
+ attr_sel[:match_way]
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRAttrSel < FFI::Struct
4
+ layout(
5
+ :name, :pointer,
6
+ :value, :pointer,
7
+ :match_way, :int,
8
+ :next, :pointer,
9
+ :prev, :pointer,
10
+ :line, :int,
11
+ :column, :int,
12
+ :byte_offset, :int
13
+ )
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRDocHandler < FFI::Struct
4
+ layout(
5
+ :priv, :pointer,
6
+ :app_data, :pointer,
7
+ :start_document, :start_document,
8
+ :end_document, :end_document,
9
+ :charset, :charset,
10
+ :import_style, :import_style,
11
+ :import_style_result, :import_style_result,
12
+ :namespace_declaration, :namespace_declaration,
13
+ :comment, :comment,
14
+ :start_selector, :start_selector,
15
+ :end_selector, :end_selector,
16
+ :property, :property,
17
+ :start_font_face, :start_font_face,
18
+ :end_font_face, :end_font_face,
19
+ :start_media, :start_media,
20
+ :end_media, :end_media
21
+ )
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRNum < FFI::Struct
4
+ layout(
5
+ :type, :int,
6
+ :value, :double,
7
+ :line, :int,
8
+ :column, :int,
9
+ :byte_offset, :int
10
+ )
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRParser < FFI::Struct
4
+ layout(:priv, :pointer)
5
+
6
+ #def self.release pointer
7
+ # CSSPool::LibCroco.cr_parser_destroy pointer
8
+ #end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRParsingLocation < FFI::Struct
4
+ layout(
5
+ :line, :uint,
6
+ :column, :uint,
7
+ :byte_offset, :uint
8
+ )
9
+
10
+ def to_h
11
+ Hash[*[:line, :column, :byte_offset].map { |k|
12
+ [k, self[k]]
13
+ }.flatten]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRPseudo < FFI::Struct
4
+ layout(
5
+ :pseudo_type, :int,
6
+ :name, :pointer,
7
+ :extra, :pointer,
8
+ :line, :int,
9
+ :column, :int,
10
+ :byte_offset, :int
11
+ )
12
+ end
13
+ end
14
+ end