Module: Cybertrain::Test::Selector

Defined in:
cybertrain/test/select.rb

Overview

Rails’ selector subset (spec section 9.2), parsed for Selector.matching_nodes.

Class Method Summary collapse

Class Method Details

.matching_nodes(nodes, chains) ⇒ Array<HtmlNode>

The elements of nodes (HtmlReader.parse’s answer) that match any chain, in document order, each once.

Parameters:

  • nodes (Array<HtmlNode>)
  • chains (Array<Array<Compound>>) —

    from parse

Returns:



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'cybertrain/test/select.rb', line 314

def self.matching_nodes(nodes, chains)
  found = Array.new(0) { HtmlNode.new("", 0, -1, 0) }
  # One memo per chain, kept across the elements: whether element
  # `at` matches chain[0..k] depends on nothing else, so each
  # (k, at) pair is worked out once (0 not yet, 1 yes, 2 no) and a
  # descendant chain that fails near the root costs depth x length,
  # not depth to the power of its length.
  memos = Array.new(0) { Array.new(0) { 0 } }
  chains.each { |chain| memos << Array.new(chain.length * nodes.length) { 0 } }
  nodes.each do |node|
    hit = false
    c = 0
    while c < chains.length
      chain = chains[c]
      hit = true if !hit && chain_match?(nodes, chain, chain.length - 1, node.node_index, memos[c])
      c += 1
    end
    found << node if hit
  end
  found
end

.parse(text) ⇒ Array<Array<Compound>>

The selector text as a list of chains, each an Array of compounds whose sel_combinator says how it relates to the one before.

Parameters:

  • text (String)

Returns:

  • (Array<Array<Compound>>)

Raises:

  • (AssertionFailed) —

    assert_select: <feature> is not supported (selector: "...") for a form outside the subset, or assert_select: invalid selector "..."



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
176
177
178
179
# File 'cybertrain/test/select.rb', line 98

def self.parse(text)
  s = text.to_s
  chains = Array.new(0) { Array.new(0) { Compound.new } }
  chain = Array.new(0) { Compound.new }
  current = Compound.new
  pending = ""
  n = s.bytesize
  i = 0
  while i < n
    c = s.getbyte(i)
    if HtmlReader.html_space?(c) || c == 62 || c == 44 || c == 43 || c == 126
      # Whitespace and the combinators between compounds.
      combinator = " "
      while i < n
        c = s.getbyte(i)
        if HtmlReader.html_space?(c)
          i += 1
        elsif c == 62 || c == 44 || c == 43 || c == 126
          refuse_selector(s, "sibling combinators (+ and ~) are") if c == 43 || c == 126
          invalid_selector(s) unless combinator == " "
          combinator = c == 62 ? ">" : ","
          i += 1
        else
          break
        end
      end
      # A combinator with nothing after it.
      invalid_selector(s) if i >= n && combinator != " "
      if combinator == ","
        invalid_selector(s) if current.sel_empty?
        chain << current
        chains << chain
        chain = Array.new(0) { Compound.new }
        current = Compound.new
        pending = ""
      elsif current.sel_empty?
        # Leading whitespace, or `>` with nothing before it.
        invalid_selector(s) if combinator == ">" || (i < n && !chain.empty?)
      elsif i < n
        chain << current
        current = Compound.new
        pending = combinator
      end
      next
    end
    current.sel_combinator = pending if current.sel_empty?
    if c == 35 || c == 46 # # .
      j = ident_end(s, i + 1)
      invalid_selector(s) if j == i + 1
      word = s.byteslice(i + 1, j - i - 1)
      if c == 35
        current.sel_ids << word
      else
        current.sel_classes << word
      end
      i = j
    elsif c == 91 # [
      i = selector_attribute(s, i + 1, current)
    elsif c == 58 # :
      refuse_selector(s, "pseudo-classes are")
    elsif c == 124 # |
      refuse_selector(s, "namespaces are")
    elsif c == 92 # \
      refuse_selector(s, "CSS escapes (\\) are")
    elsif c == 63 # ?
      refuse_selector(s, "substitution values (?) are")
    elsif c == 42 # *
      invalid_selector(s) unless current.sel_empty?
      current.sel_tag = "*"
      i += 1
    else
      j = ident_end(s, i)
      invalid_selector(s) if j == i || !current.sel_empty?
      current.sel_tag = s.byteslice(i, j - i).downcase
      i = j
    end
  end
  invalid_selector(s) if current.sel_empty?
  chain << current
  chains << chain
  chains
end