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.
- fasthtmlparser_pure/__init__.py +41 -0
- fasthtmlparser_pure/_api.py +636 -0
- fasthtmlparser_pure/_api.pyi +342 -0
- fasthtmlparser_pure/_attrs.py +270 -0
- fasthtmlparser_pure/_chars.py +124 -0
- fasthtmlparser_pure/_css.py +861 -0
- fasthtmlparser_pure/_decode.py +144 -0
- fasthtmlparser_pure/_links.py +483 -0
- fasthtmlparser_pure/_parser.py +866 -0
- fasthtmlparser_pure/_query.py +128 -0
- fasthtmlparser_pure/_tables.py +2494 -0
- fasthtmlparser_pure/_text.py +257 -0
- fasthtmlparser_pure/_xpath.py +1594 -0
- fasthtmlparser_pure/py.typed +0 -0
- fasthtmlparser_pure-0.1.0.dist-info/METADATA +166 -0
- fasthtmlparser_pure-0.1.0.dist-info/RECORD +19 -0
- fasthtmlparser_pure-0.1.0.dist-info/WHEEL +5 -0
- fasthtmlparser_pure-0.1.0.dist-info/licenses/LICENSE.md +79 -0
- fasthtmlparser_pure-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""The search criteria of find(), find_all(), matches() and the like: a tag name and, optionally, class, id, attribute
|
|
2
|
+
value, visible text, inner HTML and the source of the opening tag."""
|
|
3
|
+
|
|
4
|
+
from . import _attrs as at
|
|
5
|
+
from . import _text
|
|
6
|
+
from ._chars import ascii_lower, equal_fold
|
|
7
|
+
from ._parser import NAMES, lookup_tag
|
|
8
|
+
|
|
9
|
+
CRITERIA = ("class_", "id", "attr", "text", "id_starts_with", "text_starts_with", "body", "body_starts_with",
|
|
10
|
+
"source")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Query:
|
|
14
|
+
__slots__ = ("key", "tag_name", "plain", "cls", "id", "attr_name", "attr_value", "text", "id_starts_with",
|
|
15
|
+
"text_starts_with", "body", "body_starts_with", "source")
|
|
16
|
+
|
|
17
|
+
def __init__(self, name=None):
|
|
18
|
+
if name is None:
|
|
19
|
+
self.key = -1
|
|
20
|
+
self.tag_name = "" # any tag
|
|
21
|
+
else:
|
|
22
|
+
self.key = lookup_tag(name)
|
|
23
|
+
self.tag_name = name
|
|
24
|
+
self.plain = True # no criteria but the name
|
|
25
|
+
self.cls = None
|
|
26
|
+
self.id = None
|
|
27
|
+
self.attr_name = None
|
|
28
|
+
self.attr_value = None
|
|
29
|
+
self.text = None
|
|
30
|
+
self.id_starts_with = None
|
|
31
|
+
self.text_starts_with = None
|
|
32
|
+
self.body = None
|
|
33
|
+
self.body_starts_with = None
|
|
34
|
+
self.source = None
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def of(name, kwargs):
|
|
38
|
+
"""The query from the name and the keyword arguments; an unknown keyword is a TypeError. None values are
|
|
39
|
+
left out."""
|
|
40
|
+
if name is not None and not isinstance(name, str):
|
|
41
|
+
raise TypeError("the tag name must be a str")
|
|
42
|
+
q = Query(name)
|
|
43
|
+
for key, value in kwargs.items():
|
|
44
|
+
if value is None:
|
|
45
|
+
continue
|
|
46
|
+
if key == "attr":
|
|
47
|
+
if not (isinstance(value, tuple) and len(value) == 2 and isinstance(value[0], str)
|
|
48
|
+
and isinstance(value[1], str)):
|
|
49
|
+
raise TypeError("attr= takes a (name, value) tuple")
|
|
50
|
+
q.attr_name = ascii_lower(value[0])
|
|
51
|
+
q.attr_value = value[1]
|
|
52
|
+
q.plain = False
|
|
53
|
+
continue
|
|
54
|
+
if key not in CRITERIA:
|
|
55
|
+
raise TypeError(f"unexpected keyword argument '{key}' (the criteria are {', '.join(CRITERIA)})")
|
|
56
|
+
if not isinstance(value, str):
|
|
57
|
+
raise TypeError(f"{key}= takes a str")
|
|
58
|
+
setattr(q, "cls" if key == "class_" else key, value)
|
|
59
|
+
q.plain = False
|
|
60
|
+
return q
|
|
61
|
+
|
|
62
|
+
def name_matches_at(self, d, i):
|
|
63
|
+
"""Whether the tag at index i has the name of the query."""
|
|
64
|
+
if self.key >= 0:
|
|
65
|
+
k = d.keys[i]
|
|
66
|
+
return k == self.key or (k < 0 and self.tag_name != "" and equal_fold(d.customs[i], self.tag_name))
|
|
67
|
+
if not self.tag_name:
|
|
68
|
+
return True
|
|
69
|
+
k = d.keys[i]
|
|
70
|
+
return equal_fold(NAMES[k] if k >= 0 else d.customs[i], self.tag_name)
|
|
71
|
+
|
|
72
|
+
def matches(self, d, i):
|
|
73
|
+
return self.name_matches_at(d, i) and (self.plain or self.matches_more(d, i))
|
|
74
|
+
|
|
75
|
+
def scan(self, d, start, end, first_only):
|
|
76
|
+
"""The tags from index start on that match, up to the tags that end after end."""
|
|
77
|
+
out = []
|
|
78
|
+
ends = d.ends
|
|
79
|
+
keys = d.keys
|
|
80
|
+
key = self.key
|
|
81
|
+
plain = self.plain
|
|
82
|
+
named = self.tag_name != ""
|
|
83
|
+
count = len(keys)
|
|
84
|
+
i = start
|
|
85
|
+
while i < count:
|
|
86
|
+
if ends[i] > end:
|
|
87
|
+
break
|
|
88
|
+
# the name on the arrays first (a known name: one int compare)
|
|
89
|
+
if key >= 0:
|
|
90
|
+
k = keys[i]
|
|
91
|
+
if k != key and (k >= 0 or not named or not equal_fold(d.customs[i], self.tag_name)):
|
|
92
|
+
i += 1
|
|
93
|
+
continue
|
|
94
|
+
elif named and not self.name_matches_at(d, i):
|
|
95
|
+
i += 1
|
|
96
|
+
continue
|
|
97
|
+
if plain or self.matches_more(d, i):
|
|
98
|
+
out.append(i)
|
|
99
|
+
if first_only:
|
|
100
|
+
break
|
|
101
|
+
i += 1
|
|
102
|
+
return out
|
|
103
|
+
|
|
104
|
+
def matches_more(self, d, i):
|
|
105
|
+
"""The criteria other than the name."""
|
|
106
|
+
if self.id is not None and (not self.id or not at.attr_equals(at.attr_raw(d, i, "id"), self.id)):
|
|
107
|
+
return False
|
|
108
|
+
if self.attr_name is not None and not at.attr_equals(at.attr_raw(d, i, self.attr_name), self.attr_value):
|
|
109
|
+
return False
|
|
110
|
+
if self.cls is not None and not at.has_class(d, i, self.cls):
|
|
111
|
+
return False
|
|
112
|
+
if self.id_starts_with is not None and not at.attr_starts_with(at.attr_raw(d, i, "id"), self.id_starts_with):
|
|
113
|
+
return False
|
|
114
|
+
if self.source is not None and d.source_of(i) != self.source:
|
|
115
|
+
return False
|
|
116
|
+
if self.body is not None and d.body_of(i) != self.body:
|
|
117
|
+
return False
|
|
118
|
+
if self.body_starts_with is not None and not d.body_of(i).startswith(self.body_starts_with):
|
|
119
|
+
return False
|
|
120
|
+
# the text last: it is the most expensive check
|
|
121
|
+
if self.text is not None and not _text.text_equals(d.body_of(i), self.text, d, i):
|
|
122
|
+
return False
|
|
123
|
+
if self.text_starts_with is not None and not d.text_of(i).startswith(self.text_starts_with):
|
|
124
|
+
return False
|
|
125
|
+
return True
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
ANY = Query()
|