selectolax 0.3.28__cp38-cp38-musllinux_1_2_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,184 @@
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
+ bytes_query = query.encode(_ENCODING)
42
+ selectors_list = lxb_css_selectors_parse(self.parser, <lxb_char_t *> bytes_query, <size_t>len(query))
43
+
44
+ if selectors_list == NULL:
45
+ raise SelectolaxError("Can't parse CSS selector.")
46
+
47
+ self.current_node = node
48
+ self.results = []
49
+ status = lxb_selectors_find(self.selectors, node.node, selectors_list,
50
+ <lxb_selectors_cb_f>css_finder_callback, <void*>self)
51
+ results = list(self.results)
52
+ self.results = []
53
+ self.current_node = None
54
+ lxb_css_selector_list_destroy_memory(selectors_list)
55
+ return results
56
+
57
+ cpdef any_matches(self, str query, LexborNode node):
58
+ cdef lxb_css_selector_list_t * selectors
59
+ cdef lxb_char_t * c_selector
60
+ cdef lxb_css_selector_list_t * selectors_list
61
+
62
+ bytes_query = query.encode(_ENCODING)
63
+ selectors_list = lxb_css_selectors_parse(self.parser, <lxb_char_t *> bytes_query, <size_t> len(query))
64
+
65
+ if selectors_list == NULL:
66
+ raise SelectolaxError("Can't parse CSS selector.")
67
+
68
+ self.results = []
69
+ status = lxb_selectors_find(self.selectors, node.node, selectors_list,
70
+ <lxb_selectors_cb_f> css_matcher_callback, <void *> self)
71
+ if status != LXB_STATUS_OK:
72
+ raise SelectolaxError("Can't parse CSS selector.")
73
+ result = bool(self.results)
74
+ self.results = []
75
+ lxb_css_selector_list_destroy_memory(selectors_list)
76
+ return result
77
+
78
+
79
+ def __dealloc__(self):
80
+ if self.selectors != NULL:
81
+ lxb_selectors_destroy(self.selectors, True)
82
+ if self.parser != NULL:
83
+ lxb_css_parser_destroy(self.parser, True)
84
+ if self.css_selectors != NULL:
85
+ lxb_css_selectors_destroy(self.css_selectors, True)
86
+
87
+
88
+
89
+ cdef class LexborSelector:
90
+ """An advanced CSS selector that supports additional operations.
91
+
92
+ Think of it as a toolkit that mimics some of the features of XPath.
93
+
94
+ Please note, this is an experimental feature that can change in the future.
95
+ """
96
+ cdef LexborNode node
97
+ cdef list nodes
98
+
99
+ def __init__(self, LexborNode node, query):
100
+ self.node = node
101
+ self.nodes = self.node.parser.selector.find(query, self.node) if query else [node, ]
102
+
103
+
104
+ cpdef css(self, str query):
105
+ """Evaluate CSS selector against current scope."""
106
+ raise SelectolaxError("This features is not supported by the lexbor backend. Please use Modest backend.")
107
+
108
+ @property
109
+ def matches(self) -> list:
110
+ """Returns all possible matches"""
111
+ return self.nodes
112
+
113
+ @property
114
+ def any_matches(self) -> bool:
115
+ """Returns True if there are any matches"""
116
+ return bool(self.nodes)
117
+
118
+ def text_contains(self, str text, bool deep=True, str separator='', bool strip=False) -> LexborSelector:
119
+ """Filter all current matches given text."""
120
+ nodes = []
121
+ for node in self.nodes:
122
+ node_text = node.text(deep=deep, separator=separator, strip=strip)
123
+ if node_text and text in node_text:
124
+ nodes.append(node)
125
+ self.nodes = nodes
126
+ return self
127
+
128
+ def any_text_contains(self, str text, bool deep=True, str separator='', bool strip=False) -> bool:
129
+ """Returns True if any node in the current search scope contains specified text"""
130
+ 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
+ return True
135
+ return False
136
+
137
+ def attribute_longer_than(self, str attribute, int length, str start = None) -> LexborSelector:
138
+ """Filter all current matches by attribute length.
139
+
140
+ Similar to `string-length` in XPath.
141
+ """
142
+ nodes = []
143
+ for node in self.nodes:
144
+ attr = node.attributes.get(attribute)
145
+ if attr and start and start in attr:
146
+ attr = attr[attr.find(start) + len(start):]
147
+ if len(attr) > length:
148
+ nodes.append(node)
149
+ self.nodes = nodes
150
+ return self
151
+
152
+ def any_attribute_longer_than(self, str attribute, int length, str start = None) -> bool:
153
+ """Returns True any href attribute longer than a specified length.
154
+
155
+ Similar to `string-length` in XPath.
156
+ """
157
+ nodes = []
158
+ for node in self.nodes:
159
+ attr = node.attributes.get(attribute)
160
+ if attr and start and start in attr:
161
+ attr = attr[attr.find(start) + len(start):]
162
+ if len(attr) > length:
163
+ return True
164
+ return False
165
+
166
+ def __bool__(self):
167
+ return bool(self.nodes)
168
+
169
+
170
+ cdef lxb_status_t css_finder_callback(lxb_dom_node_t *node, lxb_css_selector_specificity_t *spec, void *ctx):
171
+ cdef LexborNode lxb_node
172
+ cdef object cls
173
+ cls = <object> ctx
174
+ lxb_node = LexborNode()
175
+ lxb_node._cinit(<lxb_dom_node_t *> node, cls.current_node.parser)
176
+ cls.results.append(lxb_node)
177
+ return LXB_STATUS_OK
178
+
179
+ cdef lxb_status_t css_matcher_callback(lxb_dom_node_t *node, lxb_css_selector_specificity_t *spec, void *ctx):
180
+ cdef LexborNode lxb_node
181
+ cdef object cls
182
+ cls = <object> ctx
183
+ cls.results.append(True)
184
+ 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)