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,342 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
from typing import final
|
|
3
|
+
|
|
4
|
+
class CssSelectorError(ValueError):
|
|
5
|
+
"""An invalid CSS selector."""
|
|
6
|
+
|
|
7
|
+
class XPathError(ValueError):
|
|
8
|
+
"""An invalid XPath expression, or one that cannot be evaluated."""
|
|
9
|
+
|
|
10
|
+
def parse(html: str | bytes, url: str | None = None) -> Document:
|
|
11
|
+
"""Parses an HTML or XML page. Bytes are read as UTF-8 (invalid sequences become U+FFFD).
|
|
12
|
+
|
|
13
|
+
With url, relative links (Node.href, Node.src) are resolved against it.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def decode_html(text: str) -> str:
|
|
17
|
+
"""Decodes the character references of a text (CR LF and a lone CR become LF)."""
|
|
18
|
+
|
|
19
|
+
def html_to_text(html: str) -> str:
|
|
20
|
+
"""The visible text of an HTML fragment: tags removed, whitespace collapsed, entities decoded."""
|
|
21
|
+
|
|
22
|
+
def html_to_text_line_breaks(html: str) -> str:
|
|
23
|
+
"""The text of an HTML fragment with the visible line structure kept."""
|
|
24
|
+
|
|
25
|
+
@final
|
|
26
|
+
class Document:
|
|
27
|
+
"""A parsed HTML or XML page: a flat list of tags in document order.
|
|
28
|
+
|
|
29
|
+
`len(doc)`, `doc[i]` (negative indexes too) and iteration give the tags, comments included.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __new__(cls, html: str | bytes, url: str | None = None) -> Document: ...
|
|
33
|
+
@property
|
|
34
|
+
def url(self) -> str:
|
|
35
|
+
"""The page URL given to parse(), or ""."""
|
|
36
|
+
@property
|
|
37
|
+
def source(self) -> str:
|
|
38
|
+
"""The page source."""
|
|
39
|
+
def __len__(self) -> int: ...
|
|
40
|
+
def __getitem__(self, index: int) -> Node: ...
|
|
41
|
+
def __iter__(self) -> Iterator[Node]: ...
|
|
42
|
+
def find(
|
|
43
|
+
self,
|
|
44
|
+
name: str | None = None,
|
|
45
|
+
*,
|
|
46
|
+
class_: str | None = None,
|
|
47
|
+
id: str | None = None,
|
|
48
|
+
attr: tuple[str, str] | None = None,
|
|
49
|
+
text: str | None = None,
|
|
50
|
+
id_starts_with: str | None = None,
|
|
51
|
+
text_starts_with: str | None = None,
|
|
52
|
+
body: str | None = None,
|
|
53
|
+
body_starts_with: str | None = None,
|
|
54
|
+
source: str | None = None,
|
|
55
|
+
) -> Node | None:
|
|
56
|
+
"""The first tag that matches: by name (any name, case-insensitive) and the criteria; or None.
|
|
57
|
+
|
|
58
|
+
class_: one of the space-separated classes (case-insensitive); attr: (name, decoded value);
|
|
59
|
+
text: the visible text; body: the inner HTML; source: the opening tag without '<' and '>'.
|
|
60
|
+
"""
|
|
61
|
+
def find_all(
|
|
62
|
+
self,
|
|
63
|
+
name: str | None = None,
|
|
64
|
+
*,
|
|
65
|
+
class_: str | None = None,
|
|
66
|
+
id: str | None = None,
|
|
67
|
+
attr: tuple[str, str] | None = None,
|
|
68
|
+
text: str | None = None,
|
|
69
|
+
id_starts_with: str | None = None,
|
|
70
|
+
text_starts_with: str | None = None,
|
|
71
|
+
body: str | None = None,
|
|
72
|
+
body_starts_with: str | None = None,
|
|
73
|
+
source: str | None = None,
|
|
74
|
+
) -> list[Node]:
|
|
75
|
+
"""All tags that match, in document order."""
|
|
76
|
+
def css(self, selector: str) -> list[Node]:
|
|
77
|
+
"""The tags that match the CSS selector (querySelectorAll). Raises CssSelectorError on an invalid one."""
|
|
78
|
+
def css_first(self, selector: str) -> Node | None:
|
|
79
|
+
"""The first tag that matches the CSS selector (querySelector), or None."""
|
|
80
|
+
def xpath(self, expression: str) -> list[Node]:
|
|
81
|
+
"""The tags the XPath 1.0 expression selects. Raises XPathError on an invalid expression or one that
|
|
82
|
+
gives a number, string or boolean (use xpath_value)."""
|
|
83
|
+
def xpath_first(self, expression: str) -> Node | None:
|
|
84
|
+
"""The first tag the XPath expression selects, or None."""
|
|
85
|
+
def xpath_values(self, expression: str) -> list[str]:
|
|
86
|
+
"""The values the XPath expression selects: decoded attributes, text(), visible text of tags, or the
|
|
87
|
+
result of count(), sum(), string(), ..."""
|
|
88
|
+
def xpath_value(self, expression: str) -> str:
|
|
89
|
+
"""The first value the XPath expression selects, or ""."""
|
|
90
|
+
|
|
91
|
+
@final
|
|
92
|
+
class Node:
|
|
93
|
+
"""One tag of a document. Positions are byte offsets into the UTF-8 source.
|
|
94
|
+
|
|
95
|
+
Nodes compare equal when they are the same tag of the same document, and are hashable.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def name(self) -> str:
|
|
100
|
+
"""The lower-case tag name ("div", "!--" for comments, "custom-tag", ...)."""
|
|
101
|
+
@property
|
|
102
|
+
def index(self) -> int:
|
|
103
|
+
"""The position of the tag in the document."""
|
|
104
|
+
@property
|
|
105
|
+
def depth(self) -> int:
|
|
106
|
+
"""The nesting depth: 0 for top-level tags."""
|
|
107
|
+
@property
|
|
108
|
+
def pos(self) -> tuple[int, int]:
|
|
109
|
+
"""The byte offsets of the opening '<' and of the last '>' in the UTF-8 source."""
|
|
110
|
+
@property
|
|
111
|
+
def text(self) -> str:
|
|
112
|
+
"""The visible text: tags, comments, script and style removed, entities decoded, whitespace collapsed."""
|
|
113
|
+
@property
|
|
114
|
+
def text_line_breaks(self) -> str:
|
|
115
|
+
"""The text with the visible line structure kept: <br> and block elements give new lines, p and
|
|
116
|
+
headings paragraphs."""
|
|
117
|
+
@property
|
|
118
|
+
def body(self) -> str:
|
|
119
|
+
"""The inner HTML (trimmed); "" for tags that are not closed."""
|
|
120
|
+
@property
|
|
121
|
+
def outer_html(self) -> str:
|
|
122
|
+
"""The source of the whole element; "" for tags that are not closed."""
|
|
123
|
+
@property
|
|
124
|
+
def source(self) -> str:
|
|
125
|
+
"""The opening tag without '<' and '>': the name and the attributes."""
|
|
126
|
+
@property
|
|
127
|
+
def attrs(self) -> dict[str, str]:
|
|
128
|
+
"""The attributes as a dict of decoded values, names in lower case (the first of a repeated name wins)."""
|
|
129
|
+
def attr(self, name: str) -> str:
|
|
130
|
+
"""The decoded value of the first attribute with the name (case-insensitive); "" when it is missing."""
|
|
131
|
+
def attr_raw(self, name: str) -> str:
|
|
132
|
+
"""The value of the attribute as written in the source."""
|
|
133
|
+
def has_class(self, class_: str) -> bool:
|
|
134
|
+
"""Whether one of the space-separated classes equals class_ (case-insensitive)."""
|
|
135
|
+
@property
|
|
136
|
+
def href(self) -> str:
|
|
137
|
+
"""The link a browser follows: href decoded and made absolute against the page URL. An empty or missing
|
|
138
|
+
href gives the page URL."""
|
|
139
|
+
@property
|
|
140
|
+
def src(self) -> str:
|
|
141
|
+
"""src decoded and made absolute against the page URL."""
|
|
142
|
+
def href_from(self, base_url: str) -> str:
|
|
143
|
+
"""href resolved against base_url instead of the page URL."""
|
|
144
|
+
def src_from(self, base_url: str) -> str:
|
|
145
|
+
"""src resolved against base_url instead of the page URL."""
|
|
146
|
+
@property
|
|
147
|
+
def mailto(self) -> str:
|
|
148
|
+
"""The address of a mailto: link, or ""."""
|
|
149
|
+
@property
|
|
150
|
+
def tel(self) -> str:
|
|
151
|
+
"""The number of a tel: link, or ""."""
|
|
152
|
+
@property
|
|
153
|
+
def text_after(self) -> str:
|
|
154
|
+
"""The source text right after the tag up to the next '<'."""
|
|
155
|
+
@property
|
|
156
|
+
def parent(self) -> Node | None:
|
|
157
|
+
"""The enclosing tag, or None."""
|
|
158
|
+
@property
|
|
159
|
+
def children(self) -> list[Node]:
|
|
160
|
+
"""The direct children."""
|
|
161
|
+
@property
|
|
162
|
+
def has_children(self) -> bool:
|
|
163
|
+
"""Whether another tag starts inside this one."""
|
|
164
|
+
@property
|
|
165
|
+
def next(self) -> Node | None:
|
|
166
|
+
"""The tag that follows in document order (the first child, if there is one), or None."""
|
|
167
|
+
@property
|
|
168
|
+
def previous(self) -> Node | None:
|
|
169
|
+
"""The tag before this one in document order, or None."""
|
|
170
|
+
@property
|
|
171
|
+
def next_sibling(self) -> Node | None:
|
|
172
|
+
"""The next tag at the same level inside the same parent, or None."""
|
|
173
|
+
@property
|
|
174
|
+
def previous_sibling(self) -> Node | None:
|
|
175
|
+
"""The previous tag at the same level inside the same parent, or None."""
|
|
176
|
+
def find(
|
|
177
|
+
self,
|
|
178
|
+
name: str | None = None,
|
|
179
|
+
*,
|
|
180
|
+
class_: str | None = None,
|
|
181
|
+
id: str | None = None,
|
|
182
|
+
attr: tuple[str, str] | None = None,
|
|
183
|
+
text: str | None = None,
|
|
184
|
+
id_starts_with: str | None = None,
|
|
185
|
+
text_starts_with: str | None = None,
|
|
186
|
+
body: str | None = None,
|
|
187
|
+
body_starts_with: str | None = None,
|
|
188
|
+
source: str | None = None,
|
|
189
|
+
) -> Node | None:
|
|
190
|
+
"""The first tag inside this one that matches, or None."""
|
|
191
|
+
def find_all(
|
|
192
|
+
self,
|
|
193
|
+
name: str | None = None,
|
|
194
|
+
*,
|
|
195
|
+
class_: str | None = None,
|
|
196
|
+
id: str | None = None,
|
|
197
|
+
attr: tuple[str, str] | None = None,
|
|
198
|
+
text: str | None = None,
|
|
199
|
+
id_starts_with: str | None = None,
|
|
200
|
+
text_starts_with: str | None = None,
|
|
201
|
+
body: str | None = None,
|
|
202
|
+
body_starts_with: str | None = None,
|
|
203
|
+
source: str | None = None,
|
|
204
|
+
) -> list[Node]:
|
|
205
|
+
"""All tags inside this one that match."""
|
|
206
|
+
def matches(
|
|
207
|
+
self,
|
|
208
|
+
name: str | None = None,
|
|
209
|
+
*,
|
|
210
|
+
class_: str | None = None,
|
|
211
|
+
id: str | None = None,
|
|
212
|
+
attr: tuple[str, str] | None = None,
|
|
213
|
+
text: str | None = None,
|
|
214
|
+
id_starts_with: str | None = None,
|
|
215
|
+
text_starts_with: str | None = None,
|
|
216
|
+
body: str | None = None,
|
|
217
|
+
body_starts_with: str | None = None,
|
|
218
|
+
source: str | None = None,
|
|
219
|
+
) -> bool:
|
|
220
|
+
"""Whether this tag matches."""
|
|
221
|
+
def find_parent(
|
|
222
|
+
self,
|
|
223
|
+
name: str | None = None,
|
|
224
|
+
*,
|
|
225
|
+
class_: str | None = None,
|
|
226
|
+
id: str | None = None,
|
|
227
|
+
attr: tuple[str, str] | None = None,
|
|
228
|
+
text: str | None = None,
|
|
229
|
+
id_starts_with: str | None = None,
|
|
230
|
+
text_starts_with: str | None = None,
|
|
231
|
+
body: str | None = None,
|
|
232
|
+
body_starts_with: str | None = None,
|
|
233
|
+
source: str | None = None,
|
|
234
|
+
) -> Node | None:
|
|
235
|
+
"""The nearest enclosing tag that matches, or None."""
|
|
236
|
+
def find_next(
|
|
237
|
+
self,
|
|
238
|
+
name: str | None = None,
|
|
239
|
+
*,
|
|
240
|
+
class_: str | None = None,
|
|
241
|
+
id: str | None = None,
|
|
242
|
+
attr: tuple[str, str] | None = None,
|
|
243
|
+
text: str | None = None,
|
|
244
|
+
id_starts_with: str | None = None,
|
|
245
|
+
text_starts_with: str | None = None,
|
|
246
|
+
body: str | None = None,
|
|
247
|
+
body_starts_with: str | None = None,
|
|
248
|
+
source: str | None = None,
|
|
249
|
+
) -> Node | None:
|
|
250
|
+
"""The first tag after the end of this one that matches (tags inside it are skipped), or None."""
|
|
251
|
+
def find_next_sibling(
|
|
252
|
+
self,
|
|
253
|
+
name: str | None = None,
|
|
254
|
+
*,
|
|
255
|
+
class_: str | None = None,
|
|
256
|
+
id: str | None = None,
|
|
257
|
+
attr: tuple[str, str] | None = None,
|
|
258
|
+
text: str | None = None,
|
|
259
|
+
id_starts_with: str | None = None,
|
|
260
|
+
text_starts_with: str | None = None,
|
|
261
|
+
body: str | None = None,
|
|
262
|
+
body_starts_with: str | None = None,
|
|
263
|
+
source: str | None = None,
|
|
264
|
+
) -> Node | None:
|
|
265
|
+
"""The next sibling that matches, or None."""
|
|
266
|
+
def find_previous_sibling(
|
|
267
|
+
self,
|
|
268
|
+
name: str | None = None,
|
|
269
|
+
*,
|
|
270
|
+
class_: str | None = None,
|
|
271
|
+
id: str | None = None,
|
|
272
|
+
attr: tuple[str, str] | None = None,
|
|
273
|
+
text: str | None = None,
|
|
274
|
+
id_starts_with: str | None = None,
|
|
275
|
+
text_starts_with: str | None = None,
|
|
276
|
+
body: str | None = None,
|
|
277
|
+
body_starts_with: str | None = None,
|
|
278
|
+
source: str | None = None,
|
|
279
|
+
) -> Node | None:
|
|
280
|
+
"""The nearest previous sibling that matches, or None."""
|
|
281
|
+
def text_after_until(
|
|
282
|
+
self,
|
|
283
|
+
name: str | None = None,
|
|
284
|
+
*,
|
|
285
|
+
class_: str | None = None,
|
|
286
|
+
id: str | None = None,
|
|
287
|
+
attr: tuple[str, str] | None = None,
|
|
288
|
+
text: str | None = None,
|
|
289
|
+
id_starts_with: str | None = None,
|
|
290
|
+
text_starts_with: str | None = None,
|
|
291
|
+
body: str | None = None,
|
|
292
|
+
body_starts_with: str | None = None,
|
|
293
|
+
source: str | None = None,
|
|
294
|
+
) -> str:
|
|
295
|
+
"""The source between the end of this tag and the next tag that matches; "" when there is none."""
|
|
296
|
+
def css(self, selector: str) -> list[Node]:
|
|
297
|
+
"""The tags inside this one that match the CSS selector; it may start with '>' for the children."""
|
|
298
|
+
def css_first(self, selector: str) -> Node | None:
|
|
299
|
+
"""The first tag inside this one that matches the CSS selector, or None."""
|
|
300
|
+
def css_matches(self, selector: str) -> bool:
|
|
301
|
+
"""Whether this tag matches the CSS selector (element.matches())."""
|
|
302
|
+
def xpath(self, expression: str) -> list[Node]:
|
|
303
|
+
"""The tags the XPath expression selects, relative paths starting at this tag."""
|
|
304
|
+
def xpath_first(self, expression: str) -> Node | None:
|
|
305
|
+
"""The first tag the XPath expression selects from this tag, or None."""
|
|
306
|
+
def xpath_values(self, expression: str) -> list[str]:
|
|
307
|
+
"""The values the XPath expression selects from this tag."""
|
|
308
|
+
def xpath_value(self, expression: str) -> str:
|
|
309
|
+
"""The first value the XPath expression selects from this tag, or ""."""
|
|
310
|
+
def __eq__(self, other: object) -> bool: ...
|
|
311
|
+
def __hash__(self) -> int: ...
|
|
312
|
+
|
|
313
|
+
@final
|
|
314
|
+
class CssQuery:
|
|
315
|
+
"""A compiled CSS selector: compile once, use on any number of pages and threads."""
|
|
316
|
+
|
|
317
|
+
def __new__(cls, selector: str) -> CssQuery: ...
|
|
318
|
+
@property
|
|
319
|
+
def selector(self) -> str: ...
|
|
320
|
+
def all(self, target: Document | Node, /) -> list[Node]:
|
|
321
|
+
"""The matching tags in a document, or inside a node."""
|
|
322
|
+
def first(self, target: Document | Node, /) -> Node | None:
|
|
323
|
+
"""The first matching tag in a document, or inside a node, or None."""
|
|
324
|
+
def matches(self, node: Node, /) -> bool:
|
|
325
|
+
"""Whether the tag matches (element.matches())."""
|
|
326
|
+
|
|
327
|
+
@final
|
|
328
|
+
class XPathQuery:
|
|
329
|
+
"""A compiled XPath 1.0 expression: compile once, use on any number of pages and threads."""
|
|
330
|
+
|
|
331
|
+
def __new__(cls, expression: str) -> XPathQuery: ...
|
|
332
|
+
@property
|
|
333
|
+
def expression(self) -> str: ...
|
|
334
|
+
@property
|
|
335
|
+
def is_node_set(self) -> bool:
|
|
336
|
+
"""Whether the expression gives tags (a node-set), not a number, string or boolean."""
|
|
337
|
+
def nodes(self, target: Document | Node, /) -> list[Node]:
|
|
338
|
+
"""The selected tags, evaluated on a document or from a node."""
|
|
339
|
+
def values(self, target: Document | Node, /) -> list[str]:
|
|
340
|
+
"""The values of the selected nodes, or the result of the expression as one value."""
|
|
341
|
+
def value(self, target: Document | Node, /) -> str | None:
|
|
342
|
+
"""The value of the first selected node, or the result of the expression; None when it selects nothing."""
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""Attributes are read from the source of the opening tag when they are asked for. The functions take the document
|
|
2
|
+
and the index of the tag."""
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
from . import _decode as dec
|
|
7
|
+
from ._chars import ASCII_SPACE, ascii_lower, equal_fold
|
|
8
|
+
from ._parser import COMMENT, NAMES
|
|
9
|
+
|
|
10
|
+
# skip '<' and the name; name chars end at whitespace, '/' or '>'
|
|
11
|
+
NAME_END = re.compile(r"[^\t-\r />]*")
|
|
12
|
+
# between attributes: whitespace and '/'
|
|
13
|
+
GAP = re.compile(r"[\t-\r /]*")
|
|
14
|
+
# an attribute name ends at whitespace, '=', '/' or '>'
|
|
15
|
+
ATTR_NAME = re.compile(r"[^\t-\r =/>]*")
|
|
16
|
+
|
|
17
|
+
# starts a name: after whitespace, a quote, '/' or '='
|
|
18
|
+
NAME_BEFORE = frozenset(" \t\n\x0b\x0c\r\"'/=")
|
|
19
|
+
# ends a name: at whitespace, '=', '/' or '>'
|
|
20
|
+
NAME_AFTER = frozenset(" \t\n\x0b\x0c\r=/>")
|
|
21
|
+
NAME_SIZES = tuple(len(n) for n in NAMES)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def open_tag_end(d, i):
|
|
25
|
+
"""The position of the '>' that ends the opening tag."""
|
|
26
|
+
inner_start = d.inner_starts[i]
|
|
27
|
+
end = inner_start - 1 if inner_start > 0 and d.inner_ends[i] > 0 else d.ends[i]
|
|
28
|
+
n = len(d.src)
|
|
29
|
+
return end if end < n else n
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def lower_tag(d, i):
|
|
33
|
+
"""The opening tag ("<name attributes", without the closing '>') in lower case (ASCII), read once."""
|
|
34
|
+
cache = d.lower_tags
|
|
35
|
+
t = cache.get(i)
|
|
36
|
+
if t is None:
|
|
37
|
+
t = ascii_lower(d.src[d.starts[i]:open_tag_end(d, i)])
|
|
38
|
+
cache[i] = t
|
|
39
|
+
return t
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def read_value(s, pos, end):
|
|
43
|
+
"""Reads the value that follows an attribute name at pos (before end): (value start (-1: no '='), length,
|
|
44
|
+
the position after the value)."""
|
|
45
|
+
p = pos
|
|
46
|
+
while p < end and s[p] in ASCII_SPACE:
|
|
47
|
+
p += 1
|
|
48
|
+
if p >= end or s[p] != "=":
|
|
49
|
+
return -1, 0, pos
|
|
50
|
+
p += 1
|
|
51
|
+
while p < end and s[p] in ASCII_SPACE:
|
|
52
|
+
p += 1
|
|
53
|
+
q = s[p] if p < end else ""
|
|
54
|
+
if q == '"' or q == "'":
|
|
55
|
+
start = p + 1
|
|
56
|
+
close = s.find(q, start) if start < end else -1
|
|
57
|
+
if close < 0 or close >= end:
|
|
58
|
+
return start, (end - start if end - start > 0 else 0), end
|
|
59
|
+
return start, close - start, close + 1
|
|
60
|
+
start = p
|
|
61
|
+
while p < end and s[p] not in ASCII_SPACE:
|
|
62
|
+
p += 1
|
|
63
|
+
return start, p - start, p
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class AttrScanner:
|
|
67
|
+
"""Reads the attributes of an opening tag one by one: positions in the source."""
|
|
68
|
+
|
|
69
|
+
__slots__ = ("src", "end", "pos", "name_start", "name_length", "value_start", "value_length")
|
|
70
|
+
|
|
71
|
+
def __init__(self, d, i):
|
|
72
|
+
self.src = d.src
|
|
73
|
+
self.end = open_tag_end(d, i)
|
|
74
|
+
p = d.starts[i] + 1
|
|
75
|
+
if p < self.end:
|
|
76
|
+
p = NAME_END.match(self.src, p).end()
|
|
77
|
+
if p > self.end:
|
|
78
|
+
p = self.end
|
|
79
|
+
self.pos = p
|
|
80
|
+
self.name_start = 0
|
|
81
|
+
self.name_length = 0
|
|
82
|
+
self.value_start = -1 # -1: no value
|
|
83
|
+
self.value_length = 0
|
|
84
|
+
|
|
85
|
+
def next(self):
|
|
86
|
+
"""Moves to the next attribute; False at the end of the tag."""
|
|
87
|
+
s = self.src
|
|
88
|
+
e = self.end
|
|
89
|
+
while True:
|
|
90
|
+
if self.pos < e:
|
|
91
|
+
p = GAP.match(s, self.pos).end()
|
|
92
|
+
self.pos = p if p < e else e
|
|
93
|
+
if self.pos >= e:
|
|
94
|
+
return False
|
|
95
|
+
self.name_start = self.pos
|
|
96
|
+
p = ATTR_NAME.match(s, self.pos).end()
|
|
97
|
+
self.pos = p if p < e else e
|
|
98
|
+
self.name_length = self.pos - self.name_start
|
|
99
|
+
if self.name_length == 0:
|
|
100
|
+
self.pos += 1 # a stray '=': skip it
|
|
101
|
+
continue
|
|
102
|
+
self.value_start, self.value_length, self.pos = read_value(s, self.pos, e)
|
|
103
|
+
return True
|
|
104
|
+
|
|
105
|
+
def name(self):
|
|
106
|
+
return self.src[self.name_start:self.name_start + self.name_length]
|
|
107
|
+
|
|
108
|
+
def raw_value(self):
|
|
109
|
+
if self.value_start < 0:
|
|
110
|
+
return ""
|
|
111
|
+
return self.src[self.value_start:self.value_start + self.value_length]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def attr_value(d, i, name):
|
|
115
|
+
"""The decoded value of the first attribute with the name (lower case); "" when missing or without a value."""
|
|
116
|
+
raw = attr_raw(d, i, name)
|
|
117
|
+
return dec.decode(raw, True) if "&" in raw or "\r" in raw else raw
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def has_class(d, i, cls):
|
|
121
|
+
"""Whether one of the space-separated classes equals cls (case-insensitive)."""
|
|
122
|
+
if not cls:
|
|
123
|
+
return False
|
|
124
|
+
value = attr_raw(d, i, "class")
|
|
125
|
+
if "&" in value or "\r" in value:
|
|
126
|
+
value = dec.decode(value, True)
|
|
127
|
+
vl = len(value)
|
|
128
|
+
cl = len(cls)
|
|
129
|
+
if vl == cl:
|
|
130
|
+
return equal_fold(value, cls)
|
|
131
|
+
if cl > vl:
|
|
132
|
+
return False
|
|
133
|
+
lv = ascii_lower(value)
|
|
134
|
+
lc = ascii_lower(cls)
|
|
135
|
+
start = 0
|
|
136
|
+
while start <= vl - cl:
|
|
137
|
+
k = lv.find(lc, start)
|
|
138
|
+
if k < 0:
|
|
139
|
+
return False
|
|
140
|
+
after = k + cl
|
|
141
|
+
if (k == 0 or lv[k - 1] in ASCII_SPACE) and (after == vl or lv[after] in ASCII_SPACE):
|
|
142
|
+
return True
|
|
143
|
+
start = k + 1
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def attr_raw(d, i, attribute):
|
|
148
|
+
"""The raw value of the first attribute with the name (lower case), as a browser reads the tag: quoted or
|
|
149
|
+
unquoted (href=/p?a=1), "" for an attribute without a value or a missing one."""
|
|
150
|
+
# a comment has no attributes, whatever its text looks like
|
|
151
|
+
key = d.keys[i]
|
|
152
|
+
if not attribute or key == COMMENT:
|
|
153
|
+
return ""
|
|
154
|
+
|
|
155
|
+
# the opening tag: "<name attributes", without the closing '>', in lower case
|
|
156
|
+
tag = lower_tag(d, i)
|
|
157
|
+
length = len(tag)
|
|
158
|
+
|
|
159
|
+
# The first place where the name stands as a word of its own. Most tags have none.
|
|
160
|
+
alen = len(attribute)
|
|
161
|
+
p = tag.find(attribute, 1)
|
|
162
|
+
while p >= 0:
|
|
163
|
+
after = p + alen
|
|
164
|
+
if tag[p - 1] in NAME_BEFORE and (after == length or tag[after] in NAME_AFTER):
|
|
165
|
+
break
|
|
166
|
+
p = tag.find(attribute, p + 1) if p + 1 < length else -1
|
|
167
|
+
if p < 0:
|
|
168
|
+
return ""
|
|
169
|
+
|
|
170
|
+
src = d.src
|
|
171
|
+
start = d.starts[i]
|
|
172
|
+
# That word is the attribute unless it is inside the value of another one (title="see id=5", x= id).
|
|
173
|
+
# Nearly always that is ruled out at once, and the value is read in place.
|
|
174
|
+
if (key >= 0 and p == NAME_SIZES[key] + 2) or _name_for_sure(tag, p):
|
|
175
|
+
q = p + alen
|
|
176
|
+
while q < length and tag[q] in ASCII_SPACE:
|
|
177
|
+
q += 1
|
|
178
|
+
if q >= length or tag[q] != "=":
|
|
179
|
+
return "" # no value
|
|
180
|
+
q += 1
|
|
181
|
+
while q < length and tag[q] in ASCII_SPACE:
|
|
182
|
+
q += 1
|
|
183
|
+
if q >= length:
|
|
184
|
+
return ""
|
|
185
|
+
c = tag[q]
|
|
186
|
+
if c == '"' or c == "'":
|
|
187
|
+
close = tag.find(c, q + 1)
|
|
188
|
+
if close < 0:
|
|
189
|
+
return src[start + q + 1:start + length]
|
|
190
|
+
return src[start + q + 1:start + close]
|
|
191
|
+
v = q
|
|
192
|
+
while v < length and tag[v] not in ASCII_SPACE:
|
|
193
|
+
v += 1
|
|
194
|
+
return src[start + q:start + v]
|
|
195
|
+
pos = start + p
|
|
196
|
+
|
|
197
|
+
# Otherwise the attributes are read one by one, up to the one that starts at that word.
|
|
198
|
+
s = AttrScanner(d, i)
|
|
199
|
+
while s.next():
|
|
200
|
+
# a word the scanner has passed was inside a value: the next one takes its place
|
|
201
|
+
while pos < s.name_start:
|
|
202
|
+
pos = _next_word(tag, start, attribute, pos + 1)
|
|
203
|
+
if pos < 0:
|
|
204
|
+
return ""
|
|
205
|
+
if pos == s.name_start and s.name_length == alen:
|
|
206
|
+
return "" if s.value_start < 0 else src[s.value_start:s.value_start + s.value_length]
|
|
207
|
+
return ""
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _next_word(tag, start, attribute, at):
|
|
211
|
+
"""The next place in the opening tag (tag, lower case, at start in the source) at or after the source position
|
|
212
|
+
at where the name stands as a word of its own, or -1."""
|
|
213
|
+
n = len(attribute)
|
|
214
|
+
length = len(tag)
|
|
215
|
+
i = at - start
|
|
216
|
+
while i < length:
|
|
217
|
+
found = tag.find(attribute, i)
|
|
218
|
+
if found < 0:
|
|
219
|
+
break
|
|
220
|
+
after = found + n
|
|
221
|
+
if tag[found - 1] in NAME_BEFORE and (after == length or tag[after] in NAME_AFTER):
|
|
222
|
+
return start + found
|
|
223
|
+
i = found + 1
|
|
224
|
+
return -1
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _name_for_sure(tag, pos):
|
|
228
|
+
"""Whether the word at pos of the opening tag is an attribute name, judged by the chars before it alone. False
|
|
229
|
+
means "not sure"."""
|
|
230
|
+
# After whitespace, and not as the unquoted value in "x= word".
|
|
231
|
+
if tag[pos - 1] not in ASCII_SPACE:
|
|
232
|
+
return False
|
|
233
|
+
e = pos
|
|
234
|
+
while e > 0 and tag[e - 1] in ASCII_SPACE:
|
|
235
|
+
e -= 1
|
|
236
|
+
if tag[e - 1] == "=":
|
|
237
|
+
return False
|
|
238
|
+
# Not inside a quoted value: it is enough that the last quote of each kind before the word does not open a value.
|
|
239
|
+
for quote in ('"', "'"):
|
|
240
|
+
k = tag.rfind(quote, 0, e)
|
|
241
|
+
if k < 0:
|
|
242
|
+
continue
|
|
243
|
+
j = k
|
|
244
|
+
while j > 0 and tag[j - 1] in ASCII_SPACE:
|
|
245
|
+
j -= 1
|
|
246
|
+
if j > 0 and tag[j - 1] == "=":
|
|
247
|
+
return False
|
|
248
|
+
return True
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def attr_equals(raw, value):
|
|
252
|
+
"""Compares the decoded value; decodes only when the raw value has '&' or CR."""
|
|
253
|
+
return (dec.decode(raw, True) if "&" in raw or "\r" in raw else raw) == value
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def attr_starts_with(raw, prefix):
|
|
257
|
+
return (dec.decode(raw, True) if "&" in raw or "\r" in raw else raw).startswith(prefix)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def has_attribute(d, i, name):
|
|
261
|
+
"""Whether the element has the attribute (lower case), with or without a value."""
|
|
262
|
+
if attr_raw(d, i, name):
|
|
263
|
+
return True
|
|
264
|
+
if d.keys[i] == COMMENT:
|
|
265
|
+
return False
|
|
266
|
+
s = AttrScanner(d, i)
|
|
267
|
+
while s.next():
|
|
268
|
+
if equal_fold(s.name(), name):
|
|
269
|
+
return True
|
|
270
|
+
return False
|