csspool 0.2.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +16 -0
- data/{CHANGELOG.txt → CHANGELOG.rdoc} +10 -0
- data/Manifest.txt +60 -72
- data/README.rdoc +78 -0
- data/Rakefile +9 -52
- data/lib/csspool/collection.rb +50 -0
- data/lib/csspool/css/charset.rb +7 -0
- data/lib/csspool/css/declaration.rb +8 -0
- data/lib/csspool/css/document.rb +34 -0
- data/lib/csspool/css/document_handler.rb +51 -0
- data/lib/csspool/css/import_rule.rb +15 -0
- data/lib/csspool/css/media.rb +7 -0
- data/lib/csspool/css/rule_set.rb +12 -0
- data/lib/csspool/css.rb +7 -0
- data/lib/csspool/lib_croco/cr_additional_sel.rb +46 -0
- data/lib/csspool/lib_croco/cr_attr_sel.rb +16 -0
- data/lib/csspool/lib_croco/cr_doc_handler.rb +24 -0
- data/lib/csspool/lib_croco/cr_num.rb +13 -0
- data/lib/csspool/lib_croco/cr_parser.rb +11 -0
- data/lib/csspool/lib_croco/cr_parsing_location.rb +17 -0
- data/lib/csspool/lib_croco/cr_pseudo.rb +14 -0
- data/lib/csspool/lib_croco/cr_rgb.rb +18 -0
- data/lib/csspool/lib_croco/cr_selector.rb +34 -0
- data/lib/csspool/lib_croco/cr_simple_sel.rb +54 -0
- data/lib/csspool/lib_croco/cr_term.rb +97 -0
- data/lib/csspool/lib_croco/glist.rb +21 -0
- data/lib/csspool/lib_croco.rb +78 -0
- data/lib/csspool/node.rb +5 -0
- data/lib/csspool/sac/document.rb +35 -0
- data/lib/csspool/sac/parser.rb +122 -0
- data/lib/csspool/sac.rb +2 -0
- data/lib/csspool/selector.rb +32 -0
- data/lib/csspool/selectors/additional.rb +6 -0
- data/lib/csspool/selectors/attribute.rb +21 -0
- data/lib/csspool/selectors/class.rb +11 -0
- data/lib/csspool/selectors/id.rb +11 -0
- data/lib/csspool/selectors/pseudo_class.rb +13 -0
- data/lib/csspool/selectors/simple.rb +22 -0
- data/lib/csspool/selectors/type.rb +6 -0
- data/lib/csspool/selectors/universal.rb +6 -0
- data/lib/csspool/selectors.rb +8 -0
- data/lib/csspool/terms/function.rb +17 -0
- data/lib/csspool/terms/hash.rb +6 -0
- data/lib/csspool/terms/ident.rb +15 -0
- data/lib/csspool/terms/number.rb +14 -0
- data/lib/csspool/terms/rgb.rb +23 -0
- data/lib/csspool/terms/string.rb +6 -0
- data/lib/csspool/terms/uri.rb +6 -0
- data/lib/csspool/terms.rb +7 -0
- data/lib/csspool/visitable.rb +18 -0
- data/lib/csspool/visitors/comparable.rb +78 -0
- data/lib/csspool/visitors/to_css.rb +201 -0
- data/lib/csspool/visitors/visitor.rb +17 -0
- data/lib/csspool/visitors.rb +3 -0
- data/lib/csspool.rb +18 -1
- data/test/css/test_document.rb +13 -0
- data/test/css/test_import_rule.rb +42 -0
- data/test/helper.rb +62 -6
- data/test/sac/test_parser.rb +115 -0
- data/test/sac/test_properties.rb +43 -0
- data/test/sac/test_terms.rb +134 -0
- data/test/test_collection.rb +81 -0
- data/test/test_parser.rb +75 -241
- data/test/test_selector.rb +45 -34
- data/test/visitors/test_comparable.rb +72 -0
- data/test/visitors/test_to_css.rb +141 -0
- metadata +98 -85
- data/README.txt +0 -79
- data/lib/css/sac/conditions/attribute_condition.rb +0 -56
- data/lib/css/sac/conditions/begin_hyphen_condition.rb +0 -22
- data/lib/css/sac/conditions/class_condition.rb +0 -22
- data/lib/css/sac/conditions/combinator_condition.rb +0 -40
- data/lib/css/sac/conditions/condition.rb +0 -35
- data/lib/css/sac/conditions/id_condition.rb +0 -27
- data/lib/css/sac/conditions/one_of_condition.rb +0 -22
- data/lib/css/sac/conditions/pseudo_class_condition.rb +0 -24
- data/lib/css/sac/conditions.rb +0 -5
- data/lib/css/sac/document_handler.rb +0 -67
- data/lib/css/sac/error_handler.rb +0 -14
- data/lib/css/sac/generated_parser.rb +0 -999
- data/lib/css/sac/generated_property_parser.rb +0 -9214
- data/lib/css/sac/lexeme.rb +0 -29
- data/lib/css/sac/lexical_unit.rb +0 -203
- data/lib/css/sac/parse_exception.rb +0 -6
- data/lib/css/sac/parser.rb +0 -115
- data/lib/css/sac/property_parser.rb +0 -47
- data/lib/css/sac/selectors/child_selector.rb +0 -40
- data/lib/css/sac/selectors/conditional_selector.rb +0 -49
- data/lib/css/sac/selectors/descendant_selector.rb +0 -40
- data/lib/css/sac/selectors/element_selector.rb +0 -39
- data/lib/css/sac/selectors/selector.rb +0 -31
- data/lib/css/sac/selectors/sibling_selector.rb +0 -39
- data/lib/css/sac/selectors/simple_selector.rb +0 -25
- data/lib/css/sac/selectors.rb +0 -5
- data/lib/css/sac/token.rb +0 -27
- data/lib/css/sac/tokenizer.rb +0 -187
- data/lib/css/sac/visitable.rb +0 -106
- data/lib/css/sac.rb +0 -14
- data/lib/css/stylesheet/rule.rb +0 -20
- data/lib/css/stylesheet/stylesheet.rb +0 -96
- data/lib/css/stylesheet.rb +0 -3
- data/lib/parser.y +0 -308
- data/lib/property_parser.y +0 -2346
- data/lib/property_parser.y.erb +0 -1321
- data/test/condition_test_case.rb +0 -6
- data/test/selector_test_case.rb +0 -20
- data/test/test_all.rb +0 -4
- data/test/test_attribute_condition.rb +0 -39
- data/test/test_begin_hyphen_condition.rb +0 -15
- data/test/test_child_selector.rb +0 -60
- data/test/test_class_condition.rb +0 -15
- data/test/test_combinator_condition.rb +0 -38
- data/test/test_condition.rb +0 -31
- data/test/test_conditional_selector.rb +0 -44
- data/test/test_descendant_selector.rb +0 -64
- data/test/test_element_selector.rb +0 -31
- data/test/test_error_recovery.rb +0 -28
- data/test/test_id_condition.rb +0 -16
- data/test/test_lexeme.rb +0 -39
- data/test/test_lexical_color.rb +0 -49
- data/test/test_lexical_function.rb +0 -31
- data/test/test_lexical_ident.rb +0 -30
- data/test/test_lexical_number.rb +0 -68
- data/test/test_lexical_string.rb +0 -23
- data/test/test_lexical_unit.rb +0 -106
- data/test/test_lexical_uri.rb +0 -28
- data/test/test_one_of_condition.rb +0 -15
- data/test/test_parse_error.rb +0 -199
- data/test/test_property_parser.rb +0 -593
- data/test/test_selector_as_string.rb +0 -94
- data/test/test_selector_parser.rb +0 -178
- data/test/test_sibling_selector.rb +0 -42
- data/test/test_simple_selector.rb +0 -9
- data/test/test_specificity.rb +0 -76
- data/test/test_stylesheet.rb +0 -95
- data/test/test_token.rb +0 -24
- data/test/test_tokenizer.rb +0 -127
- data/test/test_xpath.rb +0 -105
@@ -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,34 @@
|
|
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
|
+
LibCroco.cr_simple_sel_compute_specificity(pointer)
|
20
|
+
simple_selectors << CRSimpleSel.new(pointer)
|
21
|
+
pointer = simple_selectors.last[:next]
|
22
|
+
end
|
23
|
+
|
24
|
+
simple_selectors = simple_selectors.map { |sel| sel.to_simple_selector }
|
25
|
+
|
26
|
+
Selector.new simple_selectors, {
|
27
|
+
:line => self[:line],
|
28
|
+
:column => self[:column],
|
29
|
+
:byte_offset => self[:byte_offset]
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
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
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module LibCroco
|
3
|
+
class GList < FFI::Struct
|
4
|
+
layout(
|
5
|
+
:data, :pointer,
|
6
|
+
:next, :pointer,
|
7
|
+
:prev, :pointer
|
8
|
+
)
|
9
|
+
|
10
|
+
def to_a
|
11
|
+
list = [self]
|
12
|
+
pointer = list.last[:next]
|
13
|
+
until pointer.null?
|
14
|
+
list << GList.new(pointer)
|
15
|
+
pointer = list.last[:next]
|
16
|
+
end
|
17
|
+
list.map { |x| x[:data] }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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'
|
data/lib/csspool/node.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module SAC
|
3
|
+
class Document
|
4
|
+
def start_document
|
5
|
+
end
|
6
|
+
|
7
|
+
def end_document
|
8
|
+
end
|
9
|
+
|
10
|
+
def charset name, location
|
11
|
+
end
|
12
|
+
|
13
|
+
def import_style media_list, uri, default_ns, location
|
14
|
+
end
|
15
|
+
|
16
|
+
def start_selector selector_list
|
17
|
+
end
|
18
|
+
|
19
|
+
def end_selector selector_list
|
20
|
+
end
|
21
|
+
|
22
|
+
def property name, expression, important
|
23
|
+
end
|
24
|
+
|
25
|
+
def comment comment
|
26
|
+
end
|
27
|
+
|
28
|
+
def start_media media_list, parse_location
|
29
|
+
end
|
30
|
+
|
31
|
+
def end_media media_list, parse_location
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module SAC
|
3
|
+
class Parser
|
4
|
+
attr_accessor :document
|
5
|
+
|
6
|
+
def initialize document = CSSPool::SAC::Document.new
|
7
|
+
@document = document
|
8
|
+
@selector_stack = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse string
|
12
|
+
parse_memory(string, 5)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def push selectors
|
17
|
+
@selector_stack << selectors
|
18
|
+
end
|
19
|
+
|
20
|
+
def pop
|
21
|
+
@selector_stack.pop
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_memory string, encoding
|
25
|
+
parser = LibCroco::CRParser.new(
|
26
|
+
LibCroco.cr_parser_new_from_buf(
|
27
|
+
string,
|
28
|
+
string.length,
|
29
|
+
5,
|
30
|
+
0
|
31
|
+
)
|
32
|
+
)
|
33
|
+
sac_handler =
|
34
|
+
LibCroco::CRDocHandler.new(LibCroco.cr_doc_handler_new)
|
35
|
+
|
36
|
+
selector_stack = []
|
37
|
+
media_stack = []
|
38
|
+
|
39
|
+
sac_handler[:start_document] = lambda { |parser|
|
40
|
+
@document.start_document
|
41
|
+
}
|
42
|
+
sac_handler[:end_document] = lambda { |parser|
|
43
|
+
@document.end_document
|
44
|
+
}
|
45
|
+
sac_handler[:charset] = lambda { |dh, name, location|
|
46
|
+
@document.charset(
|
47
|
+
LibCroco.cr_string_peek_raw_str(name).read_string,
|
48
|
+
LibCroco::CRParsingLocation.new(location).to_h
|
49
|
+
)
|
50
|
+
}
|
51
|
+
|
52
|
+
sac_handler[:import_style] = lambda { |dh, media_list, uri, ns, loc|
|
53
|
+
media_list = media_list.null? ? [] : LibCroco::GList.new(media_list)
|
54
|
+
media_list = media_list.to_a.map { |data|
|
55
|
+
LibCroco.cr_string_peek_raw_str(data).read_string
|
56
|
+
}
|
57
|
+
uri = uri.null? ? nil :
|
58
|
+
LibCroco.cr_string_peek_raw_str(uri).read_string
|
59
|
+
|
60
|
+
ns = ns.null? ? nil : LibCroco.cr_string_peek_raw_str(ns).read_string
|
61
|
+
|
62
|
+
@document.import_style(
|
63
|
+
media_list,
|
64
|
+
uri,
|
65
|
+
ns,
|
66
|
+
LibCroco::CRParsingLocation.new(loc).to_h
|
67
|
+
)
|
68
|
+
}
|
69
|
+
|
70
|
+
sac_handler[:start_selector] = lambda { |dh, list|
|
71
|
+
list = [LibCroco::CRSelector.new(list)]
|
72
|
+
pointer = list.last[:next]
|
73
|
+
until pointer.null?
|
74
|
+
list << LibCroco::CRSelector.new(pointer)
|
75
|
+
pointer = list.last[:next]
|
76
|
+
end
|
77
|
+
selector_stack.push list.map { |l| l.to_selector }
|
78
|
+
@document.start_selector selector_stack.last
|
79
|
+
}
|
80
|
+
|
81
|
+
sac_handler[:end_selector] = lambda { |dh, list|
|
82
|
+
@document.end_selector selector_stack.pop
|
83
|
+
}
|
84
|
+
|
85
|
+
sac_handler[:property] = lambda { |dh, name, expr, important|
|
86
|
+
expr_list = []
|
87
|
+
until expr.null?
|
88
|
+
expr_list << LibCroco::CRTerm.new(expr)
|
89
|
+
expr = expr_list.last[:next]
|
90
|
+
end
|
91
|
+
@document.property(
|
92
|
+
LibCroco.cr_string_peek_raw_str(name).read_string,
|
93
|
+
expr_list.map { |ex| ex.to_term },
|
94
|
+
important == 1
|
95
|
+
)
|
96
|
+
}
|
97
|
+
|
98
|
+
sac_handler[:comment] = lambda { |dh, comment|
|
99
|
+
@document.comment(
|
100
|
+
LibCroco.cr_string_peek_raw_str(comment).read_string
|
101
|
+
)
|
102
|
+
}
|
103
|
+
|
104
|
+
sac_handler[:start_media] = lambda { |dh,media_list,location|
|
105
|
+
media_list = LibCroco::GList.new(media_list)
|
106
|
+
media_stack << [media_list.to_a.map { |data|
|
107
|
+
LibCroco.cr_string_peek_raw_str(data).read_string
|
108
|
+
}, LibCroco::CRParsingLocation.new(location).to_h]
|
109
|
+
@document.start_media(*media_stack.last)
|
110
|
+
}
|
111
|
+
|
112
|
+
sac_handler[:end_media] = lambda { |dh,media_list|
|
113
|
+
@document.end_media(*media_stack.pop)
|
114
|
+
}
|
115
|
+
|
116
|
+
LibCroco.cr_parser_set_sac_handler(parser, sac_handler)
|
117
|
+
LibCroco.cr_parser_parse(parser)
|
118
|
+
LibCroco.cr_doc_handler_destroy(sac_handler)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/lib/csspool/sac.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module CSSPool
|
2
|
+
class Selector < CSSPool::Node
|
3
|
+
attr_accessor :simple_selectors
|
4
|
+
attr_accessor :parse_location
|
5
|
+
attr_accessor :rule_set
|
6
|
+
|
7
|
+
def initialize simple_selectors = [], parse_location = {}
|
8
|
+
@simple_selectors = simple_selectors
|
9
|
+
@parse_location = parse_location
|
10
|
+
@rule_set = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def declarations
|
14
|
+
@rule_set.declarations
|
15
|
+
end
|
16
|
+
|
17
|
+
def specificity
|
18
|
+
a = b = c = 0
|
19
|
+
simple_selectors.each do |s|
|
20
|
+
c += 1
|
21
|
+
s.additional_selectors.each do |additional_selector|
|
22
|
+
if Selectors::Id === additional_selector
|
23
|
+
a += 1
|
24
|
+
else
|
25
|
+
b += 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
[a, b, c]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module Selectors
|
3
|
+
class Attribute < CSSPool::Selectors::Additional
|
4
|
+
attr_accessor :name
|
5
|
+
attr_accessor :value
|
6
|
+
attr_accessor :match_way
|
7
|
+
|
8
|
+
NO_MATCH = 0
|
9
|
+
SET = 1
|
10
|
+
EQUALS = 2
|
11
|
+
INCLUDES = 3
|
12
|
+
DASHMATCH = 4
|
13
|
+
|
14
|
+
def initialize name, value, match_way
|
15
|
+
@name = name
|
16
|
+
@value = value
|
17
|
+
@match_way = match_way
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module Selectors
|
3
|
+
class Simple < CSSPool::Node
|
4
|
+
NO_COMBINATOR = 0
|
5
|
+
DESCENDENT = 1
|
6
|
+
PRECEDED_BY = 2
|
7
|
+
CHILD = 3
|
8
|
+
|
9
|
+
attr_accessor :name
|
10
|
+
attr_accessor :parse_location
|
11
|
+
attr_accessor :additional_selectors
|
12
|
+
attr_accessor :combinator
|
13
|
+
|
14
|
+
def initialize name, combinator = nil
|
15
|
+
@name = name
|
16
|
+
@combinator = combinator
|
17
|
+
@parse_location = nil
|
18
|
+
@additional_selectors = []
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'csspool/selectors/simple'
|
2
|
+
require 'csspool/selectors/universal'
|
3
|
+
require 'csspool/selectors/type'
|
4
|
+
require 'csspool/selectors/additional'
|
5
|
+
require 'csspool/selectors/id'
|
6
|
+
require 'csspool/selectors/class'
|
7
|
+
require 'csspool/selectors/pseudo_class'
|
8
|
+
require 'csspool/selectors/attribute'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module Terms
|
3
|
+
class Function < CSSPool::Node
|
4
|
+
attr_accessor :name
|
5
|
+
attr_accessor :params
|
6
|
+
attr_accessor :parse_location
|
7
|
+
attr_accessor :operator
|
8
|
+
|
9
|
+
def initialize name, params, operator, parse_location
|
10
|
+
@name = name
|
11
|
+
@params = params
|
12
|
+
@operator = operator
|
13
|
+
@parse_location = parse_location
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module Terms
|
3
|
+
class Ident < CSSPool::Node
|
4
|
+
attr_accessor :value
|
5
|
+
attr_accessor :operator
|
6
|
+
attr_accessor :parse_location
|
7
|
+
|
8
|
+
def initialize value, operator, parse_location
|
9
|
+
@value = value
|
10
|
+
@operator = operator
|
11
|
+
@parse_location = parse_location
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module CSSPool
|
2
|
+
module Terms
|
3
|
+
class Number < Ident
|
4
|
+
attr_accessor :type
|
5
|
+
attr_accessor :unary_operator
|
6
|
+
|
7
|
+
def initialize type, unary_operator, value, operator, parse_location
|
8
|
+
@type = type
|
9
|
+
@unary_operator = unary_operator
|
10
|
+
super(value, operator, parse_location)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|