1 #include <libcss/libcss.h>
2 #include <strings.h> /* strncasecmp */
5 #include "svgtiny_internal.h"
7 static css_error
node_name(void *pw
, void *node
, css_qname
*qname
);
8 static css_error
node_classes(void *pw
, void *node
,
9 lwc_string
***classes
, uint32_t *n_classes
);
10 static css_error
node_id(void *pw
, void *node
, lwc_string
**id
);
11 static css_error
named_parent_node(void *pw
, void *node
,
12 const css_qname
*qname
, void **parent
);
13 static css_error
named_sibling_node(void *pw
, void *node
,
14 const css_qname
*qname
, void **sibling
);
15 static css_error
named_generic_sibling_node(void *pw
, void *node
,
16 const css_qname
*qname
, void **sibling
);
17 static css_error
parent_node(void *pw
, void *node
, void **parent
);
18 static css_error
sibling_node(void *pw
, void *node
, void **sibling
);
19 static css_error
node_has_name(void *pw
, void *node
,
20 const css_qname
*qname
, bool *match
);
21 static css_error
node_has_class(void *pw
, void *node
,
22 lwc_string
*name
, bool *match
);
23 static css_error
node_has_id(void *pw
, void *node
,
24 lwc_string
*name
, bool *match
);
25 static css_error
node_has_attribute(void *pw
, void *node
,
26 const css_qname
*qname
, bool *match
);
27 static css_error
node_has_attribute_equal(void *pw
, void *node
,
28 const css_qname
*qname
, lwc_string
*value
,
30 static css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
31 const css_qname
*qname
, lwc_string
*value
,
33 static css_error
node_has_attribute_includes(void *pw
, void *node
,
34 const css_qname
*qname
, lwc_string
*word
,
36 static css_error
node_has_attribute_prefix(void *pw
, void *node
,
37 const css_qname
*qname
, lwc_string
*prefix
,
39 static css_error
node_has_attribute_suffix(void *pw
, void *node
,
40 const css_qname
*qname
, lwc_string
*suffix
,
42 static css_error
node_has_attribute_substring(void *pw
, void *node
,
43 const css_qname
*qname
, lwc_string
*substring
,
45 static css_error
node_is_root(void *pw
, void *node
, bool *match
);
46 static css_error
node_count_siblings(void *pw
, void *node
,
47 bool same_name
, bool after
, int32_t *count
);
48 static css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
);
49 static css_error
node_is_link(void *pw
, void *node
, bool *is_link
);
50 static css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
);
51 static css_error
node_is_active(void *pw
, void *node
, bool *is_active
);
52 static css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
);
53 static css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
);
57 * Resolve a relative URL to an absolute one by doing nothing. This is
58 * the simplest possible implementation of a URL resolver, needed for
61 css_error
svgtiny_resolve_url(void *pw
,
62 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
67 /* Copy the relative URL to the absolute one (the return
69 *abs
= lwc_string_ref(rel
);
74 * Create a stylesheet with the default set of params.
76 * \param sheet A stylesheet pointer, passed in by reference, that
77 * we use to store the newly-created stylesheet.
78 * \param inline_style True if this stylesheet represents an inline
79 * style, and false otherwise.
81 * \return The return value from css_stylesheet_create() is returned.
83 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
86 css_stylesheet_params params
;
88 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
89 params
.level
= CSS_LEVEL_DEFAULT
;
90 params
.charset
= NULL
;
93 params
.allow_quirks
= false;
94 params
.inline_style
= inline_style
;
95 params
.resolve
= svgtiny_resolve_url
;
96 params
.resolve_pw
= NULL
;
98 params
.import_pw
= NULL
;
100 params
.color_pw
= NULL
;
102 params
.font_pw
= NULL
;
104 return css_stylesheet_create(¶ms
, sheet
);
108 /**************************/
109 /* libcss select handlers */
110 /**************************/
112 * From here on we implement the "select handler "API defined in
113 * libcss's include/libcss/select.h and discussed briefly in its
119 * Retrieve the given node's name
121 * \param pw Pointer to the current SVG parser state
122 * \param node Libdom SVG node
123 * \param qname Address at which to store the node name
125 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
127 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
131 struct svgtiny_parse_state
*state
;
133 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
134 if (err
!= DOM_NO_ERR
) {
138 state
= (struct svgtiny_parse_state
*)pw
;
139 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
141 err
= dom_string_intern(name
, &qname
->name
);
142 if (err
!= DOM_NO_ERR
) {
143 dom_string_unref(name
);
147 dom_string_unref(name
);
154 * Retrieve the given node's classes
156 * \param pw Pointer to the current SVG parser state
157 * \param node Libdom SVG node
158 * \param classes Address at which to store the class name array
159 * \param n_classes Address at which to store the length of the class
162 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
164 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
165 * because the underlying libdom function never fails
167 css_error
node_classes(void *pw
, void *node
,
168 lwc_string
***classes
, uint32_t *n_classes
)
173 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
175 /* The implementation does not do it, but the documentation
176 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
177 possible here, so we handle it to be on the safe side. */
178 if (err
!= DOM_NO_ERR
) {
187 * Retrieve the given node's id
189 * \param pw Pointer to the current SVG parser state
190 * \param node Libdom SVG node
191 * \param id Address at which to store the id
193 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
195 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
199 struct svgtiny_parse_state
*state
;
201 /* Begin with the assumption that this node has no id */
204 state
= (struct svgtiny_parse_state
*)pw
;
205 err
= dom_element_get_attribute((dom_node
*)node
,
206 state
->interned_id
, &attr
);
207 if (err
!= DOM_NO_ERR
) {
210 else if (attr
== NULL
) {
211 /* The node has no id attribute and our return value
212 is already set to NULL so we're done */
216 /* If we found an id attribute (a dom_string), intern it into
217 an lwc_string that we can return, and then cleanup the
219 err
= dom_string_intern(attr
, id
);
220 if (err
!= DOM_NO_ERR
) {
221 dom_string_unref(attr
);
224 dom_string_unref(attr
);
231 * Find the first parent of the given element having the given name
233 * \param pw Pointer to the current SVG parser state
234 * \param node Libdom SVG node
235 * \param qname Name of the parent node to search for
236 * \param parent Address at which to store the parent node pointer
238 * \return Always returns CSS_OK
240 * \post If a suitable element is found, a pointer to it will be
241 * stored at the address pointed to by \a parent; otherwise,
242 * NULL will be stored at the address pointed to by \a parent
244 css_error
named_parent_node(void *pw
, void *node
,
245 const css_qname
*qname
, void **parent
)
248 /* dom_element_named_parent_node() was invented to implement
249 * this select handler so there isn't much for us to do except
250 * call it. It's OK if node isn't an element, libdom checks
252 dom_element_named_parent_node((dom_element
*)node
,
254 (struct dom_element
**)parent
);
256 /* Implementation detail: dom_element_named_parent_node()
257 * increments the reference count of the parent element before
258 * returning it to us. According to docs/RefCnt in the libdom
259 * repository, this will prevent the parent element from being
260 * destroyed if it is pruned from the DOM. That sounds good,
261 * since we don't want to be using a pointer to an object that
262 * has been destroyed... but we also have no way of later
263 * decrementing the reference count ourselves, and don't want
264 * to make the returned node eternal. Decrementing the
265 * reference counter now allows it to be destroyed when the
266 * DOM no longer needs it, and so long as no other parts of
267 * libsvgtiny are messing with the DOM during parsing, that
268 * shouldn't (ha ha) cause any problems. */
269 dom_node_unref(*parent
);
276 * Find the "next-sibling" of the given element having the given name
278 * This search corresponds to the "+ foo" combinator in CSS and will
279 * find only "foo" element nodes that immediately precede the given
280 * node under the same parent in the DOM. In CSS the tree is viewed
281 * top-down and in libdom it is viewed from the bottom-up; as a result
282 * "next" and "previous" are sometimes backwards. This is case-sensitive.
284 * \param pw Pointer to the current SVG parser state
285 * \param node Libdom SVG node
286 * \param qname Name of the sibling node to search for
287 * \param sibling Address at which to store the sibling node pointer
289 * \return Always returns CSS_OK
291 * \post If a suitable element is found, a pointer to it will be
292 * stored at the address pointed to by \a sibling; otherwise,
293 * NULL will be stored at the address pointed to by \a sibling
295 css_error
named_sibling_node(void *pw
, void *node
,
296 const css_qname
*qname
, void **sibling
)
299 dom_node
*n
= node
; /* the current node */
300 dom_node
*prev
; /* the previous node */
305 *sibling
= NULL
; /* default to nothing found */
307 /* Begin the search; the first iteration we do outside of the
308 * loop. Implementation detil: dom_node_get_previous_sibling()
309 * increments the reference counter on the returned node. A
310 * comment within named_parent_node() explains why we
311 * decrement it ASAP. */
312 err
= dom_node_get_previous_sibling(n
, &n
);
313 if (err
!= DOM_NO_ERR
) {
318 /* We're looking for the first ELEMENT sibling */
319 err
= dom_node_get_node_type(n
, &type
);
320 if (err
!= DOM_NO_ERR
) {
325 if (type
== DOM_ELEMENT_NODE
) {
326 /* We found an element node, does it have the
328 err
= dom_node_get_node_name(n
, &name
);
329 if (err
!= DOM_NO_ERR
) {
334 if (dom_string_lwc_isequal(name
,
336 /* The name is right, return it */
340 /* There's only one next-sibling element node
341 * and we've already found it, so if its name
342 * wasn't right, we return the default value
344 dom_string_unref(name
);
349 /* Not an element node, so we move on the the previous
350 * previous sibling */
351 err
= dom_node_get_previous_sibling(n
, &prev
);
352 if (err
!= DOM_NO_ERR
) {
366 * Find the first "subsequent-sibling" of the given element having the
369 * This search corresponds to the "~ foo" combinator in CSS and will
370 * find only "foo" element nodes that precede the given node (under
371 * the same parent) in the DOM. In CSS the tree is viewed top-down and
372 * in libdom it is viewed from the bottom-up; as a result "next" and
373 * "previous" are sometimes backwards. This is case-sensitive.
375 * \param pw Pointer to the current SVG parser state
376 * \param node Libdom SVG node
377 * \param qname Name of the sibling node to search for
378 * \param sibling Address at which to store the sibling node pointer
380 * \return Always returns CSS_OK
382 * \post If a suitable element is found, a pointer to it will be
383 * stored at the address pointed to by \a sibling; otherwise,
384 * NULL will be stored at the address pointed to by \a sibling
386 css_error
named_generic_sibling_node(void *pw
, void *node
,
387 const css_qname
*qname
, void **sibling
)
390 dom_node
*n
= node
; /* the current node */
391 dom_node
*prev
; /* the previous node */
397 *sibling
= NULL
; /* default to nothing found */
399 /* Begin the search; the first iteration we do outside of the
400 * loop. Implementation detil: dom_node_get_previous_sibling()
401 * increments the reference counter on the returned node. A
402 * comment within named_parent_node() explains why we
403 * decrement it ASAP. */
404 err
= dom_node_get_previous_sibling(n
, &n
);
405 if (err
!= DOM_NO_ERR
) {
410 err
= dom_node_get_node_type(n
, &type
);
411 if (err
!= DOM_NO_ERR
) {
416 if (type
== DOM_ELEMENT_NODE
) {
417 /* We only want ELEMENT nodes */
418 err
= dom_node_get_node_name(n
, &name
);
419 if (err
!= DOM_NO_ERR
) {
424 if (dom_string_lwc_isequal(name
,
426 /* Found one. Save it and stop the search */
427 dom_string_unref(name
);
433 dom_string_unref(name
);
436 /* This sibling wasn't an element with the desired
437 name, so move on to the previous sibling */
438 err
= dom_node_get_previous_sibling(n
, &prev
);
439 if (err
!= DOM_NO_ERR
) {
453 * Return a pointer to the given node's parent
455 * \param pw Pointer to the current SVG parser state
456 * \param node Libdom SVG node
457 * \param parent Address at which to store the node's parent pointer
459 * \return Always returns CSS_OK
461 css_error
parent_node(void *pw
, void *node
, void **parent
)
464 /* Libdom basically implements this for us */
465 dom_element_parent_node(node
, (struct dom_element
**)parent
);
467 /* See the comment in named_parent_node() for why we decrement
468 * this reference counter here. */
469 dom_node_unref(*parent
);
476 * Find the "next-sibling" of the given element
478 * This search corresponds "+ *" in CSS and will find the first
479 * element node that immediately precedes the given node under the
480 * same parent in the DOM. In CSS the tree is viewed top-down and in
481 * libdom it is viewed from the bottom-up; as a result "next" and
482 * "previous" are sometimes backwards.
484 * \param pw Pointer to the current SVG parser state
485 * \param node Libdom SVG node
486 * \param sibling Address at which to store the sibling node pointer
488 * \return Always returns CSS_OK
490 * \post If a suitable element is found, a pointer to it will be
491 * stored at the address pointed to by \a sibling; otherwise,
492 * NULL will be stored at the address pointed to by \a sibling
494 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
497 dom_node
*n
= node
; /* the current node */
498 dom_node
*prev
; /* the previous node */
502 *sibling
= NULL
; /* default to nothing found */
504 /* Begin the search; the first iteration we do outside of the
505 * loop. Implementation detil: dom_node_get_previous_sibling()
506 * increments the reference counter on the returned node. A
507 * comment within named_parent_node() explains why we
508 * decrement it ASAP. */
509 err
= dom_node_get_previous_sibling(n
, &n
);
510 if (err
!= DOM_NO_ERR
) {
515 err
= dom_node_get_node_type(n
, &type
);
516 if (err
!= DOM_NO_ERR
) {
521 if (type
== DOM_ELEMENT_NODE
) {
522 /* We found a sibling node that is also an
523 element and that's all we wanted. */
529 /* This sibling node was not an element; move on to
530 the previous sibling */
531 err
= dom_node_get_previous_sibling(n
, &prev
);
532 if (err
!= DOM_NO_ERR
) {
546 * Test the given node for the given name
548 * This will return true (via the "match" pointer) if the libdom node
549 * has the given name or if that name is the universal selector;
550 * otherwise it returns false. The comparison is case-sensitive. It
551 * corresponds to a rule like "body { ... }" in CSS.
553 * \param pw Pointer to the current SVG parser state
554 * \param node Libdom SVG node to test
555 * \param qname Name to check for
556 * \param match Pointer to the test result
558 * \return Always returns CSS_OK
560 css_error
node_has_name(void *pw
, void *node
,
561 const css_qname
*qname
, bool *match
)
563 struct svgtiny_parse_state
*state
;
567 /* Start by checking to see if qname is the universal selector */
568 state
= (struct svgtiny_parse_state
*)pw
;
569 if (lwc_string_isequal(qname
->name
,
570 state
->interned_universal
, match
) == lwc_error_ok
) {
572 /* It's the universal selector. In NetSurf, all node
573 * names match the universal selector, and nothing in
574 * the libcss documentation suggests another approach,
575 * so we follow NetSurf here. */
580 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
581 if (err
!= DOM_NO_ERR
) {
585 /* Unlike with HTML, SVG element names are case-sensitive */
586 *match
= dom_string_lwc_isequal(name
, qname
->name
);
587 dom_string_unref(name
);
594 * Test the given node for the given class
596 * This will return true (via the "match" pointer) if the libdom node
597 * has the given class. The comparison is case-sensitive. It
598 * corresponds to node.class in CSS.
600 * \param pw Pointer to the current SVG parser state
601 * \param node Libdom SVG node to test
602 * \param name Class name to check for
603 * \param match Pointer to the test result
605 * \return Always returns CSS_OK
607 css_error
node_has_class(void *pw
, void *node
,
608 lwc_string
*name
, bool *match
)
611 /* libdom implements this for us and apparently it cannot fail */
612 dom_element_has_class((dom_node
*)node
, name
, match
);
618 * Test the given node for the given id
620 * This will return true (via the "match" pointer) if the libdom node
621 * has the given id. The comparison is case-sensitive. It corresponds
624 * \param pw Pointer to the current SVG parser state
625 * \param node Libdom SVG node to test
626 * \param name Id to check for
627 * \param match Pointer to the test result
629 * \return Always returns CSS_OK
631 css_error
node_has_id(void *pw
, void *node
,
632 lwc_string
*name
, bool *match
)
636 struct svgtiny_parse_state
*state
;
638 attr
= NULL
; /* a priori the "id" attribute may not exist */
639 *match
= false; /* default to no match */
641 state
= (struct svgtiny_parse_state
*)pw
;
642 err
= dom_element_get_attribute((dom_node
*)node
,
643 state
->interned_id
, &attr
);
644 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
648 *match
= dom_string_lwc_isequal(attr
, name
);
649 dom_string_unref(attr
);
656 * Test the given node for the given attribute
658 * This will return true (via the "match" pointer) if the libdom node
659 * has an attribute with the given name. The comparison is
660 * case-sensitive. It corresponds to node[attr] in CSS.
662 * \param pw Pointer to the current SVG parser state
663 * \param node Libdom SVG node to test
664 * \param qname Attribute name to check for
665 * \param match Pointer to the test result
667 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
670 css_error
node_has_attribute(void *pw
, void *node
,
671 const css_qname
*qname
, bool *match
)
677 /* intern the attribute name as a dom_string so we can
678 * delegate to dom_element_has_attribute() */
679 err
= dom_string_create_interned(
680 (const uint8_t *) lwc_string_data(qname
->name
),
681 lwc_string_length(qname
->name
),
683 if (err
!= DOM_NO_ERR
) {
687 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
688 if (err
!= DOM_NO_ERR
) {
689 dom_string_unref(name
);
693 dom_string_unref(name
);
699 * Test the given node for an attribute with a specific value
701 * This will return true (via the "match" pointer) if the libdom node
702 * has an attribute with the given name and value. The comparison is
703 * case-sensitive. It corresponds to node[attr=value] in CSS.
705 * \param pw Pointer to the current SVG parser state
706 * \param node Libdom SVG node to test
707 * \param qname Attribute name to check for
708 * \param value Attribute value to check for
709 * \param match Pointer to the test result
711 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
712 * intern the attribute name (which usually indicates memory
715 css_error
node_has_attribute_equal(void *pw
, void *node
,
716 const css_qname
*qname
, lwc_string
*value
,
719 /* Implementation note: NetSurf always returns "no match" when
720 * the value is empty (length zero). We allow it, because why
725 dom_string
*attr_val
;
728 /* Intern the attribute name as a dom_string so we can
729 * use dom_element_get_attribute() */
730 err
= dom_string_create_interned(
731 (const uint8_t *) lwc_string_data(qname
->name
),
732 lwc_string_length(qname
->name
),
734 if (err
!= DOM_NO_ERR
) {
738 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
739 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
740 /* There was an error getting the attribute's value or
741 * the attribute doesn't exist. So, no match? */
742 dom_string_unref(name
);
747 /* Otherwise, we have the attribute value from the given node
748 * and all we need to do is compare. */
749 dom_string_unref(name
);
750 *match
= dom_string_lwc_isequal(attr_val
, value
);
751 dom_string_unref(attr_val
);
758 * Test the given node for an attribute with a specific value,
759 * possibly followed by a single hyphen
761 * This will return true (via the "match" pointer) if the libdom node
762 * has an attribute with the given name and value or with the given
763 * name and a value that is followed by exactly one hyphen. The
764 * comparison is case-sensitive. This corresponds to [attr|=value]
767 * \param pw Pointer to the current SVG parser state
768 * \param node Libdom SVG node to test
769 * \param qname Attribute name to check for
770 * \param value Attribute value to check for
771 * \param match Pointer to the test result
773 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
774 * intern the attribute name (which usually indicates memory
777 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
778 const css_qname
*qname
, lwc_string
*value
,
781 /* Implementation note: NetSurf always returns "no match" when
782 * the value is empty (length zero). We allow it, because why
787 dom_string
*attr_val
;
790 const char *vdata
; /* to hold the data underlying "value" */
792 const char *avdata
; /* to hold the found attribute value data */
795 /* Intern the attribute name as a dom_string so we can
796 * use dom_element_get_attribute() */
797 err
= dom_string_create_interned(
798 (const uint8_t *) lwc_string_data(qname
->name
),
799 lwc_string_length(qname
->name
),
801 if (err
!= DOM_NO_ERR
) {
805 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
806 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
807 /* There was an error getting the attribute's value or
808 * the attribute doesn't exist. So, no match? */
809 dom_string_unref(name
);
814 /* Otherwise, we have the attribute value from the given node
815 * and all we need to do is compare. */
816 dom_string_unref(name
);
817 *match
= dom_string_lwc_isequal(attr_val
, value
);
819 /* Exact match, we're done */
820 dom_string_unref(attr_val
);
824 /* No exact match, try it with a hyphen on the end */
825 vdata
= lwc_string_data(value
); /* needle */
826 vdata_len
= lwc_string_length(value
);
827 avdata
= dom_string_data(attr_val
); /* haystack */
828 avdata_len
= dom_string_byte_length(attr_val
);
829 dom_string_unref(attr_val
);
831 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
832 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
833 /* If there's a hyphen in the right position,
834 * it suffices to compare the strings only up
845 * Test the given node for an attribute whose value is a
846 * space-separated list of words, one of which is the given word
848 * This will return true (via the "match" pointer) if the libdom node
849 * has an attribute with the given name and whose value when
850 * considered as a space-separated list of words contains the given
851 * word. The comparison is case-sensitive. This corresponds to
852 * [attr~=value] in CSS.
854 * \param pw Pointer to the current SVG parser state
855 * \param node Libdom SVG node to test
856 * \param qname Attribute name to check for
857 * \param word Value word to check for
858 * \param match Pointer to the test result
860 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
861 * intern the attribute name (which usually indicates memory
864 css_error
node_has_attribute_includes(void *pw
, void *node
,
865 const css_qname
*qname
, lwc_string
*word
,
871 dom_string
*attr_val
;
873 size_t wordlen
; /* length of "word" */
875 /* pointers used to parse a space-separated list of words */
880 *match
= false; /* default to no match */
882 wordlen
= lwc_string_length(word
);
884 /* In this case, the spec says that "if 'val' is the
885 * empty string, it will never represent anything." */
889 /* Intern the attribute name as a dom_string so we can
890 * use dom_element_get_attribute() */
891 err
= dom_string_create_interned(
892 (const uint8_t *) lwc_string_data(qname
->name
),
893 lwc_string_length(qname
->name
),
895 if (err
!= DOM_NO_ERR
) {
899 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
900 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
901 /* There was an error getting the attribute's value or
902 * the attribute doesn't exist. So, no match? */
903 dom_string_unref(name
);
907 /* Parse the list comparing each word against "word" */
908 start
= dom_string_data(attr_val
);
909 end
= start
+ dom_string_byte_length(attr_val
);
910 dom_string_unref(attr_val
);
912 for (p
= start
; p
<= end
; p
++) {
913 /* Move forward until we find the end of the first word */
914 if (*p
== ' ' || *p
== '\0') {
915 /* If the length of that word is the length of the
916 * word we're looking for, do the comparison. */
917 if ((size_t) (p
- start
) == wordlen
&&
919 lwc_string_data(word
),
924 /* No match? Set "start" to the beginning of
925 * the next word and loop. */
935 * Test the given node for an attribute whose value begins with the
938 * This will return true (via the "match" pointer) if the libdom node
939 * has an attribute with the given name and whose value begins with
940 * the given prefix string. The comparison is case-sensitive. This
941 * corresponds to [attr^=value] in CSS.
943 * \param pw Pointer to the current SVG parser state
944 * \param node Libdom SVG node to test
945 * \param qname Attribute name to check for
946 * \param prefix Value prefix to check for
947 * \param match Pointer to the test result
949 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
950 * intern the attribute name (which usually indicates memory
953 css_error
node_has_attribute_prefix(void *pw
, void *node
,
954 const css_qname
*qname
, lwc_string
*prefix
,
959 dom_string
*attr_val
;
961 const char *avdata
; /* attribute value data */
962 size_t avdata_len
; /* length of that attribute value data */
963 size_t prefixlen
; /* length of "prefix" */
965 prefixlen
= lwc_string_length(prefix
);
966 if (prefixlen
== 0) {
967 /* In this case, the spec says that "if 'val' is the
968 * empty string, it will never represent anything." */
972 /* Intern the attribute name as a dom_string so we can
973 * use dom_element_get_attribute() */
974 err
= dom_string_create_interned(
975 (const uint8_t *) lwc_string_data(qname
->name
),
976 lwc_string_length(qname
->name
),
978 if (err
!= DOM_NO_ERR
) {
982 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
983 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
984 /* There was an error getting the attribute's value or
985 * the attribute doesn't exist. So, no match? */
986 dom_string_unref(name
);
991 /* Otherwise, we have the attribute value from the given node,
992 * and the first thing we want to do is check to see if the
993 * whole thing matches the prefix. */
994 dom_string_unref(name
);
995 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
997 /* If not, check to see if an, uh, prefix matches the
999 if (*match
== false) {
1000 avdata
= dom_string_data(attr_val
);
1001 avdata_len
= dom_string_byte_length(attr_val
);
1002 if ((avdata_len
>= prefixlen
) &&
1003 (strncasecmp(avdata
,
1004 lwc_string_data(prefix
),
1006 /* Use strncasecmp to compare only the first
1007 * "n" characters, where "n" is the length of
1013 dom_string_unref(attr_val
);
1020 * Test the given node for an attribute whose value end with the
1023 * This will return true (via the "match" pointer) if the libdom node
1024 * has an attribute with the given name and whose value ends with
1025 * the given suffix string. The comparison is case-sensitive. This
1026 * corresponds to [attr$=value] in CSS.
1028 * \param pw Pointer to the current SVG parser state
1029 * \param node Libdom SVG node to test
1030 * \param qname Attribute name to check for
1031 * \param suffix Value suffix to check for
1032 * \param match Pointer to the test result
1034 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1035 * intern the attribute name (which usually indicates memory
1038 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1039 const css_qname
*qname
, lwc_string
*suffix
,
1044 dom_string
*attr_val
;
1046 const char *avdata
; /* attribute value data */
1047 size_t avdata_len
; /* length of that attribute value data */
1048 size_t suffixlen
; /* length of "suffix" */
1050 /* convenience pointer we'll use when matching the suffix */
1051 const char *suffix_start
;
1053 suffixlen
= lwc_string_length(suffix
);
1054 if (suffixlen
== 0) {
1055 /* In this case, the spec says that "if 'val' is the
1056 * empty string, it will never represent anything." */
1060 /* Intern the attribute name as a dom_string so we can
1061 * use dom_element_get_attribute() */
1062 err
= dom_string_create_interned(
1063 (const uint8_t *) lwc_string_data(qname
->name
),
1064 lwc_string_length(qname
->name
),
1066 if (err
!= DOM_NO_ERR
) {
1070 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1071 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1072 /* There was an error getting the attribute's value or
1073 * the attribute doesn't exist. So, no match? */
1074 dom_string_unref(name
);
1079 /* Otherwise, we have the attribute value from the given node,
1080 * and the first thing we want to do is check to see if the
1081 * whole thing matches the suffix. */
1082 dom_string_unref(name
);
1083 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1085 /* If not, check to see if an, uh, suffix matches the
1087 if (*match
== false) {
1088 avdata
= dom_string_data(attr_val
);
1089 avdata_len
= dom_string_byte_length(attr_val
);
1091 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1093 if ((avdata_len
>= suffixlen
) &&
1094 (strncasecmp(suffix_start
,
1095 lwc_string_data(suffix
),
1097 /* Use strncasecmp to compare only the last
1098 * "n" characters, where "n" is the length of
1104 dom_string_unref(attr_val
);
1111 * Test the given node for an attribute whose value contains the
1114 * This will return true (via the "match" pointer) if the libdom node
1115 * has an attribute with the given name and whose value contains the
1116 * given substring. The comparison is case-sensitive. This corresponds
1117 * to [attr*=value] in CSS.
1119 * \param pw Pointer to the current SVG parser state
1120 * \param node Libdom SVG node to test
1121 * \param qname Attribute name to check for
1122 * \param substring Value substring to check for
1123 * \param match Pointer to the test result
1125 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1126 * intern the attribute name (which usually indicates memory
1129 css_error
node_has_attribute_substring(void *pw
, void *node
,
1130 const css_qname
*qname
, lwc_string
*substring
,
1135 dom_string
*attr_val
;
1137 size_t attr_len
; /* length of attr_val */
1138 size_t substrlen
; /* length of "substring" */
1140 /* Convenience pointers we use when comparing substrings */
1144 substrlen
= lwc_string_length(substring
);
1145 if (substrlen
== 0) {
1146 /* In this case, the spec says that "if 'val' is the
1147 * empty string, it will never represent anything." */
1151 /* Intern the attribute name as a dom_string so we can
1152 * use dom_element_get_attribute() */
1153 err
= dom_string_create_interned(
1154 (const uint8_t *) lwc_string_data(qname
->name
),
1155 lwc_string_length(qname
->name
),
1157 if (err
!= DOM_NO_ERR
) {
1161 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1162 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1163 /* There was an error getting the attribute's value or
1164 * the attribute doesn't exist. So, no match? */
1165 dom_string_unref(name
);
1170 /* Otherwise, we have the attribute value from the given node,
1171 * and the first thing we want to do is check to see if the
1172 * whole thing matches the substring. */
1173 dom_string_unref(name
);
1174 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1176 /* If not, check to see if an, uh, substring matches the
1178 if (*match
== false) {
1179 p
= dom_string_data(attr_val
);
1181 /* Check every long-enough suffix for a prefix match */
1182 attr_len
= dom_string_byte_length(attr_val
);
1183 if (attr_len
>= substrlen
) {
1184 p_max
= p
+ attr_len
- substrlen
;
1185 while (p
<= p_max
) {
1187 lwc_string_data(substring
),
1197 dom_string_unref(attr_val
);
1204 * Test whether or not the given node is the document's root element
1205 * This corresponds to the CSS :root pseudo-selector.
1207 * \param pw Pointer to the current SVG parser state
1208 * \param node Libdom SVG node to test
1209 * \param match Pointer to the test result
1211 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1213 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1220 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1221 if (err
!= DOM_NO_ERR
) {
1225 /* It's the root element if it doesn't have a parent element */
1226 if (parent
!= NULL
) {
1227 err
= dom_node_get_node_type(parent
, &type
);
1228 dom_node_unref(parent
);
1229 if (err
!= DOM_NO_ERR
) {
1232 if (type
!= DOM_DOCUMENT_NODE
) {
1233 /* DOM_DOCUMENT_NODE is the only allowable
1234 * type of parent node for the root element */
1246 * Used internally in node_count_siblings() to "count" the given
1247 * sibling node. It factors out the node type and name checks.
1249 static int node_count_siblings_check(dom_node
*dnode
,
1256 dom_string
*dnode_name
;
1258 /* We flip this to 1 if/when we count this node */
1261 if (dnode
== NULL
) {
1265 exc
= dom_node_get_node_type(dnode
, &type
);
1266 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1267 /* We only count element siblings */
1271 /* ... with the right name */
1274 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1276 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1277 if (dom_string_isequal(name
,
1281 dom_string_unref(dnode_name
);
1292 * Count the given node's sibling elements
1294 * This counts the given node's sibling elements in one direction,
1295 * either forwards or backwards, in the DOM. Keep in mind that the
1296 * libdom tree is upside-down compared to the CSS one; so "next" and
1297 * "previous" are actually reversed; the default is to count preceding
1298 * libdom siblings which correspond to subsequent CSS siblings.
1300 * This operation is central to the CSS :first-child, :nth-child, and
1301 * :last-child (et cetera) pseudo-selectors.
1303 * If same_name is true, then only nodes having the same
1304 * (case-sensitive) name as the given node are counted.
1306 * \param pw Pointer to the current SVG parser state
1307 * \param node Libdom SVG node whose siblings we're counting
1308 * \param same_name Whether or not to count only siblings having
1309 * the same name as the given node
1310 * \param after Count subsequent siblings rather than precedent
1311 * ones (the default)
1312 * \param count Pointer to the return value, the number of sibling
1315 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1317 css_error
node_count_siblings(void *pw
, void *node
,
1318 bool same_name
, bool after
, int32_t *count
)
1322 dom_node
*dnode
; /* node, but with the right type */
1323 dom_string
*dnode_name
;
1324 dom_node
*next
; /* "next" sibling (depends on direction) */
1326 /* Pointer to the "next sibling" function */
1327 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1332 dnode
= (dom_node
*)node
;
1334 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1335 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1340 /* Increment the reference counter for dnode for as long as
1341 * we retain a reference to it. */
1342 dnode
= dom_node_ref(dnode
);
1344 next_func
= dom_node_get_previous_sibling
;
1346 next_func
= dom_node_get_next_sibling
;
1350 exc
= next_func(dnode
, &next
);
1351 if (exc
!= DOM_NO_ERR
) {
1355 /* If next_func worked, we're about to swap "next"
1356 * with "dnode" meaning that we will no longer retain
1357 * a reference to the current dnode. */
1358 dom_node_unref(dnode
);
1361 *count
+= node_count_siblings_check(dnode
,
1364 } while (dnode
!= NULL
);
1366 if (dnode_name
!= NULL
) {
1367 dom_string_unref(dnode_name
);
1375 * Determine whether or not the given element is empty
1377 * An element is "nonempty" if it has a child that is either an
1378 * element node or a text node.
1380 * \param pw Pointer to the current SVG parser state
1381 * \param node Libdom SVG node to check for emptiness
1382 * \param is_empty Pointer to the return value
1384 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1386 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1389 dom_node
*child
; /* current child node pointer */
1390 dom_node
*next
; /* next child node pointer */
1391 dom_node_type type
; /* what type of node is "child" */
1394 /* Assume that it's empty by default */
1397 /* Get the given node's first child. Implementation detail:
1398 * this increments the reference counter on the child node. */
1399 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1400 if (err
!= DOM_NO_ERR
) {
1404 /* And now loop through all children looking for a
1405 * text/element node. If we find one, the original
1406 * node is "nonempty" */
1407 while (child
!= NULL
) {
1408 err
= dom_node_get_node_type(child
, &type
);
1409 if (err
!= DOM_NO_ERR
) {
1410 dom_node_unref(child
);
1414 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1416 dom_node_unref(child
);
1420 err
= dom_node_get_next_sibling(child
, &next
);
1421 if (err
!= DOM_NO_ERR
) {
1422 dom_node_unref(child
);
1426 /* If we're moving to the next node, we can release
1427 * the reference to the current one */
1428 dom_node_unref(child
);
1437 * Determine whether or not the given node is a link
1439 * A node is a link if it is an element node whose name is "a" and if
1440 * it has an "href" attribute (case-sensitive). This selector
1441 * corresponds to node:link pseudo-class in CSS.
1443 * This pseudo-class is a bit awkward because the two standards (HTML5
1444 * and CSS) disagree on what it means, and because libsvgtiny does not
1445 * have enough information to determine if a link has been "visited"
1446 * yet -- that's a UI property. CSS says that :link is for unvisited
1447 * links, which we can't determine. HTML5 says that each link must
1448 * be either a :link or :visited. Since we can't decide either way,
1449 * It seems less wrong to declare that all links are unvisited; i.e.
1450 * that they match :link.
1452 * \param pw Pointer to the current SVG parser state
1453 * \param node Libdom SVG node to check
1454 * \param is_link Pointer to the boolean return value
1456 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1458 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1461 dom_node
*dnode
; /* node, but with the right type */
1462 dom_string
*dnode_name
;
1464 struct svgtiny_parse_state
* state
;
1466 dnode
= (dom_node
*)node
;
1468 has_href
= false; /* assume no href attribute */
1469 *is_link
= false; /* assume that it's not a link */
1471 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1472 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1476 state
= (struct svgtiny_parse_state
*)pw
;
1477 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1478 exc
= dom_element_has_attribute(node
,
1479 state
->interned_href
,
1481 if (exc
== DOM_NO_ERR
&& has_href
) {
1486 dom_string_unref(dnode_name
);
1492 * Check if the given node is being "hovered" over
1494 * This check always fails because the SVG DOM does not have the
1495 * necessary information (it's a UI property).
1497 * \param pw Pointer to the current SVG parser state; unused
1498 * \param node Libdom SVG node to check; unused
1499 * \param is_hover Pointer to the boolean return value
1501 * \return Always returns CSS_OK
1503 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1513 * Check if the given node is "active"
1515 * This check always fails because the SVG DOM does not have the
1516 * necessary information (it's a UI property).
1518 * \param pw Pointer to the current SVG parser state; unused
1519 * \param node Libdom SVG node to check; unused
1520 * \param is_active Pointer to the boolean return value
1522 * \return Always returns CSS_OK
1524 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1534 * Check if the given node has the focus
1536 * This check always fails because the SVG DOM does not have the
1537 * necessary information (it's a UI property).
1539 * \param pw Pointer to the current SVG parser state; unused
1540 * \param node Libdom SVG node to check; unused
1541 * \param is_focus Pointer to the boolean return value
1543 * \return Always returns CSS_OK
1545 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1555 * Check if the given node is enabled
1557 * This check always fails because the SVG DOM does not have the
1558 * necessary information (it's a UI property).
1560 * \param pw Pointer to the current SVG parser state; unused
1561 * \param node Libdom SVG node to check; unused
1562 * \param is_enabled Pointer to the boolean return value
1564 * \return Always returns CSS_OK
1566 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1570 *is_enabled
= false;