php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
selectors.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2024 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7
8#ifndef LEXBOR_SELECTORS_H
9#define LEXBOR_SELECTORS_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
16#include "lexbor/dom/dom.h"
19
20
21typedef enum {
23
24 /*
25 * Includes the passed (root) node in the search.
26 *
27 * By default, the root node does not participate in selector searches,
28 * only its children.
29 *
30 * This behavior is logical, if you have found a node and then you want to
31 * search for other nodes in it, you don't need to check it again.
32 *
33 * But there are cases when it is necessary for root node to participate
34 * in the search. That's what this option is for.
35 */
37
38 /*
39 * Stop searching after the first match with any of the selectors
40 * in the list.
41 *
42 * By default, the callback will be triggered for each selector list.
43 * That is, if your node matches different selector lists, it will be
44 * returned multiple times in the callback.
45 *
46 * For example:
47 * HTML: <div id="ok"><span>test</span></div>
48 * Selectors: div, div[id="ok"], div:has(:not(a))
49 *
50 * The default behavior will cause three callbacks with the same node (div).
51 * Because it will be found by every selector in the list.
52 *
53 * This option allows you to end the element check after the first match on
54 * any of the selectors. That is, the callback will be called only once
55 * for example above. This way we get rid of duplicates in the search.
56 */
58}
60
61typedef struct lxb_selectors lxb_selectors_t;
64
68
70(*lxb_selectors_state_cb_f)(lxb_selectors_t *selectors,
72
74 uintptr_t id;
82};
83
87
89 void *ctx;
90
94
95 size_t index;
96 bool found;
97};
98
99struct lxb_selectors {
103
106
109};
110
111
112/*
113 * Create lxb_selectors_t object.
114 *
115 * @return lxb_selectors_t * if successful, otherwise NULL.
116 */
119
120/*
121 * Initialization of lxb_selectors_t object.
122 *
123 * Caches are initialized in this function.
124 *
125 * @param[in] lxb_selectors_t *
126 *
127 * @return LXB_STATUS_OK if successful, otherwise an error status value.
128 */
131
132/*
133 * Clears the object. Returns object to states as after initialization.
134 *
135 * After each call to lxb_selectors_find() and lxb_selectors_find_for_node(),
136 * the lxb_selectors_t object is cleared. That is, you don't need to call this
137 * function every time after searching by a selector.
138 *
139 * @param[in] lxb_url_parser_t *
140 */
141LXB_API void
143
144/*
145 * Destroy lxb_selectors_t object.
146 *
147 * Destroying all caches.
148 *
149 * @param[in] lxb_selectors_t *. Can be NULL.
150 * @param[in] if false: only destroys internal caches.
151 * if true: destroys the lxb_selectors_t object and all internal caches.
152 *
153 * @return lxb_selectors_t * if self_destroy = false, otherwise NULL.
154 */
156lxb_selectors_destroy(lxb_selectors_t *selectors, bool self_destroy);
157
158/*
159 * Search for nodes by selector list.
160 *
161 * Default Behavior:
162 * 1. The root node does not participate in the search, only its child nodes.
163 * 2. If a node matches multiple selector lists, a callback with that node
164 * will be called on each list.
165 * For example:
166 * HTML: <div id="ok"><span></span></div>
167 * Selectors: div, div[id="ok"], div:has(:not(a))
168 * For each selector list, a callback with a "div" node will be called.
169 *
170 * To change the search behavior, see lxb_selectors_opt_set().
171 *
172 * @param[in] lxb_selectors_t *.
173 * @param[in] lxb_dom_node_t *. The node from which the search will begin.
174 * @param[in] const lxb_css_selector_list_t *. Selectors List.
175 * @param[in] lxb_selectors_cb_f. Callback for a found node.
176 * @param[in] void *. Context for the callback.
177 * if true: destroys the lxb_selectors_t object and all internal caches.
178 *
179 * @return LXB_STATUS_OK if successful, otherwise an error status value.
180 */
183 const lxb_css_selector_list_t *list,
184 lxb_selectors_cb_f cb, void *ctx);
185
186/*
187 * Match a node to a Selectors List.
188 *
189 * In other words, the function checks which selector lists will find the
190 * specified node.
191 *
192 * Default Behavior:
193 * 1. If a node matches multiple selector lists, a callback with that node
194 * will be called on each list.
195 * For example:
196 * HTML: <div id="ok"><span></span></div>
197 * Node: div
198 * Selectors: div, div[id="ok"], div:has(:not(a))
199 * For each selector list, a callback with a "div" node will be called.
200 *
201 * To change the search behavior, see lxb_selectors_opt_set().
202 *
203 * @param[in] lxb_selectors_t *.
204 * @param[in] lxb_dom_node_t *. The node from which the search will begin.
205 * @param[in] const lxb_css_selector_list_t *. Selectors List.
206 * @param[in] lxb_selectors_cb_f. Callback for a found node.
207 * @param[in] void *. Context for the callback.
208 * if true: destroys the lxb_selectors_t object and all internal caches.
209 *
210 * @return LXB_STATUS_OK if successful, otherwise an error status value.
211 */
214 const lxb_css_selector_list_t *list,
215 lxb_selectors_cb_f cb, void *ctx);
216
217/*
218 * Deprecated!
219 * This function does exactly the same thing as lxb_selectors_match_node().
220 */
224 lxb_selectors_cb_f cb, void *ctx));
225
226/*
227 * Inline functions.
228 */
229
230/*
231 * The function sets the node search options.
232 *
233 * For more information, see lxb_selectors_opt_t.
234 *
235 * @param[in] lxb_selectors_t *.
236 * @param[in] lxb_selectors_opt_t.
237 */
238lxb_inline void
240{
241 selectors->options = opt;
242}
243
244/*
245 * Get the current selector.
246 *
247 * Function to get the selector by which the node was found.
248 * Use context (void *ctx) to pass the lxb_selectors_t object to the callback.
249 *
250 * @param[in] const lxb_selectors_t *.
251 *
252 * @return const lxb_css_selector_list_t *.
253 */
256{
257 return selectors->current->entry->selector->list;
258}
259
260/*
261 * Not inline for inline.
262 */
263
264/*
265 * Same as lxb_selectors_opt_set() function, but not inline.
266 */
267LXB_API void
269
270/*
271 * Same as lxb_selectors_selector() function, but not inline.
272 */
275
276
277#ifdef __cplusplus
278} /* extern "C" */
279#endif
280
281#endif /* LEXBOR_SELECTORS_H */
char * cb
Definition assert.c:26
struct lxb_css_selector lxb_css_selector_t
Definition base.h:39
struct lxb_css_selector_list lxb_css_selector_list_t
Definition base.h:40
#define LXB_DEPRECATED(func)
Definition def.h:26
#define LXB_API
Definition def.h:48
DNS_STATUS status
Definition dns_win32.c:49
struct lxb_dom_node lxb_dom_node_t
Definition interface.h:38
PHP_JSON_API size_t int options
Definition php_json.h:102
uint32_t lxb_css_selector_specificity_t
Definition selector.h:107
lxb_css_selector_combinator_t
Definition selector.h:34
struct lxb_selectors_entry lxb_selectors_entry_t
Definition selectors.h:67
LXB_API lxb_status_t lxb_selectors_init(lxb_selectors_t *selectors)
Definition selectors.c:285
LXB_API void lxb_selectors_destroy(lxb_selectors_t *selectors)
Definition selectors.c:316
struct lxb_selectors lxb_selectors_t
Definition selectors.h:66
LXB_API lxb_status_t lxb_selectors_match_node(lxb_selectors_t *selectors, const xmlNode *node, const lxb_css_selector_list_t *list, lxb_selectors_cb_f cb, void *ctx)
Definition selectors.c:434
struct lxb_selectors_nested lxb_selectors_nested_t
Definition selectors.h:68
lxb_selectors_entry_t *(* lxb_selectors_state_cb_f)(lxb_selectors_t *selectors, lxb_selectors_entry_t *entry)
Definition selectors.h:75
lxb_inline void lxb_selectors_opt_set(lxb_selectors_t *selectors, lxb_selectors_opt_t opt)
Definition selectors.h:230
LXB_API lxb_status_t lxb_selectors_find(lxb_selectors_t *selectors, const xmlNode *root, const lxb_css_selector_list_t *list, lxb_selectors_cb_f cb, void *ctx)
Definition selectors.c:410
lxb_inline const lxb_css_selector_list_t * lxb_selectors_selector(const lxb_selectors_t *selectors)
Definition selectors.h:246
LXB_API void lxb_selectors_clean(lxb_selectors_t *selectors)
Definition selectors.c:309
lxb_selectors_opt_t
Definition selectors.h:23
@ LXB_SELECTORS_OPT_DEFAULT
Definition selectors.h:24
@ LXB_SELECTORS_OPT_MATCH_FIRST
Definition selectors.h:59
@ LXB_SELECTORS_OPT_MATCH_ROOT
Definition selectors.h:38
lxb_status_t(* lxb_selectors_cb_f)(const xmlNode *node, lxb_css_selector_specificity_t spec, void *ctx)
Definition selectors.h:71
lxb_status_t lxb_selectors_find_reverse(lxb_selectors_t *selectors, lxb_dom_node_t *root, lxb_css_selector_list_t *list, lxb_selectors_cb_f cb, void *ctx)
Definition selectors.c:322
LXB_API void lxb_selectors_opt_set_noi(lxb_selectors_t *selectors, lxb_selectors_opt_t opt)
Definition selectors.c:1916
LXB_API lxb_selectors_t * lxb_selectors_create(void)
Definition selectors.c:114
LXB_API const lxb_css_selector_list_t * lxb_selectors_selector_noi(const lxb_selectors_t *selectors)
Definition selectors.c:1922
zval * current
Definition session.c:1024
lxb_css_selector_list_t * list
Definition selector.h:99
Definition selectors.h:84
lxb_selectors_entry_t * following
Definition selectors.h:91
lxb_selectors_entry_t * prev
Definition selectors.h:90
lxb_selectors_nested_t * nested
Definition selectors.h:92
const xmlNode * node
Definition selectors.h:88
lxb_selectors_adapted_id id
Definition selectors.h:85
const lxb_css_selector_t * selector
Definition selectors.h:87
lxb_selectors_entry_t * next
Definition selectors.h:89
lxb_css_selector_combinator_t combinator
Definition selectors.h:86
const xmlNode * root
Definition selectors.h:102
lxb_selectors_state_cb_f return_state
Definition selectors.h:97
lxb_selectors_nested_t * parent
Definition selectors.h:104
lxb_selectors_entry_t * entry
Definition selectors.h:96
lxb_selectors_entry_t * last
Definition selectors.h:103
lxb_selectors_opt_t options
Definition selectors.h:118
lexbor_dobject_t * nested
Definition selectors.h:113
lxb_selectors_nested_t * current
Definition selectors.h:115
lexbor_dobject_t * objs
Definition selectors.h:112
unsigned int lxb_status_t
Definition types.h:28
#define lxb_inline
Definition types.h:21