1 #include <libcss/libcss.h>
4 #include "svgtiny_internal.h"
6 static css_error
node_name(void *pw
, void *node
, css_qname
*qname
);
7 static css_error
node_classes(void *pw
, void *node
,
8 lwc_string
***classes
, uint32_t *n_classes
);
9 static css_error
node_id(void *pw
, void *node
, lwc_string
**id
);
10 static css_error
named_parent_node(void *pw
, void *node
,
11 const css_qname
*qname
, void **parent
);
12 static css_error
named_sibling_node(void *pw
, void *node
,
13 const css_qname
*qname
, void **sibling
);
14 static css_error
named_generic_sibling_node(void *pw
, void *node
,
15 const css_qname
*qname
, void **sibling
);
16 static css_error
parent_node(void *pw
, void *node
, void **parent
);
17 static css_error
sibling_node(void *pw
, void *node
, void **sibling
);
18 static css_error
node_has_name(void *pw
, void *node
,
19 const css_qname
*qname
, bool *match
);
23 * Resolve a relative URL to an absolute one by doing nothing. This is
24 * the simplest possible implementation of a URL resolver, needed for
27 css_error
svgtiny_resolve_url(void *pw
,
28 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
33 /* Copy the relative URL to the absolute one (the return
35 *abs
= lwc_string_ref(rel
);
40 * Create a stylesheet with the default set of params.
42 * \param sheet A stylesheet pointer, passed in by reference, that
43 * we use to store the newly-created stylesheet.
44 * \param inline_style True if this stylesheet represents an inline
45 * style, and false otherwise.
47 * \return The return value from css_stylesheet_create() is returned.
49 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
52 css_stylesheet_params params
;
54 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
55 params
.level
= CSS_LEVEL_DEFAULT
;
56 params
.charset
= NULL
;
59 params
.allow_quirks
= false;
60 params
.inline_style
= inline_style
;
61 params
.resolve
= svgtiny_resolve_url
;
62 params
.resolve_pw
= NULL
;
64 params
.import_pw
= NULL
;
66 params
.color_pw
= NULL
;
68 params
.font_pw
= NULL
;
70 return css_stylesheet_create(¶ms
, sheet
);
74 /**************************/
75 /* libcss select handlers */
76 /**************************/
78 * From here on we implement the "select handler "API defined in
79 * libcss's include/libcss/select.h and discussed briefly in its
85 * Retrieve the given node's name
87 * \param pw Pointer to the current SVG parser state
88 * \param node Libdom SVG node
89 * \param qname Address at which to store the node name
91 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
93 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
97 struct svgtiny_parse_state
*state
;
99 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
100 if (err
!= DOM_NO_ERR
) {
104 state
= (struct svgtiny_parse_state
*)pw
;
105 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
107 err
= dom_string_intern(name
, &qname
->name
);
108 if (err
!= DOM_NO_ERR
) {
109 dom_string_unref(name
);
113 dom_string_unref(name
);
120 * Retrieve the given node's classes
122 * \param pw Pointer to the current SVG parser state
123 * \param node Libdom SVG node
124 * \param classes Address at which to store the class name array
125 * \param n_classes Address at which to store the length of the class
128 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
130 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
131 * because the underlying libdom function never fails
133 css_error
node_classes(void *pw
, void *node
,
134 lwc_string
***classes
, uint32_t *n_classes
)
139 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
141 /* The implementation does not do it, but the documentation
142 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
143 possible here, so we handle it to be on the safe side. */
144 if (err
!= DOM_NO_ERR
) {
153 * Retrieve the given node's id
155 * \param pw Pointer to the current SVG parser state
156 * \param node Libdom SVG node
157 * \param id Address at which to store the id
159 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
161 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
165 struct svgtiny_parse_state
*state
;
167 /* Begin with the assumption that this node has no id */
170 state
= (struct svgtiny_parse_state
*)pw
;
171 err
= dom_element_get_attribute((dom_node
*)node
,
172 state
->interned_id
, &attr
);
173 if (err
!= DOM_NO_ERR
) {
176 else if (attr
== NULL
) {
177 /* The node has no id attribute and our return value
178 is already set to NULL so we're done */
182 /* If we found an id attribute (a dom_string), intern it into
183 an lwc_string that we can return, and then cleanup the
185 err
= dom_string_intern(attr
, id
);
186 if (err
!= DOM_NO_ERR
) {
187 dom_string_unref(attr
);
190 dom_string_unref(attr
);
197 * Find the first parent of the given element having the given name
199 * \param pw Pointer to the current SVG parser state
200 * \param node Libdom SVG node
201 * \param qname Name of the parent node to search for
202 * \param parent Address at which to store the parent node pointer
204 * \return Always returns CSS_OK
206 * \post If a suitable element is found, a pointer to it will be
207 * stored at the address pointed to by \a parent; otherwise,
208 * NULL will be stored at the address pointed to by \a parent
210 css_error
named_parent_node(void *pw
, void *node
,
211 const css_qname
*qname
, void **parent
)
214 /* dom_element_named_parent_node() was invented to implement
215 * this select handler so there isn't much for us to do except
216 * call it. It's OK if node isn't an element, libdom checks
218 dom_element_named_parent_node((dom_element
*)node
,
220 (struct dom_element
**)parent
);
222 /* Implementation detail: dom_element_named_parent_node()
223 * increments the reference count of the parent element before
224 * returning it to us. According to docs/RefCnt in the libdom
225 * repository, this will prevent the parent element from being
226 * destroyed if it is pruned from the DOM. That sounds good,
227 * since we don't want to be using a pointer to an object that
228 * has been destroyed... but we also have no way of later
229 * decrementing the reference count ourselves, and don't want
230 * to make the returned node eternal. Decrementing the
231 * reference counter now allows it to be destroyed when the
232 * DOM no longer needs it, and so long as no other parts of
233 * libsvgtiny are messing with the DOM during parsing, that
234 * shouldn't (ha ha) cause any problems. */
235 dom_node_unref(*parent
);
242 * Find the "next-sibling" of the given element having the given name
244 * This search corresponds to the "+ foo" combinator in CSS and will
245 * find only "foo" element nodes that immediately precede the given
246 * node under the same parent in the DOM. In CSS the tree is viewed
247 * top-down and in libdom it is viewed from the bottom-up; as a result
248 * "next" and "previous" are sometimes backwards. This is case-sensitive.
250 * \param pw Pointer to the current SVG parser state
251 * \param node Libdom SVG node
252 * \param qname Name of the sibling node to search for
253 * \param sibling Address at which to store the sibling node pointer
255 * \return Always returns CSS_OK
257 * \post If a suitable element is found, a pointer to it will be
258 * stored at the address pointed to by \a sibling; otherwise,
259 * NULL will be stored at the address pointed to by \a sibling
261 css_error
named_sibling_node(void *pw
, void *node
,
262 const css_qname
*qname
, void **sibling
)
265 dom_node
*n
= node
; /* the current node */
266 dom_node
*prev
; /* the previous node */
271 *sibling
= NULL
; /* default to nothing found */
273 /* Begin the search; the first iteration we do outside of the
274 * loop. Implementation detil: dom_node_get_previous_sibling()
275 * increments the reference counter on the returned node. A
276 * comment within named_parent_node() explains why we
277 * decrement it ASAP. */
278 err
= dom_node_get_previous_sibling(n
, &n
);
279 if (err
!= DOM_NO_ERR
) {
284 /* We're looking for the first ELEMENT sibling */
285 err
= dom_node_get_node_type(n
, &type
);
286 if (err
!= DOM_NO_ERR
) {
291 if (type
== DOM_ELEMENT_NODE
) {
292 /* We found an element node, does it have the
294 err
= dom_node_get_node_name(n
, &name
);
295 if (err
!= DOM_NO_ERR
) {
300 if (dom_string_lwc_isequal(name
,
302 /* The name is right, return it */
306 /* There's only one next-sibling element node
307 * and we've already found it, so if its name
308 * wasn't right, we return the default value
310 dom_string_unref(name
);
315 /* Not an element node, so we move on the the previous
316 * previous sibling */
317 err
= dom_node_get_previous_sibling(n
, &prev
);
318 if (err
!= DOM_NO_ERR
) {
332 * Find the first "subsequent-sibling" of the given element having the
335 * This search corresponds to the "~ foo" combinator in CSS and will
336 * find only "foo" element nodes that precede the given node (under
337 * the same parent) in the DOM. In CSS the tree is viewed top-down and
338 * in libdom it is viewed from the bottom-up; as a result "next" and
339 * "previous" are sometimes backwards. This is case-sensitive.
341 * \param pw Pointer to the current SVG parser state
342 * \param node Libdom SVG node
343 * \param qname Name of the sibling node to search for
344 * \param sibling Address at which to store the sibling node pointer
346 * \return Always returns CSS_OK
348 * \post If a suitable element is found, a pointer to it will be
349 * stored at the address pointed to by \a sibling; otherwise,
350 * NULL will be stored at the address pointed to by \a sibling
352 css_error
named_generic_sibling_node(void *pw
, void *node
,
353 const css_qname
*qname
, void **sibling
)
356 dom_node
*n
= node
; /* the current node */
357 dom_node
*prev
; /* the previous node */
363 *sibling
= NULL
; /* default to nothing found */
365 /* Begin the search; the first iteration we do outside of the
366 * loop. Implementation detil: dom_node_get_previous_sibling()
367 * increments the reference counter on the returned node. A
368 * comment within named_parent_node() explains why we
369 * decrement it ASAP. */
370 err
= dom_node_get_previous_sibling(n
, &n
);
371 if (err
!= DOM_NO_ERR
) {
376 err
= dom_node_get_node_type(n
, &type
);
377 if (err
!= DOM_NO_ERR
) {
382 if (type
== DOM_ELEMENT_NODE
) {
383 /* We only want ELEMENT nodes */
384 err
= dom_node_get_node_name(n
, &name
);
385 if (err
!= DOM_NO_ERR
) {
390 if (dom_string_lwc_isequal(name
,
392 /* Found one. Save it and stop the search */
393 dom_string_unref(name
);
399 dom_string_unref(name
);
402 /* This sibling wasn't an element with the desired
403 name, so move on to the previous sibling */
404 err
= dom_node_get_previous_sibling(n
, &prev
);
405 if (err
!= DOM_NO_ERR
) {
419 * Return a pointer to the given node's parent
421 * \param pw Pointer to the current SVG parser state
422 * \param node Libdom SVG node
423 * \param parent Address at which to store the node's parent pointer
425 * \return Always returns CSS_OK
427 css_error
parent_node(void *pw
, void *node
, void **parent
)
430 /* Libdom basically implements this for us */
431 dom_element_parent_node(node
, (struct dom_element
**)parent
);
433 /* See the comment in named_parent_node() for why we decrement
434 * this reference counter here. */
435 dom_node_unref(*parent
);
442 * Find the "next-sibling" of the given element
444 * This search corresponds "+ *" in CSS and will find the first
445 * element node that immediately precedes the given node under the
446 * same parent in the DOM. In CSS the tree is viewed top-down and in
447 * libdom it is viewed from the bottom-up; as a result "next" and
448 * "previous" are sometimes backwards.
450 * \param pw Pointer to the current SVG parser state
451 * \param node Libdom SVG node
452 * \param sibling Address at which to store the sibling node pointer
454 * \return Always returns CSS_OK
456 * \post If a suitable element is found, a pointer to it will be
457 * stored at the address pointed to by \a sibling; otherwise,
458 * NULL will be stored at the address pointed to by \a sibling
460 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
463 dom_node
*n
= node
; /* the current node */
464 dom_node
*prev
; /* the previous node */
468 *sibling
= NULL
; /* default to nothing found */
470 /* Begin the search; the first iteration we do outside of the
471 * loop. Implementation detil: dom_node_get_previous_sibling()
472 * increments the reference counter on the returned node. A
473 * comment within named_parent_node() explains why we
474 * decrement it ASAP. */
475 err
= dom_node_get_previous_sibling(n
, &n
);
476 if (err
!= DOM_NO_ERR
) {
481 err
= dom_node_get_node_type(n
, &type
);
482 if (err
!= DOM_NO_ERR
) {
487 if (type
== DOM_ELEMENT_NODE
) {
488 /* We found a sibling node that is also an
489 element and that's all we wanted. */
495 /* This sibling node was not an element; move on to
496 the previous sibling */
497 err
= dom_node_get_previous_sibling(n
, &prev
);
498 if (err
!= DOM_NO_ERR
) {
512 * Test the given node for the given name
514 * This will return true (via the "match" pointer) if the libdom node
515 * has the given name or if that name is the universal selector;
516 * otherwise it returns false. The comparison is case-sensitive. It
517 * corresponds to a rule like "body { ... }" in CSS.
519 * \param pw Pointer to the current SVG parser state
520 * \param node Libdom SVG node to test
521 * \param qname Name to check for
522 * \param match Pointer to the test result
524 * \return Always returns CSS_OK
526 css_error
node_has_name(void *pw
, void *node
,
527 const css_qname
*qname
, bool *match
)
529 struct svgtiny_parse_state
*state
;
533 /* Start by checking to see if qname is the universal selector */
534 state
= (struct svgtiny_parse_state
*)pw
;
535 if (lwc_string_isequal(qname
->name
,
536 state
->interned_universal
, match
) == lwc_error_ok
) {
538 /* It's the universal selector. In NetSurf, all node
539 * names match the universal selector, and nothing in
540 * the libcss documentation suggests another approach,
541 * so we follow NetSurf here. */
546 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
547 if (err
!= DOM_NO_ERR
) {
551 /* Unlike with HTML, SVG element names are case-sensitive */
552 *match
= dom_string_lwc_isequal(name
, qname
->name
);
553 dom_string_unref(name
);