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
);
55 * Resolve a relative URL to an absolute one by doing nothing. This is
56 * the simplest possible implementation of a URL resolver, needed for
59 css_error
svgtiny_resolve_url(void *pw
,
60 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
65 /* Copy the relative URL to the absolute one (the return
67 *abs
= lwc_string_ref(rel
);
72 * Create a stylesheet with the default set of params.
74 * \param sheet A stylesheet pointer, passed in by reference, that
75 * we use to store the newly-created stylesheet.
76 * \param inline_style True if this stylesheet represents an inline
77 * style, and false otherwise.
79 * \return The return value from css_stylesheet_create() is returned.
81 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
84 css_stylesheet_params params
;
86 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
87 params
.level
= CSS_LEVEL_DEFAULT
;
88 params
.charset
= NULL
;
91 params
.allow_quirks
= false;
92 params
.inline_style
= inline_style
;
93 params
.resolve
= svgtiny_resolve_url
;
94 params
.resolve_pw
= NULL
;
96 params
.import_pw
= NULL
;
98 params
.color_pw
= NULL
;
100 params
.font_pw
= NULL
;
102 return css_stylesheet_create(¶ms
, sheet
);
106 /**************************/
107 /* libcss select handlers */
108 /**************************/
110 * From here on we implement the "select handler "API defined in
111 * libcss's include/libcss/select.h and discussed briefly in its
117 * Retrieve the given node's name
119 * \param pw Pointer to the current SVG parser state
120 * \param node Libdom SVG node
121 * \param qname Address at which to store the node name
123 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
125 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
129 struct svgtiny_parse_state
*state
;
131 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
132 if (err
!= DOM_NO_ERR
) {
136 state
= (struct svgtiny_parse_state
*)pw
;
137 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
139 err
= dom_string_intern(name
, &qname
->name
);
140 if (err
!= DOM_NO_ERR
) {
141 dom_string_unref(name
);
145 dom_string_unref(name
);
152 * Retrieve the given node's classes
154 * \param pw Pointer to the current SVG parser state
155 * \param node Libdom SVG node
156 * \param classes Address at which to store the class name array
157 * \param n_classes Address at which to store the length of the class
160 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
162 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
163 * because the underlying libdom function never fails
165 css_error
node_classes(void *pw
, void *node
,
166 lwc_string
***classes
, uint32_t *n_classes
)
171 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
173 /* The implementation does not do it, but the documentation
174 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
175 possible here, so we handle it to be on the safe side. */
176 if (err
!= DOM_NO_ERR
) {
185 * Retrieve the given node's id
187 * \param pw Pointer to the current SVG parser state
188 * \param node Libdom SVG node
189 * \param id Address at which to store the id
191 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
193 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
197 struct svgtiny_parse_state
*state
;
199 /* Begin with the assumption that this node has no id */
202 state
= (struct svgtiny_parse_state
*)pw
;
203 err
= dom_element_get_attribute((dom_node
*)node
,
204 state
->interned_id
, &attr
);
205 if (err
!= DOM_NO_ERR
) {
208 else if (attr
== NULL
) {
209 /* The node has no id attribute and our return value
210 is already set to NULL so we're done */
214 /* If we found an id attribute (a dom_string), intern it into
215 an lwc_string that we can return, and then cleanup the
217 err
= dom_string_intern(attr
, id
);
218 if (err
!= DOM_NO_ERR
) {
219 dom_string_unref(attr
);
222 dom_string_unref(attr
);
229 * Find the first parent of the given element having the given name
231 * \param pw Pointer to the current SVG parser state
232 * \param node Libdom SVG node
233 * \param qname Name of the parent node to search for
234 * \param parent Address at which to store the parent node pointer
236 * \return Always returns CSS_OK
238 * \post If a suitable element is found, a pointer to it will be
239 * stored at the address pointed to by \a parent; otherwise,
240 * NULL will be stored at the address pointed to by \a parent
242 css_error
named_parent_node(void *pw
, void *node
,
243 const css_qname
*qname
, void **parent
)
246 /* dom_element_named_parent_node() was invented to implement
247 * this select handler so there isn't much for us to do except
248 * call it. It's OK if node isn't an element, libdom checks
250 dom_element_named_parent_node((dom_element
*)node
,
252 (struct dom_element
**)parent
);
254 /* Implementation detail: dom_element_named_parent_node()
255 * increments the reference count of the parent element before
256 * returning it to us. According to docs/RefCnt in the libdom
257 * repository, this will prevent the parent element from being
258 * destroyed if it is pruned from the DOM. That sounds good,
259 * since we don't want to be using a pointer to an object that
260 * has been destroyed... but we also have no way of later
261 * decrementing the reference count ourselves, and don't want
262 * to make the returned node eternal. Decrementing the
263 * reference counter now allows it to be destroyed when the
264 * DOM no longer needs it, and so long as no other parts of
265 * libsvgtiny are messing with the DOM during parsing, that
266 * shouldn't (ha ha) cause any problems. */
267 dom_node_unref(*parent
);
274 * Find the "next-sibling" of the given element having the given name
276 * This search corresponds to the "+ foo" combinator in CSS and will
277 * find only "foo" element nodes that immediately precede the given
278 * node under the same parent in the DOM. In CSS the tree is viewed
279 * top-down and in libdom it is viewed from the bottom-up; as a result
280 * "next" and "previous" are sometimes backwards. This is case-sensitive.
282 * \param pw Pointer to the current SVG parser state
283 * \param node Libdom SVG node
284 * \param qname Name of the sibling node to search for
285 * \param sibling Address at which to store the sibling node pointer
287 * \return Always returns CSS_OK
289 * \post If a suitable element is found, a pointer to it will be
290 * stored at the address pointed to by \a sibling; otherwise,
291 * NULL will be stored at the address pointed to by \a sibling
293 css_error
named_sibling_node(void *pw
, void *node
,
294 const css_qname
*qname
, void **sibling
)
297 dom_node
*n
= node
; /* the current node */
298 dom_node
*prev
; /* the previous node */
303 *sibling
= NULL
; /* default to nothing found */
305 /* Begin the search; the first iteration we do outside of the
306 * loop. Implementation detil: dom_node_get_previous_sibling()
307 * increments the reference counter on the returned node. A
308 * comment within named_parent_node() explains why we
309 * decrement it ASAP. */
310 err
= dom_node_get_previous_sibling(n
, &n
);
311 if (err
!= DOM_NO_ERR
) {
316 /* We're looking for the first ELEMENT sibling */
317 err
= dom_node_get_node_type(n
, &type
);
318 if (err
!= DOM_NO_ERR
) {
323 if (type
== DOM_ELEMENT_NODE
) {
324 /* We found an element node, does it have the
326 err
= dom_node_get_node_name(n
, &name
);
327 if (err
!= DOM_NO_ERR
) {
332 if (dom_string_lwc_isequal(name
,
334 /* The name is right, return it */
338 /* There's only one next-sibling element node
339 * and we've already found it, so if its name
340 * wasn't right, we return the default value
342 dom_string_unref(name
);
347 /* Not an element node, so we move on the the previous
348 * previous sibling */
349 err
= dom_node_get_previous_sibling(n
, &prev
);
350 if (err
!= DOM_NO_ERR
) {
364 * Find the first "subsequent-sibling" of the given element having the
367 * This search corresponds to the "~ foo" combinator in CSS and will
368 * find only "foo" element nodes that precede the given node (under
369 * the same parent) in the DOM. In CSS the tree is viewed top-down and
370 * in libdom it is viewed from the bottom-up; as a result "next" and
371 * "previous" are sometimes backwards. This is case-sensitive.
373 * \param pw Pointer to the current SVG parser state
374 * \param node Libdom SVG node
375 * \param qname Name of the sibling node to search for
376 * \param sibling Address at which to store the sibling node pointer
378 * \return Always returns CSS_OK
380 * \post If a suitable element is found, a pointer to it will be
381 * stored at the address pointed to by \a sibling; otherwise,
382 * NULL will be stored at the address pointed to by \a sibling
384 css_error
named_generic_sibling_node(void *pw
, void *node
,
385 const css_qname
*qname
, void **sibling
)
388 dom_node
*n
= node
; /* the current node */
389 dom_node
*prev
; /* the previous node */
395 *sibling
= NULL
; /* default to nothing found */
397 /* Begin the search; the first iteration we do outside of the
398 * loop. Implementation detil: dom_node_get_previous_sibling()
399 * increments the reference counter on the returned node. A
400 * comment within named_parent_node() explains why we
401 * decrement it ASAP. */
402 err
= dom_node_get_previous_sibling(n
, &n
);
403 if (err
!= DOM_NO_ERR
) {
408 err
= dom_node_get_node_type(n
, &type
);
409 if (err
!= DOM_NO_ERR
) {
414 if (type
== DOM_ELEMENT_NODE
) {
415 /* We only want ELEMENT nodes */
416 err
= dom_node_get_node_name(n
, &name
);
417 if (err
!= DOM_NO_ERR
) {
422 if (dom_string_lwc_isequal(name
,
424 /* Found one. Save it and stop the search */
425 dom_string_unref(name
);
431 dom_string_unref(name
);
434 /* This sibling wasn't an element with the desired
435 name, so move on to the previous sibling */
436 err
= dom_node_get_previous_sibling(n
, &prev
);
437 if (err
!= DOM_NO_ERR
) {
451 * Return a pointer to the given node's parent
453 * \param pw Pointer to the current SVG parser state
454 * \param node Libdom SVG node
455 * \param parent Address at which to store the node's parent pointer
457 * \return Always returns CSS_OK
459 css_error
parent_node(void *pw
, void *node
, void **parent
)
462 /* Libdom basically implements this for us */
463 dom_element_parent_node(node
, (struct dom_element
**)parent
);
465 /* See the comment in named_parent_node() for why we decrement
466 * this reference counter here. */
467 dom_node_unref(*parent
);
474 * Find the "next-sibling" of the given element
476 * This search corresponds "+ *" in CSS and will find the first
477 * element node that immediately precedes the given node under the
478 * same parent in the DOM. In CSS the tree is viewed top-down and in
479 * libdom it is viewed from the bottom-up; as a result "next" and
480 * "previous" are sometimes backwards.
482 * \param pw Pointer to the current SVG parser state
483 * \param node Libdom SVG node
484 * \param sibling Address at which to store the sibling node pointer
486 * \return Always returns CSS_OK
488 * \post If a suitable element is found, a pointer to it will be
489 * stored at the address pointed to by \a sibling; otherwise,
490 * NULL will be stored at the address pointed to by \a sibling
492 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
495 dom_node
*n
= node
; /* the current node */
496 dom_node
*prev
; /* the previous node */
500 *sibling
= NULL
; /* default to nothing found */
502 /* Begin the search; the first iteration we do outside of the
503 * loop. Implementation detil: dom_node_get_previous_sibling()
504 * increments the reference counter on the returned node. A
505 * comment within named_parent_node() explains why we
506 * decrement it ASAP. */
507 err
= dom_node_get_previous_sibling(n
, &n
);
508 if (err
!= DOM_NO_ERR
) {
513 err
= dom_node_get_node_type(n
, &type
);
514 if (err
!= DOM_NO_ERR
) {
519 if (type
== DOM_ELEMENT_NODE
) {
520 /* We found a sibling node that is also an
521 element and that's all we wanted. */
527 /* This sibling node was not an element; move on to
528 the previous sibling */
529 err
= dom_node_get_previous_sibling(n
, &prev
);
530 if (err
!= DOM_NO_ERR
) {
544 * Test the given node for the given name
546 * This will return true (via the "match" pointer) if the libdom node
547 * has the given name or if that name is the universal selector;
548 * otherwise it returns false. The comparison is case-sensitive. It
549 * corresponds to a rule like "body { ... }" in CSS.
551 * \param pw Pointer to the current SVG parser state
552 * \param node Libdom SVG node to test
553 * \param qname Name to check for
554 * \param match Pointer to the test result
556 * \return Always returns CSS_OK
558 css_error
node_has_name(void *pw
, void *node
,
559 const css_qname
*qname
, bool *match
)
561 struct svgtiny_parse_state
*state
;
565 /* Start by checking to see if qname is the universal selector */
566 state
= (struct svgtiny_parse_state
*)pw
;
567 if (lwc_string_isequal(qname
->name
,
568 state
->interned_universal
, match
) == lwc_error_ok
) {
570 /* It's the universal selector. In NetSurf, all node
571 * names match the universal selector, and nothing in
572 * the libcss documentation suggests another approach,
573 * so we follow NetSurf here. */
578 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
579 if (err
!= DOM_NO_ERR
) {
583 /* Unlike with HTML, SVG element names are case-sensitive */
584 *match
= dom_string_lwc_isequal(name
, qname
->name
);
585 dom_string_unref(name
);
592 * Test the given node for the given class
594 * This will return true (via the "match" pointer) if the libdom node
595 * has the given class. The comparison is case-sensitive. It
596 * corresponds to node.class in CSS.
598 * \param pw Pointer to the current SVG parser state
599 * \param node Libdom SVG node to test
600 * \param name Class name to check for
601 * \param match Pointer to the test result
603 * \return Always returns CSS_OK
605 css_error
node_has_class(void *pw
, void *node
,
606 lwc_string
*name
, bool *match
)
609 /* libdom implements this for us and apparently it cannot fail */
610 dom_element_has_class((dom_node
*)node
, name
, match
);
616 * Test the given node for the given id
618 * This will return true (via the "match" pointer) if the libdom node
619 * has the given id. The comparison is case-sensitive. It corresponds
622 * \param pw Pointer to the current SVG parser state
623 * \param node Libdom SVG node to test
624 * \param name Id to check for
625 * \param match Pointer to the test result
627 * \return Always returns CSS_OK
629 css_error
node_has_id(void *pw
, void *node
,
630 lwc_string
*name
, bool *match
)
634 struct svgtiny_parse_state
*state
;
636 attr
= NULL
; /* a priori the "id" attribute may not exist */
637 *match
= false; /* default to no match */
639 state
= (struct svgtiny_parse_state
*)pw
;
640 err
= dom_element_get_attribute((dom_node
*)node
,
641 state
->interned_id
, &attr
);
642 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
646 *match
= dom_string_lwc_isequal(attr
, name
);
647 dom_string_unref(attr
);
654 * Test the given node for the given attribute
656 * This will return true (via the "match" pointer) if the libdom node
657 * has an attribute with the given name. The comparison is
658 * case-sensitive. It corresponds to node[attr] in CSS.
660 * \param pw Pointer to the current SVG parser state
661 * \param node Libdom SVG node to test
662 * \param qname Attribute name to check for
663 * \param match Pointer to the test result
665 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
668 css_error
node_has_attribute(void *pw
, void *node
,
669 const css_qname
*qname
, bool *match
)
675 /* intern the attribute name as a dom_string so we can
676 * delegate to dom_element_has_attribute() */
677 err
= dom_string_create_interned(
678 (const uint8_t *) lwc_string_data(qname
->name
),
679 lwc_string_length(qname
->name
),
681 if (err
!= DOM_NO_ERR
) {
685 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
686 if (err
!= DOM_NO_ERR
) {
687 dom_string_unref(name
);
691 dom_string_unref(name
);
697 * Test the given node for an attribute with a specific value
699 * This will return true (via the "match" pointer) if the libdom node
700 * has an attribute with the given name and value. The comparison is
701 * case-sensitive. It corresponds to node[attr=value] in CSS.
703 * \param pw Pointer to the current SVG parser state
704 * \param node Libdom SVG node to test
705 * \param qname Attribute name to check for
706 * \param value Attribute value to check for
707 * \param match Pointer to the test result
709 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
710 * intern the attribute name (which usually indicates memory
713 css_error
node_has_attribute_equal(void *pw
, void *node
,
714 const css_qname
*qname
, lwc_string
*value
,
717 /* Implementation note: NetSurf always returns "no match" when
718 * the value is empty (length zero). We allow it, because why
723 dom_string
*attr_val
;
726 /* Intern the attribute name as a dom_string so we can
727 * use dom_element_get_attribute() */
728 err
= dom_string_create_interned(
729 (const uint8_t *) lwc_string_data(qname
->name
),
730 lwc_string_length(qname
->name
),
732 if (err
!= DOM_NO_ERR
) {
736 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
737 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
738 /* There was an error getting the attribute's value or
739 * the attribute doesn't exist. So, no match? */
740 dom_string_unref(name
);
745 /* Otherwise, we have the attribute value from the given node
746 * and all we need to do is compare. */
747 dom_string_unref(name
);
748 *match
= dom_string_lwc_isequal(attr_val
, value
);
749 dom_string_unref(attr_val
);
756 * Test the given node for an attribute with a specific value,
757 * possibly followed by a single hyphen
759 * This will return true (via the "match" pointer) if the libdom node
760 * has an attribute with the given name and value or with the given
761 * name and a value that is followed by exactly one hyphen. The
762 * comparison is case-sensitive. This corresponds to [attr|=value]
765 * \param pw Pointer to the current SVG parser state
766 * \param node Libdom SVG node to test
767 * \param qname Attribute name to check for
768 * \param value Attribute value to check for
769 * \param match Pointer to the test result
771 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
772 * intern the attribute name (which usually indicates memory
775 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
776 const css_qname
*qname
, lwc_string
*value
,
779 /* Implementation note: NetSurf always returns "no match" when
780 * the value is empty (length zero). We allow it, because why
785 dom_string
*attr_val
;
788 const char *vdata
; /* to hold the data underlying "value" */
790 const char *avdata
; /* to hold the found attribute value data */
793 /* Intern the attribute name as a dom_string so we can
794 * use dom_element_get_attribute() */
795 err
= dom_string_create_interned(
796 (const uint8_t *) lwc_string_data(qname
->name
),
797 lwc_string_length(qname
->name
),
799 if (err
!= DOM_NO_ERR
) {
803 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
804 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
805 /* There was an error getting the attribute's value or
806 * the attribute doesn't exist. So, no match? */
807 dom_string_unref(name
);
812 /* Otherwise, we have the attribute value from the given node
813 * and all we need to do is compare. */
814 dom_string_unref(name
);
815 *match
= dom_string_lwc_isequal(attr_val
, value
);
817 /* Exact match, we're done */
818 dom_string_unref(attr_val
);
822 /* No exact match, try it with a hyphen on the end */
823 vdata
= lwc_string_data(value
); /* needle */
824 vdata_len
= lwc_string_length(value
);
825 avdata
= dom_string_data(attr_val
); /* haystack */
826 avdata_len
= dom_string_byte_length(attr_val
);
827 dom_string_unref(attr_val
);
829 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
830 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
831 /* If there's a hyphen in the right position,
832 * it suffices to compare the strings only up
843 * Test the given node for an attribute whose value is a
844 * space-separated list of words, one of which is the given word
846 * This will return true (via the "match" pointer) if the libdom node
847 * has an attribute with the given name and whose value when
848 * considered as a space-separated list of words contains the given
849 * word. The comparison is case-sensitive. This corresponds to
850 * [attr~=value] in CSS.
852 * \param pw Pointer to the current SVG parser state
853 * \param node Libdom SVG node to test
854 * \param qname Attribute name to check for
855 * \param word Value word to check for
856 * \param match Pointer to the test result
858 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
859 * intern the attribute name (which usually indicates memory
862 css_error
node_has_attribute_includes(void *pw
, void *node
,
863 const css_qname
*qname
, lwc_string
*word
,
869 dom_string
*attr_val
;
871 size_t wordlen
; /* length of "word" */
873 /* pointers used to parse a space-separated list of words */
878 *match
= false; /* default to no match */
880 wordlen
= lwc_string_length(word
);
882 /* In this case, the spec says that "if 'val' is the
883 * empty string, it will never represent anything." */
887 /* Intern the attribute name as a dom_string so we can
888 * use dom_element_get_attribute() */
889 err
= dom_string_create_interned(
890 (const uint8_t *) lwc_string_data(qname
->name
),
891 lwc_string_length(qname
->name
),
893 if (err
!= DOM_NO_ERR
) {
897 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
898 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
899 /* There was an error getting the attribute's value or
900 * the attribute doesn't exist. So, no match? */
901 dom_string_unref(name
);
905 /* Parse the list comparing each word against "word" */
906 start
= dom_string_data(attr_val
);
907 end
= start
+ dom_string_byte_length(attr_val
);
908 dom_string_unref(attr_val
);
910 for (p
= start
; p
<= end
; p
++) {
911 /* Move forward until we find the end of the first word */
912 if (*p
== ' ' || *p
== '\0') {
913 /* If the length of that word is the length of the
914 * word we're looking for, do the comparison. */
915 if ((size_t) (p
- start
) == wordlen
&&
917 lwc_string_data(word
),
922 /* No match? Set "start" to the beginning of
923 * the next word and loop. */
933 * Test the given node for an attribute whose value begins with the
936 * This will return true (via the "match" pointer) if the libdom node
937 * has an attribute with the given name and whose value begins with
938 * the given prefix string. The comparison is case-sensitive. This
939 * corresponds to [attr^=value] in CSS.
941 * \param pw Pointer to the current SVG parser state
942 * \param node Libdom SVG node to test
943 * \param qname Attribute name to check for
944 * \param prefix Value prefix to check for
945 * \param match Pointer to the test result
947 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
948 * intern the attribute name (which usually indicates memory
951 css_error
node_has_attribute_prefix(void *pw
, void *node
,
952 const css_qname
*qname
, lwc_string
*prefix
,
957 dom_string
*attr_val
;
959 const char *avdata
; /* attribute value data */
960 size_t avdata_len
; /* length of that attribute value data */
961 size_t prefixlen
; /* length of "prefix" */
963 prefixlen
= lwc_string_length(prefix
);
964 if (prefixlen
== 0) {
965 /* In this case, the spec says that "if 'val' is the
966 * empty string, it will never represent anything." */
970 /* Intern the attribute name as a dom_string so we can
971 * use dom_element_get_attribute() */
972 err
= dom_string_create_interned(
973 (const uint8_t *) lwc_string_data(qname
->name
),
974 lwc_string_length(qname
->name
),
976 if (err
!= DOM_NO_ERR
) {
980 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
981 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
982 /* There was an error getting the attribute's value or
983 * the attribute doesn't exist. So, no match? */
984 dom_string_unref(name
);
989 /* Otherwise, we have the attribute value from the given node,
990 * and the first thing we want to do is check to see if the
991 * whole thing matches the prefix. */
992 dom_string_unref(name
);
993 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
995 /* If not, check to see if an, uh, prefix matches the
997 if (*match
== false) {
998 avdata
= dom_string_data(attr_val
);
999 avdata_len
= dom_string_byte_length(attr_val
);
1000 if ((avdata_len
>= prefixlen
) &&
1001 (strncasecmp(avdata
,
1002 lwc_string_data(prefix
),
1004 /* Use strncasecmp to compare only the first
1005 * "n" characters, where "n" is the length of
1011 dom_string_unref(attr_val
);
1018 * Test the given node for an attribute whose value end with the
1021 * This will return true (via the "match" pointer) if the libdom node
1022 * has an attribute with the given name and whose value ends with
1023 * the given suffix string. The comparison is case-sensitive. This
1024 * corresponds to [attr$=value] in CSS.
1026 * \param pw Pointer to the current SVG parser state
1027 * \param node Libdom SVG node to test
1028 * \param qname Attribute name to check for
1029 * \param suffix Value suffix to check for
1030 * \param match Pointer to the test result
1032 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1033 * intern the attribute name (which usually indicates memory
1036 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1037 const css_qname
*qname
, lwc_string
*suffix
,
1042 dom_string
*attr_val
;
1044 const char *avdata
; /* attribute value data */
1045 size_t avdata_len
; /* length of that attribute value data */
1046 size_t suffixlen
; /* length of "suffix" */
1048 /* convenience pointer we'll use when matching the suffix */
1049 const char *suffix_start
;
1051 suffixlen
= lwc_string_length(suffix
);
1052 if (suffixlen
== 0) {
1053 /* In this case, the spec says that "if 'val' is the
1054 * empty string, it will never represent anything." */
1058 /* Intern the attribute name as a dom_string so we can
1059 * use dom_element_get_attribute() */
1060 err
= dom_string_create_interned(
1061 (const uint8_t *) lwc_string_data(qname
->name
),
1062 lwc_string_length(qname
->name
),
1064 if (err
!= DOM_NO_ERR
) {
1068 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1069 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1070 /* There was an error getting the attribute's value or
1071 * the attribute doesn't exist. So, no match? */
1072 dom_string_unref(name
);
1077 /* Otherwise, we have the attribute value from the given node,
1078 * and the first thing we want to do is check to see if the
1079 * whole thing matches the suffix. */
1080 dom_string_unref(name
);
1081 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1083 /* If not, check to see if an, uh, suffix matches the
1085 if (*match
== false) {
1086 avdata
= dom_string_data(attr_val
);
1087 avdata_len
= dom_string_byte_length(attr_val
);
1089 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1091 if ((avdata_len
>= suffixlen
) &&
1092 (strncasecmp(suffix_start
,
1093 lwc_string_data(suffix
),
1095 /* Use strncasecmp to compare only the last
1096 * "n" characters, where "n" is the length of
1102 dom_string_unref(attr_val
);
1109 * Test the given node for an attribute whose value contains the
1112 * This will return true (via the "match" pointer) if the libdom node
1113 * has an attribute with the given name and whose value contains the
1114 * given substring. The comparison is case-sensitive. This corresponds
1115 * to [attr*=value] in CSS.
1117 * \param pw Pointer to the current SVG parser state
1118 * \param node Libdom SVG node to test
1119 * \param qname Attribute name to check for
1120 * \param substring Value substring to check for
1121 * \param match Pointer to the test result
1123 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1124 * intern the attribute name (which usually indicates memory
1127 css_error
node_has_attribute_substring(void *pw
, void *node
,
1128 const css_qname
*qname
, lwc_string
*substring
,
1133 dom_string
*attr_val
;
1135 size_t attr_len
; /* length of attr_val */
1136 size_t substrlen
; /* length of "substring" */
1138 /* Convenience pointers we use when comparing substrings */
1142 substrlen
= lwc_string_length(substring
);
1143 if (substrlen
== 0) {
1144 /* In this case, the spec says that "if 'val' is the
1145 * empty string, it will never represent anything." */
1149 /* Intern the attribute name as a dom_string so we can
1150 * use dom_element_get_attribute() */
1151 err
= dom_string_create_interned(
1152 (const uint8_t *) lwc_string_data(qname
->name
),
1153 lwc_string_length(qname
->name
),
1155 if (err
!= DOM_NO_ERR
) {
1159 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1160 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1161 /* There was an error getting the attribute's value or
1162 * the attribute doesn't exist. So, no match? */
1163 dom_string_unref(name
);
1168 /* Otherwise, we have the attribute value from the given node,
1169 * and the first thing we want to do is check to see if the
1170 * whole thing matches the substring. */
1171 dom_string_unref(name
);
1172 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1174 /* If not, check to see if an, uh, substring matches the
1176 if (*match
== false) {
1177 p
= dom_string_data(attr_val
);
1179 /* Check every long-enough suffix for a prefix match */
1180 attr_len
= dom_string_byte_length(attr_val
);
1181 if (attr_len
>= substrlen
) {
1182 p_max
= p
+ attr_len
- substrlen
;
1183 while (p
<= p_max
) {
1185 lwc_string_data(substring
),
1195 dom_string_unref(attr_val
);
1202 * Test whether or not the given node is the document's root element
1203 * This corresponds to the CSS :root pseudo-selector.
1205 * \param pw Pointer to the current SVG parser state
1206 * \param node Libdom SVG node to test
1207 * \param match Pointer to the test result
1209 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1211 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1218 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1219 if (err
!= DOM_NO_ERR
) {
1223 /* It's the root element if it doesn't have a parent element */
1224 if (parent
!= NULL
) {
1225 err
= dom_node_get_node_type(parent
, &type
);
1226 dom_node_unref(parent
);
1227 if (err
!= DOM_NO_ERR
) {
1230 if (type
!= DOM_DOCUMENT_NODE
) {
1231 /* DOM_DOCUMENT_NODE is the only allowable
1232 * type of parent node for the root element */
1244 * Used internally in node_count_siblings() to "count" the given
1245 * sibling node. It factors out the node type and name checks.
1247 static int node_count_siblings_check(dom_node
*dnode
,
1254 dom_string
*dnode_name
;
1256 /* We flip this to 1 if/when we count this node */
1259 if (dnode
== NULL
) {
1263 exc
= dom_node_get_node_type(dnode
, &type
);
1264 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1265 /* We only count element siblings */
1269 /* ... with the right name */
1272 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1274 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1275 if (dom_string_isequal(name
,
1279 dom_string_unref(dnode_name
);
1290 * Count the given node's sibling elements
1292 * This counts the given node's sibling elements in one direction,
1293 * either forwards or backwards, in the DOM. Keep in mind that the
1294 * libdom tree is upside-down compared to the CSS one; so "next" and
1295 * "previous" are actually reversed; the default is to count preceding
1296 * libdom siblings which correspond to subsequent CSS siblings.
1298 * This operation is central to the CSS :first-child, :nth-child, and
1299 * :last-child (et cetera) pseudo-selectors.
1301 * If same_name is true, then only nodes having the same
1302 * (case-sensitive) name as the given node are counted.
1304 * \param pw Pointer to the current SVG parser state
1305 * \param node Libdom SVG node whose siblings we're counting
1306 * \param same_name Whether or not to count only siblings having
1307 * the same name as the given node
1308 * \param after Count subsequent siblings rather than precedent
1309 * ones (the default)
1310 * \param count Pointer to the return value, the number of sibling
1313 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1315 css_error
node_count_siblings(void *pw
, void *node
,
1316 bool same_name
, bool after
, int32_t *count
)
1320 dom_node
*dnode
; /* node, but with the right type */
1321 dom_string
*dnode_name
;
1322 dom_node
*next
; /* "next" sibling (depends on direction) */
1324 /* Pointer to the "next sibling" function */
1325 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1330 dnode
= (dom_node
*)node
;
1332 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1333 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1338 /* Increment the reference counter for dnode for as long as
1339 * we retain a reference to it. */
1340 dnode
= dom_node_ref(dnode
);
1342 next_func
= dom_node_get_previous_sibling
;
1344 next_func
= dom_node_get_next_sibling
;
1348 exc
= next_func(dnode
, &next
);
1349 if (exc
!= DOM_NO_ERR
) {
1353 /* If next_func worked, we're about to swap "next"
1354 * with "dnode" meaning that we will no longer retain
1355 * a reference to the current dnode. */
1356 dom_node_unref(dnode
);
1359 *count
+= node_count_siblings_check(dnode
,
1362 } while (dnode
!= NULL
);
1364 if (dnode_name
!= NULL
) {
1365 dom_string_unref(dnode_name
);
1373 * Determine whether or not the given element is empty
1375 * An element is "nonempty" if it has a child that is either an
1376 * element node or a text node.
1378 * \param pw Pointer to the current SVG parser state
1379 * \param node Libdom SVG node to check for emptiness
1380 * \param is_empty Pointer to the return value
1382 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1384 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1387 dom_node
*child
; /* current child node pointer */
1388 dom_node
*next
; /* next child node pointer */
1389 dom_node_type type
; /* what type of node is "child" */
1392 /* Assume that it's empty by default */
1395 /* Get the given node's first child. Implementation detail:
1396 * this increments the reference counter on the child node. */
1397 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1398 if (err
!= DOM_NO_ERR
) {
1402 /* And now loop through all children looking for a
1403 * text/element node. If we find one, the original
1404 * node is "nonempty" */
1405 while (child
!= NULL
) {
1406 err
= dom_node_get_node_type(child
, &type
);
1407 if (err
!= DOM_NO_ERR
) {
1408 dom_node_unref(child
);
1412 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1414 dom_node_unref(child
);
1418 err
= dom_node_get_next_sibling(child
, &next
);
1419 if (err
!= DOM_NO_ERR
) {
1420 dom_node_unref(child
);
1424 /* If we're moving to the next node, we can release
1425 * the reference to the current one */
1426 dom_node_unref(child
);
1435 * Determine whether or not the given node is a link
1437 * A node is a link if it is an element node whose name is "a" and if
1438 * it has an "href" attribute (case-sensitive). This selector
1439 * corresponds to node:link pseudo-class in CSS.
1441 * This pseudo-class is a bit awkward because the two standards (HTML5
1442 * and CSS) disagree on what it means, and because libsvgtiny does not
1443 * have enough information to determine if a link has been "visited"
1444 * yet -- that's a UI property. CSS says that :link is for unvisited
1445 * links, which we can't determine. HTML5 says that each link must
1446 * be either a :link or :visited. Since we can't decide either way,
1447 * It seems less wrong to declare that all links are unvisited; i.e.
1448 * that they match :link.
1450 * \param pw Pointer to the current SVG parser state
1451 * \param node Libdom SVG node to check
1452 * \param is_link Pointer to the boolean return value
1454 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1456 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1459 dom_node
*dnode
; /* node, but with the right type */
1460 dom_string
*dnode_name
;
1462 struct svgtiny_parse_state
* state
;
1464 dnode
= (dom_node
*)node
;
1466 has_href
= false; /* assume no href attribute */
1467 *is_link
= false; /* assume that it's not a link */
1469 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1470 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1474 state
= (struct svgtiny_parse_state
*)pw
;
1475 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1476 exc
= dom_element_has_attribute(node
,
1477 state
->interned_href
,
1479 if (exc
== DOM_NO_ERR
&& has_href
) {
1484 dom_string_unref(dnode_name
);
1490 * Check if the given node is being "hovered" over
1492 * This check always fails because the SVG DOM does not have the
1493 * necessary information (it's a UI property).
1495 * \param pw Pointer to the current SVG parser state; unused
1496 * \param node Libdom SVG node to check; unused
1497 * \param is_hover Pointer to the boolean return value
1499 * \return Always returns CSS_OK
1501 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1511 * Check if the given node is "active"
1513 * This check always fails because the SVG DOM does not have the
1514 * necessary information (it's a UI property).
1516 * \param pw Pointer to the current SVG parser state; unused
1517 * \param node Libdom SVG node to check; unused
1518 * \param is_active Pointer to the boolean return value
1520 * \return Always returns CSS_OK
1522 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)