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
);
22 static css_error
node_has_id(void *pw
, void *node
,
23 lwc_string
*name
, bool *match
);
24 static css_error
node_has_attribute(void *pw
, void *node
,
25 const css_qname
*qname
, bool *match
);
26 static css_error
node_has_attribute_equal(void *pw
, void *node
,
27 const css_qname
*qname
, lwc_string
*value
,
32 * Resolve a relative URL to an absolute one by doing nothing. This is
33 * the simplest possible implementation of a URL resolver, needed for
36 css_error
svgtiny_resolve_url(void *pw
,
37 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
42 /* Copy the relative URL to the absolute one (the return
44 *abs
= lwc_string_ref(rel
);
49 * Create a stylesheet with the default set of params.
51 * \param sheet A stylesheet pointer, passed in by reference, that
52 * we use to store the newly-created stylesheet.
53 * \param inline_style True if this stylesheet represents an inline
54 * style, and false otherwise.
56 * \return The return value from css_stylesheet_create() is returned.
58 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
61 css_stylesheet_params params
;
63 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
64 params
.level
= CSS_LEVEL_DEFAULT
;
65 params
.charset
= NULL
;
68 params
.allow_quirks
= false;
69 params
.inline_style
= inline_style
;
70 params
.resolve
= svgtiny_resolve_url
;
71 params
.resolve_pw
= NULL
;
73 params
.import_pw
= NULL
;
75 params
.color_pw
= NULL
;
77 params
.font_pw
= NULL
;
79 return css_stylesheet_create(¶ms
, sheet
);
83 /**************************/
84 /* libcss select handlers */
85 /**************************/
87 * From here on we implement the "select handler "API defined in
88 * libcss's include/libcss/select.h and discussed briefly in its
94 * Retrieve the given node's name
96 * \param pw Pointer to the current SVG parser state
97 * \param node Libdom SVG node
98 * \param qname Address at which to store the node name
100 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
102 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
106 struct svgtiny_parse_state
*state
;
108 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
109 if (err
!= DOM_NO_ERR
) {
113 state
= (struct svgtiny_parse_state
*)pw
;
114 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
116 err
= dom_string_intern(name
, &qname
->name
);
117 if (err
!= DOM_NO_ERR
) {
118 dom_string_unref(name
);
122 dom_string_unref(name
);
129 * Retrieve the given node's classes
131 * \param pw Pointer to the current SVG parser state
132 * \param node Libdom SVG node
133 * \param classes Address at which to store the class name array
134 * \param n_classes Address at which to store the length of the class
137 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
139 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
140 * because the underlying libdom function never fails
142 css_error
node_classes(void *pw
, void *node
,
143 lwc_string
***classes
, uint32_t *n_classes
)
148 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
150 /* The implementation does not do it, but the documentation
151 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
152 possible here, so we handle it to be on the safe side. */
153 if (err
!= DOM_NO_ERR
) {
162 * Retrieve the given node's id
164 * \param pw Pointer to the current SVG parser state
165 * \param node Libdom SVG node
166 * \param id Address at which to store the id
168 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
170 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
174 struct svgtiny_parse_state
*state
;
176 /* Begin with the assumption that this node has no id */
179 state
= (struct svgtiny_parse_state
*)pw
;
180 err
= dom_element_get_attribute((dom_node
*)node
,
181 state
->interned_id
, &attr
);
182 if (err
!= DOM_NO_ERR
) {
185 else if (attr
== NULL
) {
186 /* The node has no id attribute and our return value
187 is already set to NULL so we're done */
191 /* If we found an id attribute (a dom_string), intern it into
192 an lwc_string that we can return, and then cleanup the
194 err
= dom_string_intern(attr
, id
);
195 if (err
!= DOM_NO_ERR
) {
196 dom_string_unref(attr
);
199 dom_string_unref(attr
);
206 * Find the first parent of the given element having the given name
208 * \param pw Pointer to the current SVG parser state
209 * \param node Libdom SVG node
210 * \param qname Name of the parent node to search for
211 * \param parent Address at which to store the parent node pointer
213 * \return Always returns CSS_OK
215 * \post If a suitable element is found, a pointer to it will be
216 * stored at the address pointed to by \a parent; otherwise,
217 * NULL will be stored at the address pointed to by \a parent
219 css_error
named_parent_node(void *pw
, void *node
,
220 const css_qname
*qname
, void **parent
)
223 /* dom_element_named_parent_node() was invented to implement
224 * this select handler so there isn't much for us to do except
225 * call it. It's OK if node isn't an element, libdom checks
227 dom_element_named_parent_node((dom_element
*)node
,
229 (struct dom_element
**)parent
);
231 /* Implementation detail: dom_element_named_parent_node()
232 * increments the reference count of the parent element before
233 * returning it to us. According to docs/RefCnt in the libdom
234 * repository, this will prevent the parent element from being
235 * destroyed if it is pruned from the DOM. That sounds good,
236 * since we don't want to be using a pointer to an object that
237 * has been destroyed... but we also have no way of later
238 * decrementing the reference count ourselves, and don't want
239 * to make the returned node eternal. Decrementing the
240 * reference counter now allows it to be destroyed when the
241 * DOM no longer needs it, and so long as no other parts of
242 * libsvgtiny are messing with the DOM during parsing, that
243 * shouldn't (ha ha) cause any problems. */
244 dom_node_unref(*parent
);
251 * Find the "next-sibling" of the given element having the given name
253 * This search corresponds to the "+ foo" combinator in CSS and will
254 * find only "foo" element nodes that immediately precede the given
255 * node under the same parent in the DOM. In CSS the tree is viewed
256 * top-down and in libdom it is viewed from the bottom-up; as a result
257 * "next" and "previous" are sometimes backwards. This is case-sensitive.
259 * \param pw Pointer to the current SVG parser state
260 * \param node Libdom SVG node
261 * \param qname Name of the sibling node to search for
262 * \param sibling Address at which to store the sibling node pointer
264 * \return Always returns CSS_OK
266 * \post If a suitable element is found, a pointer to it will be
267 * stored at the address pointed to by \a sibling; otherwise,
268 * NULL will be stored at the address pointed to by \a sibling
270 css_error
named_sibling_node(void *pw
, void *node
,
271 const css_qname
*qname
, void **sibling
)
274 dom_node
*n
= node
; /* the current node */
275 dom_node
*prev
; /* the previous node */
280 *sibling
= NULL
; /* default to nothing found */
282 /* Begin the search; the first iteration we do outside of the
283 * loop. Implementation detil: dom_node_get_previous_sibling()
284 * increments the reference counter on the returned node. A
285 * comment within named_parent_node() explains why we
286 * decrement it ASAP. */
287 err
= dom_node_get_previous_sibling(n
, &n
);
288 if (err
!= DOM_NO_ERR
) {
293 /* We're looking for the first ELEMENT sibling */
294 err
= dom_node_get_node_type(n
, &type
);
295 if (err
!= DOM_NO_ERR
) {
300 if (type
== DOM_ELEMENT_NODE
) {
301 /* We found an element node, does it have the
303 err
= dom_node_get_node_name(n
, &name
);
304 if (err
!= DOM_NO_ERR
) {
309 if (dom_string_lwc_isequal(name
,
311 /* The name is right, return it */
315 /* There's only one next-sibling element node
316 * and we've already found it, so if its name
317 * wasn't right, we return the default value
319 dom_string_unref(name
);
324 /* Not an element node, so we move on the the previous
325 * previous sibling */
326 err
= dom_node_get_previous_sibling(n
, &prev
);
327 if (err
!= DOM_NO_ERR
) {
341 * Find the first "subsequent-sibling" of the given element having the
344 * This search corresponds to the "~ foo" combinator in CSS and will
345 * find only "foo" element nodes that precede the given node (under
346 * the same parent) in the DOM. In CSS the tree is viewed top-down and
347 * in libdom it is viewed from the bottom-up; as a result "next" and
348 * "previous" are sometimes backwards. This is case-sensitive.
350 * \param pw Pointer to the current SVG parser state
351 * \param node Libdom SVG node
352 * \param qname Name of the sibling node to search for
353 * \param sibling Address at which to store the sibling node pointer
355 * \return Always returns CSS_OK
357 * \post If a suitable element is found, a pointer to it will be
358 * stored at the address pointed to by \a sibling; otherwise,
359 * NULL will be stored at the address pointed to by \a sibling
361 css_error
named_generic_sibling_node(void *pw
, void *node
,
362 const css_qname
*qname
, void **sibling
)
365 dom_node
*n
= node
; /* the current node */
366 dom_node
*prev
; /* the previous node */
372 *sibling
= NULL
; /* default to nothing found */
374 /* Begin the search; the first iteration we do outside of the
375 * loop. Implementation detil: dom_node_get_previous_sibling()
376 * increments the reference counter on the returned node. A
377 * comment within named_parent_node() explains why we
378 * decrement it ASAP. */
379 err
= dom_node_get_previous_sibling(n
, &n
);
380 if (err
!= DOM_NO_ERR
) {
385 err
= dom_node_get_node_type(n
, &type
);
386 if (err
!= DOM_NO_ERR
) {
391 if (type
== DOM_ELEMENT_NODE
) {
392 /* We only want ELEMENT nodes */
393 err
= dom_node_get_node_name(n
, &name
);
394 if (err
!= DOM_NO_ERR
) {
399 if (dom_string_lwc_isequal(name
,
401 /* Found one. Save it and stop the search */
402 dom_string_unref(name
);
408 dom_string_unref(name
);
411 /* This sibling wasn't an element with the desired
412 name, so move on to the previous sibling */
413 err
= dom_node_get_previous_sibling(n
, &prev
);
414 if (err
!= DOM_NO_ERR
) {
428 * Return a pointer to the given node's parent
430 * \param pw Pointer to the current SVG parser state
431 * \param node Libdom SVG node
432 * \param parent Address at which to store the node's parent pointer
434 * \return Always returns CSS_OK
436 css_error
parent_node(void *pw
, void *node
, void **parent
)
439 /* Libdom basically implements this for us */
440 dom_element_parent_node(node
, (struct dom_element
**)parent
);
442 /* See the comment in named_parent_node() for why we decrement
443 * this reference counter here. */
444 dom_node_unref(*parent
);
451 * Find the "next-sibling" of the given element
453 * This search corresponds "+ *" in CSS and will find the first
454 * element node that immediately precedes the given node under the
455 * same parent in the DOM. In CSS the tree is viewed top-down and in
456 * libdom it is viewed from the bottom-up; as a result "next" and
457 * "previous" are sometimes backwards.
459 * \param pw Pointer to the current SVG parser state
460 * \param node Libdom SVG node
461 * \param sibling Address at which to store the sibling node pointer
463 * \return Always returns CSS_OK
465 * \post If a suitable element is found, a pointer to it will be
466 * stored at the address pointed to by \a sibling; otherwise,
467 * NULL will be stored at the address pointed to by \a sibling
469 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
472 dom_node
*n
= node
; /* the current node */
473 dom_node
*prev
; /* the previous node */
477 *sibling
= NULL
; /* default to nothing found */
479 /* Begin the search; the first iteration we do outside of the
480 * loop. Implementation detil: dom_node_get_previous_sibling()
481 * increments the reference counter on the returned node. A
482 * comment within named_parent_node() explains why we
483 * decrement it ASAP. */
484 err
= dom_node_get_previous_sibling(n
, &n
);
485 if (err
!= DOM_NO_ERR
) {
490 err
= dom_node_get_node_type(n
, &type
);
491 if (err
!= DOM_NO_ERR
) {
496 if (type
== DOM_ELEMENT_NODE
) {
497 /* We found a sibling node that is also an
498 element and that's all we wanted. */
504 /* This sibling node was not an element; move on to
505 the previous sibling */
506 err
= dom_node_get_previous_sibling(n
, &prev
);
507 if (err
!= DOM_NO_ERR
) {
521 * Test the given node for the given name
523 * This will return true (via the "match" pointer) if the libdom node
524 * has the given name or if that name is the universal selector;
525 * otherwise it returns false. The comparison is case-sensitive. It
526 * corresponds to a rule like "body { ... }" in CSS.
528 * \param pw Pointer to the current SVG parser state
529 * \param node Libdom SVG node to test
530 * \param qname Name to check for
531 * \param match Pointer to the test result
533 * \return Always returns CSS_OK
535 css_error
node_has_name(void *pw
, void *node
,
536 const css_qname
*qname
, bool *match
)
538 struct svgtiny_parse_state
*state
;
542 /* Start by checking to see if qname is the universal selector */
543 state
= (struct svgtiny_parse_state
*)pw
;
544 if (lwc_string_isequal(qname
->name
,
545 state
->interned_universal
, match
) == lwc_error_ok
) {
547 /* It's the universal selector. In NetSurf, all node
548 * names match the universal selector, and nothing in
549 * the libcss documentation suggests another approach,
550 * so we follow NetSurf here. */
555 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
556 if (err
!= DOM_NO_ERR
) {
560 /* Unlike with HTML, SVG element names are case-sensitive */
561 *match
= dom_string_lwc_isequal(name
, qname
->name
);
562 dom_string_unref(name
);
569 * Test the given node for the given class
571 * This will return true (via the "match" pointer) if the libdom node
572 * has the given class. The comparison is case-sensitive. It
573 * corresponds to node.class in CSS.
575 * \param pw Pointer to the current SVG parser state
576 * \param node Libdom SVG node to test
577 * \param name Class name to check for
578 * \param match Pointer to the test result
580 * \return Always returns CSS_OK
582 css_error
node_has_class(void *pw
, void *node
,
583 lwc_string
*name
, bool *match
)
586 /* libdom implements this for us and apparently it cannot fail */
587 dom_element_has_class((dom_node
*)node
, name
, match
);
593 * Test the given node for the given id
595 * This will return true (via the "match" pointer) if the libdom node
596 * has the given id. The comparison is case-sensitive. It corresponds
599 * \param pw Pointer to the current SVG parser state
600 * \param node Libdom SVG node to test
601 * \param name Id to check for
602 * \param match Pointer to the test result
604 * \return Always returns CSS_OK
606 css_error
node_has_id(void *pw
, void *node
,
607 lwc_string
*name
, bool *match
)
611 struct svgtiny_parse_state
*state
;
613 attr
= NULL
; /* a priori the "id" attribute may not exist */
614 *match
= false; /* default to no match */
616 state
= (struct svgtiny_parse_state
*)pw
;
617 err
= dom_element_get_attribute((dom_node
*)node
,
618 state
->interned_id
, &attr
);
619 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
623 *match
= dom_string_lwc_isequal(attr
, name
);
624 dom_string_unref(attr
);
631 * Test the given node for the given attribute
633 * This will return true (via the "match" pointer) if the libdom node
634 * has an attribute with the given name. The comparison is
635 * case-sensitive. It corresponds to node[attr] in CSS.
637 * \param pw Pointer to the current SVG parser state
638 * \param node Libdom SVG node to test
639 * \param qname Attribute name to check for
640 * \param match Pointer to the test result
642 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
645 css_error
node_has_attribute(void *pw
, void *node
,
646 const css_qname
*qname
, bool *match
)
652 /* intern the attribute name as a dom_string so we can
653 * delegate to dom_element_has_attribute() */
654 err
= dom_string_create_interned(
655 (const uint8_t *) lwc_string_data(qname
->name
),
656 lwc_string_length(qname
->name
),
658 if (err
!= DOM_NO_ERR
) {
662 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
663 if (err
!= DOM_NO_ERR
) {
664 dom_string_unref(name
);
668 dom_string_unref(name
);
674 * Test the given node for an attribute with a specific value
676 * This will return true (via the "match" pointer) if the libdom node
677 * has an attribute with the given name and value. The comparison is
678 * case-sensitive. It corresponds to node[attr=value] in CSS.
680 * \param pw Pointer to the current SVG parser state
681 * \param node Libdom SVG node to test
682 * \param qname Attribute name to check for
683 * \param value Attribute value to check for
684 * \param match Pointer to the test result
686 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
687 * intern the attribute name (which usually indicates memory
690 css_error
node_has_attribute_equal(void *pw
, void *node
,
691 const css_qname
*qname
, lwc_string
*value
,
694 /* Implementation note: NetSurf always returns "no match" when
695 * the value is empty (length zero). We allow it, because why
700 dom_string
*attr_val
;
703 /* Intern the attribute name as a dom_string so we can
704 * use dom_element_get_attribute() */
705 err
= dom_string_create_interned(
706 (const uint8_t *) lwc_string_data(qname
->name
),
707 lwc_string_length(qname
->name
),
709 if (err
!= DOM_NO_ERR
) {
713 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
714 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
715 /* There was an error getting the attribute's value or
716 * the attribute doesn't exist. So, no match? */
717 dom_string_unref(name
);
722 /* Otherwise, we have the attribute value from the given node
723 * and all we need to do is compare. */
724 dom_string_unref(name
);
725 *match
= dom_string_lwc_isequal(attr_val
, value
);
726 dom_string_unref(attr_val
);