Class: Itonoko::CSS::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/itonoko/css/matcher.rb

Constant Summary collapse

SELECTOR_CACHE =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_node) ⇒ Matcher

Returns a new instance of Matcher.



20
21
22
# File 'lib/itonoko/css/matcher.rb', line 20

def initialize(context_node)
  @context = context_node
end

Class Method Details

.element_child_count(parent) ⇒ Object



192
193
194
195
# File 'lib/itonoko/css/matcher.rb', line 192

def self.element_child_count(parent)
  return 0 unless parent
  parent.children.count { |c| c.node_type == XML::Node::ELEMENT_NODE }
end

.element_index_of(node) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/itonoko/css/matcher.rb', line 197

def self.element_index_of(node)
  return nil unless node.parent
  idx = 0
  node.parent.children.each do |c|
    next unless c.node_type == XML::Node::ELEMENT_NODE
    idx += 1
    return idx if c.equal?(node)
  end
  nil
end

.first_element_child_of(parent) ⇒ Object

── sibling helpers (no Array allocation) ─────────────────



179
180
181
182
183
# File 'lib/itonoko/css/matcher.rb', line 179

def self.first_element_child_of(parent)
  return nil unless parent
  parent.children.each { |c| return c if c.node_type == XML::Node::ELEMENT_NODE }
  nil
end

.first_of_type_in(node) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/itonoko/css/matcher.rb', line 208

def self.first_of_type_in(node)
  return nil unless node.parent
  name = node.node_name
  node.parent.children.each do |c|
    return c if c.node_type == XML::Node::ELEMENT_NODE && c.node_name == name
  end
  nil
end

.last_element_child_of(parent) ⇒ Object



185
186
187
188
189
190
# File 'lib/itonoko/css/matcher.rb', line 185

def self.last_element_child_of(parent)
  return nil unless parent
  last = nil
  parent.children.each { |c| last = c if c.node_type == XML::Node::ELEMENT_NODE }
  last
end

.last_of_type_in(node) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/itonoko/css/matcher.rb', line 217

def self.last_of_type_in(node)
  return nil unless node.parent
  name = node.node_name
  last = nil
  node.parent.children.each { |c| last = c if c.node_type == XML::Node::ELEMENT_NODE && c.node_name == name }
  last
end

.match(context_node, selector_str) ⇒ Object



11
12
13
# File 'lib/itonoko/css/matcher.rb', line 11

def self.match(context_node, selector_str)
  new(context_node).match(selector_str)
end

.matches_attr?(node, attr_spec) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/itonoko/css/matcher.rb', line 113

def self.matches_attr?(node, attr_spec)
  name   = attr_spec[:name]
  op     = attr_spec[:op]
  value  = attr_spec[:value]
  actual = node[name]
  return !actual.nil? if op.nil?
  return false        if actual.nil?
  case op
  when "="  then actual == value
  when "~=" then actual.split.include?(value)
  when "|=" then actual == value || actual.start_with?("#{value}-")
  when "^=" then actual.start_with?(value)
  when "$=" then actual.end_with?(value)
  when "*=" then actual.include?(value)
  else actual == value
  end
end

.matches_group_at?(node, steps, idx) ⇒ Boolean

Index-based group match — no Array slicing, no array allocation for ancestor walk.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/itonoko/css/matcher.rb', line 42

def self.matches_group_at?(node, steps, idx)
  return false unless node.node_type == XML::Node::ELEMENT_NODE
  return false unless matches_simple?(node, steps[idx].simple)
  return true  if idx == 0

  combinator = steps[idx].combinator || " "
  prev_idx   = idx - 1

  case combinator
  when ">"
    par = node.parent
    par && par.node_type == XML::Node::ELEMENT_NODE &&
      matches_group_at?(par, steps, prev_idx)

  when "+"
    sib = node.previous_sibling
    sib = sib.previous_sibling while sib && sib.node_type != XML::Node::ELEMENT_NODE
    sib && matches_group_at?(sib, steps, prev_idx)

  when "~"
    sib = node.previous_sibling
    while sib
      return true if sib.node_type == XML::Node::ELEMENT_NODE &&
                     matches_group_at?(sib, steps, prev_idx)
      sib = sib.previous_sibling
    end
    false

  else  # " " descendant
    cur = node.parent
    while cur
      return false if cur.node_type == XML::Node::DOCUMENT_NODE
      return true  if cur.node_type == XML::Node::ELEMENT_NODE &&
                      matches_group_at?(cur, steps, prev_idx)
      cur = cur.parent
    end
    false
  end
end

.matches_pseudo?(node, pseudo) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/itonoko/css/matcher.rb', line 131

def self.matches_pseudo?(node, pseudo)
  name = pseudo[:name]
  arg  = pseudo[:arg]

  case name
  when "first-child"
    first_element_child_of(node.parent) == node
  when "last-child"
    last_element_child_of(node.parent) == node
  when "first-of-type"
    first_of_type_in(node) == node
  when "last-of-type"
    last_of_type_in(node) == node
  when "only-child"
    element_child_count(node.parent) == 1
  when "only-of-type"
    type_child_count(node.parent, node.node_name) == 1
  when "nth-child"
    i = element_index_of(node)
    i && nth_match?(i, parse_nth(arg))
  when "nth-last-child"
    i = element_index_of(node)
    i && nth_match?(element_child_count(node.parent) - i + 1, parse_nth(arg))
  when "nth-of-type"
    i = type_index_of(node)
    i && nth_match?(i, parse_nth(arg))
  when "nth-last-of-type"
    i = type_index_of(node)
    i && nth_match?(type_child_count(node.parent, node.node_name) - i + 1, parse_nth(arg))
  when "empty"
    node.children.none? do |c|
      c.node_type == XML::Node::ELEMENT_NODE ||
        (c.is_a?(XML::Text) && !c.content.empty?)
    end
  when "root"
    node.parent&.node_type == XML::Node::DOCUMENT_NODE
  when "not"
    return true unless arg && !arg.empty?
    !matches_selector?(node, arg)
  when "checked"  then node["checked"] || node["selected"]
  when "disabled" then node["disabled"]
  when "enabled"  then !node["disabled"]
  else false
  end
end

.matches_selector?(node, selector_str) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/itonoko/css/matcher.rb', line 15

def self.matches_selector?(node, selector_str)
  groups = SELECTOR_CACHE[selector_str] ||= Parser.new.parse(selector_str)
  groups.any? { |group| matches_group_at?(node, group, group.length - 1) }
end

.matches_simple?(node, simple) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/itonoko/css/matcher.rb', line 82

def self.matches_simple?(node, simple)
  return false unless node.node_type == XML::Node::ELEMENT_NODE
  return false unless matches_tag?(node, simple.tag)
  return false unless simple.ids.all? { |id| node["id"] == id }
  return false unless simple.classes.all? { |cls| node_has_class?(node, cls) }
  return false unless simple.attrs.all? { |attr| matches_attr?(node, attr) }
  return false unless simple.pseudos.all? { |pseudo| matches_pseudo?(node, pseudo) }
  true
end

.matches_tag?(node, tag) ⇒ Boolean

Compare tag without allocating downcased strings when not needed.

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/itonoko/css/matcher.rb', line 93

def self.matches_tag?(node, tag)
  return true if tag.nil? || tag == "*"
  nn = node.node_name
  nn == tag || nn.downcase == tag
end

.node_has_class?(node, cls) ⇒ Boolean

Avoid String#split — use String#index for O(1) space word-boundary check.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/itonoko/css/matcher.rb', line 100

def self.node_has_class?(node, cls)
  val = node["class"] or return false
  len = cls.length
  i   = 0
  while (idx = val.index(cls, i))
    before_ok = idx == 0          || val.getbyte(idx - 1) == 32
    after_ok  = idx + len == val.length || val.getbyte(idx + len) == 32
    return true if before_ok && after_ok
    i = idx + 1
  end
  false
end

.nth_match?(index, nth) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
# File 'lib/itonoko/css/matcher.rb', line 261

def self.nth_match?(index, nth)
  a, b = nth[:a], nth[:b]
  if a == 0
    index == b
  else
    n = (index - b).to_f / a
    n >= 0 && n == n.floor
  end
end

.parse_nth(arg) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/itonoko/css/matcher.rb', line 242

def self.parse_nth(arg)
  return { a: 0, b: 1 } unless arg
  arg = arg.strip.downcase
  case arg
  when "odd"  then { a: 2, b: 1 }
  when "even" then { a: 2, b: 0 }
  when /\A([+-]?\d+)\z/
    { a: 0, b: $1.to_i }
  when /\A([+-]?\d*)n(?:\s*([+-]\s*\d+))?\z/
    a_str = $1
    b_str = $2&.gsub(/\s/, "")
    a = a_str.empty? || a_str == "+" ? 1 : a_str == "-" ? -1 : a_str.to_i
    b = b_str ? b_str.to_i : 0
    { a: a, b: b }
  else
    { a: 0, b: 0 }
  end
end

.type_child_count(parent, name) ⇒ Object



237
238
239
240
# File 'lib/itonoko/css/matcher.rb', line 237

def self.type_child_count(parent, name)
  return 0 unless parent
  parent.children.count { |c| c.node_type == XML::Node::ELEMENT_NODE && c.node_name == name }
end

.type_index_of(node) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/itonoko/css/matcher.rb', line 225

def self.type_index_of(node)
  return nil unless node.parent
  name = node.node_name
  idx  = 0
  node.parent.children.each do |c|
    next unless c.node_type == XML::Node::ELEMENT_NODE && c.node_name == name
    idx += 1
    return idx if c.equal?(node)
  end
  nil
end

Instance Method Details

#match(selector_str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/itonoko/css/matcher.rb', line 24

def match(selector_str)
  groups = SELECTOR_CACHE[selector_str] ||= Parser.new.parse(selector_str)
  doc    = document_of(@context)
  seen   = {}
  result = []

  all_elements(@context, result)
  result.select! do |node|
    unless seen[node.object_id]
      seen[node.object_id] = true
      groups.any? { |group| self.class.matches_group_at?(node, group, group.length - 1) }
    end
  end

  XML::NodeSet.new(doc, result)
end