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
);
56 * Resolve a relative URL to an absolute one by doing nothing. This is
57 * the simplest possible implementation of a URL resolver, needed for
60 css_error
svgtiny_resolve_url(void *pw
,
61 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
66 /* Copy the relative URL to the absolute one (the return
68 *abs
= lwc_string_ref(rel
);
73 * Create a stylesheet with the default set of params.
75 * \param sheet A stylesheet pointer, passed in by reference, that
76 * we use to store the newly-created stylesheet.
77 * \param inline_style True if this stylesheet represents an inline
78 * style, and false otherwise.
80 * \return The return value from css_stylesheet_create() is returned.
82 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
85 css_stylesheet_params params
;
87 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
88 params
.level
= CSS_LEVEL_DEFAULT
;
89 params
.charset
= NULL
;
92 params
.allow_quirks
= false;
93 params
.inline_style
= inline_style
;
94 params
.resolve
= svgtiny_resolve_url
;
95 params
.resolve_pw
= NULL
;
97 params
.import_pw
= NULL
;
99 params
.color_pw
= NULL
;
101 params
.font_pw
= NULL
;
103 return css_stylesheet_create(¶ms
, sheet
);
107 /**************************/
108 /* libcss select handlers */
109 /**************************/
111 * From here on we implement the "select handler "API defined in
112 * libcss's include/libcss/select.h and discussed briefly in its
118 * Retrieve the given node's name
120 * \param pw Pointer to the current SVG parser state
121 * \param node Libdom SVG node
122 * \param qname Address at which to store the node name
124 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
126 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
130 struct svgtiny_parse_state
*state
;
132 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
133 if (err
!= DOM_NO_ERR
) {
137 state
= (struct svgtiny_parse_state
*)pw
;
138 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
140 err
= dom_string_intern(name
, &qname
->name
);
141 if (err
!= DOM_NO_ERR
) {
142 dom_string_unref(name
);
146 dom_string_unref(name
);
153 * Retrieve the given node's classes
155 * \param pw Pointer to the current SVG parser state
156 * \param node Libdom SVG node
157 * \param classes Address at which to store the class name array
158 * \param n_classes Address at which to store the length of the class
161 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
163 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
164 * because the underlying libdom function never fails
166 css_error
node_classes(void *pw
, void *node
,
167 lwc_string
***classes
, uint32_t *n_classes
)
172 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
174 /* The implementation does not do it, but the documentation
175 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
176 possible here, so we handle it to be on the safe side. */
177 if (err
!= DOM_NO_ERR
) {
186 * Retrieve the given node's id
188 * \param pw Pointer to the current SVG parser state
189 * \param node Libdom SVG node
190 * \param id Address at which to store the id
192 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
194 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
198 struct svgtiny_parse_state
*state
;
200 /* Begin with the assumption that this node has no id */
203 state
= (struct svgtiny_parse_state
*)pw
;
204 err
= dom_element_get_attribute((dom_node
*)node
,
205 state
->interned_id
, &attr
);
206 if (err
!= DOM_NO_ERR
) {
209 else if (attr
== NULL
) {
210 /* The node has no id attribute and our return value
211 is already set to NULL so we're done */
215 /* If we found an id attribute (a dom_string), intern it into
216 an lwc_string that we can return, and then cleanup the
218 err
= dom_string_intern(attr
, id
);
219 if (err
!= DOM_NO_ERR
) {
220 dom_string_unref(attr
);
223 dom_string_unref(attr
);
230 * Find the first parent of the given element having the given name
232 * \param pw Pointer to the current SVG parser state
233 * \param node Libdom SVG node
234 * \param qname Name of the parent node to search for
235 * \param parent Address at which to store the parent node pointer
237 * \return Always returns CSS_OK
239 * \post If a suitable element is found, a pointer to it will be
240 * stored at the address pointed to by \a parent; otherwise,
241 * NULL will be stored at the address pointed to by \a parent
243 css_error
named_parent_node(void *pw
, void *node
,
244 const css_qname
*qname
, void **parent
)
247 /* dom_element_named_parent_node() was invented to implement
248 * this select handler so there isn't much for us to do except
249 * call it. It's OK if node isn't an element, libdom checks
251 dom_element_named_parent_node((dom_element
*)node
,
253 (struct dom_element
**)parent
);
255 /* Implementation detail: dom_element_named_parent_node()
256 * increments the reference count of the parent element before
257 * returning it to us. According to docs/RefCnt in the libdom
258 * repository, this will prevent the parent element from being
259 * destroyed if it is pruned from the DOM. That sounds good,
260 * since we don't want to be using a pointer to an object that
261 * has been destroyed... but we also have no way of later
262 * decrementing the reference count ourselves, and don't want
263 * to make the returned node eternal. Decrementing the
264 * reference counter now allows it to be destroyed when the
265 * DOM no longer needs it, and so long as no other parts of
266 * libsvgtiny are messing with the DOM during parsing, that
267 * shouldn't (ha ha) cause any problems. */
268 dom_node_unref(*parent
);
275 * Find the "next-sibling" of the given element having the given name
277 * This search corresponds to the "+ foo" combinator in CSS and will
278 * find only "foo" element nodes that immediately precede the given
279 * node under the same parent in the DOM. In CSS the tree is viewed
280 * top-down and in libdom it is viewed from the bottom-up; as a result
281 * "next" and "previous" are sometimes backwards. This is case-sensitive.
283 * \param pw Pointer to the current SVG parser state
284 * \param node Libdom SVG node
285 * \param qname Name of the sibling node to search for
286 * \param sibling Address at which to store the sibling node pointer
288 * \return Always returns CSS_OK
290 * \post If a suitable element is found, a pointer to it will be
291 * stored at the address pointed to by \a sibling; otherwise,
292 * NULL will be stored at the address pointed to by \a sibling
294 css_error
named_sibling_node(void *pw
, void *node
,
295 const css_qname
*qname
, void **sibling
)
298 dom_node
*n
= node
; /* the current node */
299 dom_node
*prev
; /* the previous node */
304 *sibling
= NULL
; /* default to nothing found */
306 /* Begin the search; the first iteration we do outside of the
307 * loop. Implementation detil: dom_node_get_previous_sibling()
308 * increments the reference counter on the returned node. A
309 * comment within named_parent_node() explains why we
310 * decrement it ASAP. */
311 err
= dom_node_get_previous_sibling(n
, &n
);
312 if (err
!= DOM_NO_ERR
) {
317 /* We're looking for the first ELEMENT sibling */
318 err
= dom_node_get_node_type(n
, &type
);
319 if (err
!= DOM_NO_ERR
) {
324 if (type
== DOM_ELEMENT_NODE
) {
325 /* We found an element node, does it have the
327 err
= dom_node_get_node_name(n
, &name
);
328 if (err
!= DOM_NO_ERR
) {
333 if (dom_string_lwc_isequal(name
,
335 /* The name is right, return it */
339 /* There's only one next-sibling element node
340 * and we've already found it, so if its name
341 * wasn't right, we return the default value
343 dom_string_unref(name
);
348 /* Not an element node, so we move on the the previous
349 * previous sibling */
350 err
= dom_node_get_previous_sibling(n
, &prev
);
351 if (err
!= DOM_NO_ERR
) {
365 * Find the first "subsequent-sibling" of the given element having the
368 * This search corresponds to the "~ foo" combinator in CSS and will
369 * find only "foo" element nodes that precede the given node (under
370 * the same parent) in the DOM. In CSS the tree is viewed top-down and
371 * in libdom it is viewed from the bottom-up; as a result "next" and
372 * "previous" are sometimes backwards. This is case-sensitive.
374 * \param pw Pointer to the current SVG parser state
375 * \param node Libdom SVG node
376 * \param qname Name of the sibling node to search for
377 * \param sibling Address at which to store the sibling node pointer
379 * \return Always returns CSS_OK
381 * \post If a suitable element is found, a pointer to it will be
382 * stored at the address pointed to by \a sibling; otherwise,
383 * NULL will be stored at the address pointed to by \a sibling
385 css_error
named_generic_sibling_node(void *pw
, void *node
,
386 const css_qname
*qname
, void **sibling
)
389 dom_node
*n
= node
; /* the current node */
390 dom_node
*prev
; /* the previous node */
396 *sibling
= NULL
; /* default to nothing found */
398 /* Begin the search; the first iteration we do outside of the
399 * loop. Implementation detil: dom_node_get_previous_sibling()
400 * increments the reference counter on the returned node. A
401 * comment within named_parent_node() explains why we
402 * decrement it ASAP. */
403 err
= dom_node_get_previous_sibling(n
, &n
);
404 if (err
!= DOM_NO_ERR
) {
409 err
= dom_node_get_node_type(n
, &type
);
410 if (err
!= DOM_NO_ERR
) {
415 if (type
== DOM_ELEMENT_NODE
) {
416 /* We only want ELEMENT nodes */
417 err
= dom_node_get_node_name(n
, &name
);
418 if (err
!= DOM_NO_ERR
) {
423 if (dom_string_lwc_isequal(name
,
425 /* Found one. Save it and stop the search */
426 dom_string_unref(name
);
432 dom_string_unref(name
);
435 /* This sibling wasn't an element with the desired
436 name, so move on to the previous sibling */
437 err
= dom_node_get_previous_sibling(n
, &prev
);
438 if (err
!= DOM_NO_ERR
) {
452 * Return a pointer to the given node's parent
454 * \param pw Pointer to the current SVG parser state
455 * \param node Libdom SVG node
456 * \param parent Address at which to store the node's parent pointer
458 * \return Always returns CSS_OK
460 css_error
parent_node(void *pw
, void *node
, void **parent
)
463 /* Libdom basically implements this for us */
464 dom_element_parent_node(node
, (struct dom_element
**)parent
);
466 /* See the comment in named_parent_node() for why we decrement
467 * this reference counter here. */
468 dom_node_unref(*parent
);
475 * Find the "next-sibling" of the given element
477 * This search corresponds "+ *" in CSS and will find the first
478 * element node that immediately precedes the given node under the
479 * same parent in the DOM. In CSS the tree is viewed top-down and in
480 * libdom it is viewed from the bottom-up; as a result "next" and
481 * "previous" are sometimes backwards.
483 * \param pw Pointer to the current SVG parser state
484 * \param node Libdom SVG node
485 * \param sibling Address at which to store the sibling node pointer
487 * \return Always returns CSS_OK
489 * \post If a suitable element is found, a pointer to it will be
490 * stored at the address pointed to by \a sibling; otherwise,
491 * NULL will be stored at the address pointed to by \a sibling
493 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
496 dom_node
*n
= node
; /* the current node */
497 dom_node
*prev
; /* the previous node */
501 *sibling
= NULL
; /* default to nothing found */
503 /* Begin the search; the first iteration we do outside of the
504 * loop. Implementation detil: dom_node_get_previous_sibling()
505 * increments the reference counter on the returned node. A
506 * comment within named_parent_node() explains why we
507 * decrement it ASAP. */
508 err
= dom_node_get_previous_sibling(n
, &n
);
509 if (err
!= DOM_NO_ERR
) {
514 err
= dom_node_get_node_type(n
, &type
);
515 if (err
!= DOM_NO_ERR
) {
520 if (type
== DOM_ELEMENT_NODE
) {
521 /* We found a sibling node that is also an
522 element and that's all we wanted. */
528 /* This sibling node was not an element; move on to
529 the previous sibling */
530 err
= dom_node_get_previous_sibling(n
, &prev
);
531 if (err
!= DOM_NO_ERR
) {
545 * Test the given node for the given name
547 * This will return true (via the "match" pointer) if the libdom node
548 * has the given name or if that name is the universal selector;
549 * otherwise it returns false. The comparison is case-sensitive. It
550 * corresponds to a rule like "body { ... }" in CSS.
552 * \param pw Pointer to the current SVG parser state
553 * \param node Libdom SVG node to test
554 * \param qname Name to check for
555 * \param match Pointer to the test result
557 * \return Always returns CSS_OK
559 css_error
node_has_name(void *pw
, void *node
,
560 const css_qname
*qname
, bool *match
)
562 struct svgtiny_parse_state
*state
;
566 /* Start by checking to see if qname is the universal selector */
567 state
= (struct svgtiny_parse_state
*)pw
;
568 if (lwc_string_isequal(qname
->name
,
569 state
->interned_universal
, match
) == lwc_error_ok
) {
571 /* It's the universal selector. In NetSurf, all node
572 * names match the universal selector, and nothing in
573 * the libcss documentation suggests another approach,
574 * so we follow NetSurf here. */
579 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
580 if (err
!= DOM_NO_ERR
) {
584 /* Unlike with HTML, SVG element names are case-sensitive */
585 *match
= dom_string_lwc_isequal(name
, qname
->name
);
586 dom_string_unref(name
);
593 * Test the given node for the given class
595 * This will return true (via the "match" pointer) if the libdom node
596 * has the given class. The comparison is case-sensitive. It
597 * corresponds to node.class in CSS.
599 * \param pw Pointer to the current SVG parser state
600 * \param node Libdom SVG node to test
601 * \param name Class name to check for
602 * \param match Pointer to the test result
604 * \return Always returns CSS_OK
606 css_error
node_has_class(void *pw
, void *node
,
607 lwc_string
*name
, bool *match
)
610 /* libdom implements this for us and apparently it cannot fail */
611 dom_element_has_class((dom_node
*)node
, name
, match
);
617 * Test the given node for the given id
619 * This will return true (via the "match" pointer) if the libdom node
620 * has the given id. The comparison is case-sensitive. It corresponds
623 * \param pw Pointer to the current SVG parser state
624 * \param node Libdom SVG node to test
625 * \param name Id to check for
626 * \param match Pointer to the test result
628 * \return Always returns CSS_OK
630 css_error
node_has_id(void *pw
, void *node
,
631 lwc_string
*name
, bool *match
)
635 struct svgtiny_parse_state
*state
;
637 attr
= NULL
; /* a priori the "id" attribute may not exist */
638 *match
= false; /* default to no match */
640 state
= (struct svgtiny_parse_state
*)pw
;
641 err
= dom_element_get_attribute((dom_node
*)node
,
642 state
->interned_id
, &attr
);
643 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
647 *match
= dom_string_lwc_isequal(attr
, name
);
648 dom_string_unref(attr
);
655 * Test the given node for the given attribute
657 * This will return true (via the "match" pointer) if the libdom node
658 * has an attribute with the given name. The comparison is
659 * case-sensitive. It corresponds to node[attr] in CSS.
661 * \param pw Pointer to the current SVG parser state
662 * \param node Libdom SVG node to test
663 * \param qname Attribute name to check for
664 * \param match Pointer to the test result
666 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
669 css_error
node_has_attribute(void *pw
, void *node
,
670 const css_qname
*qname
, bool *match
)
676 /* intern the attribute name as a dom_string so we can
677 * delegate to dom_element_has_attribute() */
678 err
= dom_string_create_interned(
679 (const uint8_t *) lwc_string_data(qname
->name
),
680 lwc_string_length(qname
->name
),
682 if (err
!= DOM_NO_ERR
) {
686 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
687 if (err
!= DOM_NO_ERR
) {
688 dom_string_unref(name
);
692 dom_string_unref(name
);
698 * Test the given node for an attribute with a specific value
700 * This will return true (via the "match" pointer) if the libdom node
701 * has an attribute with the given name and value. The comparison is
702 * case-sensitive. It corresponds to node[attr=value] in CSS.
704 * \param pw Pointer to the current SVG parser state
705 * \param node Libdom SVG node to test
706 * \param qname Attribute name to check for
707 * \param value Attribute value to check for
708 * \param match Pointer to the test result
710 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
711 * intern the attribute name (which usually indicates memory
714 css_error
node_has_attribute_equal(void *pw
, void *node
,
715 const css_qname
*qname
, lwc_string
*value
,
718 /* Implementation note: NetSurf always returns "no match" when
719 * the value is empty (length zero). We allow it, because why
724 dom_string
*attr_val
;
727 /* Intern the attribute name as a dom_string so we can
728 * use dom_element_get_attribute() */
729 err
= dom_string_create_interned(
730 (const uint8_t *) lwc_string_data(qname
->name
),
731 lwc_string_length(qname
->name
),
733 if (err
!= DOM_NO_ERR
) {
737 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
738 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
739 /* There was an error getting the attribute's value or
740 * the attribute doesn't exist. So, no match? */
741 dom_string_unref(name
);
746 /* Otherwise, we have the attribute value from the given node
747 * and all we need to do is compare. */
748 dom_string_unref(name
);
749 *match
= dom_string_lwc_isequal(attr_val
, value
);
750 dom_string_unref(attr_val
);
757 * Test the given node for an attribute with a specific value,
758 * possibly followed by a single hyphen
760 * This will return true (via the "match" pointer) if the libdom node
761 * has an attribute with the given name and value or with the given
762 * name and a value that is followed by exactly one hyphen. The
763 * comparison is case-sensitive. This corresponds to [attr|=value]
766 * \param pw Pointer to the current SVG parser state
767 * \param node Libdom SVG node to test
768 * \param qname Attribute name to check for
769 * \param value Attribute value to check for
770 * \param match Pointer to the test result
772 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
773 * intern the attribute name (which usually indicates memory
776 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
777 const css_qname
*qname
, lwc_string
*value
,
780 /* Implementation note: NetSurf always returns "no match" when
781 * the value is empty (length zero). We allow it, because why
786 dom_string
*attr_val
;
789 const char *vdata
; /* to hold the data underlying "value" */
791 const char *avdata
; /* to hold the found attribute value data */
794 /* Intern the attribute name as a dom_string so we can
795 * use dom_element_get_attribute() */
796 err
= dom_string_create_interned(
797 (const uint8_t *) lwc_string_data(qname
->name
),
798 lwc_string_length(qname
->name
),
800 if (err
!= DOM_NO_ERR
) {
804 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
805 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
806 /* There was an error getting the attribute's value or
807 * the attribute doesn't exist. So, no match? */
808 dom_string_unref(name
);
813 /* Otherwise, we have the attribute value from the given node
814 * and all we need to do is compare. */
815 dom_string_unref(name
);
816 *match
= dom_string_lwc_isequal(attr_val
, value
);
818 /* Exact match, we're done */
819 dom_string_unref(attr_val
);
823 /* No exact match, try it with a hyphen on the end */
824 vdata
= lwc_string_data(value
); /* needle */
825 vdata_len
= lwc_string_length(value
);
826 avdata
= dom_string_data(attr_val
); /* haystack */
827 avdata_len
= dom_string_byte_length(attr_val
);
828 dom_string_unref(attr_val
);
830 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
831 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
832 /* If there's a hyphen in the right position,
833 * it suffices to compare the strings only up
844 * Test the given node for an attribute whose value is a
845 * space-separated list of words, one of which is the given word
847 * This will return true (via the "match" pointer) if the libdom node
848 * has an attribute with the given name and whose value when
849 * considered as a space-separated list of words contains the given
850 * word. The comparison is case-sensitive. This corresponds to
851 * [attr~=value] in CSS.
853 * \param pw Pointer to the current SVG parser state
854 * \param node Libdom SVG node to test
855 * \param qname Attribute name to check for
856 * \param word Value word to check for
857 * \param match Pointer to the test result
859 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
860 * intern the attribute name (which usually indicates memory
863 css_error
node_has_attribute_includes(void *pw
, void *node
,
864 const css_qname
*qname
, lwc_string
*word
,
870 dom_string
*attr_val
;
872 size_t wordlen
; /* length of "word" */
874 /* pointers used to parse a space-separated list of words */
879 *match
= false; /* default to no match */
881 wordlen
= lwc_string_length(word
);
883 /* In this case, the spec says that "if 'val' is the
884 * empty string, it will never represent anything." */
888 /* Intern the attribute name as a dom_string so we can
889 * use dom_element_get_attribute() */
890 err
= dom_string_create_interned(
891 (const uint8_t *) lwc_string_data(qname
->name
),
892 lwc_string_length(qname
->name
),
894 if (err
!= DOM_NO_ERR
) {
898 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
899 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
900 /* There was an error getting the attribute's value or
901 * the attribute doesn't exist. So, no match? */
902 dom_string_unref(name
);
906 /* Parse the list comparing each word against "word" */
907 start
= dom_string_data(attr_val
);
908 end
= start
+ dom_string_byte_length(attr_val
);
909 dom_string_unref(attr_val
);
911 for (p
= start
; p
<= end
; p
++) {
912 /* Move forward until we find the end of the first word */
913 if (*p
== ' ' || *p
== '\0') {
914 /* If the length of that word is the length of the
915 * word we're looking for, do the comparison. */
916 if ((size_t) (p
- start
) == wordlen
&&
918 lwc_string_data(word
),
923 /* No match? Set "start" to the beginning of
924 * the next word and loop. */
934 * Test the given node for an attribute whose value begins with the
937 * This will return true (via the "match" pointer) if the libdom node
938 * has an attribute with the given name and whose value begins with
939 * the given prefix string. The comparison is case-sensitive. This
940 * corresponds to [attr^=value] in CSS.
942 * \param pw Pointer to the current SVG parser state
943 * \param node Libdom SVG node to test
944 * \param qname Attribute name to check for
945 * \param prefix Value prefix to check for
946 * \param match Pointer to the test result
948 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
949 * intern the attribute name (which usually indicates memory
952 css_error
node_has_attribute_prefix(void *pw
, void *node
,
953 const css_qname
*qname
, lwc_string
*prefix
,
958 dom_string
*attr_val
;
960 const char *avdata
; /* attribute value data */
961 size_t avdata_len
; /* length of that attribute value data */
962 size_t prefixlen
; /* length of "prefix" */
964 prefixlen
= lwc_string_length(prefix
);
965 if (prefixlen
== 0) {
966 /* In this case, the spec says that "if 'val' is the
967 * empty string, it will never represent anything." */
971 /* Intern the attribute name as a dom_string so we can
972 * use dom_element_get_attribute() */
973 err
= dom_string_create_interned(
974 (const uint8_t *) lwc_string_data(qname
->name
),
975 lwc_string_length(qname
->name
),
977 if (err
!= DOM_NO_ERR
) {
981 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
982 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
983 /* There was an error getting the attribute's value or
984 * the attribute doesn't exist. So, no match? */
985 dom_string_unref(name
);
990 /* Otherwise, we have the attribute value from the given node,
991 * and the first thing we want to do is check to see if the
992 * whole thing matches the prefix. */
993 dom_string_unref(name
);
994 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
996 /* If not, check to see if an, uh, prefix matches the
998 if (*match
== false) {
999 avdata
= dom_string_data(attr_val
);
1000 avdata_len
= dom_string_byte_length(attr_val
);
1001 if ((avdata_len
>= prefixlen
) &&
1002 (strncasecmp(avdata
,
1003 lwc_string_data(prefix
),
1005 /* Use strncasecmp to compare only the first
1006 * "n" characters, where "n" is the length of
1012 dom_string_unref(attr_val
);
1019 * Test the given node for an attribute whose value end with the
1022 * This will return true (via the "match" pointer) if the libdom node
1023 * has an attribute with the given name and whose value ends with
1024 * the given suffix string. The comparison is case-sensitive. This
1025 * corresponds to [attr$=value] in CSS.
1027 * \param pw Pointer to the current SVG parser state
1028 * \param node Libdom SVG node to test
1029 * \param qname Attribute name to check for
1030 * \param suffix Value suffix to check for
1031 * \param match Pointer to the test result
1033 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1034 * intern the attribute name (which usually indicates memory
1037 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1038 const css_qname
*qname
, lwc_string
*suffix
,
1043 dom_string
*attr_val
;
1045 const char *avdata
; /* attribute value data */
1046 size_t avdata_len
; /* length of that attribute value data */
1047 size_t suffixlen
; /* length of "suffix" */
1049 /* convenience pointer we'll use when matching the suffix */
1050 const char *suffix_start
;
1052 suffixlen
= lwc_string_length(suffix
);
1053 if (suffixlen
== 0) {
1054 /* In this case, the spec says that "if 'val' is the
1055 * empty string, it will never represent anything." */
1059 /* Intern the attribute name as a dom_string so we can
1060 * use dom_element_get_attribute() */
1061 err
= dom_string_create_interned(
1062 (const uint8_t *) lwc_string_data(qname
->name
),
1063 lwc_string_length(qname
->name
),
1065 if (err
!= DOM_NO_ERR
) {
1069 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1070 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1071 /* There was an error getting the attribute's value or
1072 * the attribute doesn't exist. So, no match? */
1073 dom_string_unref(name
);
1078 /* Otherwise, we have the attribute value from the given node,
1079 * and the first thing we want to do is check to see if the
1080 * whole thing matches the suffix. */
1081 dom_string_unref(name
);
1082 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1084 /* If not, check to see if an, uh, suffix matches the
1086 if (*match
== false) {
1087 avdata
= dom_string_data(attr_val
);
1088 avdata_len
= dom_string_byte_length(attr_val
);
1090 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1092 if ((avdata_len
>= suffixlen
) &&
1093 (strncasecmp(suffix_start
,
1094 lwc_string_data(suffix
),
1096 /* Use strncasecmp to compare only the last
1097 * "n" characters, where "n" is the length of
1103 dom_string_unref(attr_val
);
1110 * Test the given node for an attribute whose value contains the
1113 * This will return true (via the "match" pointer) if the libdom node
1114 * has an attribute with the given name and whose value contains the
1115 * given substring. The comparison is case-sensitive. This corresponds
1116 * to [attr*=value] in CSS.
1118 * \param pw Pointer to the current SVG parser state
1119 * \param node Libdom SVG node to test
1120 * \param qname Attribute name to check for
1121 * \param substring Value substring to check for
1122 * \param match Pointer to the test result
1124 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1125 * intern the attribute name (which usually indicates memory
1128 css_error
node_has_attribute_substring(void *pw
, void *node
,
1129 const css_qname
*qname
, lwc_string
*substring
,
1134 dom_string
*attr_val
;
1136 size_t attr_len
; /* length of attr_val */
1137 size_t substrlen
; /* length of "substring" */
1139 /* Convenience pointers we use when comparing substrings */
1143 substrlen
= lwc_string_length(substring
);
1144 if (substrlen
== 0) {
1145 /* In this case, the spec says that "if 'val' is the
1146 * empty string, it will never represent anything." */
1150 /* Intern the attribute name as a dom_string so we can
1151 * use dom_element_get_attribute() */
1152 err
= dom_string_create_interned(
1153 (const uint8_t *) lwc_string_data(qname
->name
),
1154 lwc_string_length(qname
->name
),
1156 if (err
!= DOM_NO_ERR
) {
1160 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1161 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1162 /* There was an error getting the attribute's value or
1163 * the attribute doesn't exist. So, no match? */
1164 dom_string_unref(name
);
1169 /* Otherwise, we have the attribute value from the given node,
1170 * and the first thing we want to do is check to see if the
1171 * whole thing matches the substring. */
1172 dom_string_unref(name
);
1173 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1175 /* If not, check to see if an, uh, substring matches the
1177 if (*match
== false) {
1178 p
= dom_string_data(attr_val
);
1180 /* Check every long-enough suffix for a prefix match */
1181 attr_len
= dom_string_byte_length(attr_val
);
1182 if (attr_len
>= substrlen
) {
1183 p_max
= p
+ attr_len
- substrlen
;
1184 while (p
<= p_max
) {
1186 lwc_string_data(substring
),
1196 dom_string_unref(attr_val
);
1203 * Test whether or not the given node is the document's root element
1204 * This corresponds to the CSS :root pseudo-selector.
1206 * \param pw Pointer to the current SVG parser state
1207 * \param node Libdom SVG node to test
1208 * \param match Pointer to the test result
1210 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1212 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1219 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1220 if (err
!= DOM_NO_ERR
) {
1224 /* It's the root element if it doesn't have a parent element */
1225 if (parent
!= NULL
) {
1226 err
= dom_node_get_node_type(parent
, &type
);
1227 dom_node_unref(parent
);
1228 if (err
!= DOM_NO_ERR
) {
1231 if (type
!= DOM_DOCUMENT_NODE
) {
1232 /* DOM_DOCUMENT_NODE is the only allowable
1233 * type of parent node for the root element */
1245 * Used internally in node_count_siblings() to "count" the given
1246 * sibling node. It factors out the node type and name checks.
1248 static int node_count_siblings_check(dom_node
*dnode
,
1255 dom_string
*dnode_name
;
1257 /* We flip this to 1 if/when we count this node */
1260 if (dnode
== NULL
) {
1264 exc
= dom_node_get_node_type(dnode
, &type
);
1265 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1266 /* We only count element siblings */
1270 /* ... with the right name */
1273 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1275 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1276 if (dom_string_isequal(name
,
1280 dom_string_unref(dnode_name
);
1291 * Count the given node's sibling elements
1293 * This counts the given node's sibling elements in one direction,
1294 * either forwards or backwards, in the DOM. Keep in mind that the
1295 * libdom tree is upside-down compared to the CSS one; so "next" and
1296 * "previous" are actually reversed; the default is to count preceding
1297 * libdom siblings which correspond to subsequent CSS siblings.
1299 * This operation is central to the CSS :first-child, :nth-child, and
1300 * :last-child (et cetera) pseudo-selectors.
1302 * If same_name is true, then only nodes having the same
1303 * (case-sensitive) name as the given node are counted.
1305 * \param pw Pointer to the current SVG parser state
1306 * \param node Libdom SVG node whose siblings we're counting
1307 * \param same_name Whether or not to count only siblings having
1308 * the same name as the given node
1309 * \param after Count subsequent siblings rather than precedent
1310 * ones (the default)
1311 * \param count Pointer to the return value, the number of sibling
1314 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1316 css_error
node_count_siblings(void *pw
, void *node
,
1317 bool same_name
, bool after
, int32_t *count
)
1321 dom_node
*dnode
; /* node, but with the right type */
1322 dom_string
*dnode_name
;
1323 dom_node
*next
; /* "next" sibling (depends on direction) */
1325 /* Pointer to the "next sibling" function */
1326 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1331 dnode
= (dom_node
*)node
;
1333 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1334 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1339 /* Increment the reference counter for dnode for as long as
1340 * we retain a reference to it. */
1341 dnode
= dom_node_ref(dnode
);
1343 next_func
= dom_node_get_previous_sibling
;
1345 next_func
= dom_node_get_next_sibling
;
1349 exc
= next_func(dnode
, &next
);
1350 if (exc
!= DOM_NO_ERR
) {
1354 /* If next_func worked, we're about to swap "next"
1355 * with "dnode" meaning that we will no longer retain
1356 * a reference to the current dnode. */
1357 dom_node_unref(dnode
);
1360 *count
+= node_count_siblings_check(dnode
,
1363 } while (dnode
!= NULL
);
1365 if (dnode_name
!= NULL
) {
1366 dom_string_unref(dnode_name
);
1374 * Determine whether or not the given element is empty
1376 * An element is "nonempty" if it has a child that is either an
1377 * element node or a text node.
1379 * \param pw Pointer to the current SVG parser state
1380 * \param node Libdom SVG node to check for emptiness
1381 * \param is_empty Pointer to the return value
1383 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1385 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1388 dom_node
*child
; /* current child node pointer */
1389 dom_node
*next
; /* next child node pointer */
1390 dom_node_type type
; /* what type of node is "child" */
1393 /* Assume that it's empty by default */
1396 /* Get the given node's first child. Implementation detail:
1397 * this increments the reference counter on the child node. */
1398 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1399 if (err
!= DOM_NO_ERR
) {
1403 /* And now loop through all children looking for a
1404 * text/element node. If we find one, the original
1405 * node is "nonempty" */
1406 while (child
!= NULL
) {
1407 err
= dom_node_get_node_type(child
, &type
);
1408 if (err
!= DOM_NO_ERR
) {
1409 dom_node_unref(child
);
1413 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1415 dom_node_unref(child
);
1419 err
= dom_node_get_next_sibling(child
, &next
);
1420 if (err
!= DOM_NO_ERR
) {
1421 dom_node_unref(child
);
1425 /* If we're moving to the next node, we can release
1426 * the reference to the current one */
1427 dom_node_unref(child
);
1436 * Determine whether or not the given node is a link
1438 * A node is a link if it is an element node whose name is "a" and if
1439 * it has an "href" attribute (case-sensitive). This selector
1440 * corresponds to node:link pseudo-class in CSS.
1442 * This pseudo-class is a bit awkward because the two standards (HTML5
1443 * and CSS) disagree on what it means, and because libsvgtiny does not
1444 * have enough information to determine if a link has been "visited"
1445 * yet -- that's a UI property. CSS says that :link is for unvisited
1446 * links, which we can't determine. HTML5 says that each link must
1447 * be either a :link or :visited. Since we can't decide either way,
1448 * It seems less wrong to declare that all links are unvisited; i.e.
1449 * that they match :link.
1451 * \param pw Pointer to the current SVG parser state
1452 * \param node Libdom SVG node to check
1453 * \param is_link Pointer to the boolean return value
1455 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1457 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1460 dom_node
*dnode
; /* node, but with the right type */
1461 dom_string
*dnode_name
;
1463 struct svgtiny_parse_state
* state
;
1465 dnode
= (dom_node
*)node
;
1467 has_href
= false; /* assume no href attribute */
1468 *is_link
= false; /* assume that it's not a link */
1470 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1471 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1475 state
= (struct svgtiny_parse_state
*)pw
;
1476 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1477 exc
= dom_element_has_attribute(node
,
1478 state
->interned_href
,
1480 if (exc
== DOM_NO_ERR
&& has_href
) {
1485 dom_string_unref(dnode_name
);
1491 * Check if the given node is being "hovered" over
1493 * This check always fails because the SVG DOM does not have the
1494 * necessary information (it's a UI property).
1496 * \param pw Pointer to the current SVG parser state; unused
1497 * \param node Libdom SVG node to check; unused
1498 * \param is_hover Pointer to the boolean return value
1500 * \return Always returns CSS_OK
1502 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1512 * Check if the given node is "active"
1514 * This check always fails because the SVG DOM does not have the
1515 * necessary information (it's a UI property).
1517 * \param pw Pointer to the current SVG parser state; unused
1518 * \param node Libdom SVG node to check; unused
1519 * \param is_active Pointer to the boolean return value
1521 * \return Always returns CSS_OK
1523 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1533 * Check if the given node has the focus
1535 * This check always fails because the SVG DOM does not have the
1536 * necessary information (it's a UI property).
1538 * \param pw Pointer to the current SVG parser state; unused
1539 * \param node Libdom SVG node to check; unused
1540 * \param is_focus Pointer to the boolean return value
1542 * \return Always returns CSS_OK
1544 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)