1 #include <libcss/libcss.h>
2 #include <strings.h> /* strncasecmp */
5 #include "svgtiny_internal.h"
7 /* select handler callbacks */
8 static css_error
node_name(void *pw
, void *node
, css_qname
*qname
);
9 static css_error
node_classes(void *pw
, void *node
,
10 lwc_string
***classes
, uint32_t *n_classes
);
11 static css_error
node_id(void *pw
, void *node
, lwc_string
**id
);
12 static css_error
named_ancestor_node(void *pw
, void *node
,
13 const css_qname
*qname
, void **ancestor
);
14 static css_error
named_parent_node(void *pw
, void *node
,
15 const css_qname
*qname
, void **parent
);
16 static css_error
named_sibling_node(void *pw
, void *node
,
17 const css_qname
*qname
, void **sibling
);
18 static css_error
named_generic_sibling_node(void *pw
, void *node
,
19 const css_qname
*qname
, void **sibling
);
20 static css_error
parent_node(void *pw
, void *node
, void **parent
);
21 static css_error
sibling_node(void *pw
, void *node
, void **sibling
);
22 static css_error
node_has_name(void *pw
, void *node
,
23 const css_qname
*qname
, bool *match
);
24 static css_error
node_has_class(void *pw
, void *node
,
25 lwc_string
*name
, bool *match
);
26 static css_error
node_has_id(void *pw
, void *node
,
27 lwc_string
*name
, bool *match
);
28 static css_error
node_has_attribute(void *pw
, void *node
,
29 const css_qname
*qname
, bool *match
);
30 static css_error
node_has_attribute_equal(void *pw
, void *node
,
31 const css_qname
*qname
, lwc_string
*value
,
33 static css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
34 const css_qname
*qname
, lwc_string
*value
,
36 static css_error
node_has_attribute_includes(void *pw
, void *node
,
37 const css_qname
*qname
, lwc_string
*word
,
39 static css_error
node_has_attribute_prefix(void *pw
, void *node
,
40 const css_qname
*qname
, lwc_string
*prefix
,
42 static css_error
node_has_attribute_suffix(void *pw
, void *node
,
43 const css_qname
*qname
, lwc_string
*suffix
,
45 static css_error
node_has_attribute_substring(void *pw
, void *node
,
46 const css_qname
*qname
, lwc_string
*substring
,
48 static css_error
node_is_root(void *pw
, void *node
, bool *match
);
49 static css_error
node_count_siblings(void *pw
, void *node
,
50 bool same_name
, bool after
, int32_t *count
);
51 static css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
);
52 static css_error
node_is_link(void *pw
, void *node
, bool *is_link
);
53 static css_error
node_is_visited(void *pw
, void *node
, bool *is_visited
);
54 static css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
);
55 static css_error
node_is_active(void *pw
, void *node
, bool *is_active
);
56 static css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
);
57 static css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
);
58 static css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
);
59 static css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
);
60 static css_error
node_is_target(void *pw
, void *node
, bool *is_target
);
61 static css_error
node_is_lang(void *pw
, void *node
,
62 lwc_string
*lang
, bool *is_lang
);
63 static css_error
node_presentational_hint(void *pw
, void *node
,
64 uint32_t *nhints
, css_hint
**hints
);
65 static css_error
ua_default_for_property(void *pw
, uint32_t property
,
67 static css_error
set_libcss_node_data(void *pw
, void *node
,
68 void *libcss_node_data
);
69 static css_error
get_libcss_node_data(void *pw
, void *node
,
70 void **libcss_node_data
);
72 /* select handler vtable */
73 static struct css_select_handler svgtiny_select_handler
;
77 * Resolve a relative URL to an absolute one by doing nothing. This is
78 * the simplest possible implementation of a URL resolver, needed for
81 css_error
svgtiny_resolve_url(void *pw
,
82 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
87 /* Copy the relative URL to the absolute one (the return
89 *abs
= lwc_string_ref(rel
);
94 * Create a stylesheet with the default set of params.
96 * \param sheet A stylesheet pointer, passed in by reference, that
97 * we use to store the newly-created stylesheet.
98 * \param inline_style True if this stylesheet represents an inline
99 * style, and false otherwise.
101 * \return The return value from css_stylesheet_create() is returned.
103 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
106 css_stylesheet_params params
;
108 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
109 params
.level
= CSS_LEVEL_DEFAULT
;
110 params
.charset
= NULL
;
113 params
.allow_quirks
= false;
114 params
.inline_style
= inline_style
;
115 params
.resolve
= svgtiny_resolve_url
;
116 params
.resolve_pw
= NULL
;
117 params
.import = NULL
;
118 params
.import_pw
= NULL
;
120 params
.color_pw
= NULL
;
122 params
.font_pw
= NULL
;
124 return css_stylesheet_create(¶ms
, sheet
);
128 /**************************/
129 /* libcss select handlers */
130 /**************************/
132 * From here on we implement the "select handler "API defined in
133 * libcss's include/libcss/select.h and discussed briefly in its
139 * Retrieve the given node's name
141 * \param pw Pointer to the current SVG parser state
142 * \param node Libdom SVG node
143 * \param qname Address at which to store the node name
145 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
147 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
151 struct svgtiny_parse_state
*state
;
153 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
154 if (err
!= DOM_NO_ERR
) {
158 state
= (struct svgtiny_parse_state
*)pw
;
159 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
161 err
= dom_string_intern(name
, &qname
->name
);
162 if (err
!= DOM_NO_ERR
) {
163 dom_string_unref(name
);
167 dom_string_unref(name
);
174 * Retrieve the given node's classes
176 * \param pw Pointer to the current SVG parser state
177 * \param node Libdom SVG node
178 * \param classes Address at which to store the class name array
179 * \param n_classes Address at which to store the length of the class
182 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
184 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
185 * because the underlying libdom function never fails
187 css_error
node_classes(void *pw
, void *node
,
188 lwc_string
***classes
, uint32_t *n_classes
)
193 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
195 /* The implementation does not do it, but the documentation
196 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
197 possible here, so we handle it to be on the safe side. */
198 if (err
!= DOM_NO_ERR
) {
207 * Retrieve the given node's id
209 * \param pw Pointer to the current SVG parser state
210 * \param node Libdom SVG node
211 * \param id Address at which to store the id
213 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
215 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
219 struct svgtiny_parse_state
*state
;
221 /* Begin with the assumption that this node has no id */
224 state
= (struct svgtiny_parse_state
*)pw
;
225 err
= dom_element_get_attribute((dom_node
*)node
,
226 state
->interned_id
, &attr
);
227 if (err
!= DOM_NO_ERR
) {
230 else if (attr
== NULL
) {
231 /* The node has no id attribute and our return value
232 is already set to NULL so we're done */
236 /* If we found an id attribute (a dom_string), intern it into
237 an lwc_string that we can return, and then cleanup the
239 err
= dom_string_intern(attr
, id
);
240 if (err
!= DOM_NO_ERR
) {
241 dom_string_unref(attr
);
244 dom_string_unref(attr
);
250 * Find the first ancestor of the given element having the given name
252 * This function thinly wraps dom_element_named_ancestor_node(), which
253 * performs exactly the search we want.
255 * \param pw Pointer to the current SVG parser state
256 * \param node Libdom SVG node whose ancestor we want
257 * \param qname Name of the ancestor node to search for
258 * \param ancestor Address at which to store the ancestor node pointer
260 * \return Always returns CSS_OK
262 * \post If a suitable element is found, a pointer to it will be
263 * stored at the address pointed to by \a ancestor; otherwise,
264 * NULL will be stored at the address pointed to by \a ancestor
266 css_error
named_ancestor_node(void *pw
, void *node
,
267 const css_qname
*qname
, void **ancestor
)
271 /* It's OK if node isn't an element, libdom checks for it. */
272 dom_element_named_ancestor_node((dom_element
*)node
,
274 (struct dom_element
**)ancestor
);
276 /* A comment in named_parent_node() explains why we unref
278 dom_node_unref(*ancestor
);
285 * Find the first parent of the given element having the given name
287 * \param pw Pointer to the current SVG parser state
288 * \param node Libdom SVG node
289 * \param qname Name of the parent node to search for
290 * \param parent Address at which to store the parent node pointer
292 * \return Always returns CSS_OK
294 * \post If a suitable element is found, a pointer to it will be
295 * stored at the address pointed to by \a parent; otherwise,
296 * NULL will be stored at the address pointed to by \a parent
298 css_error
named_parent_node(void *pw
, void *node
,
299 const css_qname
*qname
, void **parent
)
302 /* dom_element_named_parent_node() was invented to implement
303 * this select handler so there isn't much for us to do except
304 * call it. It's OK if node isn't an element, libdom checks
306 dom_element_named_parent_node((dom_element
*)node
,
308 (struct dom_element
**)parent
);
310 /* Implementation detail: dom_element_named_parent_node()
311 * increments the reference count of the parent element before
312 * returning it to us. According to docs/RefCnt in the libdom
313 * repository, this will prevent the parent element from being
314 * destroyed if it is pruned from the DOM. That sounds good,
315 * since we don't want to be using a pointer to an object that
316 * has been destroyed... but we also have no way of later
317 * decrementing the reference count ourselves, and don't want
318 * to make the returned node eternal. Decrementing the
319 * reference counter now allows it to be destroyed when the
320 * DOM no longer needs it, and so long as no other parts of
321 * libsvgtiny are messing with the DOM during parsing, that
322 * shouldn't (ha ha) cause any problems. */
323 dom_node_unref(*parent
);
330 * Find the "next-sibling" of the given element having the given name
332 * This search corresponds to the "+ foo" combinator in CSS and will
333 * find only "foo" element nodes that immediately precede the given
334 * node under the same parent in the DOM. In CSS the tree is viewed
335 * top-down and in libdom it is viewed from the bottom-up; as a result
336 * "next" and "previous" are sometimes backwards. This is case-sensitive.
338 * \param pw Pointer to the current SVG parser state
339 * \param node Libdom SVG node
340 * \param qname Name of the sibling node to search for
341 * \param sibling Address at which to store the sibling node pointer
343 * \return Always returns CSS_OK
345 * \post If a suitable element is found, a pointer to it will be
346 * stored at the address pointed to by \a sibling; otherwise,
347 * NULL will be stored at the address pointed to by \a sibling
349 css_error
named_sibling_node(void *pw
, void *node
,
350 const css_qname
*qname
, void **sibling
)
353 dom_node
*n
= node
; /* the current node */
354 dom_node
*prev
; /* the previous node */
359 *sibling
= NULL
; /* default to nothing found */
361 /* Begin the search; the first iteration we do outside of the
362 * loop. Implementation detil: dom_node_get_previous_sibling()
363 * increments the reference counter on the returned node. A
364 * comment within named_parent_node() explains why we
365 * decrement it ASAP. */
366 err
= dom_node_get_previous_sibling(n
, &n
);
367 if (err
!= DOM_NO_ERR
) {
372 /* We're looking for the first ELEMENT sibling */
373 err
= dom_node_get_node_type(n
, &type
);
374 if (err
!= DOM_NO_ERR
) {
379 if (type
== DOM_ELEMENT_NODE
) {
380 /* We found an element node, does it have the
382 err
= dom_node_get_node_name(n
, &name
);
383 if (err
!= DOM_NO_ERR
) {
388 if (dom_string_lwc_isequal(name
,
390 /* The name is right, return it */
394 /* There's only one next-sibling element node
395 * and we've already found it, so if its name
396 * wasn't right, we return the default value
398 dom_string_unref(name
);
403 /* Not an element node, so we move on the the previous
404 * previous sibling */
405 err
= dom_node_get_previous_sibling(n
, &prev
);
406 if (err
!= DOM_NO_ERR
) {
420 * Find the first "subsequent-sibling" of the given element having the
423 * This search corresponds to the "~ foo" combinator in CSS and will
424 * find only "foo" element nodes that precede the given node (under
425 * the same parent) in the DOM. In CSS the tree is viewed top-down and
426 * in libdom it is viewed from the bottom-up; as a result "next" and
427 * "previous" are sometimes backwards. This is case-sensitive.
429 * \param pw Pointer to the current SVG parser state
430 * \param node Libdom SVG node
431 * \param qname Name of the sibling node to search for
432 * \param sibling Address at which to store the sibling node pointer
434 * \return Always returns CSS_OK
436 * \post If a suitable element is found, a pointer to it will be
437 * stored at the address pointed to by \a sibling; otherwise,
438 * NULL will be stored at the address pointed to by \a sibling
440 css_error
named_generic_sibling_node(void *pw
, void *node
,
441 const css_qname
*qname
, void **sibling
)
444 dom_node
*n
= node
; /* the current node */
445 dom_node
*prev
; /* the previous node */
451 *sibling
= NULL
; /* default to nothing found */
453 /* Begin the search; the first iteration we do outside of the
454 * loop. Implementation detil: dom_node_get_previous_sibling()
455 * increments the reference counter on the returned node. A
456 * comment within named_parent_node() explains why we
457 * decrement it ASAP. */
458 err
= dom_node_get_previous_sibling(n
, &n
);
459 if (err
!= DOM_NO_ERR
) {
464 err
= dom_node_get_node_type(n
, &type
);
465 if (err
!= DOM_NO_ERR
) {
470 if (type
== DOM_ELEMENT_NODE
) {
471 /* We only want ELEMENT nodes */
472 err
= dom_node_get_node_name(n
, &name
);
473 if (err
!= DOM_NO_ERR
) {
478 if (dom_string_lwc_isequal(name
,
480 /* Found one. Save it and stop the search */
481 dom_string_unref(name
);
487 dom_string_unref(name
);
490 /* This sibling wasn't an element with the desired
491 name, so move on to the previous sibling */
492 err
= dom_node_get_previous_sibling(n
, &prev
);
493 if (err
!= DOM_NO_ERR
) {
507 * Return a pointer to the given node's parent
509 * \param pw Pointer to the current SVG parser state
510 * \param node Libdom SVG node
511 * \param parent Address at which to store the node's parent pointer
513 * \return Always returns CSS_OK
515 css_error
parent_node(void *pw
, void *node
, void **parent
)
518 /* Libdom basically implements this for us */
519 dom_element_parent_node(node
, (struct dom_element
**)parent
);
521 /* See the comment in named_parent_node() for why we decrement
522 * this reference counter here. */
523 dom_node_unref(*parent
);
530 * Find the "next-sibling" of the given element
532 * This search corresponds "+ *" in CSS and will find the first
533 * element node that immediately precedes the given node under the
534 * same parent in the DOM. In CSS the tree is viewed top-down and in
535 * libdom it is viewed from the bottom-up; as a result "next" and
536 * "previous" are sometimes backwards.
538 * \param pw Pointer to the current SVG parser state
539 * \param node Libdom SVG node
540 * \param sibling Address at which to store the sibling node pointer
542 * \return Always returns CSS_OK
544 * \post If a suitable element is found, a pointer to it will be
545 * stored at the address pointed to by \a sibling; otherwise,
546 * NULL will be stored at the address pointed to by \a sibling
548 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
551 dom_node
*n
= node
; /* the current node */
552 dom_node
*prev
; /* the previous node */
556 *sibling
= NULL
; /* default to nothing found */
558 /* Begin the search; the first iteration we do outside of the
559 * loop. Implementation detil: dom_node_get_previous_sibling()
560 * increments the reference counter on the returned node. A
561 * comment within named_parent_node() explains why we
562 * decrement it ASAP. */
563 err
= dom_node_get_previous_sibling(n
, &n
);
564 if (err
!= DOM_NO_ERR
) {
569 err
= dom_node_get_node_type(n
, &type
);
570 if (err
!= DOM_NO_ERR
) {
575 if (type
== DOM_ELEMENT_NODE
) {
576 /* We found a sibling node that is also an
577 element and that's all we wanted. */
583 /* This sibling node was not an element; move on to
584 the previous sibling */
585 err
= dom_node_get_previous_sibling(n
, &prev
);
586 if (err
!= DOM_NO_ERR
) {
600 * Test the given node for the given name
602 * This will return true (via the "match" pointer) if the libdom node
603 * has the given name or if that name is the universal selector;
604 * otherwise it returns false. The comparison is case-sensitive. It
605 * corresponds to a rule like "body { ... }" in CSS.
607 * \param pw Pointer to the current SVG parser state
608 * \param node Libdom SVG node to test
609 * \param qname Name to check for
610 * \param match Pointer to the test result
612 * \return Always returns CSS_OK
614 css_error
node_has_name(void *pw
, void *node
,
615 const css_qname
*qname
, bool *match
)
617 struct svgtiny_parse_state
*state
;
621 /* Start by checking to see if qname is the universal selector */
622 state
= (struct svgtiny_parse_state
*)pw
;
623 if (lwc_string_isequal(qname
->name
,
624 state
->interned_universal
, match
) == lwc_error_ok
) {
626 /* It's the universal selector. In NetSurf, all node
627 * names match the universal selector, and nothing in
628 * the libcss documentation suggests another approach,
629 * so we follow NetSurf here. */
634 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
635 if (err
!= DOM_NO_ERR
) {
639 /* Unlike with HTML, SVG element names are case-sensitive */
640 *match
= dom_string_lwc_isequal(name
, qname
->name
);
641 dom_string_unref(name
);
648 * Test the given node for the given class
650 * This will return true (via the "match" pointer) if the libdom node
651 * has the given class. The comparison is case-sensitive. It
652 * corresponds to node.class in CSS.
654 * \param pw Pointer to the current SVG parser state
655 * \param node Libdom SVG node to test
656 * \param name Class name to check for
657 * \param match Pointer to the test result
659 * \return Always returns CSS_OK
661 css_error
node_has_class(void *pw
, void *node
,
662 lwc_string
*name
, bool *match
)
665 /* libdom implements this for us and apparently it cannot fail */
666 dom_element_has_class((dom_node
*)node
, name
, match
);
672 * Test the given node for the given id
674 * This will return true (via the "match" pointer) if the libdom node
675 * has the given id. The comparison is case-sensitive. It corresponds
678 * \param pw Pointer to the current SVG parser state
679 * \param node Libdom SVG node to test
680 * \param name Id to check for
681 * \param match Pointer to the test result
683 * \return Always returns CSS_OK
685 css_error
node_has_id(void *pw
, void *node
,
686 lwc_string
*name
, bool *match
)
690 struct svgtiny_parse_state
*state
;
692 attr
= NULL
; /* a priori the "id" attribute may not exist */
693 *match
= false; /* default to no match */
695 state
= (struct svgtiny_parse_state
*)pw
;
696 err
= dom_element_get_attribute((dom_node
*)node
,
697 state
->interned_id
, &attr
);
698 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
702 *match
= dom_string_lwc_isequal(attr
, name
);
703 dom_string_unref(attr
);
710 * Test the given node for the given attribute
712 * This will return true (via the "match" pointer) if the libdom node
713 * has an attribute with the given name. The comparison is
714 * case-sensitive. It corresponds to node[attr] in CSS.
716 * \param pw Pointer to the current SVG parser state
717 * \param node Libdom SVG node to test
718 * \param qname Attribute name to check for
719 * \param match Pointer to the test result
721 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
724 css_error
node_has_attribute(void *pw
, void *node
,
725 const css_qname
*qname
, bool *match
)
731 /* intern the attribute name as a dom_string so we can
732 * delegate to dom_element_has_attribute() */
733 err
= dom_string_create_interned(
734 (const uint8_t *) lwc_string_data(qname
->name
),
735 lwc_string_length(qname
->name
),
737 if (err
!= DOM_NO_ERR
) {
741 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
742 if (err
!= DOM_NO_ERR
) {
743 dom_string_unref(name
);
747 dom_string_unref(name
);
753 * Test the given node for an attribute with a specific value
755 * This will return true (via the "match" pointer) if the libdom node
756 * has an attribute with the given name and value. The comparison is
757 * case-sensitive. It corresponds to node[attr=value] in CSS.
759 * \param pw Pointer to the current SVG parser state
760 * \param node Libdom SVG node to test
761 * \param qname Attribute name to check for
762 * \param value Attribute value to check for
763 * \param match Pointer to the test result
765 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
766 * intern the attribute name (which usually indicates memory
769 css_error
node_has_attribute_equal(void *pw
, void *node
,
770 const css_qname
*qname
, lwc_string
*value
,
773 /* Implementation note: NetSurf always returns "no match" when
774 * the value is empty (length zero). We allow it, because why
779 dom_string
*attr_val
;
782 /* Intern the attribute name as a dom_string so we can
783 * use dom_element_get_attribute() */
784 err
= dom_string_create_interned(
785 (const uint8_t *) lwc_string_data(qname
->name
),
786 lwc_string_length(qname
->name
),
788 if (err
!= DOM_NO_ERR
) {
792 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
793 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
794 /* There was an error getting the attribute's value or
795 * the attribute doesn't exist. So, no match? */
796 dom_string_unref(name
);
801 /* Otherwise, we have the attribute value from the given node
802 * and all we need to do is compare. */
803 dom_string_unref(name
);
804 *match
= dom_string_lwc_isequal(attr_val
, value
);
805 dom_string_unref(attr_val
);
812 * Test the given node for an attribute with a specific value,
813 * possibly followed by a single hyphen
815 * This will return true (via the "match" pointer) if the libdom node
816 * has an attribute with the given name and value or with the given
817 * name and a value that is followed by exactly one hyphen. The
818 * comparison is case-sensitive. This corresponds to [attr|=value]
821 * \param pw Pointer to the current SVG parser state
822 * \param node Libdom SVG node to test
823 * \param qname Attribute name to check for
824 * \param value Attribute value to check for
825 * \param match Pointer to the test result
827 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
828 * intern the attribute name (which usually indicates memory
831 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
832 const css_qname
*qname
, lwc_string
*value
,
835 /* Implementation note: NetSurf always returns "no match" when
836 * the value is empty (length zero). We allow it, because why
841 dom_string
*attr_val
;
844 const char *vdata
; /* to hold the data underlying "value" */
846 const char *avdata
; /* to hold the found attribute value data */
849 /* Intern the attribute name as a dom_string so we can
850 * use dom_element_get_attribute() */
851 err
= dom_string_create_interned(
852 (const uint8_t *) lwc_string_data(qname
->name
),
853 lwc_string_length(qname
->name
),
855 if (err
!= DOM_NO_ERR
) {
859 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
860 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
861 /* There was an error getting the attribute's value or
862 * the attribute doesn't exist. So, no match? */
863 dom_string_unref(name
);
868 /* Otherwise, we have the attribute value from the given node
869 * and all we need to do is compare. */
870 dom_string_unref(name
);
871 *match
= dom_string_lwc_isequal(attr_val
, value
);
873 /* Exact match, we're done */
874 dom_string_unref(attr_val
);
878 /* No exact match, try it with a hyphen on the end */
879 vdata
= lwc_string_data(value
); /* needle */
880 vdata_len
= lwc_string_length(value
);
881 avdata
= dom_string_data(attr_val
); /* haystack */
882 avdata_len
= dom_string_byte_length(attr_val
);
883 dom_string_unref(attr_val
);
885 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
886 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
887 /* If there's a hyphen in the right position,
888 * it suffices to compare the strings only up
899 * Test the given node for an attribute whose value is a
900 * space-separated list of words, one of which is the given word
902 * This will return true (via the "match" pointer) if the libdom node
903 * has an attribute with the given name and whose value when
904 * considered as a space-separated list of words contains the given
905 * word. The comparison is case-sensitive. This corresponds to
906 * [attr~=value] in CSS.
908 * \param pw Pointer to the current SVG parser state
909 * \param node Libdom SVG node to test
910 * \param qname Attribute name to check for
911 * \param word Value word to check for
912 * \param match Pointer to the test result
914 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
915 * intern the attribute name (which usually indicates memory
918 css_error
node_has_attribute_includes(void *pw
, void *node
,
919 const css_qname
*qname
, lwc_string
*word
,
925 dom_string
*attr_val
;
927 size_t wordlen
; /* length of "word" */
929 /* pointers used to parse a space-separated list of words */
934 *match
= false; /* default to no match */
936 wordlen
= lwc_string_length(word
);
938 /* In this case, the spec says that "if 'val' is the
939 * empty string, it will never represent anything." */
943 /* Intern the attribute name as a dom_string so we can
944 * use dom_element_get_attribute() */
945 err
= dom_string_create_interned(
946 (const uint8_t *) lwc_string_data(qname
->name
),
947 lwc_string_length(qname
->name
),
949 if (err
!= DOM_NO_ERR
) {
953 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
954 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
955 /* There was an error getting the attribute's value or
956 * the attribute doesn't exist. So, no match? */
957 dom_string_unref(name
);
961 /* Parse the list comparing each word against "word" */
962 start
= dom_string_data(attr_val
);
963 end
= start
+ dom_string_byte_length(attr_val
);
964 dom_string_unref(attr_val
);
966 for (p
= start
; p
<= end
; p
++) {
967 /* Move forward until we find the end of the first word */
968 if (*p
== ' ' || *p
== '\0') {
969 /* If the length of that word is the length of the
970 * word we're looking for, do the comparison. */
971 if ((size_t) (p
- start
) == wordlen
&&
973 lwc_string_data(word
),
978 /* No match? Set "start" to the beginning of
979 * the next word and loop. */
989 * Test the given node for an attribute whose value begins with the
992 * This will return true (via the "match" pointer) if the libdom node
993 * has an attribute with the given name and whose value begins with
994 * the given prefix string. The comparison is case-sensitive. This
995 * corresponds to [attr^=value] in CSS.
997 * \param pw Pointer to the current SVG parser state
998 * \param node Libdom SVG node to test
999 * \param qname Attribute name to check for
1000 * \param prefix Value prefix to check for
1001 * \param match Pointer to the test result
1003 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1004 * intern the attribute name (which usually indicates memory
1007 css_error
node_has_attribute_prefix(void *pw
, void *node
,
1008 const css_qname
*qname
, lwc_string
*prefix
,
1013 dom_string
*attr_val
;
1015 const char *avdata
; /* attribute value data */
1016 size_t avdata_len
; /* length of that attribute value data */
1017 size_t prefixlen
; /* length of "prefix" */
1019 prefixlen
= lwc_string_length(prefix
);
1020 if (prefixlen
== 0) {
1021 /* In this case, the spec says that "if 'val' is the
1022 * empty string, it will never represent anything." */
1026 /* Intern the attribute name as a dom_string so we can
1027 * use dom_element_get_attribute() */
1028 err
= dom_string_create_interned(
1029 (const uint8_t *) lwc_string_data(qname
->name
),
1030 lwc_string_length(qname
->name
),
1032 if (err
!= DOM_NO_ERR
) {
1036 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1037 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1038 /* There was an error getting the attribute's value or
1039 * the attribute doesn't exist. So, no match? */
1040 dom_string_unref(name
);
1045 /* Otherwise, we have the attribute value from the given node,
1046 * and the first thing we want to do is check to see if the
1047 * whole thing matches the prefix. */
1048 dom_string_unref(name
);
1049 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
1051 /* If not, check to see if an, uh, prefix matches the
1053 if (*match
== false) {
1054 avdata
= dom_string_data(attr_val
);
1055 avdata_len
= dom_string_byte_length(attr_val
);
1056 if ((avdata_len
>= prefixlen
) &&
1057 (strncasecmp(avdata
,
1058 lwc_string_data(prefix
),
1060 /* Use strncasecmp to compare only the first
1061 * "n" characters, where "n" is the length of
1067 dom_string_unref(attr_val
);
1074 * Test the given node for an attribute whose value end with the
1077 * This will return true (via the "match" pointer) if the libdom node
1078 * has an attribute with the given name and whose value ends with
1079 * the given suffix string. The comparison is case-sensitive. This
1080 * corresponds to [attr$=value] in CSS.
1082 * \param pw Pointer to the current SVG parser state
1083 * \param node Libdom SVG node to test
1084 * \param qname Attribute name to check for
1085 * \param suffix Value suffix to check for
1086 * \param match Pointer to the test result
1088 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1089 * intern the attribute name (which usually indicates memory
1092 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1093 const css_qname
*qname
, lwc_string
*suffix
,
1098 dom_string
*attr_val
;
1100 const char *avdata
; /* attribute value data */
1101 size_t avdata_len
; /* length of that attribute value data */
1102 size_t suffixlen
; /* length of "suffix" */
1104 /* convenience pointer we'll use when matching the suffix */
1105 const char *suffix_start
;
1107 suffixlen
= lwc_string_length(suffix
);
1108 if (suffixlen
== 0) {
1109 /* In this case, the spec says that "if 'val' is the
1110 * empty string, it will never represent anything." */
1114 /* Intern the attribute name as a dom_string so we can
1115 * use dom_element_get_attribute() */
1116 err
= dom_string_create_interned(
1117 (const uint8_t *) lwc_string_data(qname
->name
),
1118 lwc_string_length(qname
->name
),
1120 if (err
!= DOM_NO_ERR
) {
1124 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1125 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1126 /* There was an error getting the attribute's value or
1127 * the attribute doesn't exist. So, no match? */
1128 dom_string_unref(name
);
1133 /* Otherwise, we have the attribute value from the given node,
1134 * and the first thing we want to do is check to see if the
1135 * whole thing matches the suffix. */
1136 dom_string_unref(name
);
1137 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1139 /* If not, check to see if an, uh, suffix matches the
1141 if (*match
== false) {
1142 avdata
= dom_string_data(attr_val
);
1143 avdata_len
= dom_string_byte_length(attr_val
);
1145 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1147 if ((avdata_len
>= suffixlen
) &&
1148 (strncasecmp(suffix_start
,
1149 lwc_string_data(suffix
),
1151 /* Use strncasecmp to compare only the last
1152 * "n" characters, where "n" is the length of
1158 dom_string_unref(attr_val
);
1165 * Implement node_has_attribute_substring() with optional case-
1166 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1167 * not supported by libcss yet, but it allows us to factor out some
1170 static css_error
_node_has_attribute_substring(void *pw
, void *node
,
1171 const css_qname
*qname
, lwc_string
*substring
,
1172 bool *match
, bool insensitive
)
1176 dom_string
*attr_val
;
1178 size_t attr_len
; /* length of attr_val */
1179 size_t substrlen
; /* length of "substring" */
1181 /* Convenience pointers we use when comparing substrings */
1185 substrlen
= lwc_string_length(substring
);
1186 if (substrlen
== 0) {
1187 /* In this case, the spec says that "if 'val' is the
1188 * empty string, it will never represent anything." */
1192 /* Intern the attribute name as a dom_string so we can
1193 * use dom_element_get_attribute() */
1194 err
= dom_string_create_interned(
1195 (const uint8_t *) lwc_string_data(qname
->name
),
1196 lwc_string_length(qname
->name
),
1198 if (err
!= DOM_NO_ERR
) {
1202 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1203 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1204 /* There was an error getting the attribute's value or
1205 * the attribute doesn't exist. So, no match? */
1206 dom_string_unref(name
);
1211 /* Otherwise, we have the attribute value from the given node,
1212 * and the first thing we want to do is check to see if the
1213 * whole thing matches the substring. */
1214 dom_string_unref(name
);
1217 *match
= dom_string_caseless_lwc_isequal(attr_val
, substring
);
1220 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1223 /* If not, check to see if an, uh, substring matches the
1225 if (*match
== false) {
1226 p
= dom_string_data(attr_val
);
1228 /* Check every long-enough suffix for a prefix match */
1229 attr_len
= dom_string_byte_length(attr_val
);
1230 if (attr_len
>= substrlen
) {
1231 p_max
= p
+ attr_len
- substrlen
;
1232 while (p
<= p_max
) {
1234 lwc_string_data(substring
),
1244 dom_string_unref(attr_val
);
1250 * Test the given node for an attribute whose value contains the
1253 * This will return true (via the "match" pointer) if the libdom node
1254 * has an attribute with the given name and whose value contains the
1255 * given substring. The comparison is case-sensitive. This corresponds
1256 * to [attr*=value] in CSS.
1258 * \param pw Pointer to the current SVG parser state
1259 * \param node Libdom SVG node to test
1260 * \param qname Attribute name to check for
1261 * \param substring Value substring to check for
1262 * \param match Pointer to the test result
1264 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1265 * intern the attribute name (which usually indicates memory
1268 css_error
node_has_attribute_substring(void *pw
, void *node
,
1269 const css_qname
*qname
, lwc_string
*substring
,
1272 return _node_has_attribute_substring(pw
, node
, qname
, substring
,
1278 * Test whether or not the given node is the document's root element
1279 * This corresponds to the CSS :root pseudo-selector.
1281 * \param pw Pointer to the current SVG parser state
1282 * \param node Libdom SVG node to test
1283 * \param match Pointer to the test result
1285 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1287 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1294 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1295 if (err
!= DOM_NO_ERR
) {
1299 /* It's the root element if it doesn't have a parent element */
1300 if (parent
!= NULL
) {
1301 err
= dom_node_get_node_type(parent
, &type
);
1302 dom_node_unref(parent
);
1303 if (err
!= DOM_NO_ERR
) {
1306 if (type
!= DOM_DOCUMENT_NODE
) {
1307 /* DOM_DOCUMENT_NODE is the only allowable
1308 * type of parent node for the root element */
1320 * Used internally in node_count_siblings() to "count" the given
1321 * sibling node. It factors out the node type and name checks.
1323 static int node_count_siblings_check(dom_node
*dnode
,
1330 dom_string
*dnode_name
;
1332 /* We flip this to 1 if/when we count this node */
1335 if (dnode
== NULL
) {
1339 exc
= dom_node_get_node_type(dnode
, &type
);
1340 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1341 /* We only count element siblings */
1345 /* ... with the right name */
1348 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1350 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1351 if (dom_string_isequal(name
,
1355 dom_string_unref(dnode_name
);
1366 * Count the given node's sibling elements
1368 * This counts the given node's sibling elements in one direction,
1369 * either forwards or backwards, in the DOM. Keep in mind that the
1370 * libdom tree is upside-down compared to the CSS one; so "next" and
1371 * "previous" are actually reversed; the default is to count preceding
1372 * libdom siblings which correspond to subsequent CSS siblings.
1374 * This operation is central to the CSS :first-child, :nth-child, and
1375 * :last-child (et cetera) pseudo-selectors.
1377 * If same_name is true, then only nodes having the same
1378 * (case-sensitive) name as the given node are counted.
1380 * \param pw Pointer to the current SVG parser state
1381 * \param node Libdom SVG node whose siblings we're counting
1382 * \param same_name Whether or not to count only siblings having
1383 * the same name as the given node
1384 * \param after Count subsequent siblings rather than precedent
1385 * ones (the default)
1386 * \param count Pointer to the return value, the number of sibling
1389 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1391 css_error
node_count_siblings(void *pw
, void *node
,
1392 bool same_name
, bool after
, int32_t *count
)
1396 dom_node
*dnode
; /* node, but with the right type */
1397 dom_string
*dnode_name
;
1398 dom_node
*next
; /* "next" sibling (depends on direction) */
1400 /* Pointer to the "next sibling" function */
1401 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1406 dnode
= (dom_node
*)node
;
1408 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1409 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1414 /* Increment the reference counter for dnode for as long as
1415 * we retain a reference to it. */
1416 dnode
= dom_node_ref(dnode
);
1418 next_func
= dom_node_get_previous_sibling
;
1420 next_func
= dom_node_get_next_sibling
;
1424 exc
= next_func(dnode
, &next
);
1425 if (exc
!= DOM_NO_ERR
) {
1429 /* If next_func worked, we're about to swap "next"
1430 * with "dnode" meaning that we will no longer retain
1431 * a reference to the current dnode. */
1432 dom_node_unref(dnode
);
1435 *count
+= node_count_siblings_check(dnode
,
1438 } while (dnode
!= NULL
);
1440 if (dnode_name
!= NULL
) {
1441 dom_string_unref(dnode_name
);
1449 * Determine whether or not the given element is empty
1451 * An element is "nonempty" if it has a child that is either an
1452 * element node or a text node.
1454 * \param pw Pointer to the current SVG parser state
1455 * \param node Libdom SVG node to check for emptiness
1456 * \param is_empty Pointer to the return value
1458 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1460 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1463 dom_node
*child
; /* current child node pointer */
1464 dom_node
*next
; /* next child node pointer */
1465 dom_node_type type
; /* what type of node is "child" */
1468 /* Assume that it's empty by default */
1471 /* Get the given node's first child. Implementation detail:
1472 * this increments the reference counter on the child node. */
1473 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1474 if (err
!= DOM_NO_ERR
) {
1478 /* And now loop through all children looking for a
1479 * text/element node. If we find one, the original
1480 * node is "nonempty" */
1481 while (child
!= NULL
) {
1482 err
= dom_node_get_node_type(child
, &type
);
1483 if (err
!= DOM_NO_ERR
) {
1484 dom_node_unref(child
);
1488 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1490 dom_node_unref(child
);
1494 err
= dom_node_get_next_sibling(child
, &next
);
1495 if (err
!= DOM_NO_ERR
) {
1496 dom_node_unref(child
);
1500 /* If we're moving to the next node, we can release
1501 * the reference to the current one */
1502 dom_node_unref(child
);
1511 * Determine whether or not the given node is a link
1513 * A node is a link if it is an element node whose name is "a" and if
1514 * it has an "href" attribute (case-sensitive). This selector
1515 * corresponds to node:link pseudo-class in CSS.
1517 * This pseudo-class is a bit awkward because the two standards (HTML5
1518 * and CSS) disagree on what it means, and because libsvgtiny does not
1519 * have enough information to determine if a link has been "visited"
1520 * yet -- that's a UI property. CSS says that :link is for unvisited
1521 * links, which we can't determine. HTML5 says that each link must
1522 * be either a :link or :visited. Since we can't decide either way,
1523 * It seems less wrong to declare that all links are unvisited; i.e.
1524 * that they match :link.
1526 * \param pw Pointer to the current SVG parser state
1527 * \param node Libdom SVG node to check
1528 * \param is_link Pointer to the boolean return value
1530 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1532 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1535 dom_node
*dnode
; /* node, but with the right type */
1536 dom_string
*dnode_name
;
1538 struct svgtiny_parse_state
* state
;
1540 dnode
= (dom_node
*)node
;
1542 has_href
= false; /* assume no href attribute */
1543 *is_link
= false; /* assume that it's not a link */
1545 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1546 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1550 state
= (struct svgtiny_parse_state
*)pw
;
1551 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1552 exc
= dom_element_has_attribute(node
,
1553 state
->interned_href
,
1555 if (exc
== DOM_NO_ERR
&& has_href
) {
1560 dom_string_unref(dnode_name
);
1565 * Check if the given node is a link that has been visited already
1567 * This check always fails because the SVG DOM does not have the
1568 * necessary information (it's a UI property).
1570 * \param pw Pointer to the current SVG parser state; unused
1571 * \param node Libdom SVG node to check; unused
1572 * \param is_visited Pointer to the boolean return value
1574 * \return Always returns CSS_OK
1576 css_error
node_is_visited(void *pw
, void *node
, bool *is_visited
)
1580 *is_visited
= false;
1586 * Check if the given node is being "hovered" over
1588 * This check always fails because the SVG DOM does not have the
1589 * necessary information (it's a UI property).
1591 * \param pw Pointer to the current SVG parser state; unused
1592 * \param node Libdom SVG node to check; unused
1593 * \param is_hover Pointer to the boolean return value
1595 * \return Always returns CSS_OK
1597 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1607 * Check if the given node is "active"
1609 * This check always fails because the SVG DOM does not have the
1610 * necessary information (it's a UI property).
1612 * \param pw Pointer to the current SVG parser state; unused
1613 * \param node Libdom SVG node to check; unused
1614 * \param is_active Pointer to the boolean return value
1616 * \return Always returns CSS_OK
1618 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1628 * Check if the given node has the focus
1630 * This check always fails because the SVG DOM does not have the
1631 * necessary information (it's a UI property).
1633 * \param pw Pointer to the current SVG parser state; unused
1634 * \param node Libdom SVG node to check; unused
1635 * \param is_focus Pointer to the boolean return value
1637 * \return Always returns CSS_OK
1639 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1649 * Check if the given node is enabled
1651 * This check always fails because the SVG DOM does not have the
1652 * necessary information (it's a UI property).
1654 * \param pw Pointer to the current SVG parser state; unused
1655 * \param node Libdom SVG node to check; unused
1656 * \param is_enabled Pointer to the boolean return value
1658 * \return Always returns CSS_OK
1660 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1664 *is_enabled
= false;
1670 * Check if the given node is disabled
1672 * This check always fails because the SVG DOM does not have the
1673 * necessary information (it's a UI property). Beware, until they are
1674 * implemented, this is NOT the logical negation of node_is_enabled!
1676 * \param pw Pointer to the current SVG parser state; unused
1677 * \param node Libdom SVG node to check; unused
1678 * \param is_disabled Pointer to the boolean return value
1680 * \return Always returns CSS_OK
1682 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1686 *is_disabled
= false;
1692 * Test whether or not the given node is "checked"
1694 * This test always fails because the SVG DOM does not have the
1695 * necessary information (it's a UI property).
1697 * \param pw Pointer to the current SVG parser state; unused
1698 * \param node Libdom SVG node to check; unused
1699 * \param is_checked Pointer to the boolean return value
1701 * \return Always returns CSS_OK
1703 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1707 *is_checked
= false;
1713 * Check if the given node is the "target" of the document URL
1715 * This test always fails because the SVG DOM does not have the
1716 * necessary information (it's a UI property).
1718 * \param pw Pointer to the current SVG parser state; unused
1719 * \param node Libdom SVG node to check; unused
1720 * \param is_target Pointer to the boolean return value
1722 * \return Always returns CSS_OK
1724 css_error
node_is_target(void *pw
, void *node
, bool *is_target
)
1734 * Check if the given node is the given language
1736 * This test is corresponds to the CSS :lang() selector and is not
1737 * fully implemented yet: it looks only for "lang" attributes on the
1738 * given element and its parents, and performs a simple substring
1739 * check. This results in a partial implementation of CSS Level 3 for
1740 * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
1741 * favor of the "lang" attribute that is defined only in SVG 2.0.
1743 * \param pw Pointer to the current SVG parser state; unused
1744 * \param node Libdom SVG node to check
1745 * \param lang The language to match
1746 * \param is_lang Pointer to the boolean return value
1748 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1750 static css_error
node_is_lang(void *pw
, void *node
,
1751 lwc_string
*lang
, bool *is_lang
)
1754 /* SVG2 elements support both "lang" and "xml:lang"
1755 * attributes; earlier versions have only the XML
1756 * attribute. It would not be too hard to add support for
1757 * xml:lang" here. The main difficulty standing in the way of
1758 * a full Level 4 implementation is the complexity of the
1761 * https://www.w3.org/TR/selectors-4/#the-lang-pseudo
1766 dom_exception d_err
;
1767 dom_node
*n
; /* current node */
1768 dom_node
*p
; /* parent node */
1769 bool match
; /* retval from node_has_attribute_substring() */
1771 /* Define the attribute name "lang" that we're looking for.
1772 * We only use a css_qname here because that's what the
1773 * node_has_attribute_substring() takes; the namespace
1774 * portion of it is irrelevant. */
1778 if (lwc_intern_string("lang", 4, &attr
.name
) != lwc_error_ok
) {
1782 *is_lang
= false; /* default to no match */
1783 n
= (dom_node
*)node
;
1785 /* Loop through all parents of the given node looking for a
1786 * substring match */
1788 c_err
= _node_has_attribute_substring(pw
, (void *)n
, &attr
,
1789 lang
, &match
, true);
1790 if (c_err
!= CSS_OK
) {
1791 lwc_string_destroy(attr
.name
);
1795 /* matched this element; we're done */
1796 lwc_string_destroy(attr
.name
);
1801 /* no match on this element, try its parent */
1802 d_err
= dom_node_get_parent_node(n
, &p
);
1803 if (d_err
!= DOM_NO_ERR
) {
1804 lwc_string_destroy(attr
.name
);
1810 /* If we never find a match we may wind up here */
1811 lwc_string_destroy(attr
.name
);
1817 * Return presentational hints for the given node
1819 * Unless an SVG is being rendered from within an HTML document,
1820 * there are no presentational hints. We always return an empty
1823 * \param pw Pointer to the current SVG parser state; unused
1824 * \param node Libdom SVG node whose hints we want; unused
1825 * \param nhints How many hints are returned (return by reference)
1826 * \param hints Array of css_hint structures (return by reference)
1828 * \return Always returns CSS_OK
1830 css_error
node_presentational_hint(void *pw
, void *node
,
1831 uint32_t *nhints
, css_hint
**hints
)
1842 * User-agent defaults for CSS properties
1844 * For the moment, we provide no defaults, because libsvgtiny does not
1845 * yet support any CSS properties that might need them.
1847 * \param pw Pointer to the current SVG parser state; unused
1848 * \param property LibCSS property identifier; unused
1849 * \param hint Pointer to hint object (a return value); unused
1851 * \return Always returns CSS_INVALID
1853 css_error
ua_default_for_property(void *pw
, uint32_t property
,
1864 * A "user handler" function that we pass to dom_node_set_user_data()
1865 * in set_libcss_node_data(). The set_libcss_node_data() documentation
1866 * says that if the node (on which data is set) is is deleted or
1867 * cloned, or if its ancestors are modified, then we must call
1868 * css_libcss_node_data_handler() on the user data. This function
1869 * basically just checks to see which DOM event has happened and
1870 * calls css_libcss_node_data_handler() when any of those criteria
1873 static void svgtiny_dom_user_data_handler(dom_node_operation operation
,
1874 dom_string
*key
, void *data
, struct dom_node
*src
,
1875 struct dom_node
*dst
)
1877 /* We only care about the userdata that is identified by our
1878 * userdata key. Unfortunately I see no obvious way to obtain
1879 * the copy of the userdata key that is already interned in
1880 * svgtiny_strings.h; so we duplicate it here (ugh). */
1882 dom_string_create((const uint8_t *)"_libcss_user_data", 17, &str
);
1883 if (dom_string_isequal(str
,key
) == false || data
== NULL
) {
1884 /* Wrong key, or no data */
1888 /* Check the DOM operation, and make the corresponding call to
1889 * css_libcss_node_data_handler(). No error handling is done.
1891 switch (operation
) {
1892 case DOM_NODE_CLONED
:
1893 css_libcss_node_data_handler(&svgtiny_select_handler
,
1895 NULL
, src
, dst
, data
);
1897 case DOM_NODE_RENAMED
:
1898 css_libcss_node_data_handler(&svgtiny_select_handler
,
1900 NULL
, src
, NULL
, data
);
1902 case DOM_NODE_IMPORTED
:
1903 case DOM_NODE_ADOPTED
:
1904 case DOM_NODE_DELETED
:
1905 css_libcss_node_data_handler(&svgtiny_select_handler
,
1907 NULL
, src
, NULL
, data
);
1909 /* Our list of cases should have been exhaustive */
1915 * Store libcss data on a node
1917 * This is part of the libcss select handler API that we need to
1918 * implement. It is essentially a thin dom_node_set_user_data()
1921 * \param pw Pointer to the current SVG parser state
1922 * \param node Libdom SVG node on which to store the data
1923 * \param libcss_node_data Pointer to the data to store
1925 * \return Always returns CSS_OK
1927 css_error
set_libcss_node_data(void *pw
, void *node
,
1928 void *libcss_node_data
)
1930 struct svgtiny_parse_state
*state
;
1933 /* A unique "userdata key" (a string) is used to identify this
1934 * data. The fourth parameter (NULL) is a "user handler
1935 * function." We will eventually have one, but for now we set
1936 * it to NULL to avoid a circular reference mess that would
1937 * break the build temporarily. */
1938 state
= (struct svgtiny_parse_state
*)pw
;
1939 dom_node_set_user_data((dom_node
*)node
,
1940 state
->interned_userdata_key
,
1945 /* dom_node_set_user_data() always returns DOM_NO_ERR */
1951 * Retrieve libcss data from a node
1953 * This is part of the libcss select handler API that we need to
1954 * implement. It is essentially a thin dom_node_get_user_data()
1957 * \param pw Pointer to the current SVG parser state
1958 * \param node Libdom SVG node from which to get the data
1959 * \param libcss_node_data Address at which to store a pointer to the data
1961 * \return Always returns CSS_OK
1963 css_error
get_libcss_node_data(void *pw
, void *node
,
1964 void **libcss_node_data
)
1966 struct svgtiny_parse_state
*state
;
1968 /* A unique "userdata key" (a string) is used to identify this
1970 state
= (struct svgtiny_parse_state
*)pw
;
1971 dom_node_get_user_data((dom_node
*)node
,
1972 state
->interned_userdata_key
,
1975 /* dom_node_get_user_data() always returns DOM_NO_ERR */
1981 * The vtable of select handler callbacks passed by libsvgtiny to
1982 * css_select_style().
1984 static css_select_handler svgtiny_select_handler
= {
1985 CSS_SELECT_HANDLER_VERSION_1
,
1989 named_ancestor_node
,
1992 named_generic_sibling_node
,
1999 node_has_attribute_equal
,
2000 node_has_attribute_dashmatch
,
2001 node_has_attribute_includes
,
2002 node_has_attribute_prefix
,
2003 node_has_attribute_suffix
,
2004 node_has_attribute_substring
,
2006 node_count_siblings
,
2018 node_presentational_hint
,
2019 ua_default_for_property
,
2020 set_libcss_node_data
,
2021 get_libcss_node_data
,