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
data/lib/css/sac/lexeme.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module CSS
|
2
|
-
module SAC
|
3
|
-
class Lexeme
|
4
|
-
attr_reader :name, :pattern
|
5
|
-
|
6
|
-
def initialize(name, pattern=nil, &block)
|
7
|
-
raise ArgumentError, "name required" unless name
|
8
|
-
|
9
|
-
@name = name
|
10
|
-
patterns = []
|
11
|
-
|
12
|
-
patterns << pattern if pattern
|
13
|
-
yield(patterns) if block_given?
|
14
|
-
|
15
|
-
if patterns.empty?
|
16
|
-
raise ArgumentError, "at least one pattern required"
|
17
|
-
end
|
18
|
-
|
19
|
-
patterns.collect! do |pattern|
|
20
|
-
source = pattern.source
|
21
|
-
source = "\\A#{source}"
|
22
|
-
Regexp.new(source, Regexp::IGNORECASE + Regexp::MULTILINE)
|
23
|
-
end
|
24
|
-
|
25
|
-
@pattern = Regexp.union(*patterns)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/css/sac/lexical_unit.rb
DELETED
@@ -1,203 +0,0 @@
|
|
1
|
-
module CSS
|
2
|
-
module SAC
|
3
|
-
class LexicalUnit
|
4
|
-
attr_accessor :dimension_unit_text,
|
5
|
-
:lexical_unit_type,
|
6
|
-
:float_value,
|
7
|
-
:integer_value,
|
8
|
-
:string_value,
|
9
|
-
:parameters,
|
10
|
-
:function_name
|
11
|
-
|
12
|
-
def ==(other)
|
13
|
-
self.class === other && self.lexical_unit_type == other.lexical_unit_type
|
14
|
-
end
|
15
|
-
|
16
|
-
def eql?(other)
|
17
|
-
self == other
|
18
|
-
end
|
19
|
-
|
20
|
-
alias :to_s :string_value
|
21
|
-
end
|
22
|
-
|
23
|
-
class Function < LexicalUnit
|
24
|
-
FUNCTIONS = {
|
25
|
-
'counter' => :SAC_COUNTER_FUNCTION,
|
26
|
-
'counters' => :SAC_COUNTERS_FUNCTION,
|
27
|
-
'rect' => :SAC_RECT_FUNCTION,
|
28
|
-
}
|
29
|
-
def initialize(name, params)
|
30
|
-
self.string_value = "#{name}#{params.join(', ')})"
|
31
|
-
name =~ /^(.*)\(/
|
32
|
-
self.function_name = $1
|
33
|
-
self.parameters = params
|
34
|
-
self.lexical_unit_type = FUNCTIONS[self.function_name] || :SAC_FUNCTION
|
35
|
-
end
|
36
|
-
|
37
|
-
def ==(other)
|
38
|
-
super && %w{ function_name parameters }.all? { |x|
|
39
|
-
self.send(x.to_sym) == other.send(x.to_sym)
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
def hash
|
44
|
-
([self.function_name] + parameters).hash
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class Color < LexicalUnit
|
49
|
-
def initialize(value)
|
50
|
-
self.string_value = value
|
51
|
-
self.lexical_unit_type = :SAC_RGBCOLOR
|
52
|
-
if value =~ /^#([A-F\d]{1,2})([A-F\d]{1,2})([A-F\d]{1,2})$/
|
53
|
-
self.parameters = [$1, $2, $3].map { |x|
|
54
|
-
x.length == 1 ? (x * 2).hex : x.hex
|
55
|
-
}.map { |x|
|
56
|
-
Number.new(x, '', :SAC_INTEGER)
|
57
|
-
}
|
58
|
-
else
|
59
|
-
self.parameters = [LexicalIdent.new(value)]
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def ==(other)
|
64
|
-
super && self.parameters == other.parameters
|
65
|
-
end
|
66
|
-
|
67
|
-
def hash
|
68
|
-
self.parameters.hash
|
69
|
-
end
|
70
|
-
|
71
|
-
def to_s
|
72
|
-
if self.parameters.length < 3
|
73
|
-
super
|
74
|
-
else
|
75
|
-
hex = self.parameters.map { |x|
|
76
|
-
sprintf("%02X", x.integer_value).split('').uniq
|
77
|
-
}.flatten
|
78
|
-
hex.length != 3 ? super : "##{hex.join()}"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
class LexicalString < LexicalUnit
|
84
|
-
def initialize(value)
|
85
|
-
self.string_value = value
|
86
|
-
self.lexical_unit_type = :SAC_STRING_VALUE
|
87
|
-
end
|
88
|
-
|
89
|
-
def ==(other)
|
90
|
-
super && self.string_value == other.string_value
|
91
|
-
end
|
92
|
-
|
93
|
-
def hash
|
94
|
-
self.string_value.hash
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
class LexicalIdent < LexicalUnit
|
99
|
-
def initialize(value)
|
100
|
-
self.string_value = value
|
101
|
-
self.lexical_unit_type = :SAC_IDENT
|
102
|
-
end
|
103
|
-
|
104
|
-
def ==(other)
|
105
|
-
super && self.string_value == other.string_value
|
106
|
-
end
|
107
|
-
|
108
|
-
def hash
|
109
|
-
self.string_value.hash
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
class LexicalURI < LexicalUnit
|
114
|
-
def initialize(value)
|
115
|
-
self.string_value = value.gsub(/^url\(/, '').gsub(/\)$/, '')
|
116
|
-
self.lexical_unit_type = :SAC_URI
|
117
|
-
end
|
118
|
-
|
119
|
-
def ==(other)
|
120
|
-
super && self.string_value == other.string_value
|
121
|
-
end
|
122
|
-
|
123
|
-
def hash
|
124
|
-
self.string_value.hash
|
125
|
-
end
|
126
|
-
|
127
|
-
def to_s
|
128
|
-
"url(#{string_value})"
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
class Number < LexicalUnit
|
133
|
-
NON_NEGATIVE_UNITS = [
|
134
|
-
:SAC_DEGREE,
|
135
|
-
:SAC_GRADIAN,
|
136
|
-
:SAC_RADIAN,
|
137
|
-
:SAC_MILLISECOND,
|
138
|
-
:SAC_SECOND,
|
139
|
-
:SAC_HERTZ,
|
140
|
-
:SAC_KILOHERTZ,
|
141
|
-
]
|
142
|
-
UNITS = {
|
143
|
-
'deg' => :SAC_DEGREE,
|
144
|
-
'rad' => :SAC_RADIAN,
|
145
|
-
'grad' => :SAC_GRADIAN,
|
146
|
-
'ms' => :SAC_MILLISECOND,
|
147
|
-
's' => :SAC_SECOND,
|
148
|
-
'hz' => :SAC_HERTZ,
|
149
|
-
'khz' => :SAC_KILOHERTZ,
|
150
|
-
'px' => :SAC_PIXEL,
|
151
|
-
'cm' => :SAC_CENTIMETER,
|
152
|
-
'mm' => :SAC_MILLIMETER,
|
153
|
-
'in' => :SAC_INCH,
|
154
|
-
'pt' => :SAC_POINT,
|
155
|
-
'pc' => :SAC_PICA,
|
156
|
-
'%' => :SAC_PERCENTAGE,
|
157
|
-
'em' => :SAC_EM,
|
158
|
-
'ex' => :SAC_EX,
|
159
|
-
}
|
160
|
-
def initialize(value, unit = nil, type = nil)
|
161
|
-
if value.is_a?(String)
|
162
|
-
value =~ /^(-?[0-9.]*)(.*)$/
|
163
|
-
value = $1
|
164
|
-
unit ||= $2
|
165
|
-
end
|
166
|
-
type ||= UNITS[self.dimension_unit_text]
|
167
|
-
self.string_value = "#{value}#{unit}"
|
168
|
-
self.float_value = value.to_f
|
169
|
-
self.integer_value = value.to_i
|
170
|
-
self.dimension_unit_text = unit.downcase
|
171
|
-
self.lexical_unit_type = UNITS[self.dimension_unit_text] ||
|
172
|
-
(value =~ /\./ ? :SAC_NUMBER : :SAC_INTEGER)
|
173
|
-
end
|
174
|
-
|
175
|
-
def ==(other)
|
176
|
-
return true if self.float_value == 0 && other.float_value == 0
|
177
|
-
return false unless super
|
178
|
-
|
179
|
-
%w{ float_value integer_value dimension_unit_text }.all? { |x|
|
180
|
-
self.send(x.to_sym) == other.send(x.to_sym)
|
181
|
-
}
|
182
|
-
end
|
183
|
-
|
184
|
-
def hash
|
185
|
-
if self.float_value == 0
|
186
|
-
self.float_value.hash
|
187
|
-
else
|
188
|
-
%w{ float_value integer_value dimension_unit_text }.map { |x|
|
189
|
-
self.send(x.to_sym)
|
190
|
-
}.hash
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
def to_s
|
195
|
-
if self.float_value == 0
|
196
|
-
"0"
|
197
|
-
else
|
198
|
-
super
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
data/lib/css/sac/parser.rb
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
require "css/sac/visitable"
|
2
|
-
require "css/sac/document_handler"
|
3
|
-
require "css/sac/error_handler"
|
4
|
-
require "css/sac/generated_parser"
|
5
|
-
require "css/sac/lexical_unit"
|
6
|
-
require "css/sac/parse_exception"
|
7
|
-
require "css/sac/tokenizer"
|
8
|
-
require "css/sac/property_parser"
|
9
|
-
|
10
|
-
require "css/sac/conditions"
|
11
|
-
require "css/sac/selectors"
|
12
|
-
|
13
|
-
module CSS
|
14
|
-
module SAC
|
15
|
-
class Parser < CSS::SAC::GeneratedParser
|
16
|
-
# The version of CSSPool you're using
|
17
|
-
VERSION = '0.2.6'
|
18
|
-
|
19
|
-
TOKENIZER = Tokenizer.new
|
20
|
-
|
21
|
-
attr_accessor :document_handler, :error_handler, :logger
|
22
|
-
|
23
|
-
def initialize(document_handler = StyleSheet.new(self))
|
24
|
-
@error_handler = ErrorHandler.new
|
25
|
-
@document_handler = document_handler
|
26
|
-
@property_parser = PropertyParser.new()
|
27
|
-
@tokenizer = TOKENIZER
|
28
|
-
@logger = nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def parse_style_sheet(string)
|
32
|
-
@yydebug = true
|
33
|
-
@tokens = TOKENIZER.tokenize(string)
|
34
|
-
@position = 0
|
35
|
-
|
36
|
-
self.document_handler.start_document(string)
|
37
|
-
do_parse
|
38
|
-
self.document_handler.end_document(string)
|
39
|
-
self.document_handler
|
40
|
-
end
|
41
|
-
|
42
|
-
alias :parse :parse_style_sheet
|
43
|
-
|
44
|
-
def parse_rule(rule)
|
45
|
-
returner = Class.new(DocumentHandler) {
|
46
|
-
attr_accessor :selector
|
47
|
-
alias :start_selector :selector=
|
48
|
-
}.new
|
49
|
-
old_document_handler = self.document_handler
|
50
|
-
self.document_handler = returner
|
51
|
-
self.parse("#{rule} { }")
|
52
|
-
self.document_handler = old_document_handler
|
53
|
-
returner.selector
|
54
|
-
end
|
55
|
-
|
56
|
-
# Returns the parser version. We return CSS2, but its actually
|
57
|
-
# CSS2.1. No font-face tags. Sorry.
|
58
|
-
def parser_version
|
59
|
-
"http://www.w3.org/TR/REC-CSS2"
|
60
|
-
end
|
61
|
-
|
62
|
-
attr_reader :property_parser
|
63
|
-
attr_reader :tokenizer
|
64
|
-
|
65
|
-
private # Bro.
|
66
|
-
|
67
|
-
# We have to eliminate matching pairs.
|
68
|
-
# http://www.w3.org/TR/CSS21/syndata.html#parsing-errors
|
69
|
-
# See the malformed declarations section
|
70
|
-
def eliminate_pair_matches(error_value)
|
71
|
-
pairs = {}
|
72
|
-
pairs['"'] = '"'
|
73
|
-
pairs["'"] = "'"
|
74
|
-
pairs['{'] = '}'
|
75
|
-
pairs['['] = ']'
|
76
|
-
pairs['('] = ')'
|
77
|
-
|
78
|
-
error_value.strip!
|
79
|
-
if pairs[error_value]
|
80
|
-
logger.warn("Eliminating pair for: #{error_value}") if logger
|
81
|
-
loop {
|
82
|
-
token = next_token
|
83
|
-
eliminate_pair_matches(token[1])
|
84
|
-
logger.warn("Eliminated token: #{token.join(' ')}") if logger
|
85
|
-
if token[1] == pairs[error_value]
|
86
|
-
@position -= 1
|
87
|
-
@tokens[@position] = Token.new(:S, ' ', nil) # super hack
|
88
|
-
break
|
89
|
-
end
|
90
|
-
}
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def on_error(error_token_id, error_value, value_stack)
|
95
|
-
if logger
|
96
|
-
logger.error(token_to_str(error_token_id))
|
97
|
-
logger.error("error value: #{error_value}")
|
98
|
-
end
|
99
|
-
eliminate_pair_matches(error_value)
|
100
|
-
end
|
101
|
-
|
102
|
-
def next_token
|
103
|
-
return [false, false] if @position >= @tokens.length
|
104
|
-
|
105
|
-
n_token = @tokens[@position]
|
106
|
-
@position += 1
|
107
|
-
if n_token.name == :COMMENT
|
108
|
-
self.document_handler.comment(n_token.value)
|
109
|
-
return next_token
|
110
|
-
end
|
111
|
-
n_token.to_racc_token
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require "css/sac/generated_property_parser"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
class PropertyParser < CSS::SAC::GeneratedPropertyParser
|
6
|
-
def initialize
|
7
|
-
@tokens = []
|
8
|
-
@token_table = Racc_arg[10]
|
9
|
-
end
|
10
|
-
|
11
|
-
def parse_tokens(tokens)
|
12
|
-
negate = false # Nasty hack for unary minus
|
13
|
-
@tokens = tokens.find_all { |x| x.name != :S }.map { |token|
|
14
|
-
tok = if @token_table.has_key?(token.value)
|
15
|
-
[token.value, token.value]
|
16
|
-
else
|
17
|
-
if token.name == :delim && !@token_table.has_key?(token.value)
|
18
|
-
negate = true if token.value == '-'
|
19
|
-
nil
|
20
|
-
else
|
21
|
-
token.to_racc_token
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
if negate && tok
|
26
|
-
tok[1] = "-#{tok[1]}"
|
27
|
-
negate = false
|
28
|
-
end
|
29
|
-
|
30
|
-
tok
|
31
|
-
}.compact
|
32
|
-
|
33
|
-
begin
|
34
|
-
return do_parse
|
35
|
-
rescue ParseError => e
|
36
|
-
return nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
def next_token
|
42
|
-
return [false, false] if @tokens.empty?
|
43
|
-
@tokens.shift
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class ChildSelector < SimpleSelector
|
7
|
-
attr_accessor :ancestor_selector, :simple_selector
|
8
|
-
alias :parent :ancestor_selector
|
9
|
-
alias :selector :simple_selector
|
10
|
-
|
11
|
-
def initialize(parent, selector)
|
12
|
-
super(:SAC_CHILD_SELECTOR)
|
13
|
-
|
14
|
-
@ancestor_selector = parent
|
15
|
-
@simple_selector = selector
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_css
|
19
|
-
"#{parent.to_css} > #{selector.to_css}"
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_xpath(prefix=true)
|
23
|
-
"#{parent.to_xpath(prefix)}/#{selector.to_xpath(false)}"
|
24
|
-
end
|
25
|
-
|
26
|
-
def specificity
|
27
|
-
parent.specificity.zip(selector.specificity).map { |x,y| x + y }
|
28
|
-
end
|
29
|
-
|
30
|
-
def ==(other)
|
31
|
-
super && parent == other.parent && selector == other.selector
|
32
|
-
end
|
33
|
-
|
34
|
-
def hash
|
35
|
-
[parent, selector].hash
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class ConditionalSelector < SimpleSelector
|
7
|
-
attr_accessor :condition, :simple_selector
|
8
|
-
alias :selector :simple_selector
|
9
|
-
|
10
|
-
def initialize(selector, condition)
|
11
|
-
super(:SAC_CONDITIONAL_SELECTOR)
|
12
|
-
|
13
|
-
@condition = condition
|
14
|
-
@simple_selector = selector
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_css
|
18
|
-
[selector, condition].map { |x|
|
19
|
-
x ? x.to_css : ''
|
20
|
-
}.join('')
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_xpath(prefix=true)
|
24
|
-
atoms = []
|
25
|
-
atoms << "//" if prefix
|
26
|
-
atoms << (selector ? selector.to_xpath(false) : "*")
|
27
|
-
atoms << condition.to_xpath
|
28
|
-
|
29
|
-
atoms.join("")
|
30
|
-
end
|
31
|
-
|
32
|
-
def specificity
|
33
|
-
(selector ? selector.specificity : ([0] * 4)).zip(
|
34
|
-
(condition ? condition.specificity : ([0] * 4))).map { |x,y|
|
35
|
-
x + y
|
36
|
-
}
|
37
|
-
end
|
38
|
-
|
39
|
-
def ==(other)
|
40
|
-
super && condition == other.condition && selector == other.selector
|
41
|
-
end
|
42
|
-
|
43
|
-
def hash
|
44
|
-
[condition, selector].hash
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class DescendantSelector < SimpleSelector
|
7
|
-
attr_accessor :ancestor_selector, :simple_selector
|
8
|
-
alias :ancestor :ancestor_selector
|
9
|
-
alias :selector :simple_selector
|
10
|
-
|
11
|
-
def initialize(ancestor, selector)
|
12
|
-
super(:SAC_DESCENDANT_SELECTOR)
|
13
|
-
|
14
|
-
@ancestor_selector = ancestor
|
15
|
-
@simple_selector = selector
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_css
|
19
|
-
"#{ancestor.to_css} #{selector.to_css}"
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_xpath(prefix=true)
|
23
|
-
"#{ancestor.to_xpath(prefix)}//#{selector.to_xpath(false)}"
|
24
|
-
end
|
25
|
-
|
26
|
-
def specificity
|
27
|
-
ancestor.specificity.zip(selector.specificity).map { |x,y| x + y }
|
28
|
-
end
|
29
|
-
|
30
|
-
def ==(other)
|
31
|
-
super && selector == other.selector && ancestor == other.ancestor
|
32
|
-
end
|
33
|
-
|
34
|
-
def hash
|
35
|
-
[selector, ancestor].hash
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class ElementSelector < SimpleSelector
|
7
|
-
attr_reader :local_name
|
8
|
-
alias :name :local_name
|
9
|
-
|
10
|
-
def initialize(name)
|
11
|
-
super(:SAC_ELEMENT_NODE_SELECTOR)
|
12
|
-
@local_name = name
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_css
|
16
|
-
local_name
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_xpath(prefix=true)
|
20
|
-
atoms = [local_name]
|
21
|
-
atoms.unshift("//") if prefix
|
22
|
-
atoms.join
|
23
|
-
end
|
24
|
-
|
25
|
-
def specificity
|
26
|
-
[0, 0, 0, 1]
|
27
|
-
end
|
28
|
-
|
29
|
-
def ==(other)
|
30
|
-
super && name == other.name
|
31
|
-
end
|
32
|
-
|
33
|
-
def hash
|
34
|
-
name.hash
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module CSS
|
2
|
-
module SAC
|
3
|
-
module Selectors
|
4
|
-
class Selector
|
5
|
-
include CSS::SAC::Visitable
|
6
|
-
|
7
|
-
attr_reader :selector_type
|
8
|
-
|
9
|
-
def initialize(selector_type)
|
10
|
-
@selector_type = selector_type
|
11
|
-
end
|
12
|
-
|
13
|
-
def ==(other)
|
14
|
-
self.class === other && selector_type == other.selector_type
|
15
|
-
end
|
16
|
-
|
17
|
-
def hash
|
18
|
-
selector_type.hash
|
19
|
-
end
|
20
|
-
|
21
|
-
def eql?(other)
|
22
|
-
self == other
|
23
|
-
end
|
24
|
-
|
25
|
-
def =~(node)
|
26
|
-
MatchesVisitor.new(node).accept(self)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class SiblingSelector < SimpleSelector
|
7
|
-
attr_accessor :selector, :sibling_selector
|
8
|
-
alias :sibling :sibling_selector
|
9
|
-
|
10
|
-
def initialize(selector, sibling)
|
11
|
-
super(:SAC_DIRECT_ADJACENT_SELECTOR)
|
12
|
-
|
13
|
-
@selector = selector
|
14
|
-
@sibling_selector = sibling
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_css
|
18
|
-
"#{selector.to_css} + #{sibling.to_css}"
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_xpath(prefix=true)
|
22
|
-
"#{selector.to_xpath(prefix)}/following-sibling::#{sibling.to_xpath(false)}"
|
23
|
-
end
|
24
|
-
|
25
|
-
def specificity
|
26
|
-
selector.specificity.zip(sibling.specificity).map { |x,y| x + y }
|
27
|
-
end
|
28
|
-
|
29
|
-
def ==(other)
|
30
|
-
super && selector == other.selector && sibling == other.sibling
|
31
|
-
end
|
32
|
-
|
33
|
-
def hash
|
34
|
-
[selector, sibling].hash
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require "css/sac/selectors"
|
2
|
-
|
3
|
-
module CSS
|
4
|
-
module SAC
|
5
|
-
module Selectors
|
6
|
-
class SimpleSelector < Selector
|
7
|
-
def initialize(selector_type=:SAC_ANY_NODE_SELECTOR)
|
8
|
-
super(selector_type)
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_css
|
12
|
-
'*'
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_xpath
|
16
|
-
"//*"
|
17
|
-
end
|
18
|
-
|
19
|
-
def specificity
|
20
|
-
[0, 0, 0, 0]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/lib/css/sac/selectors.rb
DELETED
data/lib/css/sac/token.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module CSS
|
2
|
-
module SAC
|
3
|
-
class Token
|
4
|
-
attr_reader :name, :value, :position
|
5
|
-
|
6
|
-
def initialize(name, value, position)
|
7
|
-
@name = name
|
8
|
-
@value = value
|
9
|
-
@position = position
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_racc_token
|
13
|
-
[name, value]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class DelimiterToken < Token
|
18
|
-
def initialize(value, position)
|
19
|
-
super(:delim, value, position)
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_racc_token
|
23
|
-
[value, value]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|