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 * Adapted for PHP libxml2 by: Niels Dossche <nielsdos@php.net>
6 */
7
8
9#ifndef LEXBOR_SELECTORS_H
10#define LEXBOR_SELECTORS_H
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
17#include "lexbor/dom/dom.h"
20#include <libxml/tree.h>
21
22
23typedef enum {
25
26 /*
27 * Includes the passed (root) node in the search.
28 *
29 * By default, the root node does not participate in selector searches,
30 * only its children.
31 *
32 * This behavior is logical, if you have found a node and then you want to
33 * search for other nodes in it, you don't need to check it again.
34 *
35 * But there are cases when it is necessary for root node to participate
36 * in the search. That's what this option is for.
37 */
39
40 /*
41 * Stop searching after the first match with any of the selectors
42 * in the list.
43 *
44 * By default, the callback will be triggered for each selector list.
45 * That is, if your node matches different selector lists, it will be
46 * returned multiple times in the callback.
47 *
48 * For example:
49 * HTML: <div id="ok"><span>test</span></div>
50 * Selectors: div, div[id="ok"], div:has(:not(a))
51 *
52 * The default behavior will cause three callbacks with the same node (div).
53 * Because it will be found by every selector in the list.
54 *
55 * This option allows you to end the element check after the first match on
56 * any of the selectors. That is, the callback will be called only once
57 * for example above. This way we get rid of duplicates in the search.
58 */
60
61 /* Quirks mode (sigh) */
63}
65
69
71(*lxb_selectors_cb_f)(const xmlNode *node,
73
75(*lxb_selectors_state_cb_f)(lxb_selectors_t *selectors,
77
78typedef struct {
79 const xmlChar *name;
83
94
109
121
122
123/*
124 * Initialization of lxb_selectors_t object.
125 *
126 * Caches are initialized in this function.
127 *
128 * @param[in] lxb_selectors_t *
129 *
130 * @return LXB_STATUS_OK if successful, otherwise an error status value.
131 */
134
135/*
136 * Clears the object. Returns object to states as after initialization.
137 *
138 * After each call to lxb_selectors_find() and lxb_selectors_find_for_node(),
139 * the lxb_selectors_t object is cleared. That is, you don't need to call this
140 * function every time after searching by a selector.
141 *
142 * @param[in] lxb_url_parser_t *
143 */
144LXB_API void
146
147/*
148 * Destroy lxb_selectors_t object.
149 *
150 * Destroying all caches.
151 *
152 * @param[in] lxb_selectors_t *. Can be NULL.
153 * if true: destroys the lxb_selectors_t object and all internal caches.
154 */
155LXB_API void
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] const xmlNode *. 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 */
182lxb_selectors_find(lxb_selectors_t *selectors, const xmlNode *root,
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] const xmlNode *. 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 */
213lxb_selectors_match_node(lxb_selectors_t *selectors, const xmlNode *node,
214 const lxb_css_selector_list_t *list,
215 lxb_selectors_cb_f cb, void *ctx);
216
217/*
218 * Inline functions.
219 */
220
221/*
222 * The function sets the node search options.
223 *
224 * For more information, see lxb_selectors_opt_t.
225 *
226 * @param[in] lxb_selectors_t *.
227 * @param[in] lxb_selectors_opt_t.
228 */
229lxb_inline void
231{
232 selectors->options = opt;
233}
234
235/*
236 * Get the current selector.
237 *
238 * Function to get the selector by which the node was found.
239 * Use context (void *ctx) to pass the lxb_selectors_t object to the callback.
240 *
241 * @param[in] const lxb_selectors_t *.
242 *
243 * @return const lxb_css_selector_list_t *.
244 */
247{
248 return selectors->current->entry->selector->list;
249}
250
251#ifdef __cplusplus
252} /* extern "C" */
253#endif
254
255#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_API
Definition def.h:48
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_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_QUIRKS_MODE
Definition selectors.h:62
@ 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_selectors_entry_t *(* lxb_selectors_state_cb_f)(lxb_selectors_t *selectors, lxb_selectors_entry_t *entry)
Definition selectors.h:70
lxb_status_t(* lxb_selectors_cb_f)(lxb_dom_node_t *node, lxb_css_selector_specificity_t spec, void *ctx)
Definition selectors.h:66
lxb_selectors_opt_t
Definition selectors.h:21
lxb_css_selector_list_t * list
Definition selector.h:99
const xmlChar * name
Definition selectors.h:79
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
lxb_selectors_cb_f cb
Definition selectors.h:99
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_state_cb_f state
Definition selectors.h:111
lxb_selectors_opt_t options
Definition selectors.h:118
lexbor_dobject_t * nested
Definition selectors.h:113
lxb_selectors_entry_t * first
Definition selectors.h:116
lxb_selectors_nested_t * current
Definition selectors.h:115
lexbor_dobject_t * objs
Definition selectors.h:112
lxb_status_t status
Definition selectors.h:119
unsigned int lxb_status_t
Definition types.h:28
#define lxb_inline
Definition types.h:21