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