fasthtmlparser-pure 0.1.0__py3-none-any.whl

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,861 @@
1
+ """CSS selectors (Selectors Level 3 and the common Level 4 pseudo-classes), matched right to left over the flat tag
2
+ list as browsers do. Tags are document indexes; the scope and the anchor are an index or None (the document)."""
3
+
4
+ import re
5
+
6
+ from . import _attrs as at
7
+ from . import _decode as dec
8
+ from ._chars import ascii_lower, equal_fold, is_alpha, is_digit, is_hex, lower, quote
9
+ from ._parser import COMMENT, DOCTYPE, XML_DECLARATION, lookup_tag
10
+
11
+
12
+ class CssSelectorError(ValueError):
13
+ """An invalid CSS selector."""
14
+
15
+
16
+ # combinators
17
+ COMB_NONE = 0
18
+ COMB_DESCENDANT = 1 # "a b"
19
+ COMB_CHILD = 2 # "a > b"
20
+ COMB_ADJACENT = 3 # "a + b"
21
+ COMB_SIBLING = 4 # "a ~ b"
22
+
23
+ # simple selectors: (kind, ...)
24
+ S_ID = 0 # (S_ID, id)
25
+ S_CLASS = 1 # (S_CLASS, name)
26
+ S_ATTR = 2 # (S_ATTR, name, op, value, fold)
27
+ S_NTH = 3 # (S_NTH, a, b, from_end, of_type, of (a list or None))
28
+ S_NOT = 4 # (S_NOT, list)
29
+ S_IS = 5 # (S_IS, list)
30
+ S_HAS = 6 # (S_HAS, list)
31
+ S_CONTAINS = 7 # (S_CONTAINS, text)
32
+ S_PSEUDO = 8 # (S_PSEUDO, kind)
33
+
34
+ OP_EXISTS = 0 # [a]
35
+ OP_EQUALS = 1 # [a=v]
36
+ OP_INCLUDES = 2 # [a~=v]
37
+ OP_DASH = 3 # [a|=v]
38
+ OP_PREFIX = 4 # [a^=v]
39
+ OP_SUFFIX = 5 # [a$=v]
40
+ OP_SUBSTRING = 6 # [a*=v]
41
+
42
+ PSEUDO_ROOT = 0
43
+ PSEUDO_SCOPE = 1
44
+ PSEUDO_EMPTY = 2
45
+ PSEUDO_LINK = 3
46
+ PSEUDO_CHECKED = 4
47
+ PSEUDO_DISABLED = 5
48
+ PSEUDO_ENABLED = 6
49
+ PSEUDO_REQUIRED = 7
50
+ PSEUDO_OPTIONAL = 8
51
+ PSEUDOS = {
52
+ "root": PSEUDO_ROOT, "scope": PSEUDO_SCOPE, "empty": PSEUDO_EMPTY, "link": PSEUDO_LINK, "any-link": PSEUDO_LINK,
53
+ "checked": PSEUDO_CHECKED, "disabled": PSEUDO_DISABLED, "enabled": PSEUDO_ENABLED, "required": PSEUDO_REQUIRED,
54
+ "optional": PSEUDO_OPTIONAL,
55
+ }
56
+
57
+ CSS_SPACE = " \t\n\x0c\r"
58
+ CSS_SPACE_RE = re.compile(r"[ \t\n\f\r]")
59
+
60
+ # HTML: the values of these attributes are compared ignoring ASCII case unless the selector has the 's' flag.
61
+ CASE_INSENSITIVE_VALUES = frozenset((
62
+ "accept accept-charset align alink axis bgcolor charset checked clear codetype color compact declare defer dir "
63
+ "direction disabled enctype face frame hreflang http-equiv lang language link media method multiple nohref "
64
+ "noresize noshade nowrap readonly rel rev rules scope scrolling selected shape target text type valign valuetype "
65
+ "vlink").split())
66
+
67
+ FORM_CONTROLS = frozenset("button input select textarea optgroup option fieldset".split())
68
+ INPUTS = frozenset("input select textarea".split())
69
+
70
+ REPLACEMENT = chr(0xFFFD)
71
+
72
+
73
+ class Complex:
74
+ """A complex selector: lead combinator, compounds c, combinators k (k[i] stands between c[i] and c[i+1])."""
75
+
76
+ __slots__ = ("lead", "c", "k")
77
+
78
+ def __init__(self):
79
+ self.lead = COMB_NONE
80
+ self.c = []
81
+ self.k = []
82
+
83
+
84
+ class Compound:
85
+ """Tag key or -1, custom name ("": any element when key is -1), simple selectors."""
86
+
87
+ __slots__ = ("key", "name", "s")
88
+
89
+ def __init__(self):
90
+ self.key = -1
91
+ self.name = ""
92
+ self.s = []
93
+
94
+
95
+ def element_at(d, i):
96
+ """Not a comment, the doctype or the XML declaration."""
97
+ key = d.keys[i]
98
+ if key >= 0:
99
+ return key != COMMENT and key != DOCTYPE and key != XML_DECLARATION
100
+ custom = d.customs[i]
101
+ return custom != "" and custom[0] != "!" and custom[0] != "?"
102
+
103
+
104
+ # ----- the compiler -----
105
+
106
+ class _Compiler:
107
+ def __init__(self, s):
108
+ self.s = s
109
+ self.n = len(s)
110
+ self.p = 0
111
+
112
+ def fail(self, msg):
113
+ raise CssSelectorError(f"invalid CSS selector {quote(self.s)} at position {self.p}: {msg}")
114
+
115
+ def peek(self, offset):
116
+ i = self.p + offset
117
+ return self.s[i] if i < self.n else ""
118
+
119
+ def skip_space(self):
120
+ k = self.p
121
+ while self.p < self.n and self.s[self.p] in CSS_SPACE:
122
+ self.p += 1
123
+ return self.p > k
124
+
125
+ def expect(self, ch):
126
+ if self.peek(0) != ch:
127
+ if self.p >= self.n:
128
+ self.fail(f"'{ch}' expected")
129
+ self.fail(f"'{ch}' expected instead of '{self.s[self.p]}'")
130
+ self.p += 1
131
+
132
+ def parse_list(self, relative, top_level):
133
+ """selector-list: complex (',' complex)*"""
134
+ out = []
135
+ while True:
136
+ out.append(self.parse_complex(relative, top_level))
137
+ self.skip_space()
138
+ if self.peek(0) != ",":
139
+ return out
140
+ self.p += 1
141
+
142
+ def parse_complex(self, relative, top_level):
143
+ """complex: [combinator] compound (combinator compound)*"""
144
+ self.skip_space()
145
+ cx = Complex()
146
+ if relative or top_level:
147
+ comb = self.read_combinator()
148
+ if comb != COMB_NONE:
149
+ if top_level and comb != COMB_CHILD:
150
+ self.fail("a selector can start only with '>' (the children of the tag it is called on)")
151
+ cx.lead = comb
152
+ elif relative:
153
+ cx.lead = COMB_DESCENDANT
154
+ cx.c.append(self.parse_compound())
155
+ while True:
156
+ ws = self.skip_space()
157
+ ch = self.peek(0)
158
+ if ch == "" or ch == "," or ch == ")":
159
+ return cx
160
+ comb = self.read_combinator()
161
+ if comb == COMB_NONE:
162
+ if not ws:
163
+ self.fail(f"unexpected '{self.s[self.p]}'")
164
+ comb = COMB_DESCENDANT
165
+ cx.k.append(comb)
166
+ cx.c.append(self.parse_compound())
167
+
168
+ def read_combinator(self):
169
+ c = self.peek(0)
170
+ comb = COMB_CHILD if c == ">" else COMB_ADJACENT if c == "+" else COMB_SIBLING if c == "~" else COMB_NONE
171
+ if comb != COMB_NONE:
172
+ self.p += 1
173
+ self.skip_space()
174
+ return comb
175
+
176
+ def parse_compound(self):
177
+ """compound: [type | '*'] (#id | .class | [attribute] | :pseudo)*"""
178
+ cp = Compound()
179
+ has_type = False
180
+ if self.peek(0) == "*":
181
+ self.p += 1
182
+ has_type = True
183
+ elif self.ident_start(self.p):
184
+ name = lower(self.read_name())
185
+ has_type = True
186
+ t = lookup_tag(name)
187
+ if t >= 0:
188
+ cp.key = t
189
+ else:
190
+ cp.name = name
191
+ if self.peek(0) == "|":
192
+ self.fail("namespaces are not supported; escape the colon of a prefixed name: dc\\:creator")
193
+
194
+ while True:
195
+ ch = self.peek(0)
196
+ if ch == "#":
197
+ self.p += 1
198
+ if not self.name_char(self.p):
199
+ self.fail("an id expected after '#'")
200
+ cp.s.append((S_ID, self.read_name()))
201
+ elif ch == ".":
202
+ self.p += 1
203
+ if not self.ident_start(self.p):
204
+ self.fail("a class name expected after '.'")
205
+ cp.s.append((S_CLASS, self.read_name()))
206
+ elif ch == "[":
207
+ cp.s.append(self.parse_attribute())
208
+ elif ch == ":":
209
+ self.parse_pseudo(cp)
210
+ else:
211
+ break
212
+
213
+ if not has_type and not cp.s:
214
+ if self.p >= self.n:
215
+ self.fail("a selector expected")
216
+ self.fail(f"unexpected '{self.s[self.p]}'")
217
+ return cp
218
+
219
+ def parse_attribute(self):
220
+ """'[' name [op value [i|s]] ']'"""
221
+ self.p += 1 # '['
222
+ self.skip_space()
223
+ if not self.ident_start(self.p):
224
+ self.fail("an attribute name expected")
225
+ name = self.read_name()
226
+ self.skip_space()
227
+ if self.peek(0) == "]":
228
+ self.p += 1
229
+ return attribute_selector(name, "", "", None)
230
+ ch = self.peek(0)
231
+ if ch == "=":
232
+ op = "="
233
+ self.p += 1
234
+ elif ch != "" and ch in "~|^$*":
235
+ op = ch
236
+ if self.peek(1) != "=":
237
+ self.fail(f"'=' expected after '{op}'")
238
+ self.p += 2
239
+ else:
240
+ self.fail("'=', '~=', '|=', '^=', '$=', '*=' or ']' expected")
241
+
242
+ self.skip_space()
243
+ if self.peek(0) == '"' or self.peek(0) == "'":
244
+ value = self.read_string()
245
+ elif self.ident_start(self.p):
246
+ value = self.read_name()
247
+ else:
248
+ self.fail("an attribute value expected (quote it if it is not a plain word)")
249
+
250
+ self.skip_space()
251
+ flag = None
252
+ if self.ident_start(self.p):
253
+ f = self.read_name()
254
+ if equal_fold(f, "i"):
255
+ flag = True
256
+ elif equal_fold(f, "s"):
257
+ flag = False
258
+ else:
259
+ self.fail(f"unknown attribute flag '{f}'")
260
+ self.skip_space()
261
+ self.expect("]")
262
+ return attribute_selector(name, op, value, flag)
263
+
264
+ def parse_pseudo(self, cp):
265
+ self.p += 1 # ':'
266
+ if self.peek(0) == ":":
267
+ self.fail("pseudo-elements (::before, ::text) select no tags")
268
+ if not self.ident_start(self.p):
269
+ self.fail("a pseudo-class name expected after ':'")
270
+ start = self.p
271
+ name = lower(self.read_name())
272
+
273
+ if self.peek(0) == "(":
274
+ self.p += 1
275
+ self.skip_space()
276
+ if name == "not":
277
+ cp.s.append((S_NOT, self.parse_list(False, False)))
278
+ elif name in ("is", "where", "matches", "any"):
279
+ cp.s.append((S_IS, self.parse_list(False, False)))
280
+ elif name == "has":
281
+ cp.s.append((S_HAS, self.parse_list(True, False)))
282
+ elif name in ("nth-child", "nth-last-child", "nth-of-type", "nth-last-of-type"):
283
+ a, b = self.parse_nth()
284
+ of_type = name.endswith("of-type")
285
+ of = None
286
+ self.skip_space()
287
+ if not of_type and self.word("of"):
288
+ self.p += 2
289
+ of = self.parse_list(False, False)
290
+ cp.s.append((S_NTH, a, b, "last" in name, of_type, of))
291
+ elif name == "contains":
292
+ if self.peek(0) == '"' or self.peek(0) == "'":
293
+ text = self.read_string()
294
+ elif self.ident_start(self.p):
295
+ text = self.read_name()
296
+ else:
297
+ self.fail("a string expected in :contains()")
298
+ cp.s.append((S_CONTAINS, text))
299
+ else:
300
+ self.p = start
301
+ self.fail(f"unknown pseudo-class ':{name}()'")
302
+ self.skip_space()
303
+ self.expect(")")
304
+ return
305
+
306
+ if name == "first-child":
307
+ cp.s.append((S_NTH, 0, 1, False, False, None))
308
+ elif name == "last-child":
309
+ cp.s.append((S_NTH, 0, 1, True, False, None))
310
+ elif name == "only-child":
311
+ cp.s.append((S_NTH, 0, 1, False, False, None))
312
+ cp.s.append((S_NTH, 0, 1, True, False, None))
313
+ elif name == "first-of-type":
314
+ cp.s.append((S_NTH, 0, 1, False, True, None))
315
+ elif name == "last-of-type":
316
+ cp.s.append((S_NTH, 0, 1, True, True, None))
317
+ elif name == "only-of-type":
318
+ cp.s.append((S_NTH, 0, 1, False, True, None))
319
+ cp.s.append((S_NTH, 0, 1, True, True, None))
320
+ else:
321
+ kind = PSEUDOS.get(name)
322
+ if kind is None:
323
+ self.p = start
324
+ self.fail(f"unknown pseudo-class ':{name}'")
325
+ cp.s.append((S_PSEUDO, kind))
326
+
327
+ def parse_nth(self):
328
+ """An+B: odd, even, 3, n, -n+3, 2n+1, +2n - 1; (a, b)"""
329
+ if self.word("odd"):
330
+ self.p += 3
331
+ return 2, 1
332
+ if self.word("even"):
333
+ self.p += 4
334
+ return 2, 0
335
+ start = self.p
336
+ t = []
337
+ while self.p < self.n:
338
+ ch = self.s[self.p]
339
+ if not (ch in "+-nN" or is_digit(ch) or ch in CSS_SPACE):
340
+ break
341
+ if ch not in CSS_SPACE:
342
+ t.append("n" if ch == "N" else ch)
343
+ self.p += 1
344
+ t = "".join(t)
345
+ n = t.find("n")
346
+ if n < 0:
347
+ v = _parse_int32(t)
348
+ if v is not None:
349
+ return 0, v
350
+ elif t.find("n", n + 1) < 0:
351
+ a_text = t[:n]
352
+ b_text = t[n + 1:]
353
+ if a_text == "" or a_text == "+":
354
+ a = 1
355
+ else:
356
+ a = -1 if a_text == "-" else _parse_int32(a_text)
357
+ b = 0
358
+ if b_text:
359
+ ok = b_text[0] in "+-" and len(b_text) > 1 and is_digit(b_text[1])
360
+ b = _parse_int32(b_text) if ok else None
361
+ if a is not None and b is not None:
362
+ return a, b
363
+ self.p = start
364
+ self.fail("An+B expected (odd, even, 3, 2n+1, -n+3)")
365
+
366
+ def word(self, w):
367
+ """The identifier at the position is w (case-insensitive) and ends there."""
368
+ n = len(w)
369
+ return self.p + n <= self.n and ascii_lower(self.s[self.p:self.p + n]) == w and not self.name_char(self.p + n)
370
+
371
+ def escape(self, p):
372
+ if p >= self.n or self.s[p] != "\\":
373
+ return False
374
+ if p + 1 >= self.n:
375
+ return True
376
+ c = self.s[p + 1]
377
+ return c != "\n" and c != "\r" and c != "\x0c"
378
+
379
+ @staticmethod
380
+ def name_start_char(c):
381
+ return is_alpha(c) or c == "_" or c >= "\x80"
382
+
383
+ def name_char(self, p):
384
+ if p >= self.n:
385
+ return False
386
+ c = self.s[p]
387
+ return self.name_start_char(c) or is_digit(c) or c == "-" or self.escape(p)
388
+
389
+ def ident_start(self, p):
390
+ if p >= self.n:
391
+ return False
392
+ if self.s[p] == "-":
393
+ p += 1
394
+ if p >= self.n:
395
+ return False
396
+ c = self.s[p]
397
+ return self.name_start_char(c) or c == "-" or self.escape(p)
398
+ return self.name_start_char(self.s[p]) or self.escape(p)
399
+
400
+ def read_name(self):
401
+ b = []
402
+ while self.name_char(self.p):
403
+ if self.s[self.p] == "\\":
404
+ b.append(self.read_escape())
405
+ else:
406
+ b.append(self.s[self.p])
407
+ self.p += 1
408
+ return "".join(b)
409
+
410
+ def read_string(self):
411
+ q = self.s[self.p]
412
+ start = self.p
413
+ self.p += 1
414
+ b = []
415
+ while True:
416
+ if self.p >= self.n:
417
+ self.p = start
418
+ self.fail("unterminated string")
419
+ ch = self.s[self.p]
420
+ if ch == q:
421
+ self.p += 1
422
+ return "".join(b)
423
+ if ch == "\\":
424
+ nxt = self.peek(1)
425
+ if self.p + 1 < self.n and (nxt == "\n" or nxt == "\x0c"):
426
+ self.p += 2 # an escaped line break continues the string
427
+ elif self.p + 2 < self.n and nxt == "\r":
428
+ self.p += 3 if self.peek(2) == "\n" else 2
429
+ else:
430
+ b.append(self.read_escape())
431
+ else:
432
+ b.append(ch)
433
+ self.p += 1
434
+
435
+ def read_escape(self):
436
+ """'\\' then up to 6 hex digits and one optional whitespace, or any other char as itself."""
437
+ self.p += 1 # '\'
438
+ if self.p >= self.n:
439
+ return REPLACEMENT
440
+ if not is_hex(self.s[self.p]):
441
+ ch = self.s[self.p]
442
+ self.p += 1
443
+ return ch
444
+ digits = 0
445
+ while digits < 6 and self.p + digits < self.n and is_hex(self.s[self.p + digits]):
446
+ digits += 1
447
+ code = int(self.s[self.p:self.p + digits], 16)
448
+ self.p += digits
449
+ if self.p < self.n and self.s[self.p] in CSS_SPACE:
450
+ self.p += 2 if self.s[self.p] == "\r" and self.peek(1) == "\n" else 1
451
+ if code == 0 or code > 0x10FFFF or 0xD800 <= code <= 0xDFFF:
452
+ return REPLACEMENT
453
+ return chr(code)
454
+
455
+
456
+ def _parse_int32(s):
457
+ i = 1 if s and s[0] in "+-" else 0
458
+ rest = s[i:]
459
+ if not rest or not rest.isascii() or not rest.isdigit():
460
+ return None
461
+ digits = rest.lstrip("0")
462
+ if len(digits) > 10:
463
+ return None
464
+ v = int(digits) if digits else 0
465
+ if s[0] == "-":
466
+ v = -v
467
+ return v if -2147483648 <= v <= 2147483647 else None
468
+
469
+
470
+ def attribute_selector(name, op, value, flag):
471
+ """op: "" (exists), "=", "~", "|", "^", "$", "*" """
472
+ code = {"": OP_EXISTS, "=": OP_EQUALS, "~": OP_INCLUDES, "|": OP_DASH, "^": OP_PREFIX, "$": OP_SUFFIX}.get(
473
+ op, OP_SUBSTRING)
474
+ fold = (lower(name) in CASE_INSENSITIVE_VALUES) if flag is None else flag
475
+ return (S_ATTR, ascii_lower(name), code, value, False if op == "" else fold)
476
+
477
+
478
+ def compile_css(selector):
479
+ """The selector list of a CSS selector; raises CssSelectorError on an invalid one."""
480
+ c = _Compiler(selector)
481
+ lst = c.parse_list(False, True)
482
+ if c.p < len(selector):
483
+ c.fail(f"unexpected '{selector[c.p]}'")
484
+ return lst
485
+
486
+
487
+ # ----- sibling links and positions, built once per document by the first selector that needs them -----
488
+
489
+ class CssTree:
490
+ __slots__ = ("d", "previous", "next", "position", "from_end", "types")
491
+
492
+ def __init__(self, d):
493
+ self.d = d
494
+ count = len(d.keys)
495
+ parents = d.parents
496
+ previous = [-1] * count
497
+ nxt = [-1] * count
498
+ position = [0] * count
499
+ from_end = [0] * count
500
+ # per parent (slot = parent index + 1, slot 0: the top level): its last element child so far and their number
501
+ last_child = [-1] * (count + 1)
502
+ children = [0] * (count + 1)
503
+ for i in range(count):
504
+ if element_at(d, i):
505
+ slot = parents[i] + 1
506
+ prev = last_child[slot]
507
+ previous[i] = prev
508
+ if prev >= 0:
509
+ nxt[prev] = i
510
+ last_child[slot] = i
511
+ children[slot] += 1
512
+ position[i] = children[slot]
513
+ for i in range(count):
514
+ if position[i] > 0:
515
+ from_end[i] = children[parents[i] + 1] - position[i] + 1
516
+ self.previous = previous
517
+ self.next = nxt
518
+ self.position = position
519
+ self.from_end = from_end
520
+ self.types = None
521
+
522
+ def type_positions(self):
523
+ """(position, from_end) among the siblings of the same type."""
524
+ if self.types is not None:
525
+ return self.types
526
+ d = self.d
527
+ count = len(d.keys)
528
+ pos = [0] * count
529
+ fin = [0] * count
530
+ seen = {}
531
+ keys = [None] * count
532
+ for i in range(count):
533
+ if self.position[i] != 0:
534
+ key = d.keys[i]
535
+ k = (d.parents[i], key, d.customs[i] if key < 0 else None)
536
+ keys[i] = k
537
+ seen[k] = seen.get(k, 0) + 1
538
+ pos[i] = seen[k]
539
+ for i in range(count):
540
+ if pos[i] > 0:
541
+ fin[i] = seen[keys[i]] - pos[i] + 1
542
+ self.types = (pos, fin)
543
+ return self.types
544
+
545
+
546
+ # ----- matching -----
547
+
548
+ class CssMatcher:
549
+ """A compiled selector list and its matching."""
550
+
551
+ __slots__ = ("selector", "list", "names")
552
+
553
+ def __init__(self, selector):
554
+ self.selector = selector
555
+ self.list = compile_css(selector)
556
+ # the name of the rightmost compound of each selector, checked on the arrays first
557
+ names = []
558
+ for item in self.list:
559
+ last = item.c[-1]
560
+ if last.key < 0 and last.name == "":
561
+ names = None # any element
562
+ break
563
+ names.append(last)
564
+ self.names = names
565
+
566
+ def scan(self, d, scope, start, end, first_only):
567
+ out = []
568
+ ends = d.ends
569
+ keys = d.keys
570
+ customs = d.customs
571
+ lst = self.list
572
+ names = self.names
573
+ # one selector with a known tag name: one int compare per tag
574
+ single = names[0].key if names is not None and len(names) == 1 and names[0].key >= 0 else None
575
+ count = len(keys)
576
+ i = start
577
+ while i < count:
578
+ if ends[i] > end:
579
+ break
580
+ if single is not None:
581
+ if keys[i] != single:
582
+ i += 1
583
+ continue
584
+ elif names is not None:
585
+ k = keys[i]
586
+ for cp in names:
587
+ if (k == cp.key) if cp.key >= 0 else (k < 0 and customs[i] == cp.name):
588
+ break
589
+ else:
590
+ i += 1
591
+ continue
592
+ elif not element_at(d, i):
593
+ i += 1
594
+ continue
595
+ if matches_list(lst, d, i, scope, scope):
596
+ out.append(i)
597
+ if first_only:
598
+ break
599
+ i += 1
600
+ return out
601
+
602
+ def matches(self, d, i):
603
+ """element.matches(): :scope is the tag itself; False for comments."""
604
+ return element_at(d, i) and matches_list(self.list, d, i, i, i)
605
+
606
+
607
+ def matches_list(lst, d, n, scope, anchor):
608
+ """scope: the tag the query was called on (:scope, None for the document); anchor: the tag a relative selector
609
+ starts from (:has(> a), or node.css("> li"); None: the document)."""
610
+ for item in lst:
611
+ if matches_complex(item, len(item.c) - 1, d, n, scope, anchor):
612
+ return True
613
+ return False
614
+
615
+
616
+ def matches_complex(cx, i, d, n, scope, anchor):
617
+ """n matches compound i and the part to the left of it matches the tags the combinators lead to."""
618
+ if not matches_compound(cx.c[i], d, n, scope, anchor):
619
+ return False
620
+ if i == 0:
621
+ return cx.lead == COMB_NONE or related(cx.lead, d, anchor, n)
622
+ k = cx.k[i - 1]
623
+ if k == COMB_CHILD:
624
+ p = d.parents[n]
625
+ return p >= 0 and matches_complex(cx, i - 1, d, p, scope, anchor)
626
+ if k == COMB_DESCENDANT:
627
+ parents = d.parents
628
+ p = parents[n]
629
+ while p >= 0:
630
+ if matches_complex(cx, i - 1, d, p, scope, anchor):
631
+ return True
632
+ p = parents[p]
633
+ return False
634
+ if k == COMB_ADJACENT:
635
+ p = d.css_tree().previous[n]
636
+ return p >= 0 and matches_complex(cx, i - 1, d, p, scope, anchor)
637
+ if k == COMB_SIBLING:
638
+ prev = d.css_tree().previous
639
+ p = prev[n]
640
+ while p >= 0:
641
+ if matches_complex(cx, i - 1, d, p, scope, anchor):
642
+ return True
643
+ p = prev[p]
644
+ return False
645
+ return False
646
+
647
+
648
+ def matches_compound(cp, d, n, scope, anchor):
649
+ if cp.key >= 0:
650
+ if d.keys[n] != cp.key:
651
+ return False
652
+ elif cp.name:
653
+ if d.keys[n] >= 0 or d.customs[n] != cp.name:
654
+ return False
655
+ elif not element_at(d, n):
656
+ return False
657
+ for s in cp.s:
658
+ if not matches_simple(s, d, n, scope, anchor):
659
+ return False
660
+ return True
661
+
662
+
663
+ def related(c, d, anchor, n):
664
+ """n stands in the combinator relation to anchor (None: the document)."""
665
+ if c == COMB_CHILD:
666
+ return d.parents[n] < 0 if anchor is None else d.parents[n] == anchor
667
+ if c == COMB_DESCENDANT:
668
+ if anchor is None:
669
+ return True
670
+ parents = d.parents
671
+ p = parents[n]
672
+ while p >= 0:
673
+ if p == anchor:
674
+ return True
675
+ p = parents[p]
676
+ return False
677
+ if c == COMB_ADJACENT:
678
+ return anchor is not None and d.css_tree().previous[n] == anchor
679
+ if c == COMB_SIBLING:
680
+ return anchor is not None and anchor < n and d.parents[anchor] == d.parents[n] and element_at(d, anchor)
681
+ return False
682
+
683
+
684
+ def has_match(relative, d, anchor, scope):
685
+ """:has(): whether a tag in the anchor's scope matches the relative selector."""
686
+ ends = d.ends
687
+ count = len(ends)
688
+ anchor_end = ends[anchor]
689
+ for item in relative:
690
+ last = len(item.c) - 1
691
+ i = anchor + 1
692
+ if item.lead == COMB_DESCENDANT or item.lead == COMB_CHILD:
693
+ # the tags inside the anchor
694
+ while i < count and ends[i] <= anchor_end:
695
+ if element_at(d, i) and matches_complex(item, last, d, i, scope, anchor):
696
+ return True
697
+ i += 1
698
+ else:
699
+ # the following siblings and the tags inside them, up to the end of the parent
700
+ while i < count and ends[i] <= anchor_end:
701
+ i += 1
702
+ parent = d.parents[anchor]
703
+ end = ends[parent] if parent >= 0 else float("inf")
704
+ while i < count and ends[i] <= end:
705
+ if element_at(d, i) and matches_complex(item, last, d, i, scope, anchor):
706
+ return True
707
+ i += 1
708
+ return False
709
+
710
+
711
+ def contains_word(lst, word, fold):
712
+ """Whether a whitespace-separated list contains word."""
713
+ if not word or len(lst) < len(word):
714
+ return False
715
+ if not fold and CSS_SPACE_RE.search(lst) is None:
716
+ return lst == word
717
+ for w in CSS_SPACE_RE.split(lst):
718
+ if w and (equal_fold(w, word) if fold else w == word):
719
+ return True
720
+ return False
721
+
722
+
723
+ def _decoded(raw):
724
+ return dec.decode(raw, True) if "&" in raw or "\r" in raw else raw
725
+
726
+
727
+ def matches_simple(s, d, n, scope, anchor):
728
+ kind = s[0]
729
+ if kind == S_ID:
730
+ return at.attr_equals(at.attr_raw(d, n, "id"), s[1])
731
+ if kind == S_CLASS:
732
+ raw = at.attr_raw(d, n, "class")
733
+ return raw != "" and contains_word(_decoded(raw), s[1], False)
734
+ if kind == S_ATTR:
735
+ return matches_attribute(d, n, s[1], s[2], s[3], s[4])
736
+ if kind == S_NTH:
737
+ return matches_nth(d, n, s[1], s[2], s[3], s[4], s[5], scope, anchor)
738
+ if kind == S_NOT:
739
+ return not matches_list(s[1], d, n, scope, anchor)
740
+ if kind == S_IS:
741
+ return matches_list(s[1], d, n, scope, anchor)
742
+ if kind == S_HAS:
743
+ return has_match(s[1], d, n, scope)
744
+ if kind == S_CONTAINS:
745
+ return s[1] in d.text_of(n)
746
+ return matches_pseudo(s[1], d, n, scope)
747
+
748
+
749
+ def _eq(a, b, fold):
750
+ return equal_fold(a, b) if fold else a == b
751
+
752
+
753
+ def matches_attribute(d, n, name, op, v, fold):
754
+ raw = at.attr_raw(d, n, name)
755
+ if not raw:
756
+ # missing or empty: only these can match an empty value
757
+ if op == OP_EXISTS:
758
+ return at.has_attribute(d, n, name)
759
+ if op == OP_EQUALS or op == OP_DASH:
760
+ return v == "" and at.has_attribute(d, n, name)
761
+ return False
762
+ if op == OP_EXISTS:
763
+ return True
764
+ actual = _decoded(raw)
765
+ vl = len(v)
766
+ if op == OP_EQUALS:
767
+ return _eq(actual, v, fold)
768
+ if op == OP_INCLUDES:
769
+ return CSS_SPACE_RE.search(v) is None and contains_word(actual, v, fold)
770
+ if op == OP_DASH:
771
+ return _eq(actual, v, fold) or (len(actual) > vl and actual[vl] == "-" and _eq(actual[:vl], v, fold))
772
+ if op == OP_PREFIX:
773
+ return v != "" and len(actual) >= vl and _eq(actual[:vl], v, fold)
774
+ if op == OP_SUFFIX:
775
+ return v != "" and len(actual) >= vl and _eq(actual[len(actual) - vl:], v, fold)
776
+ if op == OP_SUBSTRING:
777
+ if not v:
778
+ return False
779
+ if not fold:
780
+ return v in actual
781
+ if v.isascii():
782
+ return ascii_lower(v) in ascii_lower(actual)
783
+ return lower(v) in lower(actual)
784
+ return False
785
+
786
+
787
+ def matches_nth(d, n, a, b, from_end, of_type, of, scope, anchor):
788
+ """:nth-child(An+B [of S]) and its relatives; :first-child is :nth-child(1), and so on."""
789
+ t = d.css_tree()
790
+ if of is not None:
791
+ if not matches_list(of, d, n, scope, anchor):
792
+ return False
793
+ position = 1
794
+ links = t.next if from_end else t.previous
795
+ sib = links[n]
796
+ while sib >= 0:
797
+ if matches_list(of, d, sib, scope, anchor):
798
+ position += 1
799
+ sib = links[sib]
800
+ elif of_type:
801
+ tp = t.type_positions()
802
+ position = tp[1][n] if from_end else tp[0][n]
803
+ else:
804
+ position = t.from_end[n] if from_end else t.position[n]
805
+ if position <= 0:
806
+ return False # not an element
807
+ # position = a*n + b for some n >= 0
808
+ if a == 0:
809
+ return position == b
810
+ diff = position - b
811
+ return diff % a == 0 and diff // a >= 0
812
+
813
+
814
+ def _root(d, n):
815
+ """the first top-level element: <html> on a page"""
816
+ return d.parents[n] < 0 and d.css_tree().previous[n] < 0
817
+
818
+
819
+ def _empty_element(d, n):
820
+ """No child elements and no text; comments are allowed."""
821
+ inner_start = d.inner_starts[n]
822
+ inner_end = d.inner_ends[n]
823
+ if inner_start <= 0 or inner_end <= 0:
824
+ return True # a void or unclosed tag has no content
825
+ position = inner_start
826
+ ends = d.ends
827
+ starts = d.starts
828
+ end = ends[n]
829
+ i = n + 1
830
+ count = len(ends)
831
+ while i < count and ends[i] <= end:
832
+ if element_at(d, i) or starts[i] > position:
833
+ return False # an element, or text before a comment
834
+ if ends[i] + 1 > position:
835
+ position = ends[i] + 1
836
+ i += 1
837
+ return position > inner_end
838
+
839
+
840
+ def matches_pseudo(kind, d, n, scope):
841
+ name = d.name_of(n)
842
+ if kind == PSEUDO_ROOT:
843
+ return _root(d, n)
844
+ if kind == PSEUDO_SCOPE:
845
+ return _root(d, n) if scope is None else n == scope
846
+ if kind == PSEUDO_EMPTY:
847
+ return _empty_element(d, n)
848
+ if kind == PSEUDO_LINK:
849
+ return (name == "a" or name == "area") and at.has_attribute(d, n, "href")
850
+ if kind == PSEUDO_CHECKED:
851
+ return (name == "input" and at.has_attribute(d, n, "checked")) or \
852
+ (name == "option" and at.has_attribute(d, n, "selected"))
853
+ if kind == PSEUDO_DISABLED:
854
+ return name in FORM_CONTROLS and at.has_attribute(d, n, "disabled")
855
+ if kind == PSEUDO_ENABLED:
856
+ return name in FORM_CONTROLS and not at.has_attribute(d, n, "disabled")
857
+ if kind == PSEUDO_REQUIRED:
858
+ return name in INPUTS and at.has_attribute(d, n, "required")
859
+ if kind == PSEUDO_OPTIONAL:
860
+ return name in INPUTS and not at.has_attribute(d, n, "required")
861
+ return False