fasthtmlparser 0.1.0

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.
@@ -0,0 +1,1061 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastHtmlParser
4
+ # @api private Sibling links and positions of the elements, built once per document by the first selector that
5
+ # needs them.
6
+ class CssTree
7
+ # previous / next element sibling (-1 for none); 1-based position among the element siblings (0: not an
8
+ # element) and from the end
9
+ attr_reader :previous, :next, :position, :from_end
10
+
11
+ def initialize(doc)
12
+ @doc = doc
13
+ count = doc.size
14
+ parents = doc.parents
15
+ previous = Array.new(count, -1)
16
+ nxt = Array.new(count, -1)
17
+ position = Array.new(count, 0)
18
+ from_end = Array.new(count, 0)
19
+ # per parent (slot = parent index + 1, slot 0: the top level): its last element child so far and their number
20
+ last_child = Array.new(count + 1, -1)
21
+ children = Array.new(count + 1, 0)
22
+ i = 0
23
+ while i < count
24
+ if CssQuery.element_at?(doc, i)
25
+ slot = parents[i] + 1
26
+ prev = last_child[slot]
27
+ previous[i] = prev
28
+ nxt[prev] = i if prev >= 0
29
+ last_child[slot] = i
30
+ position[i] = children[slot] += 1
31
+ end
32
+ i += 1
33
+ end
34
+ i = 0
35
+ while i < count
36
+ from_end[i] = children[parents[i] + 1] - position[i] + 1 if position[i] > 0
37
+ i += 1
38
+ end
39
+ @previous = previous
40
+ @next = nxt
41
+ @position = position
42
+ @from_end = from_end
43
+ @types = nil
44
+ end
45
+
46
+ def prev(n)
47
+ i = @previous[n.index]
48
+ i < 0 ? nil : @doc.at(i)
49
+ end
50
+
51
+ def next_of(n)
52
+ i = @next[n.index]
53
+ i < 0 ? nil : @doc.at(i)
54
+ end
55
+
56
+ # [position, from_end] among the siblings of the same type
57
+ def type_positions
58
+ return @types if @types
59
+
60
+ d = @doc
61
+ count = d.size
62
+ pos = Array.new(count, 0)
63
+ fin = Array.new(count, 0)
64
+ seen = Hash.new(0)
65
+ keys = Array.new(count)
66
+ i = 0
67
+ while i < count
68
+ unless @position[i] == 0
69
+ key = d.keys[i]
70
+ k = [d.parents[i], key, key < 0 ? d.customs[i] : nil]
71
+ keys[i] = k
72
+ pos[i] = seen[k] += 1
73
+ end
74
+ i += 1
75
+ end
76
+ i = 0
77
+ while i < count
78
+ fin[i] = seen[keys[i]] - pos[i] + 1 if pos[i] > 0
79
+ i += 1
80
+ end
81
+ @types = [pos, fin]
82
+ end
83
+ end
84
+
85
+ # @api private Parses a selector list (Selectors Level 3 and the common Level 4 pseudo-classes). Identifiers and
86
+ # strings follow the CSS syntax, escapes included (#a\:b, .\31 0, [title="a\"b"]).
87
+ class CssCompiler
88
+ CSS_SPACE = " \t\n\f\r"
89
+
90
+ Complex = Struct.new(:lead, :c, :k)
91
+ Compound = Struct.new(:key, :name, :s)
92
+
93
+ attr_reader :p
94
+
95
+ def initialize(s)
96
+ @s = s
97
+ @n = s.bytesize
98
+ @p = 0
99
+ end
100
+
101
+ def fail!(msg)
102
+ raise CssSelectorError, "invalid CSS selector #{Chars.quote(@s)} at position #{@p}: #{msg}"
103
+ end
104
+
105
+ def peek(offset)
106
+ i = @p + offset
107
+ i < @n ? @s.getbyte(i) : nil
108
+ end
109
+
110
+ def char_at(i)
111
+ Chars.u(@s.byteslice(i, 1))
112
+ end
113
+
114
+ def css_space?(c)
115
+ c == 32 || c == 9 || c == 10 || c == 12 || c == 13
116
+ end
117
+
118
+ def skip_space
119
+ k = @p
120
+ @p += 1 while @p < @n && css_space?(@s.getbyte(@p))
121
+ @p > k
122
+ end
123
+
124
+ def expect(ch)
125
+ if peek(0) != ch.ord
126
+ fail!("'#{ch}' expected") if @p >= @n
127
+ fail!("'#{ch}' expected instead of '#{char_at(@p)}'")
128
+ end
129
+ @p += 1
130
+ end
131
+
132
+ # selector-list: complex (',' complex)*
133
+ def parse_list(relative, top_level)
134
+ list = []
135
+ loop do
136
+ list << parse_complex(relative, top_level)
137
+ skip_space
138
+ return list.freeze if peek(0) != 44
139
+
140
+ @p += 1
141
+ end
142
+ end
143
+
144
+ private
145
+
146
+ # complex: [combinator] compound (combinator compound)*
147
+ def parse_complex(relative, top_level)
148
+ skip_space
149
+ cx = Complex.new(CssQuery::COMB_NONE, [], [])
150
+ if relative || top_level
151
+ comb = read_combinator
152
+ if comb != CssQuery::COMB_NONE
153
+ if top_level && comb != CssQuery::COMB_CHILD
154
+ fail!("a selector can start only with '>' (the children of the tag it is called on)")
155
+ end
156
+ cx.lead = comb
157
+ elsif relative
158
+ cx.lead = CssQuery::COMB_DESCENDANT
159
+ end
160
+ end
161
+ cx.c << parse_compound
162
+ loop do
163
+ ws = skip_space
164
+ ch = peek(0)
165
+ return cx.freeze if ch.nil? || ch == 44 || ch == 41 # end, ',' or ')'
166
+
167
+ comb = read_combinator
168
+ if comb == CssQuery::COMB_NONE
169
+ fail!("unexpected '#{char_at(@p)}'") unless ws
170
+ comb = CssQuery::COMB_DESCENDANT
171
+ end
172
+ cx.k << comb
173
+ cx.c << parse_compound
174
+ end
175
+ end
176
+
177
+ def read_combinator
178
+ comb = case peek(0)
179
+ when 62 then CssQuery::COMB_CHILD
180
+ when 43 then CssQuery::COMB_ADJACENT
181
+ when 126 then CssQuery::COMB_SIBLING
182
+ else CssQuery::COMB_NONE
183
+ end
184
+ if comb != CssQuery::COMB_NONE
185
+ @p += 1
186
+ skip_space
187
+ end
188
+ comb
189
+ end
190
+
191
+ # compound: [type | '*'] (#id | .class | [attribute] | :pseudo)*
192
+ def parse_compound
193
+ cp = Compound.new(-1, Chars::EMPTY, [])
194
+ has_type = false
195
+ if peek(0) == 42
196
+ @p += 1
197
+ has_type = true
198
+ elsif ident_start?(@p)
199
+ name = Chars.lower(read_name)
200
+ has_type = true
201
+ t = Parser.lookup_tag(name)
202
+ if t >= 0
203
+ cp.key = t
204
+ else
205
+ cp.name = name.freeze
206
+ end
207
+ end
208
+ fail!("namespaces are not supported; escape the colon of a prefixed name: dc\\:creator") if peek(0) == 124
209
+
210
+ loop do
211
+ ch = peek(0)
212
+ if ch == 35
213
+ @p += 1
214
+ fail!("an id expected after '#'") unless name_char?(@p)
215
+ cp.s << CssQuery.id_selector(read_name)
216
+ elsif ch == 46
217
+ @p += 1
218
+ fail!("a class name expected after '.'") unless ident_start?(@p)
219
+ cp.s << CssQuery.class_selector(read_name)
220
+ elsif ch == 91
221
+ cp.s << parse_attribute
222
+ elsif ch == 58
223
+ parse_pseudo(cp)
224
+ else
225
+ break
226
+ end
227
+ end
228
+
229
+ if !has_type && cp.s.empty?
230
+ fail!("a selector expected") if @p >= @n
231
+ fail!("unexpected '#{char_at(@p)}'")
232
+ end
233
+ cp.s.freeze
234
+ cp.freeze
235
+ end
236
+
237
+ # '[' name [op value [i|s]] ']'
238
+ def parse_attribute
239
+ @p += 1 # '['
240
+ skip_space
241
+ fail!("an attribute name expected") unless ident_start?(@p)
242
+ name = read_name
243
+ skip_space
244
+ if peek(0) == 93
245
+ @p += 1
246
+ return CssQuery.attribute_selector(name, "", Chars::EMPTY, nil)
247
+ end
248
+
249
+ ch = peek(0)
250
+ if ch == 61
251
+ op = "="
252
+ @p += 1
253
+ elsif [126, 124, 94, 36, 42].include?(ch)
254
+ op = ch.chr
255
+ fail!("'=' expected after '#{op}'") if peek(1) != 61
256
+ @p += 2
257
+ else
258
+ fail!("'=', '~=', '|=', '^=', '$=', '*=' or ']' expected")
259
+ end
260
+
261
+ skip_space
262
+ if peek(0) == 34 || peek(0) == 39
263
+ value = read_string
264
+ elsif ident_start?(@p)
265
+ value = read_name
266
+ else
267
+ fail!("an attribute value expected (quote it if it is not a plain word)")
268
+ end
269
+
270
+ skip_space
271
+ flag = nil
272
+ if ident_start?(@p)
273
+ f = read_name
274
+ if Chars.equal_fold(f, "i")
275
+ flag = true
276
+ elsif Chars.equal_fold(f, "s")
277
+ flag = false
278
+ else
279
+ fail!("unknown attribute flag '#{Chars.u(f.dup)}'")
280
+ end
281
+ skip_space
282
+ end
283
+ expect("]")
284
+ CssQuery.attribute_selector(name, op, value, flag)
285
+ end
286
+
287
+ def parse_pseudo(cp)
288
+ @p += 1 # ':'
289
+ fail!("pseudo-elements (::before, ::text) select no tags") if peek(0) == 58
290
+ fail!("a pseudo-class name expected after ':'") unless ident_start?(@p)
291
+ start = @p
292
+ name = Chars.lower(read_name)
293
+
294
+ if peek(0) == 40
295
+ @p += 1
296
+ skip_space
297
+ case name
298
+ when "not"
299
+ cp.s << CssQuery.logical(:not, parse_list(false, false))
300
+ when "is", "where", "matches", "any"
301
+ cp.s << CssQuery.logical(:is, parse_list(false, false))
302
+ when "has"
303
+ cp.s << CssQuery.logical(:has, parse_list(true, false))
304
+ when "nth-child", "nth-last-child", "nth-of-type", "nth-last-of-type"
305
+ a, b = parse_nth
306
+ of_type = name.end_with?("of-type")
307
+ of = nil
308
+ skip_space
309
+ if !of_type && word?("of")
310
+ @p += 2
311
+ of = parse_list(false, false)
312
+ end
313
+ cp.s << CssQuery.nth(a, b, name.include?("last"), of_type, of)
314
+ when "contains"
315
+ if peek(0) == 34 || peek(0) == 39
316
+ text = read_string
317
+ elsif ident_start?(@p)
318
+ text = read_name
319
+ else
320
+ fail!("a string expected in :contains()")
321
+ end
322
+ cp.s << CssQuery.contains(text)
323
+ else
324
+ @p = start
325
+ fail!("unknown pseudo-class ':#{Chars.u(name.dup)}()'")
326
+ end
327
+ skip_space
328
+ expect(")")
329
+ return
330
+ end
331
+
332
+ case name
333
+ when "first-child"
334
+ cp.s << CssQuery.nth(0, 1, false, false, nil)
335
+ when "last-child"
336
+ cp.s << CssQuery.nth(0, 1, true, false, nil)
337
+ when "only-child"
338
+ cp.s << CssQuery.nth(0, 1, false, false, nil)
339
+ cp.s << CssQuery.nth(0, 1, true, false, nil)
340
+ when "first-of-type"
341
+ cp.s << CssQuery.nth(0, 1, false, true, nil)
342
+ when "last-of-type"
343
+ cp.s << CssQuery.nth(0, 1, true, true, nil)
344
+ when "only-of-type"
345
+ cp.s << CssQuery.nth(0, 1, false, true, nil)
346
+ cp.s << CssQuery.nth(0, 1, true, true, nil)
347
+ else
348
+ pseudo = CssQuery.pseudo(name)
349
+ if pseudo.nil?
350
+ @p = start
351
+ fail!("unknown pseudo-class ':#{Chars.u(name.dup)}'")
352
+ end
353
+ cp.s << pseudo
354
+ end
355
+ end
356
+
357
+ # An+B: odd, even, 3, n, -n+3, 2n+1, +2n - 1; [a, b]
358
+ def parse_nth
359
+ if word?("odd")
360
+ @p += 3
361
+ return [2, 1]
362
+ end
363
+ if word?("even")
364
+ @p += 4
365
+ return [2, 0]
366
+ end
367
+ start = @p
368
+ t = +""
369
+ while @p < @n
370
+ ch = @s.getbyte(@p)
371
+ break unless ch == 43 || ch == 45 || ch == 110 || ch == 78 || Chars.digit_byte?(ch) || css_space?(ch)
372
+
373
+ t << (ch == 78 ? "n" : ch.chr) unless css_space?(ch)
374
+ @p += 1
375
+ end
376
+ n = t.index("n")
377
+ if n.nil?
378
+ v = parse_int32(t)
379
+ return [0, v] if v
380
+ elsif t.index("n", n + 1).nil?
381
+ a_text = t[0, n]
382
+ b_text = t[(n + 1)..]
383
+ a = if a_text.empty? || a_text == "+"
384
+ 1
385
+ else
386
+ a_text == "-" ? -1 : parse_int32(a_text)
387
+ end
388
+ b = 0
389
+ unless b_text.empty?
390
+ ok = (b_text[0] == "+" || b_text[0] == "-") && b_text.size > 1 && Chars.digit_byte?(b_text.getbyte(1))
391
+ b = ok ? parse_int32(b_text) : nil
392
+ end
393
+ return [a, b] if a && b
394
+ end
395
+ @p = start
396
+ fail!("An+B expected (odd, even, 3, 2n+1, -n+3)")
397
+ end
398
+
399
+ def parse_int32(s)
400
+ i = !s.empty? && (s[0] == "+" || s[0] == "-") ? 1 : 0
401
+ return nil if i == s.size || !s[i..].match?(/\A[0-9]+\z/)
402
+
403
+ digits = s[i..].sub(/\A0+/, "")
404
+ return nil if digits.size > 10
405
+
406
+ v = digits.to_i
407
+ v = -v if s[0] == "-"
408
+ v >= -2_147_483_648 && v <= 2_147_483_647 ? v : nil
409
+ end
410
+
411
+ # The identifier at the position is word (case-insensitive) and ends there.
412
+ def word?(word)
413
+ len = word.bytesize
414
+ @p + len <= @n && @s.byteslice(@p, len).casecmp?(word) && !name_char?(@p + len)
415
+ end
416
+
417
+ def escape?(p)
418
+ return false if p >= @n || @s.getbyte(p) != 92
419
+ return true if p + 1 >= @n
420
+
421
+ c = @s.getbyte(p + 1)
422
+ c != 10 && c != 13 && c != 12
423
+ end
424
+
425
+ def name_start_byte?(c)
426
+ Chars.alpha_byte?(c) || c == 95 || c >= 0x80
427
+ end
428
+
429
+ def name_char?(p)
430
+ return false if p >= @n
431
+
432
+ c = @s.getbyte(p)
433
+ name_start_byte?(c) || Chars.digit_byte?(c) || c == 45 || escape?(p)
434
+ end
435
+
436
+ def ident_start?(p)
437
+ return false if p >= @n
438
+
439
+ if @s.getbyte(p) == 45
440
+ p += 1
441
+ return false if p >= @n
442
+
443
+ c = @s.getbyte(p)
444
+ return name_start_byte?(c) || c == 45 || escape?(p)
445
+ end
446
+ name_start_byte?(@s.getbyte(p)) || escape?(p)
447
+ end
448
+
449
+ def read_name
450
+ b = String.new(encoding: Encoding::BINARY)
451
+ while name_char?(@p)
452
+ if @s.getbyte(@p) == 92
453
+ b << read_escape
454
+ else
455
+ b << @s.getbyte(@p)
456
+ @p += 1
457
+ end
458
+ end
459
+ b
460
+ end
461
+
462
+ def read_string
463
+ quote = @s.getbyte(@p)
464
+ start = @p
465
+ @p += 1
466
+ b = String.new(encoding: Encoding::BINARY)
467
+ loop do
468
+ if @p >= @n
469
+ @p = start
470
+ fail!("unterminated string")
471
+ end
472
+ ch = @s.getbyte(@p)
473
+ if ch == quote
474
+ @p += 1
475
+ return b
476
+ end
477
+ if ch == 92
478
+ nxt = peek(1)
479
+ if @p + 1 < @n && (nxt == 10 || nxt == 12)
480
+ @p += 2 # an escaped line break continues the string
481
+ elsif @p + 2 < @n && nxt == 13
482
+ @p += peek(2) == 10 ? 3 : 2
483
+ else
484
+ b << read_escape
485
+ end
486
+ else
487
+ b << ch
488
+ @p += 1
489
+ end
490
+ end
491
+ end
492
+
493
+ # '\' then up to 6 hex digits and one optional whitespace, or any other char as itself.
494
+ def read_escape
495
+ @p += 1 # '\'
496
+ return Chars::REPLACEMENT if @p >= @n
497
+
498
+ unless Chars.hex_byte?(@s.getbyte(@p))
499
+ c = @s.getbyte(@p)
500
+ len = if c >= 0xf0 then 4
501
+ elsif c >= 0xe0 then 3
502
+ elsif c >= 0xc0 then 2
503
+ else 1
504
+ end
505
+ ch = @s.byteslice(@p, len)
506
+ @p += ch.bytesize
507
+ return ch
508
+ end
509
+ digits = 0
510
+ digits += 1 while digits < 6 && @p + digits < @n && Chars.hex_byte?(@s.getbyte(@p + digits))
511
+ code = @s.byteslice(@p, digits).to_i(16)
512
+ @p += digits
513
+ if @p < @n && css_space?(@s.getbyte(@p))
514
+ @p += @s.getbyte(@p) == 13 && peek(1) == 10 ? 2 : 1
515
+ end
516
+ return Chars::REPLACEMENT if code == 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)
517
+
518
+ Chars.utf8(code)
519
+ end
520
+ end
521
+
522
+ # A compiled CSS selector (or a comma-separated list of them), matched right to left over the flat tag list as
523
+ # browsers do. Compile once and reuse it on any number of documents; the string methods (doc.css(...),
524
+ # node.css_first(...), ...) compile and cache selectors automatically.
525
+ #
526
+ # Supported: type, *, #id, .class, attribute selectors ([a], =, ~=, |=, ^=, $=, *=, with the i / s flags), the
527
+ # combinators (descendant, >, +, ~), selector lists and the pseudo-classes :first-child, :last-child, :only-child,
528
+ # :first-of-type, :last-of-type, :only-of-type, :nth-child(An+B [of S]), :nth-last-child(), :nth-of-type(),
529
+ # :nth-last-of-type(), :not(), :is(), :where(), :has(), :empty, :root, :scope, :link, :any-link, :checked,
530
+ # :disabled, :enabled, :required, :optional, and :contains("text"). Pseudo-elements and namespaces are not
531
+ # supported.
532
+ #
533
+ # HTML rules: tag and attribute names are case-insensitive; ids, classes and attribute values are case-sensitive,
534
+ # except the values the HTML standard lists (type, rel, lang, method, ...). Only elements are selected, never
535
+ # comments.
536
+ class CssQuery
537
+ # combinators
538
+ COMB_NONE = 0
539
+ COMB_DESCENDANT = 1 # "a b"
540
+ COMB_CHILD = 2 # "a > b"
541
+ COMB_ADJACENT = 3 # "a + b"
542
+ COMB_SIBLING = 4 # "a ~ b"
543
+
544
+ # simple selectors: [kind, ...]
545
+ S_ID = 0 # [S_ID, id]
546
+ S_CLASS = 1 # [S_CLASS, name]
547
+ S_ATTR = 2 # [S_ATTR, name, op, value, fold]
548
+ S_NTH = 3 # [S_NTH, a, b, from_end, of_type, of (a list or nil)]
549
+ S_NOT = 4 # [S_NOT, list]
550
+ S_IS = 5 # [S_IS, list]
551
+ S_HAS = 6 # [S_HAS, list]
552
+ S_CONTAINS = 7 # [S_CONTAINS, text]
553
+ S_PSEUDO = 8 # [S_PSEUDO, kind]
554
+
555
+ OP_EXISTS = 0 # [a]
556
+ OP_EQUALS = 1 # [a=v]
557
+ OP_INCLUDES = 2 # [a~=v]
558
+ OP_DASH = 3 # [a|=v]
559
+ OP_PREFIX = 4 # [a^=v]
560
+ OP_SUFFIX = 5 # [a$=v]
561
+ OP_SUBSTRING = 6 # [a*=v]
562
+
563
+ PSEUDO_ROOT = 0
564
+ PSEUDO_SCOPE = 1
565
+ PSEUDO_EMPTY = 2
566
+ PSEUDO_LINK = 3
567
+ PSEUDO_CHECKED = 4
568
+ PSEUDO_DISABLED = 5
569
+ PSEUDO_ENABLED = 6
570
+ PSEUDO_REQUIRED = 7
571
+ PSEUDO_OPTIONAL = 8
572
+
573
+ COMMENT = TagNames::KEYS["!--"]
574
+ DOCTYPE = TagNames::KEYS["!doctype"]
575
+ XML_DECLARATION = TagNames::KEYS["?xml"]
576
+
577
+ CSS_SPACE_RE = /[ \t\n\f\r]/n
578
+
579
+ # HTML: the values of these attributes are compared ignoring ASCII case unless the selector has the 's' flag.
580
+ CASE_INSENSITIVE_VALUES = %w[accept accept-charset align alink axis bgcolor charset checked clear codetype color
581
+ compact declare defer dir direction disabled enctype face frame hreflang http-equiv
582
+ lang language link media method multiple nohref noresize noshade nowrap readonly rel
583
+ rev rules scope scrolling selected shape target text type valign valuetype
584
+ vlink].to_h { |n| [n, true] }.freeze
585
+
586
+ FORM_CONTROLS = %w[button input select textarea optgroup option fieldset].to_h { |n| [n, true] }.freeze
587
+ INPUTS = %w[input select textarea].to_h { |n| [n, true] }.freeze
588
+
589
+ @cache = {}
590
+ @cache_lock = Mutex.new
591
+
592
+ # A selector list is a list of complex selectors (lead combinator, compounds c, combinators k: k[i] stands
593
+ # between c[i] and c[i+1]). A compound is (Tag key or -1, custom name ("": any element when key is -1), simple
594
+ # selectors).
595
+ def initialize(selector, list)
596
+ @selector = selector
597
+ @list = list
598
+ # the name of the rightmost compound of each selector, checked on the arrays before a tag object is made
599
+ names = []
600
+ list.each do |item|
601
+ last = item.c[-1]
602
+ if last.key < 0 && last.name.empty?
603
+ names = nil # any element
604
+ break
605
+ end
606
+ names << last
607
+ end
608
+ @names = names
609
+ freeze
610
+ end
611
+
612
+ # Compiles a CSS selector; raises CssSelectorError on an invalid one.
613
+ def self.compile(selector)
614
+ s = Chars.b(selector.to_s)
615
+ c = CssCompiler.new(s)
616
+ list = c.parse_list(false, true)
617
+ c.fail!("unexpected '#{Chars.u(s.byteslice(c.p, 1))}'") if c.p < s.bytesize
618
+ new(selector.to_s.dup.freeze, list)
619
+ end
620
+
621
+ # @api private compiles and caches
622
+ def self.cached(selector)
623
+ q = @cache[selector]
624
+ return q if q
625
+
626
+ q = compile(selector)
627
+ @cache_lock.synchronize do
628
+ @cache.clear if @cache.size >= 1024 # dynamically built selectors must not grow the cache forever
629
+ @cache[selector.frozen? ? selector : selector.dup.freeze] = q
630
+ end
631
+ q
632
+ end
633
+
634
+ # The source of the selector.
635
+ attr_reader :selector
636
+
637
+ def to_s
638
+ @selector
639
+ end
640
+
641
+ def inspect
642
+ "#<#{self.class.name} #{@selector}>"
643
+ end
644
+
645
+ # The matching tags in the document, in document order.
646
+ def all(doc)
647
+ scan(doc, nil, 0, Float::INFINITY, false)
648
+ end
649
+
650
+ # The first matching tag in the document, or nil.
651
+ def first(doc)
652
+ scan(doc, nil, 0, Float::INFINITY, true).first
653
+ end
654
+
655
+ # The matching tags inside the tag (as querySelectorAll).
656
+ def all_in(node)
657
+ scan(node.doc, node, node.index + 1, node.end, false)
658
+ end
659
+
660
+ # The first matching tag inside the tag, or nil.
661
+ def first_in(node)
662
+ scan(node.doc, node, node.index + 1, node.end, true).first
663
+ end
664
+
665
+ # Whether the tag matches (as element.matches(): :scope is the tag itself); false for comments and nil.
666
+ def matches?(node)
667
+ !node.nil? && CssQuery.element?(node) && CssQuery.matches_list?(@list, node, node, node)
668
+ end
669
+
670
+ # @api private Not a comment, the doctype or the XML declaration.
671
+ def self.element?(n)
672
+ key = n.key
673
+ return key != COMMENT && key != DOCTYPE && key != XML_DECLARATION if key >= 0
674
+
675
+ custom = n.custom
676
+ !custom.empty? && custom.getbyte(0) != 33 && custom.getbyte(0) != 63
677
+ end
678
+
679
+ # @api private element? for the tag at index i
680
+ def self.element_at?(d, i)
681
+ key = d.keys[i]
682
+ return key != COMMENT && key != DOCTYPE && key != XML_DECLARATION if key >= 0
683
+
684
+ custom = d.customs[i]
685
+ !custom.empty? && custom.getbyte(0) != 33 && custom.getbyte(0) != 63
686
+ end
687
+
688
+ private
689
+
690
+ def scan(d, scope, from, end_, first_only)
691
+ out = []
692
+ ends = d.ends
693
+ keys = d.keys
694
+ customs = d.customs
695
+ list = @list
696
+ names = @names
697
+ # one selector with a known tag name: one int compare per tag
698
+ single = names && names.size == 1 && names[0].key >= 0 ? names[0].key : nil
699
+ count = d.size
700
+ i = from
701
+ while i < count
702
+ break if ends[i] > end_
703
+
704
+ if single
705
+ if keys[i] != single
706
+ i += 1
707
+ next
708
+ end
709
+ elsif names
710
+ k = keys[i]
711
+ candidate = false
712
+ j = 0
713
+ while j < names.size
714
+ cp = names[j]
715
+ if cp.key >= 0 ? k == cp.key : k < 0 && customs[i] == cp.name
716
+ candidate = true
717
+ break
718
+ end
719
+ j += 1
720
+ end
721
+ unless candidate
722
+ i += 1
723
+ next
724
+ end
725
+ elsif !CssQuery.element_at?(d, i)
726
+ i += 1
727
+ next
728
+ end
729
+ n = d.at(i)
730
+ if CssQuery.matches_list?(list, n, scope, scope)
731
+ out << n
732
+ break if first_only
733
+ end
734
+ i += 1
735
+ end
736
+ out
737
+ end
738
+
739
+ class << self
740
+ # ----- matching -----
741
+
742
+ # scope: the tag the query was called on (:scope, nil for the document); anchor: the tag a relative selector
743
+ # starts from (:has(> a), or node.css("> li"); nil: the document).
744
+ def matches_list?(list, n, scope, anchor)
745
+ i = 0
746
+ while i < list.size
747
+ item = list[i]
748
+ return true if matches_complex?(item, item.c.size - 1, n, scope, anchor)
749
+
750
+ i += 1
751
+ end
752
+ false
753
+ end
754
+
755
+ # n matches compound i and the part to the left of it matches the tags the combinators lead to.
756
+ def matches_complex?(cx, i, n, scope, anchor)
757
+ return false unless matches_compound?(cx.c[i], n, scope, anchor)
758
+ return cx.lead == COMB_NONE || related?(cx.lead, anchor, n) if i == 0
759
+
760
+ case cx.k[i - 1]
761
+ when 2 # COMB_CHILD
762
+ p = n.parent
763
+ !p.nil? && matches_complex?(cx, i - 1, p, scope, anchor)
764
+ when 1 # COMB_DESCENDANT
765
+ p = n.parent
766
+ until p.nil?
767
+ return true if matches_complex?(cx, i - 1, p, scope, anchor)
768
+
769
+ p = p.parent
770
+ end
771
+ false
772
+ when 3 # COMB_ADJACENT
773
+ p = n.doc.css_tree.prev(n)
774
+ !p.nil? && matches_complex?(cx, i - 1, p, scope, anchor)
775
+ when 4 # COMB_SIBLING
776
+ t = n.doc.css_tree
777
+ p = t.prev(n)
778
+ until p.nil?
779
+ return true if matches_complex?(cx, i - 1, p, scope, anchor)
780
+
781
+ p = t.prev(p)
782
+ end
783
+ false
784
+ else
785
+ false
786
+ end
787
+ end
788
+
789
+ def matches_compound?(cp, n, scope, anchor)
790
+ if cp.key >= 0
791
+ return false if n.key != cp.key
792
+ elsif !cp.name.empty?
793
+ return false if n.key >= 0 || n.custom != cp.name
794
+ elsif !element?(n)
795
+ return false
796
+ end
797
+ simple = cp.s
798
+ i = 0
799
+ while i < simple.size
800
+ return false unless matches_simple?(simple[i], n, scope, anchor)
801
+
802
+ i += 1
803
+ end
804
+ true
805
+ end
806
+
807
+ # n stands in the combinator relation to anchor (nil: the document).
808
+ def related?(c, anchor, n)
809
+ case c
810
+ when 2 # COMB_CHILD
811
+ anchor.nil? ? n.parent_index < 0 : n.parent_index == anchor.index
812
+ when 1 # COMB_DESCENDANT
813
+ return true if anchor.nil?
814
+
815
+ p = n.parent
816
+ until p.nil?
817
+ return true if p.equal?(anchor)
818
+
819
+ p = p.parent
820
+ end
821
+ false
822
+ when 3 # COMB_ADJACENT
823
+ !anchor.nil? && n.doc.css_tree.prev(n).equal?(anchor)
824
+ when 4 # COMB_SIBLING
825
+ !anchor.nil? && anchor.index < n.index && anchor.parent_index == n.parent_index && element?(anchor)
826
+ else
827
+ false
828
+ end
829
+ end
830
+
831
+ # :has(): whether a tag in the anchor's scope matches the relative selector.
832
+ def has_match?(relative, anchor, scope)
833
+ d = anchor.doc
834
+ ends = d.ends
835
+ count = d.size
836
+ relative.each do |item|
837
+ last = item.c.size - 1
838
+ i = anchor.index + 1
839
+ if item.lead == COMB_DESCENDANT || item.lead == COMB_CHILD
840
+ # the tags inside the anchor
841
+ while i < count && ends[i] <= anchor.end
842
+ return true if element_at?(d, i) && matches_complex?(item, last, d.at(i), scope, anchor)
843
+
844
+ i += 1
845
+ end
846
+ else
847
+ # the following siblings and the tags inside them, up to the end of the parent
848
+ i += 1 while i < count && ends[i] <= anchor.end
849
+ end_ = anchor.parent_index >= 0 ? ends[anchor.parent_index] : Float::INFINITY
850
+ while i < count && ends[i] <= end_
851
+ return true if element_at?(d, i) && matches_complex?(item, last, d.at(i), scope, anchor)
852
+
853
+ i += 1
854
+ end
855
+ end
856
+ end
857
+ false
858
+ end
859
+
860
+ # Whether a whitespace-separated list contains word.
861
+ def contains_word?(list, word, fold)
862
+ return false if word.empty? || list.bytesize < word.bytesize
863
+ return list == word if !fold && !CSS_SPACE_RE.match?(list)
864
+
865
+ list.split(CSS_SPACE_RE).any? { |w| !w.empty? && equal?(w, word, fold) }
866
+ end
867
+
868
+ def equal?(a, b, fold)
869
+ fold ? Chars.equal_fold(a, b) : a == b
870
+ end
871
+
872
+ def decoded(raw)
873
+ Decode.needs_decoding?(raw) ? Decode.decode(raw, true) : raw
874
+ end
875
+
876
+ def matches_simple?(s, n, scope, anchor)
877
+ case s[0]
878
+ when 0 # S_ID
879
+ Attrs.attr_equals?(Attrs.attr_raw(n, Attrs::ID), s[1])
880
+ when 1 # S_CLASS
881
+ raw = Attrs.attr_raw(n, Attrs::CLASS)
882
+ !raw.empty? && contains_word?(decoded(raw), s[1], false)
883
+ when 2 # S_ATTR
884
+ matches_attribute?(n, s[1], s[2], s[3], s[4])
885
+ when 3 # S_NTH
886
+ matches_nth?(n, s[1], s[2], s[3], s[4], s[5], scope, anchor)
887
+ when 4 # S_NOT
888
+ !matches_list?(s[1], n, scope, anchor)
889
+ when 5 # S_IS
890
+ matches_list?(s[1], n, scope, anchor)
891
+ when 6 # S_HAS
892
+ has_match?(s[1], n, scope)
893
+ when 7 # S_CONTAINS
894
+ n.text_bytes.include?(s[1])
895
+ else
896
+ matches_pseudo?(s[1], n, scope)
897
+ end
898
+ end
899
+
900
+ def matches_attribute?(n, name, op, v, fold)
901
+ raw = Attrs.attr_raw(n, name)
902
+ if raw.empty?
903
+ # missing or empty: only these can match an empty value
904
+ return Attrs.has_attribute?(n, name) if op == OP_EXISTS
905
+ return v.empty? && Attrs.has_attribute?(n, name) if op == OP_EQUALS || op == OP_DASH
906
+
907
+ return false
908
+ end
909
+ return true if op == OP_EXISTS
910
+
911
+ actual = decoded(raw)
912
+ vl = v.bytesize
913
+ case op
914
+ when 1 # OP_EQUALS
915
+ equal?(actual, v, fold)
916
+ when 2 # OP_INCLUDES
917
+ !CSS_SPACE_RE.match?(v) && contains_word?(actual, v, fold)
918
+ when 3 # OP_DASH
919
+ equal?(actual, v, fold) ||
920
+ (actual.bytesize > vl && actual.getbyte(vl) == 45 && equal?(actual.byteslice(0, vl), v, fold))
921
+ when 4 # OP_PREFIX
922
+ !v.empty? && actual.bytesize >= vl && equal?(actual.byteslice(0, vl), v, fold)
923
+ when 5 # OP_SUFFIX
924
+ !v.empty? && actual.bytesize >= vl && equal?(actual.byteslice(actual.bytesize - vl, vl), v, fold)
925
+ when 6 # OP_SUBSTRING
926
+ return false if v.empty?
927
+ return actual.include?(v) unless fold
928
+ return actual.downcase(:ascii).include?(v.downcase(:ascii)) if v.ascii_only?
929
+
930
+ Chars.lower(actual).include?(Chars.lower(v))
931
+ else
932
+ false
933
+ end
934
+ end
935
+
936
+ # :nth-child(An+B [of S]) and its relatives; :first-child is :nth-child(1), and so on.
937
+ def matches_nth?(n, a, b, from_end, of_type, of, scope, anchor)
938
+ t = n.doc.css_tree
939
+ if of
940
+ return false unless matches_list?(of, n, scope, anchor)
941
+
942
+ position = 1
943
+ sib = from_end ? t.next_of(n) : t.prev(n)
944
+ until sib.nil?
945
+ position += 1 if matches_list?(of, sib, scope, anchor)
946
+ sib = from_end ? t.next_of(sib) : t.prev(sib)
947
+ end
948
+ elsif of_type
949
+ tp = t.type_positions
950
+ position = from_end ? tp[1][n.index] : tp[0][n.index]
951
+ else
952
+ position = from_end ? t.from_end[n.index] : t.position[n.index]
953
+ end
954
+ return false if position <= 0 # not an element
955
+
956
+ # position = a*n + b for some n >= 0
957
+ return position == b if a == 0
958
+
959
+ diff = position - b
960
+ (diff % a).zero? && (diff / a) >= 0
961
+ end
962
+
963
+ # the first top-level element: <html> on a page
964
+ def root?(n)
965
+ n.parent_index < 0 && n.doc.css_tree.prev(n).nil?
966
+ end
967
+
968
+ # No child elements and no text; comments are allowed.
969
+ def empty_element?(n)
970
+ return true if n.inner_start <= 0 || n.inner_end <= 0 # a void or unclosed tag has no content
971
+
972
+ position = n.inner_start
973
+ d = n.doc
974
+ ends = d.ends
975
+ starts = d.starts
976
+ i = n.index + 1
977
+ count = d.size
978
+ while i < count && ends[i] <= n.end
979
+ return false if element_at?(d, i) || starts[i] > position # an element, or text before a comment
980
+
981
+ position = ends[i] + 1 if ends[i] + 1 > position
982
+ i += 1
983
+ end
984
+ position > n.inner_end
985
+ end
986
+
987
+ def matches_pseudo?(kind, n, scope)
988
+ name = n.name_bytes
989
+ case kind
990
+ when 0 then root?(n) # PSEUDO_ROOT
991
+ when 1 then scope.nil? ? root?(n) : n.equal?(scope) # PSEUDO_SCOPE
992
+ when 2 then empty_element?(n) # PSEUDO_EMPTY
993
+ when 3 then (name == "a" || name == "area") && Attrs.has_attribute?(n, "href") # PSEUDO_LINK
994
+ when 4 # PSEUDO_CHECKED
995
+ (name == "input" && Attrs.has_attribute?(n, "checked")) ||
996
+ (name == "option" && Attrs.has_attribute?(n, "selected"))
997
+ when 5 then FORM_CONTROLS.key?(name) && Attrs.has_attribute?(n, "disabled") # PSEUDO_DISABLED
998
+ when 6 then FORM_CONTROLS.key?(name) && !Attrs.has_attribute?(n, "disabled") # PSEUDO_ENABLED
999
+ when 7 then INPUTS.key?(name) && Attrs.has_attribute?(n, "required") # PSEUDO_REQUIRED
1000
+ when 8 then INPUTS.key?(name) && !Attrs.has_attribute?(n, "required") # PSEUDO_OPTIONAL
1001
+ else false
1002
+ end
1003
+ end
1004
+
1005
+ # ----- the parts the compiler builds -----
1006
+
1007
+ def id_selector(id)
1008
+ [S_ID, id.freeze].freeze
1009
+ end
1010
+
1011
+ def class_selector(name)
1012
+ [S_CLASS, name.freeze].freeze
1013
+ end
1014
+
1015
+ # op: "" (exists), "=", "~", "|", "^", "$", "*"
1016
+ def attribute_selector(name, op, value, flag)
1017
+ code = case op
1018
+ when "" then OP_EXISTS
1019
+ when "=" then OP_EQUALS
1020
+ when "~" then OP_INCLUDES
1021
+ when "|" then OP_DASH
1022
+ when "^" then OP_PREFIX
1023
+ when "$" then OP_SUFFIX
1024
+ else OP_SUBSTRING
1025
+ end
1026
+ lname = Chars.lower(name)
1027
+ fold = flag.nil? ? CASE_INSENSITIVE_VALUES.key?(lname) : flag
1028
+ [S_ATTR, name.downcase(:ascii).freeze, code, value.freeze, op == "" ? false : fold].freeze
1029
+ end
1030
+
1031
+ def nth(a, b, from_end, of_type, of)
1032
+ [S_NTH, a, b, from_end, of_type, of].freeze
1033
+ end
1034
+
1035
+ # kind: :not, :is, :has
1036
+ def logical(kind, list)
1037
+ [kind == :not ? S_NOT : (kind == :has ? S_HAS : S_IS), list].freeze
1038
+ end
1039
+
1040
+ def contains(text)
1041
+ [S_CONTAINS, text.freeze].freeze
1042
+ end
1043
+
1044
+ # a pseudo-class without arguments, or nil for an unknown one
1045
+ def pseudo(name)
1046
+ kind = case name
1047
+ when "root" then PSEUDO_ROOT
1048
+ when "scope" then PSEUDO_SCOPE
1049
+ when "empty" then PSEUDO_EMPTY
1050
+ when "link", "any-link" then PSEUDO_LINK
1051
+ when "checked" then PSEUDO_CHECKED
1052
+ when "disabled" then PSEUDO_DISABLED
1053
+ when "enabled" then PSEUDO_ENABLED
1054
+ when "required" then PSEUDO_REQUIRED
1055
+ when "optional" then PSEUDO_OPTIONAL
1056
+ end
1057
+ kind.nil? ? nil : [S_PSEUDO, kind].freeze
1058
+ end
1059
+ end
1060
+ end
1061
+ end