maca-fork-csspool 2.0.2

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 (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,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,78 @@
1
+ require 'ffi'
2
+
3
+ ENV['LIBCROCO'] ||= 'libcroco-0.6'
4
+
5
+ module CSSPool
6
+ module LibCroco
7
+ extend FFI::Library
8
+
9
+ retry_times = 0
10
+
11
+ retry_places = {
12
+ 0 => '/opt/local/lib/libcroco-0.6.dylib',
13
+ 1 => '/opt/local/lib/libcroco-0.6.so',
14
+ 2 => '/usr/local/lib/libcroco-0.6.dylib',
15
+ 3 => '/usr/local/lib/libcroco-0.6.so',
16
+ 4 => '/usr/lib/libcroco-0.6.dylib',
17
+ 5 => '/usr/lib/libcroco-0.6.so',
18
+ }
19
+
20
+ begin
21
+ ffi_lib ENV['LIBCROCO']
22
+ rescue LoadError => ex
23
+ if retry_places.key?(retry_times)
24
+ ENV['LIBCROCO'] = retry_places[retry_times]
25
+ retry_times += 1
26
+ retry
27
+ end
28
+ warn "### Please install libcroco"
29
+ warn "### Set LD_LIBRARY_PATH *or* set LIBCROCO to point at libcroco-0.6.dylib"
30
+ raise ex
31
+ end
32
+
33
+ attach_function :cr_doc_handler_new, [], :pointer
34
+ attach_function :cr_parser_new_from_buf, [:string, :int, :int, :int], :pointer
35
+ attach_function :cr_parser_set_sac_handler, [:pointer, :pointer], :int
36
+ attach_function :cr_parser_parse, [:pointer], :int
37
+ attach_function :cr_parser_destroy, [:pointer], :void
38
+ attach_function :cr_doc_handler_destroy, [:pointer], :void
39
+ attach_function :cr_string_peek_raw_str, [:pointer], :pointer
40
+ attach_function :cr_simple_sel_compute_specificity, [:pointer], :int
41
+
42
+ callback :start_document, [:pointer], :void
43
+ callback :end_document, [:pointer], :void
44
+ callback :charset, [:pointer, :pointer, :pointer], :void
45
+ callback :import_style, [:pointer] * 5, :void
46
+ callback :import_style_result, [:pointer] * 5, :void
47
+ callback :namespace_declaration, [:pointer] * 4, :void
48
+ callback :comment, [:pointer, :pointer], :void
49
+ callback :start_selector, [:pointer, :pointer], :void
50
+ callback :end_selector, [:pointer, :pointer], :void
51
+ callback :property, [:pointer, :pointer, :pointer, :int], :void
52
+ callback :start_font_face, [:pointer] * 2, :void
53
+ callback :end_font_face, [:pointer], :void
54
+ callback :start_media, [:pointer] * 3, :void
55
+ callback :end_media, [:pointer] * 2, :void
56
+
57
+ def self.location_to_h thing
58
+ {
59
+ :line => thing[:line],
60
+ :column => thing[:column],
61
+ :byte_offset => thing[:byte_offset]
62
+ }
63
+ end
64
+ end
65
+ end
66
+
67
+ require 'csspool/lib_croco/cr_doc_handler'
68
+ require 'csspool/lib_croco/cr_pseudo'
69
+ require 'csspool/lib_croco/cr_parsing_location'
70
+ require 'csspool/lib_croco/glist'
71
+ require 'csspool/lib_croco/cr_simple_sel'
72
+ require 'csspool/lib_croco/cr_selector'
73
+ require 'csspool/lib_croco/cr_additional_sel'
74
+ require 'csspool/lib_croco/cr_attr_sel'
75
+ require 'csspool/lib_croco/cr_term'
76
+ require 'csspool/lib_croco/cr_parser'
77
+ require 'csspool/lib_croco/cr_num'
78
+ require 'csspool/lib_croco/cr_rgb'
@@ -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
@@ -0,0 +1,18 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRRgb < FFI::Struct
4
+ layout(
5
+ :name, :string,
6
+ :red, :long,
7
+ :green, :long,
8
+ :blue, :long,
9
+ :is_percentage, :int,
10
+ :inherit, :int,
11
+ :is_transparent,:int,
12
+ :line, :int,
13
+ :column, :int,
14
+ :byte_offset, :int
15
+ )
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRSelector < FFI::Struct
4
+ layout(
5
+ :simple_sel, :pointer,
6
+ :next, :pointer,
7
+ :prev, :pointer,
8
+ :line, :int,
9
+ :column, :int,
10
+ :byte_offset, :int,
11
+ :ref_count, :long
12
+ )
13
+
14
+ def to_selector
15
+ simple_selectors = []
16
+ pointer = self[:simple_sel]
17
+
18
+ until pointer.null?
19
+ simple_selectors << CRSimpleSel.new(pointer)
20
+ pointer = simple_selectors.last[:next]
21
+ end
22
+
23
+ simple_selectors = simple_selectors.map { |sel| sel.to_simple_selector }
24
+
25
+ Selector.new simple_selectors, {
26
+ :line => self[:line],
27
+ :column => self[:column],
28
+ :byte_offset => self[:byte_offset]
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,54 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRSimpleSel < FFI::Struct
4
+ layout(
5
+ :type_mask, :int,
6
+ :case_sensitive, :int,
7
+ :name, :pointer,
8
+ :combinator, :int,
9
+ :add_sel, :pointer,
10
+ :specificity, :ulong,
11
+ :next, :pointer,
12
+ :prev, :pointer,
13
+ :line, :int,
14
+ :column, :int,
15
+ :byte_offset, :int
16
+ )
17
+
18
+ def to_simple_selector
19
+ klass = CSSPool::Selectors::Simple
20
+
21
+ case self[:type_mask]
22
+ when 1 # UNIVERSAL_SELECTOR
23
+ klass = CSSPool::Selectors::Universal
24
+ when 1 << 1 # TYPE_SELECTOR
25
+ klass = CSSPool::Selectors::Type
26
+ end
27
+
28
+ simple_sel = klass.new(
29
+ self[:name].null? ? nil :
30
+ LibCroco.cr_string_peek_raw_str(self[:name]).read_string,
31
+ self[:combinator]
32
+ )
33
+ simple_sel.parse_location = {
34
+ :line => self[:line],
35
+ :column => self[:column],
36
+ :byte_offset => self[:byte_offset]
37
+ }
38
+
39
+ additional_selectors = []
40
+ pointer = self[:add_sel]
41
+ until pointer.null?
42
+ additional_selectors << CRAdditionalSel.new(pointer)
43
+ pointer = additional_selectors.last[:next]
44
+ end
45
+
46
+ simple_sel.additional_selectors = additional_selectors.map { |as|
47
+ as.to_additional_selector
48
+ }
49
+
50
+ simple_sel
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,97 @@
1
+ module CSSPool
2
+ module LibCroco
3
+ class CRTerm < FFI::Struct
4
+ layout(
5
+ :type, :int,
6
+ :unary_op, :int,
7
+ :operator, :int,
8
+ :content, :pointer,
9
+ :ext_content, :pointer,
10
+ :app_data, :pointer,
11
+ :ref_count, :long,
12
+ :next, :pointer,
13
+ :pref, :pointer,
14
+ :line, :int,
15
+ :column, :int,
16
+ :byte_offset, :int
17
+ )
18
+
19
+ def to_term
20
+ operator = {
21
+ 0 => nil,
22
+ 1 => '/',
23
+ 2 => ','
24
+ }[self[:operator]]
25
+
26
+ unary_op = {
27
+ 0 => nil,
28
+ 1 => :plus,
29
+ 2 => :minus
30
+ }[self[:unary_op]]
31
+
32
+ case self[:type]
33
+ when 0 # TERM_NO_TYPE
34
+ when 1 # TERM_NUMBER
35
+ num = CRNum.new(self[:content])
36
+ CSSPool::Terms::Number.new(
37
+ num[:type],
38
+ unary_op,
39
+ num[:value],
40
+ operator,
41
+ LibCroco.location_to_h(self)
42
+ )
43
+ when 2 # TERM_FUNCTION
44
+ name = LibCroco.cr_string_peek_raw_str(self[:content]).read_string
45
+ params = []
46
+ term = self[:ext_content]
47
+ until term.null?
48
+ params << LibCroco::CRTerm.new(term)
49
+ term = params.last[:next]
50
+ end
51
+ CSSPool::Terms::Function.new(
52
+ name,
53
+ params.map { |param| param.to_term },
54
+ operator,
55
+ LibCroco.location_to_h(self)
56
+ )
57
+ when 3 # TERM_STRING
58
+ CSSPool::Terms::String.new(
59
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string,
60
+ operator,
61
+ LibCroco.location_to_h(self)
62
+ )
63
+ when 4 # TERM_IDENT
64
+ CSSPool::Terms::Ident.new(
65
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string,
66
+ operator,
67
+ LibCroco.location_to_h(self)
68
+ )
69
+ when 5 # TERM_URI
70
+ CSSPool::Terms::URI.new(
71
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string,
72
+ operator,
73
+ LibCroco.location_to_h(self)
74
+ )
75
+ when 6 # TERM_RGB
76
+ rgb = LibCroco::CRRgb.new(self[:content])
77
+ CSSPool::Terms::Rgb.new(
78
+ rgb[:red],
79
+ rgb[:green],
80
+ rgb[:blue],
81
+ rgb[:is_percentage] == 1,
82
+ operator,
83
+ LibCroco.location_to_h(rgb)
84
+ )
85
+ when 7 # TERM_UNICODERANGE
86
+ raise "libcroco doesn't seem to support this term"
87
+ when 8 # TERM_HASH
88
+ CSSPool::Terms::Hash.new(
89
+ LibCroco.cr_string_peek_raw_str(self[:content]).read_string,
90
+ operator,
91
+ LibCroco.location_to_h(self)
92
+ )
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end