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
);
20 static css_error
node_has_class(void *pw
, void *node
,
21 lwc_string
*name
, bool *match
);
25 * Resolve a relative URL to an absolute one by doing nothing. This is
26 * the simplest possible implementation of a URL resolver, needed for
29 css_error
svgtiny_resolve_url(void *pw
,
30 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
35 /* Copy the relative URL to the absolute one (the return
37 *abs
= lwc_string_ref(rel
);
42 * Create a stylesheet with the default set of params.
44 * \param sheet A stylesheet pointer, passed in by reference, that
45 * we use to store the newly-created stylesheet.
46 * \param inline_style True if this stylesheet represents an inline
47 * style, and false otherwise.
49 * \return The return value from css_stylesheet_create() is returned.
51 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
54 css_stylesheet_params params
;
56 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
57 params
.level
= CSS_LEVEL_DEFAULT
;
58 params
.charset
= NULL
;
61 params
.allow_quirks
= false;
62 params
.inline_style
= inline_style
;
63 params
.resolve
= svgtiny_resolve_url
;
64 params
.resolve_pw
= NULL
;
66 params
.import_pw
= NULL
;
68 params
.color_pw
= NULL
;
70 params
.font_pw
= NULL
;
72 return css_stylesheet_create(¶ms
, sheet
);
76 /**************************/
77 /* libcss select handlers */
78 /**************************/
80 * From here on we implement the "select handler "API defined in
81 * libcss's include/libcss/select.h and discussed briefly in its
87 * Retrieve the given node's name
89 * \param pw Pointer to the current SVG parser state
90 * \param node Libdom SVG node
91 * \param qname Address at which to store the node name
93 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
95 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
99 struct svgtiny_parse_state
*state
;
101 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
102 if (err
!= DOM_NO_ERR
) {
106 state
= (struct svgtiny_parse_state
*)pw
;
107 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
109 err
= dom_string_intern(name
, &qname
->name
);
110 if (err
!= DOM_NO_ERR
) {
111 dom_string_unref(name
);
115 dom_string_unref(name
);
122 * Retrieve the given node's classes
124 * \param pw Pointer to the current SVG parser state
125 * \param node Libdom SVG node
126 * \param classes Address at which to store the class name array
127 * \param n_classes Address at which to store the length of the class
130 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
132 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
133 * because the underlying libdom function never fails
135 css_error
node_classes(void *pw
, void *node
,
136 lwc_string
***classes
, uint32_t *n_classes
)
141 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
143 /* The implementation does not do it, but the documentation
144 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
145 possible here, so we handle it to be on the safe side. */
146 if (err
!= DOM_NO_ERR
) {
155 * Retrieve the given node's id
157 * \param pw Pointer to the current SVG parser state
158 * \param node Libdom SVG node
159 * \param id Address at which to store the id
161 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
163 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
167 struct svgtiny_parse_state
*state
;
169 /* Begin with the assumption that this node has no id */
172 state
= (struct svgtiny_parse_state
*)pw
;
173 err
= dom_element_get_attribute((dom_node
*)node
,
174 state
->interned_id
, &attr
);
175 if (err
!= DOM_NO_ERR
) {
178 else if (attr
== NULL
) {
179 /* The node has no id attribute and our return value
180 is already set to NULL so we're done */
184 /* If we found an id attribute (a dom_string), intern it into
185 an lwc_string that we can return, and then cleanup the
187 err
= dom_string_intern(attr
, id
);
188 if (err
!= DOM_NO_ERR
) {
189 dom_string_unref(attr
);
192 dom_string_unref(attr
);
199 * Find the first parent of the given element having the given name
201 * \param pw Pointer to the current SVG parser state
202 * \param node Libdom SVG node
203 * \param qname Name of the parent node to search for
204 * \param parent Address at which to store the parent node pointer
206 * \return Always returns CSS_OK
208 * \post If a suitable element is found, a pointer to it will be
209 * stored at the address pointed to by \a parent; otherwise,
210 * NULL will be stored at the address pointed to by \a parent
212 css_error
named_parent_node(void *pw
, void *node
,
213 const css_qname
*qname
, void **parent
)
216 /* dom_element_named_parent_node() was invented to implement
217 * this select handler so there isn't much for us to do except
218 * call it. It's OK if node isn't an element, libdom checks
220 dom_element_named_parent_node((dom_element
*)node
,
222 (struct dom_element
**)parent
);
224 /* Implementation detail: dom_element_named_parent_node()
225 * increments the reference count of the parent element before
226 * returning it to us. According to docs/RefCnt in the libdom
227 * repository, this will prevent the parent element from being
228 * destroyed if it is pruned from the DOM. That sounds good,
229 * since we don't want to be using a pointer to an object that
230 * has been destroyed... but we also have no way of later
231 * decrementing the reference count ourselves, and don't want
232 * to make the returned node eternal. Decrementing the
233 * reference counter now allows it to be destroyed when the
234 * DOM no longer needs it, and so long as no other parts of
235 * libsvgtiny are messing with the DOM during parsing, that
236 * shouldn't (ha ha) cause any problems. */
237 dom_node_unref(*parent
);
244 * Find the "next-sibling" of the given element having the given name
246 * This search corresponds to the "+ foo" combinator in CSS and will
247 * find only "foo" element nodes that immediately precede the given
248 * node under the same parent in the DOM. In CSS the tree is viewed
249 * top-down and in libdom it is viewed from the bottom-up; as a result
250 * "next" and "previous" are sometimes backwards. This is case-sensitive.
252 * \param pw Pointer to the current SVG parser state
253 * \param node Libdom SVG node
254 * \param qname Name of the sibling node to search for
255 * \param sibling Address at which to store the sibling node pointer
257 * \return Always returns CSS_OK
259 * \post If a suitable element is found, a pointer to it will be
260 * stored at the address pointed to by \a sibling; otherwise,
261 * NULL will be stored at the address pointed to by \a sibling
263 css_error
named_sibling_node(void *pw
, void *node
,
264 const css_qname
*qname
, void **sibling
)
267 dom_node
*n
= node
; /* the current node */
268 dom_node
*prev
; /* the previous node */
273 *sibling
= NULL
; /* default to nothing found */
275 /* Begin the search; the first iteration we do outside of the
276 * loop. Implementation detil: dom_node_get_previous_sibling()
277 * increments the reference counter on the returned node. A
278 * comment within named_parent_node() explains why we
279 * decrement it ASAP. */
280 err
= dom_node_get_previous_sibling(n
, &n
);
281 if (err
!= DOM_NO_ERR
) {
286 /* We're looking for the first ELEMENT sibling */
287 err
= dom_node_get_node_type(n
, &type
);
288 if (err
!= DOM_NO_ERR
) {
293 if (type
== DOM_ELEMENT_NODE
) {
294 /* We found an element node, does it have the
296 err
= dom_node_get_node_name(n
, &name
);
297 if (err
!= DOM_NO_ERR
) {
302 if (dom_string_lwc_isequal(name
,
304 /* The name is right, return it */
308 /* There's only one next-sibling element node
309 * and we've already found it, so if its name
310 * wasn't right, we return the default value
312 dom_string_unref(name
);
317 /* Not an element node, so we move on the the previous
318 * previous sibling */
319 err
= dom_node_get_previous_sibling(n
, &prev
);
320 if (err
!= DOM_NO_ERR
) {
334 * Find the first "subsequent-sibling" of the given element having the
337 * This search corresponds to the "~ foo" combinator in CSS and will
338 * find only "foo" element nodes that precede the given node (under
339 * the same parent) in the DOM. In CSS the tree is viewed top-down and
340 * in libdom it is viewed from the bottom-up; as a result "next" and
341 * "previous" are sometimes backwards. This is case-sensitive.
343 * \param pw Pointer to the current SVG parser state
344 * \param node Libdom SVG node
345 * \param qname Name of the sibling node to search for
346 * \param sibling Address at which to store the sibling node pointer
348 * \return Always returns CSS_OK
350 * \post If a suitable element is found, a pointer to it will be
351 * stored at the address pointed to by \a sibling; otherwise,
352 * NULL will be stored at the address pointed to by \a sibling
354 css_error
named_generic_sibling_node(void *pw
, void *node
,
355 const css_qname
*qname
, void **sibling
)
358 dom_node
*n
= node
; /* the current node */
359 dom_node
*prev
; /* the previous node */
365 *sibling
= NULL
; /* default to nothing found */
367 /* Begin the search; the first iteration we do outside of the
368 * loop. Implementation detil: dom_node_get_previous_sibling()
369 * increments the reference counter on the returned node. A
370 * comment within named_parent_node() explains why we
371 * decrement it ASAP. */
372 err
= dom_node_get_previous_sibling(n
, &n
);
373 if (err
!= DOM_NO_ERR
) {
378 err
= dom_node_get_node_type(n
, &type
);
379 if (err
!= DOM_NO_ERR
) {
384 if (type
== DOM_ELEMENT_NODE
) {
385 /* We only want ELEMENT nodes */
386 err
= dom_node_get_node_name(n
, &name
);
387 if (err
!= DOM_NO_ERR
) {
392 if (dom_string_lwc_isequal(name
,
394 /* Found one. Save it and stop the search */
395 dom_string_unref(name
);
401 dom_string_unref(name
);
404 /* This sibling wasn't an element with the desired
405 name, so move on to the previous sibling */
406 err
= dom_node_get_previous_sibling(n
, &prev
);
407 if (err
!= DOM_NO_ERR
) {
421 * Return a pointer to the given node's parent
423 * \param pw Pointer to the current SVG parser state
424 * \param node Libdom SVG node
425 * \param parent Address at which to store the node's parent pointer
427 * \return Always returns CSS_OK
429 css_error
parent_node(void *pw
, void *node
, void **parent
)
432 /* Libdom basically implements this for us */
433 dom_element_parent_node(node
, (struct dom_element
**)parent
);
435 /* See the comment in named_parent_node() for why we decrement
436 * this reference counter here. */
437 dom_node_unref(*parent
);
444 * Find the "next-sibling" of the given element
446 * This search corresponds "+ *" in CSS and will find the first
447 * element node that immediately precedes the given node under the
448 * same parent in the DOM. In CSS the tree is viewed top-down and in
449 * libdom it is viewed from the bottom-up; as a result "next" and
450 * "previous" are sometimes backwards.
452 * \param pw Pointer to the current SVG parser state
453 * \param node Libdom SVG node
454 * \param sibling Address at which to store the sibling node pointer
456 * \return Always returns CSS_OK
458 * \post If a suitable element is found, a pointer to it will be
459 * stored at the address pointed to by \a sibling; otherwise,
460 * NULL will be stored at the address pointed to by \a sibling
462 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
465 dom_node
*n
= node
; /* the current node */
466 dom_node
*prev
; /* the previous node */
470 *sibling
= NULL
; /* default to nothing found */
472 /* Begin the search; the first iteration we do outside of the
473 * loop. Implementation detil: dom_node_get_previous_sibling()
474 * increments the reference counter on the returned node. A
475 * comment within named_parent_node() explains why we
476 * decrement it ASAP. */
477 err
= dom_node_get_previous_sibling(n
, &n
);
478 if (err
!= DOM_NO_ERR
) {
483 err
= dom_node_get_node_type(n
, &type
);
484 if (err
!= DOM_NO_ERR
) {
489 if (type
== DOM_ELEMENT_NODE
) {
490 /* We found a sibling node that is also an
491 element and that's all we wanted. */
497 /* This sibling node was not an element; move on to
498 the previous sibling */
499 err
= dom_node_get_previous_sibling(n
, &prev
);
500 if (err
!= DOM_NO_ERR
) {
514 * Test the given node for the given name
516 * This will return true (via the "match" pointer) if the libdom node
517 * has the given name or if that name is the universal selector;
518 * otherwise it returns false. The comparison is case-sensitive. It
519 * corresponds to a rule like "body { ... }" in CSS.
521 * \param pw Pointer to the current SVG parser state
522 * \param node Libdom SVG node to test
523 * \param qname Name to check for
524 * \param match Pointer to the test result
526 * \return Always returns CSS_OK
528 css_error
node_has_name(void *pw
, void *node
,
529 const css_qname
*qname
, bool *match
)
531 struct svgtiny_parse_state
*state
;
535 /* Start by checking to see if qname is the universal selector */
536 state
= (struct svgtiny_parse_state
*)pw
;
537 if (lwc_string_isequal(qname
->name
,
538 state
->interned_universal
, match
) == lwc_error_ok
) {
540 /* It's the universal selector. In NetSurf, all node
541 * names match the universal selector, and nothing in
542 * the libcss documentation suggests another approach,
543 * so we follow NetSurf here. */
548 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
549 if (err
!= DOM_NO_ERR
) {
553 /* Unlike with HTML, SVG element names are case-sensitive */
554 *match
= dom_string_lwc_isequal(name
, qname
->name
);
555 dom_string_unref(name
);
562 * Test the given node for the given class
564 * This will return true (via the "match" pointer) if the libdom node
565 * has the given class. The comparison is case-sensitive. It
566 * corresponds to node.class in CSS.
568 * \param pw Pointer to the current SVG parser state
569 * \param node Libdom SVG node to test
570 * \param name Class name to check for
571 * \param match Pointer to the test result
573 * \return Always returns CSS_OK
575 css_error
node_has_class(void *pw
, void *node
,
576 lwc_string
*name
, bool *match
)
579 /* libdom implements this for us and apparently it cannot fail */
580 dom_element_has_class((dom_node
*)node
, name
, match
);