selectolax 0.3.34__cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.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.

Potentially problematic release.


This version of selectolax might be problematic. Click here for more details.

@@ -0,0 +1,193 @@
1
+ cimport cython
2
+ from cpython.exc cimport PyErr_SetObject
3
+ from cpython.list cimport PyList_GET_SIZE
4
+
5
+
6
+ @cython.final
7
+ cdef class LexborCSSSelector:
8
+
9
+ def __init__(self):
10
+ self._create_css_parser()
11
+ self.results = []
12
+ self.current_node = None
13
+
14
+ cdef int _create_css_parser(self) except -1:
15
+ cdef lxb_status_t status
16
+
17
+ self.parser = lxb_css_parser_create()
18
+ status = lxb_css_parser_init(self.parser, NULL)
19
+
20
+ if status != LXB_STATUS_OK:
21
+ PyErr_SetObject(SelectolaxError, "Can't initialize CSS parser.")
22
+ return -1
23
+
24
+ self.css_selectors = lxb_css_selectors_create()
25
+ status = lxb_css_selectors_init(self.css_selectors)
26
+
27
+ if status != LXB_STATUS_OK:
28
+ PyErr_SetObject(SelectolaxError, "Can't initialize CSS selector.")
29
+ return -1
30
+
31
+ lxb_css_parser_selectors_set(self.parser, self.css_selectors)
32
+
33
+ self.selectors = lxb_selectors_create()
34
+ status = lxb_selectors_init(self.selectors)
35
+ lxb_selectors_opt_set(self.selectors, LXB_SELECTORS_OPT_MATCH_ROOT)
36
+ if status != LXB_STATUS_OK:
37
+ PyErr_SetObject(SelectolaxError, "Can't initialize CSS selector.")
38
+ return -1
39
+ return 0
40
+
41
+ cpdef list find(self, str query, LexborNode node):
42
+ cdef lxb_css_selector_list_t* selectors
43
+ cdef lxb_char_t* c_selector
44
+ cdef lxb_css_selector_list_t * selectors_list
45
+
46
+ if not isinstance(query, str):
47
+ raise TypeError("Query must be a string.")
48
+
49
+ bytes_query = query.encode(_ENCODING)
50
+ selectors_list = lxb_css_selectors_parse(self.parser, <lxb_char_t *> bytes_query, <size_t>len(query))
51
+
52
+ if selectors_list == NULL:
53
+ raise SelectolaxError("Can't parse CSS selector.")
54
+
55
+ self.current_node = node
56
+ self.results = []
57
+ status = lxb_selectors_find(self.selectors, node.node, selectors_list,
58
+ <lxb_selectors_cb_f>css_finder_callback, <void*>self)
59
+ results = list(self.results)
60
+ self.results = []
61
+ self.current_node = None
62
+ lxb_css_selector_list_destroy_memory(selectors_list)
63
+ return results
64
+
65
+ cpdef int any_matches(self, str query, LexborNode node) except -1:
66
+ cdef lxb_css_selector_list_t * selectors
67
+ cdef lxb_char_t * c_selector
68
+ cdef lxb_css_selector_list_t * selectors_list
69
+ cdef int result
70
+
71
+ if not isinstance(query, str):
72
+ raise TypeError("Query must be a string.")
73
+
74
+ bytes_query = query.encode(_ENCODING)
75
+ selectors_list = lxb_css_selectors_parse(self.parser, <lxb_char_t *> bytes_query, <size_t> len(query))
76
+
77
+ if selectors_list == NULL:
78
+ PyErr_SetObject(SelectolaxError, "Can't parse CSS selector.")
79
+
80
+ self.results = []
81
+ status = lxb_selectors_find(self.selectors, node.node, selectors_list,
82
+ <lxb_selectors_cb_f> css_matcher_callback, <void *> self)
83
+ if status != LXB_STATUS_OK:
84
+ lxb_css_selector_list_destroy_memory(selectors_list)
85
+ PyErr_SetObject(SelectolaxError, "Can't parse CSS selector.")
86
+ result = PyList_GET_SIZE(self.results) > 0
87
+ self.results = []
88
+ lxb_css_selector_list_destroy_memory(selectors_list)
89
+ return result
90
+
91
+ def __dealloc__(self):
92
+ if self.selectors != NULL:
93
+ lxb_selectors_destroy(self.selectors, True)
94
+ if self.parser != NULL:
95
+ lxb_css_parser_destroy(self.parser, True)
96
+ if self.css_selectors != NULL:
97
+ lxb_css_selectors_destroy(self.css_selectors, True)
98
+
99
+
100
+ cdef class LexborSelector:
101
+ """An advanced CSS selector that supports additional operations.
102
+
103
+ Think of it as a toolkit that mimics some of the features of XPath.
104
+
105
+ Please note, this is an experimental feature that can change in the future.
106
+ """
107
+ cdef LexborNode node
108
+ cdef list nodes
109
+
110
+ def __init__(self, LexborNode node, query):
111
+ self.node = node
112
+ self.nodes = self.node.parser.selector.find(query, self.node) if query else [node, ]
113
+
114
+ cpdef css(self, str query):
115
+ """Evaluate CSS selector against current scope."""
116
+ raise NotImplementedError("This features is not supported by the lexbor backend. Please use Modest backend.")
117
+
118
+ @property
119
+ def matches(self) -> list:
120
+ """Returns all possible matches"""
121
+ return self.nodes
122
+
123
+ @property
124
+ def any_matches(self) -> bool:
125
+ """Returns True if there are any matches"""
126
+ return bool(self.nodes)
127
+
128
+ def text_contains(self, str text, bool deep=True, str separator='', bool strip=False) -> LexborSelector:
129
+ """Filter all current matches given text."""
130
+ cdef list nodes = []
131
+ for node in self.nodes:
132
+ node_text = node.text(deep=deep, separator=separator, strip=strip)
133
+ if node_text and text in node_text:
134
+ nodes.append(node)
135
+ self.nodes = nodes
136
+ return self
137
+
138
+ def any_text_contains(self, str text, bool deep=True, str separator='', bool strip=False) -> bool:
139
+ """Returns True if any node in the current search scope contains specified text"""
140
+ cdef LexborNode node
141
+ for node in self.nodes:
142
+ node_text = node.text(deep=deep, separator=separator, strip=strip)
143
+ if node_text and text in node_text:
144
+ return True
145
+ return False
146
+
147
+ def attribute_longer_than(self, str attribute, int length, str start = None) -> LexborSelector:
148
+ """Filter all current matches by attribute length.
149
+
150
+ Similar to `string-length` in XPath.
151
+ """
152
+ cdef list nodes = []
153
+ for node in self.nodes:
154
+ attr = node.attributes.get(attribute)
155
+ if attr and start and start in attr:
156
+ attr = attr[attr.find(start) + len(start):]
157
+ if len(attr) > length:
158
+ nodes.append(node)
159
+ self.nodes = nodes
160
+ return self
161
+
162
+ def any_attribute_longer_than(self, str attribute, int length, str start = None) -> bool:
163
+ """Returns True any href attribute longer than a specified length.
164
+
165
+ Similar to `string-length` in XPath.
166
+ """
167
+ cdef LexborNode node
168
+ for node in self.nodes:
169
+ attr = node.attributes.get(attribute)
170
+ if attr and start and start in attr:
171
+ attr = attr[attr.find(start) + len(start):]
172
+ if len(attr) > length:
173
+ return True
174
+ return False
175
+
176
+ def __bool__(self):
177
+ return bool(self.nodes)
178
+
179
+
180
+ cdef lxb_status_t css_finder_callback(lxb_dom_node_t *node, lxb_css_selector_specificity_t *spec, void *ctx):
181
+ cdef LexborNode lxb_node
182
+ cdef LexborCSSSelector cls
183
+ cls = <LexborCSSSelector> ctx
184
+ lxb_node = LexborNode.new(<lxb_dom_node_t *> node, cls.current_node.parser)
185
+ cls.results.append(lxb_node)
186
+ return LXB_STATUS_OK
187
+
188
+ cdef lxb_status_t css_matcher_callback(lxb_dom_node_t *node, lxb_css_selector_specificity_t *spec, void *ctx):
189
+ cdef LexborNode lxb_node
190
+ cdef LexborCSSSelector cls
191
+ cls = <LexborCSSSelector> ctx
192
+ cls.results.append(True)
193
+ return LXB_STATUS_STOP
@@ -0,0 +1,20 @@
1
+ include "../utils.pxi"
2
+
3
+
4
+ def create_tag(tag: str):
5
+ """
6
+ Given an HTML tag name, e.g. `"div"`, create a single empty node for that tag,
7
+ e.g. `"<div></div>"`.
8
+ """
9
+ return do_create_tag(tag, LexborHTMLParser)
10
+
11
+
12
+ def parse_fragment(html: str):
13
+ """
14
+ Given HTML, parse it into a list of Nodes, such that the nodes
15
+ correspond to the given HTML.
16
+
17
+ For contrast, HTMLParser adds `<html>`, `<head>`, and `<body>` tags
18
+ if they are missing. This function does not add these tags.
19
+ """
20
+ return do_parse_fragment(html, LexborHTMLParser)