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
);
54 * Resolve a relative URL to an absolute one by doing nothing. This is
55 * the simplest possible implementation of a URL resolver, needed for
58 css_error
svgtiny_resolve_url(void *pw
,
59 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
64 /* Copy the relative URL to the absolute one (the return
66 *abs
= lwc_string_ref(rel
);
71 * Create a stylesheet with the default set of params.
73 * \param sheet A stylesheet pointer, passed in by reference, that
74 * we use to store the newly-created stylesheet.
75 * \param inline_style True if this stylesheet represents an inline
76 * style, and false otherwise.
78 * \return The return value from css_stylesheet_create() is returned.
80 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
83 css_stylesheet_params params
;
85 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
86 params
.level
= CSS_LEVEL_DEFAULT
;
87 params
.charset
= NULL
;
90 params
.allow_quirks
= false;
91 params
.inline_style
= inline_style
;
92 params
.resolve
= svgtiny_resolve_url
;
93 params
.resolve_pw
= NULL
;
95 params
.import_pw
= NULL
;
97 params
.color_pw
= NULL
;
99 params
.font_pw
= NULL
;
101 return css_stylesheet_create(¶ms
, sheet
);
105 /**************************/
106 /* libcss select handlers */
107 /**************************/
109 * From here on we implement the "select handler "API defined in
110 * libcss's include/libcss/select.h and discussed briefly in its
116 * Retrieve the given node's name
118 * \param pw Pointer to the current SVG parser state
119 * \param node Libdom SVG node
120 * \param qname Address at which to store the node name
122 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
124 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
128 struct svgtiny_parse_state
*state
;
130 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
131 if (err
!= DOM_NO_ERR
) {
135 state
= (struct svgtiny_parse_state
*)pw
;
136 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
138 err
= dom_string_intern(name
, &qname
->name
);
139 if (err
!= DOM_NO_ERR
) {
140 dom_string_unref(name
);
144 dom_string_unref(name
);
151 * Retrieve the given node's classes
153 * \param pw Pointer to the current SVG parser state
154 * \param node Libdom SVG node
155 * \param classes Address at which to store the class name array
156 * \param n_classes Address at which to store the length of the class
159 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
161 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
162 * because the underlying libdom function never fails
164 css_error
node_classes(void *pw
, void *node
,
165 lwc_string
***classes
, uint32_t *n_classes
)
170 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
172 /* The implementation does not do it, but the documentation
173 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
174 possible here, so we handle it to be on the safe side. */
175 if (err
!= DOM_NO_ERR
) {
184 * Retrieve the given node's id
186 * \param pw Pointer to the current SVG parser state
187 * \param node Libdom SVG node
188 * \param id Address at which to store the id
190 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
192 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
196 struct svgtiny_parse_state
*state
;
198 /* Begin with the assumption that this node has no id */
201 state
= (struct svgtiny_parse_state
*)pw
;
202 err
= dom_element_get_attribute((dom_node
*)node
,
203 state
->interned_id
, &attr
);
204 if (err
!= DOM_NO_ERR
) {
207 else if (attr
== NULL
) {
208 /* The node has no id attribute and our return value
209 is already set to NULL so we're done */
213 /* If we found an id attribute (a dom_string), intern it into
214 an lwc_string that we can return, and then cleanup the
216 err
= dom_string_intern(attr
, id
);
217 if (err
!= DOM_NO_ERR
) {
218 dom_string_unref(attr
);
221 dom_string_unref(attr
);
228 * Find the first parent of the given element having the given name
230 * \param pw Pointer to the current SVG parser state
231 * \param node Libdom SVG node
232 * \param qname Name of the parent node to search for
233 * \param parent Address at which to store the parent node pointer
235 * \return Always returns CSS_OK
237 * \post If a suitable element is found, a pointer to it will be
238 * stored at the address pointed to by \a parent; otherwise,
239 * NULL will be stored at the address pointed to by \a parent
241 css_error
named_parent_node(void *pw
, void *node
,
242 const css_qname
*qname
, void **parent
)
245 /* dom_element_named_parent_node() was invented to implement
246 * this select handler so there isn't much for us to do except
247 * call it. It's OK if node isn't an element, libdom checks
249 dom_element_named_parent_node((dom_element
*)node
,
251 (struct dom_element
**)parent
);
253 /* Implementation detail: dom_element_named_parent_node()
254 * increments the reference count of the parent element before
255 * returning it to us. According to docs/RefCnt in the libdom
256 * repository, this will prevent the parent element from being
257 * destroyed if it is pruned from the DOM. That sounds good,
258 * since we don't want to be using a pointer to an object that
259 * has been destroyed... but we also have no way of later
260 * decrementing the reference count ourselves, and don't want
261 * to make the returned node eternal. Decrementing the
262 * reference counter now allows it to be destroyed when the
263 * DOM no longer needs it, and so long as no other parts of
264 * libsvgtiny are messing with the DOM during parsing, that
265 * shouldn't (ha ha) cause any problems. */
266 dom_node_unref(*parent
);
273 * Find the "next-sibling" of the given element having the given name
275 * This search corresponds to the "+ foo" combinator in CSS and will
276 * find only "foo" element nodes that immediately precede the given
277 * node under the same parent in the DOM. In CSS the tree is viewed
278 * top-down and in libdom it is viewed from the bottom-up; as a result
279 * "next" and "previous" are sometimes backwards. This is case-sensitive.
281 * \param pw Pointer to the current SVG parser state
282 * \param node Libdom SVG node
283 * \param qname Name of the sibling node to search for
284 * \param sibling Address at which to store the sibling node pointer
286 * \return Always returns CSS_OK
288 * \post If a suitable element is found, a pointer to it will be
289 * stored at the address pointed to by \a sibling; otherwise,
290 * NULL will be stored at the address pointed to by \a sibling
292 css_error
named_sibling_node(void *pw
, void *node
,
293 const css_qname
*qname
, void **sibling
)
296 dom_node
*n
= node
; /* the current node */
297 dom_node
*prev
; /* the previous node */
302 *sibling
= NULL
; /* default to nothing found */
304 /* Begin the search; the first iteration we do outside of the
305 * loop. Implementation detil: dom_node_get_previous_sibling()
306 * increments the reference counter on the returned node. A
307 * comment within named_parent_node() explains why we
308 * decrement it ASAP. */
309 err
= dom_node_get_previous_sibling(n
, &n
);
310 if (err
!= DOM_NO_ERR
) {
315 /* We're looking for the first ELEMENT sibling */
316 err
= dom_node_get_node_type(n
, &type
);
317 if (err
!= DOM_NO_ERR
) {
322 if (type
== DOM_ELEMENT_NODE
) {
323 /* We found an element node, does it have the
325 err
= dom_node_get_node_name(n
, &name
);
326 if (err
!= DOM_NO_ERR
) {
331 if (dom_string_lwc_isequal(name
,
333 /* The name is right, return it */
337 /* There's only one next-sibling element node
338 * and we've already found it, so if its name
339 * wasn't right, we return the default value
341 dom_string_unref(name
);
346 /* Not an element node, so we move on the the previous
347 * previous sibling */
348 err
= dom_node_get_previous_sibling(n
, &prev
);
349 if (err
!= DOM_NO_ERR
) {
363 * Find the first "subsequent-sibling" of the given element having the
366 * This search corresponds to the "~ foo" combinator in CSS and will
367 * find only "foo" element nodes that precede the given node (under
368 * the same parent) in the DOM. In CSS the tree is viewed top-down and
369 * in libdom it is viewed from the bottom-up; as a result "next" and
370 * "previous" are sometimes backwards. This is case-sensitive.
372 * \param pw Pointer to the current SVG parser state
373 * \param node Libdom SVG node
374 * \param qname Name of the sibling node to search for
375 * \param sibling Address at which to store the sibling node pointer
377 * \return Always returns CSS_OK
379 * \post If a suitable element is found, a pointer to it will be
380 * stored at the address pointed to by \a sibling; otherwise,
381 * NULL will be stored at the address pointed to by \a sibling
383 css_error
named_generic_sibling_node(void *pw
, void *node
,
384 const css_qname
*qname
, void **sibling
)
387 dom_node
*n
= node
; /* the current node */
388 dom_node
*prev
; /* the previous node */
394 *sibling
= NULL
; /* default to nothing found */
396 /* Begin the search; the first iteration we do outside of the
397 * loop. Implementation detil: dom_node_get_previous_sibling()
398 * increments the reference counter on the returned node. A
399 * comment within named_parent_node() explains why we
400 * decrement it ASAP. */
401 err
= dom_node_get_previous_sibling(n
, &n
);
402 if (err
!= DOM_NO_ERR
) {
407 err
= dom_node_get_node_type(n
, &type
);
408 if (err
!= DOM_NO_ERR
) {
413 if (type
== DOM_ELEMENT_NODE
) {
414 /* We only want ELEMENT nodes */
415 err
= dom_node_get_node_name(n
, &name
);
416 if (err
!= DOM_NO_ERR
) {
421 if (dom_string_lwc_isequal(name
,
423 /* Found one. Save it and stop the search */
424 dom_string_unref(name
);
430 dom_string_unref(name
);
433 /* This sibling wasn't an element with the desired
434 name, so move on to the previous sibling */
435 err
= dom_node_get_previous_sibling(n
, &prev
);
436 if (err
!= DOM_NO_ERR
) {
450 * Return a pointer to the given node's parent
452 * \param pw Pointer to the current SVG parser state
453 * \param node Libdom SVG node
454 * \param parent Address at which to store the node's parent pointer
456 * \return Always returns CSS_OK
458 css_error
parent_node(void *pw
, void *node
, void **parent
)
461 /* Libdom basically implements this for us */
462 dom_element_parent_node(node
, (struct dom_element
**)parent
);
464 /* See the comment in named_parent_node() for why we decrement
465 * this reference counter here. */
466 dom_node_unref(*parent
);
473 * Find the "next-sibling" of the given element
475 * This search corresponds "+ *" in CSS and will find the first
476 * element node that immediately precedes the given node under the
477 * same parent in the DOM. In CSS the tree is viewed top-down and in
478 * libdom it is viewed from the bottom-up; as a result "next" and
479 * "previous" are sometimes backwards.
481 * \param pw Pointer to the current SVG parser state
482 * \param node Libdom SVG node
483 * \param sibling Address at which to store the sibling node pointer
485 * \return Always returns CSS_OK
487 * \post If a suitable element is found, a pointer to it will be
488 * stored at the address pointed to by \a sibling; otherwise,
489 * NULL will be stored at the address pointed to by \a sibling
491 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
494 dom_node
*n
= node
; /* the current node */
495 dom_node
*prev
; /* the previous node */
499 *sibling
= NULL
; /* default to nothing found */
501 /* Begin the search; the first iteration we do outside of the
502 * loop. Implementation detil: dom_node_get_previous_sibling()
503 * increments the reference counter on the returned node. A
504 * comment within named_parent_node() explains why we
505 * decrement it ASAP. */
506 err
= dom_node_get_previous_sibling(n
, &n
);
507 if (err
!= DOM_NO_ERR
) {
512 err
= dom_node_get_node_type(n
, &type
);
513 if (err
!= DOM_NO_ERR
) {
518 if (type
== DOM_ELEMENT_NODE
) {
519 /* We found a sibling node that is also an
520 element and that's all we wanted. */
526 /* This sibling node was not an element; move on to
527 the previous sibling */
528 err
= dom_node_get_previous_sibling(n
, &prev
);
529 if (err
!= DOM_NO_ERR
) {
543 * Test the given node for the given name
545 * This will return true (via the "match" pointer) if the libdom node
546 * has the given name or if that name is the universal selector;
547 * otherwise it returns false. The comparison is case-sensitive. It
548 * corresponds to a rule like "body { ... }" in CSS.
550 * \param pw Pointer to the current SVG parser state
551 * \param node Libdom SVG node to test
552 * \param qname Name to check for
553 * \param match Pointer to the test result
555 * \return Always returns CSS_OK
557 css_error
node_has_name(void *pw
, void *node
,
558 const css_qname
*qname
, bool *match
)
560 struct svgtiny_parse_state
*state
;
564 /* Start by checking to see if qname is the universal selector */
565 state
= (struct svgtiny_parse_state
*)pw
;
566 if (lwc_string_isequal(qname
->name
,
567 state
->interned_universal
, match
) == lwc_error_ok
) {
569 /* It's the universal selector. In NetSurf, all node
570 * names match the universal selector, and nothing in
571 * the libcss documentation suggests another approach,
572 * so we follow NetSurf here. */
577 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
578 if (err
!= DOM_NO_ERR
) {
582 /* Unlike with HTML, SVG element names are case-sensitive */
583 *match
= dom_string_lwc_isequal(name
, qname
->name
);
584 dom_string_unref(name
);
591 * Test the given node for the given class
593 * This will return true (via the "match" pointer) if the libdom node
594 * has the given class. The comparison is case-sensitive. It
595 * corresponds to node.class in CSS.
597 * \param pw Pointer to the current SVG parser state
598 * \param node Libdom SVG node to test
599 * \param name Class name to check for
600 * \param match Pointer to the test result
602 * \return Always returns CSS_OK
604 css_error
node_has_class(void *pw
, void *node
,
605 lwc_string
*name
, bool *match
)
608 /* libdom implements this for us and apparently it cannot fail */
609 dom_element_has_class((dom_node
*)node
, name
, match
);
615 * Test the given node for the given id
617 * This will return true (via the "match" pointer) if the libdom node
618 * has the given id. The comparison is case-sensitive. It corresponds
621 * \param pw Pointer to the current SVG parser state
622 * \param node Libdom SVG node to test
623 * \param name Id to check for
624 * \param match Pointer to the test result
626 * \return Always returns CSS_OK
628 css_error
node_has_id(void *pw
, void *node
,
629 lwc_string
*name
, bool *match
)
633 struct svgtiny_parse_state
*state
;
635 attr
= NULL
; /* a priori the "id" attribute may not exist */
636 *match
= false; /* default to no match */
638 state
= (struct svgtiny_parse_state
*)pw
;
639 err
= dom_element_get_attribute((dom_node
*)node
,
640 state
->interned_id
, &attr
);
641 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
645 *match
= dom_string_lwc_isequal(attr
, name
);
646 dom_string_unref(attr
);
653 * Test the given node for the given attribute
655 * This will return true (via the "match" pointer) if the libdom node
656 * has an attribute with the given name. The comparison is
657 * case-sensitive. It corresponds to node[attr] in CSS.
659 * \param pw Pointer to the current SVG parser state
660 * \param node Libdom SVG node to test
661 * \param qname Attribute name to check for
662 * \param match Pointer to the test result
664 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
667 css_error
node_has_attribute(void *pw
, void *node
,
668 const css_qname
*qname
, bool *match
)
674 /* intern the attribute name as a dom_string so we can
675 * delegate to dom_element_has_attribute() */
676 err
= dom_string_create_interned(
677 (const uint8_t *) lwc_string_data(qname
->name
),
678 lwc_string_length(qname
->name
),
680 if (err
!= DOM_NO_ERR
) {
684 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
685 if (err
!= DOM_NO_ERR
) {
686 dom_string_unref(name
);
690 dom_string_unref(name
);
696 * Test the given node for an attribute with a specific value
698 * This will return true (via the "match" pointer) if the libdom node
699 * has an attribute with the given name and value. The comparison is
700 * case-sensitive. It corresponds to node[attr=value] in CSS.
702 * \param pw Pointer to the current SVG parser state
703 * \param node Libdom SVG node to test
704 * \param qname Attribute name to check for
705 * \param value Attribute value to check for
706 * \param match Pointer to the test result
708 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
709 * intern the attribute name (which usually indicates memory
712 css_error
node_has_attribute_equal(void *pw
, void *node
,
713 const css_qname
*qname
, lwc_string
*value
,
716 /* Implementation note: NetSurf always returns "no match" when
717 * the value is empty (length zero). We allow it, because why
722 dom_string
*attr_val
;
725 /* Intern the attribute name as a dom_string so we can
726 * use dom_element_get_attribute() */
727 err
= dom_string_create_interned(
728 (const uint8_t *) lwc_string_data(qname
->name
),
729 lwc_string_length(qname
->name
),
731 if (err
!= DOM_NO_ERR
) {
735 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
736 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
737 /* There was an error getting the attribute's value or
738 * the attribute doesn't exist. So, no match? */
739 dom_string_unref(name
);
744 /* Otherwise, we have the attribute value from the given node
745 * and all we need to do is compare. */
746 dom_string_unref(name
);
747 *match
= dom_string_lwc_isequal(attr_val
, value
);
748 dom_string_unref(attr_val
);
755 * Test the given node for an attribute with a specific value,
756 * possibly followed by a single hyphen
758 * This will return true (via the "match" pointer) if the libdom node
759 * has an attribute with the given name and value or with the given
760 * name and a value that is followed by exactly one hyphen. The
761 * comparison is case-sensitive. This corresponds to [attr|=value]
764 * \param pw Pointer to the current SVG parser state
765 * \param node Libdom SVG node to test
766 * \param qname Attribute name to check for
767 * \param value Attribute value to check for
768 * \param match Pointer to the test result
770 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
771 * intern the attribute name (which usually indicates memory
774 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
775 const css_qname
*qname
, lwc_string
*value
,
778 /* Implementation note: NetSurf always returns "no match" when
779 * the value is empty (length zero). We allow it, because why
784 dom_string
*attr_val
;
787 const char *vdata
; /* to hold the data underlying "value" */
789 const char *avdata
; /* to hold the found attribute value data */
792 /* Intern the attribute name as a dom_string so we can
793 * use dom_element_get_attribute() */
794 err
= dom_string_create_interned(
795 (const uint8_t *) lwc_string_data(qname
->name
),
796 lwc_string_length(qname
->name
),
798 if (err
!= DOM_NO_ERR
) {
802 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
803 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
804 /* There was an error getting the attribute's value or
805 * the attribute doesn't exist. So, no match? */
806 dom_string_unref(name
);
811 /* Otherwise, we have the attribute value from the given node
812 * and all we need to do is compare. */
813 dom_string_unref(name
);
814 *match
= dom_string_lwc_isequal(attr_val
, value
);
816 /* Exact match, we're done */
817 dom_string_unref(attr_val
);
821 /* No exact match, try it with a hyphen on the end */
822 vdata
= lwc_string_data(value
); /* needle */
823 vdata_len
= lwc_string_length(value
);
824 avdata
= dom_string_data(attr_val
); /* haystack */
825 avdata_len
= dom_string_byte_length(attr_val
);
826 dom_string_unref(attr_val
);
828 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
829 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
830 /* If there's a hyphen in the right position,
831 * it suffices to compare the strings only up
842 * Test the given node for an attribute whose value is a
843 * space-separated list of words, one of which is the given word
845 * This will return true (via the "match" pointer) if the libdom node
846 * has an attribute with the given name and whose value when
847 * considered as a space-separated list of words contains the given
848 * word. The comparison is case-sensitive. This corresponds to
849 * [attr~=value] in CSS.
851 * \param pw Pointer to the current SVG parser state
852 * \param node Libdom SVG node to test
853 * \param qname Attribute name to check for
854 * \param word Value word to check for
855 * \param match Pointer to the test result
857 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
858 * intern the attribute name (which usually indicates memory
861 css_error
node_has_attribute_includes(void *pw
, void *node
,
862 const css_qname
*qname
, lwc_string
*word
,
868 dom_string
*attr_val
;
870 size_t wordlen
; /* length of "word" */
872 /* pointers used to parse a space-separated list of words */
877 *match
= false; /* default to no match */
879 wordlen
= lwc_string_length(word
);
881 /* In this case, the spec says that "if 'val' is the
882 * empty string, it will never represent anything." */
886 /* Intern the attribute name as a dom_string so we can
887 * use dom_element_get_attribute() */
888 err
= dom_string_create_interned(
889 (const uint8_t *) lwc_string_data(qname
->name
),
890 lwc_string_length(qname
->name
),
892 if (err
!= DOM_NO_ERR
) {
896 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
897 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
898 /* There was an error getting the attribute's value or
899 * the attribute doesn't exist. So, no match? */
900 dom_string_unref(name
);
904 /* Parse the list comparing each word against "word" */
905 start
= dom_string_data(attr_val
);
906 end
= start
+ dom_string_byte_length(attr_val
);
907 dom_string_unref(attr_val
);
909 for (p
= start
; p
<= end
; p
++) {
910 /* Move forward until we find the end of the first word */
911 if (*p
== ' ' || *p
== '\0') {
912 /* If the length of that word is the length of the
913 * word we're looking for, do the comparison. */
914 if ((size_t) (p
- start
) == wordlen
&&
916 lwc_string_data(word
),
921 /* No match? Set "start" to the beginning of
922 * the next word and loop. */
932 * Test the given node for an attribute whose value begins with the
935 * This will return true (via the "match" pointer) if the libdom node
936 * has an attribute with the given name and whose value begins with
937 * the given prefix string. The comparison is case-sensitive. This
938 * corresponds to [attr^=value] in CSS.
940 * \param pw Pointer to the current SVG parser state
941 * \param node Libdom SVG node to test
942 * \param qname Attribute name to check for
943 * \param prefix Value prefix to check for
944 * \param match Pointer to the test result
946 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
947 * intern the attribute name (which usually indicates memory
950 css_error
node_has_attribute_prefix(void *pw
, void *node
,
951 const css_qname
*qname
, lwc_string
*prefix
,
956 dom_string
*attr_val
;
958 const char *avdata
; /* attribute value data */
959 size_t avdata_len
; /* length of that attribute value data */
960 size_t prefixlen
; /* length of "prefix" */
962 prefixlen
= lwc_string_length(prefix
);
963 if (prefixlen
== 0) {
964 /* In this case, the spec says that "if 'val' is the
965 * empty string, it will never represent anything." */
969 /* Intern the attribute name as a dom_string so we can
970 * use dom_element_get_attribute() */
971 err
= dom_string_create_interned(
972 (const uint8_t *) lwc_string_data(qname
->name
),
973 lwc_string_length(qname
->name
),
975 if (err
!= DOM_NO_ERR
) {
979 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
980 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
981 /* There was an error getting the attribute's value or
982 * the attribute doesn't exist. So, no match? */
983 dom_string_unref(name
);
988 /* Otherwise, we have the attribute value from the given node,
989 * and the first thing we want to do is check to see if the
990 * whole thing matches the prefix. */
991 dom_string_unref(name
);
992 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
994 /* If not, check to see if an, uh, prefix matches the
996 if (*match
== false) {
997 avdata
= dom_string_data(attr_val
);
998 avdata_len
= dom_string_byte_length(attr_val
);
999 if ((avdata_len
>= prefixlen
) &&
1000 (strncasecmp(avdata
,
1001 lwc_string_data(prefix
),
1003 /* Use strncasecmp to compare only the first
1004 * "n" characters, where "n" is the length of
1010 dom_string_unref(attr_val
);
1017 * Test the given node for an attribute whose value end with the
1020 * This will return true (via the "match" pointer) if the libdom node
1021 * has an attribute with the given name and whose value ends with
1022 * the given suffix string. The comparison is case-sensitive. This
1023 * corresponds to [attr$=value] in CSS.
1025 * \param pw Pointer to the current SVG parser state
1026 * \param node Libdom SVG node to test
1027 * \param qname Attribute name to check for
1028 * \param suffix Value suffix to check for
1029 * \param match Pointer to the test result
1031 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1032 * intern the attribute name (which usually indicates memory
1035 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1036 const css_qname
*qname
, lwc_string
*suffix
,
1041 dom_string
*attr_val
;
1043 const char *avdata
; /* attribute value data */
1044 size_t avdata_len
; /* length of that attribute value data */
1045 size_t suffixlen
; /* length of "suffix" */
1047 /* convenience pointer we'll use when matching the suffix */
1048 const char *suffix_start
;
1050 suffixlen
= lwc_string_length(suffix
);
1051 if (suffixlen
== 0) {
1052 /* In this case, the spec says that "if 'val' is the
1053 * empty string, it will never represent anything." */
1057 /* Intern the attribute name as a dom_string so we can
1058 * use dom_element_get_attribute() */
1059 err
= dom_string_create_interned(
1060 (const uint8_t *) lwc_string_data(qname
->name
),
1061 lwc_string_length(qname
->name
),
1063 if (err
!= DOM_NO_ERR
) {
1067 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1068 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1069 /* There was an error getting the attribute's value or
1070 * the attribute doesn't exist. So, no match? */
1071 dom_string_unref(name
);
1076 /* Otherwise, we have the attribute value from the given node,
1077 * and the first thing we want to do is check to see if the
1078 * whole thing matches the suffix. */
1079 dom_string_unref(name
);
1080 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1082 /* If not, check to see if an, uh, suffix matches the
1084 if (*match
== false) {
1085 avdata
= dom_string_data(attr_val
);
1086 avdata_len
= dom_string_byte_length(attr_val
);
1088 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1090 if ((avdata_len
>= suffixlen
) &&
1091 (strncasecmp(suffix_start
,
1092 lwc_string_data(suffix
),
1094 /* Use strncasecmp to compare only the last
1095 * "n" characters, where "n" is the length of
1101 dom_string_unref(attr_val
);
1108 * Test the given node for an attribute whose value contains the
1111 * This will return true (via the "match" pointer) if the libdom node
1112 * has an attribute with the given name and whose value contains the
1113 * given substring. The comparison is case-sensitive. This corresponds
1114 * to [attr*=value] in CSS.
1116 * \param pw Pointer to the current SVG parser state
1117 * \param node Libdom SVG node to test
1118 * \param qname Attribute name to check for
1119 * \param substring Value substring to check for
1120 * \param match Pointer to the test result
1122 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1123 * intern the attribute name (which usually indicates memory
1126 css_error
node_has_attribute_substring(void *pw
, void *node
,
1127 const css_qname
*qname
, lwc_string
*substring
,
1132 dom_string
*attr_val
;
1134 size_t attr_len
; /* length of attr_val */
1135 size_t substrlen
; /* length of "substring" */
1137 /* Convenience pointers we use when comparing substrings */
1141 substrlen
= lwc_string_length(substring
);
1142 if (substrlen
== 0) {
1143 /* In this case, the spec says that "if 'val' is the
1144 * empty string, it will never represent anything." */
1148 /* Intern the attribute name as a dom_string so we can
1149 * use dom_element_get_attribute() */
1150 err
= dom_string_create_interned(
1151 (const uint8_t *) lwc_string_data(qname
->name
),
1152 lwc_string_length(qname
->name
),
1154 if (err
!= DOM_NO_ERR
) {
1158 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1159 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1160 /* There was an error getting the attribute's value or
1161 * the attribute doesn't exist. So, no match? */
1162 dom_string_unref(name
);
1167 /* Otherwise, we have the attribute value from the given node,
1168 * and the first thing we want to do is check to see if the
1169 * whole thing matches the substring. */
1170 dom_string_unref(name
);
1171 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1173 /* If not, check to see if an, uh, substring matches the
1175 if (*match
== false) {
1176 p
= dom_string_data(attr_val
);
1178 /* Check every long-enough suffix for a prefix match */
1179 attr_len
= dom_string_byte_length(attr_val
);
1180 if (attr_len
>= substrlen
) {
1181 p_max
= p
+ attr_len
- substrlen
;
1182 while (p
<= p_max
) {
1184 lwc_string_data(substring
),
1194 dom_string_unref(attr_val
);
1201 * Test whether or not the given node is the document's root element
1202 * This corresponds to the CSS :root pseudo-selector.
1204 * \param pw Pointer to the current SVG parser state
1205 * \param node Libdom SVG node to test
1206 * \param match Pointer to the test result
1208 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1210 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1217 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1218 if (err
!= DOM_NO_ERR
) {
1222 /* It's the root element if it doesn't have a parent element */
1223 if (parent
!= NULL
) {
1224 err
= dom_node_get_node_type(parent
, &type
);
1225 dom_node_unref(parent
);
1226 if (err
!= DOM_NO_ERR
) {
1229 if (type
!= DOM_DOCUMENT_NODE
) {
1230 /* DOM_DOCUMENT_NODE is the only allowable
1231 * type of parent node for the root element */
1243 * Used internally in node_count_siblings() to "count" the given
1244 * sibling node. It factors out the node type and name checks.
1246 static int node_count_siblings_check(dom_node
*dnode
,
1253 dom_string
*dnode_name
;
1255 /* We flip this to 1 if/when we count this node */
1258 if (dnode
== NULL
) {
1262 exc
= dom_node_get_node_type(dnode
, &type
);
1263 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1264 /* We only count element siblings */
1268 /* ... with the right name */
1271 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1273 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1274 if (dom_string_isequal(name
,
1278 dom_string_unref(dnode_name
);
1289 * Count the given node's sibling elements
1291 * This counts the given node's sibling elements in one direction,
1292 * either forwards or backwards, in the DOM. Keep in mind that the
1293 * libdom tree is upside-down compared to the CSS one; so "next" and
1294 * "previous" are actually reversed; the default is to count preceding
1295 * libdom siblings which correspond to subsequent CSS siblings.
1297 * This operation is central to the CSS :first-child, :nth-child, and
1298 * :last-child (et cetera) pseudo-selectors.
1300 * If same_name is true, then only nodes having the same
1301 * (case-sensitive) name as the given node are counted.
1303 * \param pw Pointer to the current SVG parser state
1304 * \param node Libdom SVG node whose siblings we're counting
1305 * \param same_name Whether or not to count only siblings having
1306 * the same name as the given node
1307 * \param after Count subsequent siblings rather than precedent
1308 * ones (the default)
1309 * \param count Pointer to the return value, the number of sibling
1312 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1314 css_error
node_count_siblings(void *pw
, void *node
,
1315 bool same_name
, bool after
, int32_t *count
)
1319 dom_node
*dnode
; /* node, but with the right type */
1320 dom_string
*dnode_name
;
1321 dom_node
*next
; /* "next" sibling (depends on direction) */
1323 /* Pointer to the "next sibling" function */
1324 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1329 dnode
= (dom_node
*)node
;
1331 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1332 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1337 /* Increment the reference counter for dnode for as long as
1338 * we retain a reference to it. */
1339 dnode
= dom_node_ref(dnode
);
1341 next_func
= dom_node_get_previous_sibling
;
1343 next_func
= dom_node_get_next_sibling
;
1347 exc
= next_func(dnode
, &next
);
1348 if (exc
!= DOM_NO_ERR
) {
1352 /* If next_func worked, we're about to swap "next"
1353 * with "dnode" meaning that we will no longer retain
1354 * a reference to the current dnode. */
1355 dom_node_unref(dnode
);
1358 *count
+= node_count_siblings_check(dnode
,
1361 } while (dnode
!= NULL
);
1363 if (dnode_name
!= NULL
) {
1364 dom_string_unref(dnode_name
);
1372 * Determine whether or not the given element is empty
1374 * An element is "nonempty" if it has a child that is either an
1375 * element node or a text node.
1377 * \param pw Pointer to the current SVG parser state
1378 * \param node Libdom SVG node to check for emptiness
1379 * \param is_empty Pointer to the return value
1381 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1383 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1386 dom_node
*child
; /* current child node pointer */
1387 dom_node
*next
; /* next child node pointer */
1388 dom_node_type type
; /* what type of node is "child" */
1391 /* Assume that it's empty by default */
1394 /* Get the given node's first child. Implementation detail:
1395 * this increments the reference counter on the child node. */
1396 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1397 if (err
!= DOM_NO_ERR
) {
1401 /* And now loop through all children looking for a
1402 * text/element node. If we find one, the original
1403 * node is "nonempty" */
1404 while (child
!= NULL
) {
1405 err
= dom_node_get_node_type(child
, &type
);
1406 if (err
!= DOM_NO_ERR
) {
1407 dom_node_unref(child
);
1411 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1413 dom_node_unref(child
);
1417 err
= dom_node_get_next_sibling(child
, &next
);
1418 if (err
!= DOM_NO_ERR
) {
1419 dom_node_unref(child
);
1423 /* If we're moving to the next node, we can release
1424 * the reference to the current one */
1425 dom_node_unref(child
);
1434 * Determine whether or not the given node is a link
1436 * A node is a link if it is an element node whose name is "a" and if
1437 * it has an "href" attribute (case-sensitive). This selector
1438 * corresponds to node:link pseudo-class in CSS.
1440 * This pseudo-class is a bit awkward because the two standards (HTML5
1441 * and CSS) disagree on what it means, and because libsvgtiny does not
1442 * have enough information to determine if a link has been "visited"
1443 * yet -- that's a UI property. CSS says that :link is for unvisited
1444 * links, which we can't determine. HTML5 says that each link must
1445 * be either a :link or :visited. Since we can't decide either way,
1446 * It seems less wrong to declare that all links are unvisited; i.e.
1447 * that they match :link.
1449 * \param pw Pointer to the current SVG parser state
1450 * \param node Libdom SVG node to check
1451 * \param is_link Pointer to the boolean return value
1453 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1455 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1458 dom_node
*dnode
; /* node, but with the right type */
1459 dom_string
*dnode_name
;
1461 struct svgtiny_parse_state
* state
;
1463 dnode
= (dom_node
*)node
;
1465 has_href
= false; /* assume no href attribute */
1466 *is_link
= false; /* assume that it's not a link */
1468 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1469 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1473 state
= (struct svgtiny_parse_state
*)pw
;
1474 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1475 exc
= dom_element_has_attribute(node
,
1476 state
->interned_href
,
1478 if (exc
== DOM_NO_ERR
&& has_href
) {
1483 dom_string_unref(dnode_name
);
1489 * Check if the given node is being "hovered" over
1491 * This check always fails because the SVG DOM does not have the
1492 * necessary information (it's a UI property).
1494 * \param pw Pointer to the current SVG parser state; unused
1495 * \param node Libdom SVG node to check; unused
1496 * \param is_hover Pointer to the boolean return value
1498 * \return Always returns CSS_OK
1500 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)