1 #include <libcss/libcss.h>
2 #include <strings.h> /* strncasecmp */
5 #include "svgtiny_internal.h"
7 static css_error
node_name(void *pw
, void *node
, css_qname
*qname
);
8 static css_error
node_classes(void *pw
, void *node
,
9 lwc_string
***classes
, uint32_t *n_classes
);
10 static css_error
node_id(void *pw
, void *node
, lwc_string
**id
);
11 static css_error
named_parent_node(void *pw
, void *node
,
12 const css_qname
*qname
, void **parent
);
13 static css_error
named_sibling_node(void *pw
, void *node
,
14 const css_qname
*qname
, void **sibling
);
15 static css_error
named_generic_sibling_node(void *pw
, void *node
,
16 const css_qname
*qname
, void **sibling
);
17 static css_error
parent_node(void *pw
, void *node
, void **parent
);
18 static css_error
sibling_node(void *pw
, void *node
, void **sibling
);
19 static css_error
node_has_name(void *pw
, void *node
,
20 const css_qname
*qname
, bool *match
);
21 static css_error
node_has_class(void *pw
, void *node
,
22 lwc_string
*name
, bool *match
);
23 static css_error
node_has_id(void *pw
, void *node
,
24 lwc_string
*name
, bool *match
);
25 static css_error
node_has_attribute(void *pw
, void *node
,
26 const css_qname
*qname
, bool *match
);
27 static css_error
node_has_attribute_equal(void *pw
, void *node
,
28 const css_qname
*qname
, lwc_string
*value
,
30 static css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
31 const css_qname
*qname
, lwc_string
*value
,
33 static css_error
node_has_attribute_includes(void *pw
, void *node
,
34 const css_qname
*qname
, lwc_string
*word
,
36 static css_error
node_has_attribute_prefix(void *pw
, void *node
,
37 const css_qname
*qname
, lwc_string
*prefix
,
39 static css_error
node_has_attribute_suffix(void *pw
, void *node
,
40 const css_qname
*qname
, lwc_string
*suffix
,
42 static css_error
node_has_attribute_substring(void *pw
, void *node
,
43 const css_qname
*qname
, lwc_string
*substring
,
45 static css_error
node_is_root(void *pw
, void *node
, bool *match
);
46 static css_error
node_count_siblings(void *pw
, void *node
,
47 bool same_name
, bool after
, int32_t *count
);
48 static css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
);
49 static css_error
node_is_link(void *pw
, void *node
, bool *is_link
);
50 static css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
);
51 static css_error
node_is_active(void *pw
, void *node
, bool *is_active
);
52 static css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
);
53 static css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
);
54 static css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
);
55 static css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
);
59 * Resolve a relative URL to an absolute one by doing nothing. This is
60 * the simplest possible implementation of a URL resolver, needed for
63 css_error
svgtiny_resolve_url(void *pw
,
64 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
69 /* Copy the relative URL to the absolute one (the return
71 *abs
= lwc_string_ref(rel
);
76 * Create a stylesheet with the default set of params.
78 * \param sheet A stylesheet pointer, passed in by reference, that
79 * we use to store the newly-created stylesheet.
80 * \param inline_style True if this stylesheet represents an inline
81 * style, and false otherwise.
83 * \return The return value from css_stylesheet_create() is returned.
85 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
88 css_stylesheet_params params
;
90 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
91 params
.level
= CSS_LEVEL_DEFAULT
;
92 params
.charset
= NULL
;
95 params
.allow_quirks
= false;
96 params
.inline_style
= inline_style
;
97 params
.resolve
= svgtiny_resolve_url
;
98 params
.resolve_pw
= NULL
;
100 params
.import_pw
= NULL
;
102 params
.color_pw
= NULL
;
104 params
.font_pw
= NULL
;
106 return css_stylesheet_create(¶ms
, sheet
);
110 /**************************/
111 /* libcss select handlers */
112 /**************************/
114 * From here on we implement the "select handler "API defined in
115 * libcss's include/libcss/select.h and discussed briefly in its
121 * Retrieve the given node's name
123 * \param pw Pointer to the current SVG parser state
124 * \param node Libdom SVG node
125 * \param qname Address at which to store the node name
127 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
129 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
133 struct svgtiny_parse_state
*state
;
135 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
136 if (err
!= DOM_NO_ERR
) {
140 state
= (struct svgtiny_parse_state
*)pw
;
141 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
143 err
= dom_string_intern(name
, &qname
->name
);
144 if (err
!= DOM_NO_ERR
) {
145 dom_string_unref(name
);
149 dom_string_unref(name
);
156 * Retrieve the given node's classes
158 * \param pw Pointer to the current SVG parser state
159 * \param node Libdom SVG node
160 * \param classes Address at which to store the class name array
161 * \param n_classes Address at which to store the length of the class
164 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
166 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
167 * because the underlying libdom function never fails
169 css_error
node_classes(void *pw
, void *node
,
170 lwc_string
***classes
, uint32_t *n_classes
)
175 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
177 /* The implementation does not do it, but the documentation
178 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
179 possible here, so we handle it to be on the safe side. */
180 if (err
!= DOM_NO_ERR
) {
189 * Retrieve the given node's id
191 * \param pw Pointer to the current SVG parser state
192 * \param node Libdom SVG node
193 * \param id Address at which to store the id
195 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
197 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
201 struct svgtiny_parse_state
*state
;
203 /* Begin with the assumption that this node has no id */
206 state
= (struct svgtiny_parse_state
*)pw
;
207 err
= dom_element_get_attribute((dom_node
*)node
,
208 state
->interned_id
, &attr
);
209 if (err
!= DOM_NO_ERR
) {
212 else if (attr
== NULL
) {
213 /* The node has no id attribute and our return value
214 is already set to NULL so we're done */
218 /* If we found an id attribute (a dom_string), intern it into
219 an lwc_string that we can return, and then cleanup the
221 err
= dom_string_intern(attr
, id
);
222 if (err
!= DOM_NO_ERR
) {
223 dom_string_unref(attr
);
226 dom_string_unref(attr
);
233 * Find the first parent of the given element having the given name
235 * \param pw Pointer to the current SVG parser state
236 * \param node Libdom SVG node
237 * \param qname Name of the parent node to search for
238 * \param parent Address at which to store the parent node pointer
240 * \return Always returns CSS_OK
242 * \post If a suitable element is found, a pointer to it will be
243 * stored at the address pointed to by \a parent; otherwise,
244 * NULL will be stored at the address pointed to by \a parent
246 css_error
named_parent_node(void *pw
, void *node
,
247 const css_qname
*qname
, void **parent
)
250 /* dom_element_named_parent_node() was invented to implement
251 * this select handler so there isn't much for us to do except
252 * call it. It's OK if node isn't an element, libdom checks
254 dom_element_named_parent_node((dom_element
*)node
,
256 (struct dom_element
**)parent
);
258 /* Implementation detail: dom_element_named_parent_node()
259 * increments the reference count of the parent element before
260 * returning it to us. According to docs/RefCnt in the libdom
261 * repository, this will prevent the parent element from being
262 * destroyed if it is pruned from the DOM. That sounds good,
263 * since we don't want to be using a pointer to an object that
264 * has been destroyed... but we also have no way of later
265 * decrementing the reference count ourselves, and don't want
266 * to make the returned node eternal. Decrementing the
267 * reference counter now allows it to be destroyed when the
268 * DOM no longer needs it, and so long as no other parts of
269 * libsvgtiny are messing with the DOM during parsing, that
270 * shouldn't (ha ha) cause any problems. */
271 dom_node_unref(*parent
);
278 * Find the "next-sibling" of the given element having the given name
280 * This search corresponds to the "+ foo" combinator in CSS and will
281 * find only "foo" element nodes that immediately precede the given
282 * node under the same parent in the DOM. In CSS the tree is viewed
283 * top-down and in libdom it is viewed from the bottom-up; as a result
284 * "next" and "previous" are sometimes backwards. This is case-sensitive.
286 * \param pw Pointer to the current SVG parser state
287 * \param node Libdom SVG node
288 * \param qname Name of the sibling node to search for
289 * \param sibling Address at which to store the sibling node pointer
291 * \return Always returns CSS_OK
293 * \post If a suitable element is found, a pointer to it will be
294 * stored at the address pointed to by \a sibling; otherwise,
295 * NULL will be stored at the address pointed to by \a sibling
297 css_error
named_sibling_node(void *pw
, void *node
,
298 const css_qname
*qname
, void **sibling
)
301 dom_node
*n
= node
; /* the current node */
302 dom_node
*prev
; /* the previous node */
307 *sibling
= NULL
; /* default to nothing found */
309 /* Begin the search; the first iteration we do outside of the
310 * loop. Implementation detil: dom_node_get_previous_sibling()
311 * increments the reference counter on the returned node. A
312 * comment within named_parent_node() explains why we
313 * decrement it ASAP. */
314 err
= dom_node_get_previous_sibling(n
, &n
);
315 if (err
!= DOM_NO_ERR
) {
320 /* We're looking for the first ELEMENT sibling */
321 err
= dom_node_get_node_type(n
, &type
);
322 if (err
!= DOM_NO_ERR
) {
327 if (type
== DOM_ELEMENT_NODE
) {
328 /* We found an element node, does it have the
330 err
= dom_node_get_node_name(n
, &name
);
331 if (err
!= DOM_NO_ERR
) {
336 if (dom_string_lwc_isequal(name
,
338 /* The name is right, return it */
342 /* There's only one next-sibling element node
343 * and we've already found it, so if its name
344 * wasn't right, we return the default value
346 dom_string_unref(name
);
351 /* Not an element node, so we move on the the previous
352 * previous sibling */
353 err
= dom_node_get_previous_sibling(n
, &prev
);
354 if (err
!= DOM_NO_ERR
) {
368 * Find the first "subsequent-sibling" of the given element having the
371 * This search corresponds to the "~ foo" combinator in CSS and will
372 * find only "foo" element nodes that precede the given node (under
373 * the same parent) in the DOM. In CSS the tree is viewed top-down and
374 * in libdom it is viewed from the bottom-up; as a result "next" and
375 * "previous" are sometimes backwards. This is case-sensitive.
377 * \param pw Pointer to the current SVG parser state
378 * \param node Libdom SVG node
379 * \param qname Name of the sibling node to search for
380 * \param sibling Address at which to store the sibling node pointer
382 * \return Always returns CSS_OK
384 * \post If a suitable element is found, a pointer to it will be
385 * stored at the address pointed to by \a sibling; otherwise,
386 * NULL will be stored at the address pointed to by \a sibling
388 css_error
named_generic_sibling_node(void *pw
, void *node
,
389 const css_qname
*qname
, void **sibling
)
392 dom_node
*n
= node
; /* the current node */
393 dom_node
*prev
; /* the previous node */
399 *sibling
= NULL
; /* default to nothing found */
401 /* Begin the search; the first iteration we do outside of the
402 * loop. Implementation detil: dom_node_get_previous_sibling()
403 * increments the reference counter on the returned node. A
404 * comment within named_parent_node() explains why we
405 * decrement it ASAP. */
406 err
= dom_node_get_previous_sibling(n
, &n
);
407 if (err
!= DOM_NO_ERR
) {
412 err
= dom_node_get_node_type(n
, &type
);
413 if (err
!= DOM_NO_ERR
) {
418 if (type
== DOM_ELEMENT_NODE
) {
419 /* We only want ELEMENT nodes */
420 err
= dom_node_get_node_name(n
, &name
);
421 if (err
!= DOM_NO_ERR
) {
426 if (dom_string_lwc_isequal(name
,
428 /* Found one. Save it and stop the search */
429 dom_string_unref(name
);
435 dom_string_unref(name
);
438 /* This sibling wasn't an element with the desired
439 name, so move on to the previous sibling */
440 err
= dom_node_get_previous_sibling(n
, &prev
);
441 if (err
!= DOM_NO_ERR
) {
455 * Return a pointer to the given node's parent
457 * \param pw Pointer to the current SVG parser state
458 * \param node Libdom SVG node
459 * \param parent Address at which to store the node's parent pointer
461 * \return Always returns CSS_OK
463 css_error
parent_node(void *pw
, void *node
, void **parent
)
466 /* Libdom basically implements this for us */
467 dom_element_parent_node(node
, (struct dom_element
**)parent
);
469 /* See the comment in named_parent_node() for why we decrement
470 * this reference counter here. */
471 dom_node_unref(*parent
);
478 * Find the "next-sibling" of the given element
480 * This search corresponds "+ *" in CSS and will find the first
481 * element node that immediately precedes the given node under the
482 * same parent in the DOM. In CSS the tree is viewed top-down and in
483 * libdom it is viewed from the bottom-up; as a result "next" and
484 * "previous" are sometimes backwards.
486 * \param pw Pointer to the current SVG parser state
487 * \param node Libdom SVG node
488 * \param sibling Address at which to store the sibling node pointer
490 * \return Always returns CSS_OK
492 * \post If a suitable element is found, a pointer to it will be
493 * stored at the address pointed to by \a sibling; otherwise,
494 * NULL will be stored at the address pointed to by \a sibling
496 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
499 dom_node
*n
= node
; /* the current node */
500 dom_node
*prev
; /* the previous node */
504 *sibling
= NULL
; /* default to nothing found */
506 /* Begin the search; the first iteration we do outside of the
507 * loop. Implementation detil: dom_node_get_previous_sibling()
508 * increments the reference counter on the returned node. A
509 * comment within named_parent_node() explains why we
510 * decrement it ASAP. */
511 err
= dom_node_get_previous_sibling(n
, &n
);
512 if (err
!= DOM_NO_ERR
) {
517 err
= dom_node_get_node_type(n
, &type
);
518 if (err
!= DOM_NO_ERR
) {
523 if (type
== DOM_ELEMENT_NODE
) {
524 /* We found a sibling node that is also an
525 element and that's all we wanted. */
531 /* This sibling node was not an element; move on to
532 the previous sibling */
533 err
= dom_node_get_previous_sibling(n
, &prev
);
534 if (err
!= DOM_NO_ERR
) {
548 * Test the given node for the given name
550 * This will return true (via the "match" pointer) if the libdom node
551 * has the given name or if that name is the universal selector;
552 * otherwise it returns false. The comparison is case-sensitive. It
553 * corresponds to a rule like "body { ... }" in CSS.
555 * \param pw Pointer to the current SVG parser state
556 * \param node Libdom SVG node to test
557 * \param qname Name to check for
558 * \param match Pointer to the test result
560 * \return Always returns CSS_OK
562 css_error
node_has_name(void *pw
, void *node
,
563 const css_qname
*qname
, bool *match
)
565 struct svgtiny_parse_state
*state
;
569 /* Start by checking to see if qname is the universal selector */
570 state
= (struct svgtiny_parse_state
*)pw
;
571 if (lwc_string_isequal(qname
->name
,
572 state
->interned_universal
, match
) == lwc_error_ok
) {
574 /* It's the universal selector. In NetSurf, all node
575 * names match the universal selector, and nothing in
576 * the libcss documentation suggests another approach,
577 * so we follow NetSurf here. */
582 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
583 if (err
!= DOM_NO_ERR
) {
587 /* Unlike with HTML, SVG element names are case-sensitive */
588 *match
= dom_string_lwc_isequal(name
, qname
->name
);
589 dom_string_unref(name
);
596 * Test the given node for the given class
598 * This will return true (via the "match" pointer) if the libdom node
599 * has the given class. The comparison is case-sensitive. It
600 * corresponds to node.class in CSS.
602 * \param pw Pointer to the current SVG parser state
603 * \param node Libdom SVG node to test
604 * \param name Class name to check for
605 * \param match Pointer to the test result
607 * \return Always returns CSS_OK
609 css_error
node_has_class(void *pw
, void *node
,
610 lwc_string
*name
, bool *match
)
613 /* libdom implements this for us and apparently it cannot fail */
614 dom_element_has_class((dom_node
*)node
, name
, match
);
620 * Test the given node for the given id
622 * This will return true (via the "match" pointer) if the libdom node
623 * has the given id. The comparison is case-sensitive. It corresponds
626 * \param pw Pointer to the current SVG parser state
627 * \param node Libdom SVG node to test
628 * \param name Id to check for
629 * \param match Pointer to the test result
631 * \return Always returns CSS_OK
633 css_error
node_has_id(void *pw
, void *node
,
634 lwc_string
*name
, bool *match
)
638 struct svgtiny_parse_state
*state
;
640 attr
= NULL
; /* a priori the "id" attribute may not exist */
641 *match
= false; /* default to no match */
643 state
= (struct svgtiny_parse_state
*)pw
;
644 err
= dom_element_get_attribute((dom_node
*)node
,
645 state
->interned_id
, &attr
);
646 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
650 *match
= dom_string_lwc_isequal(attr
, name
);
651 dom_string_unref(attr
);
658 * Test the given node for the given attribute
660 * This will return true (via the "match" pointer) if the libdom node
661 * has an attribute with the given name. The comparison is
662 * case-sensitive. It corresponds to node[attr] in CSS.
664 * \param pw Pointer to the current SVG parser state
665 * \param node Libdom SVG node to test
666 * \param qname Attribute name to check for
667 * \param match Pointer to the test result
669 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
672 css_error
node_has_attribute(void *pw
, void *node
,
673 const css_qname
*qname
, bool *match
)
679 /* intern the attribute name as a dom_string so we can
680 * delegate to dom_element_has_attribute() */
681 err
= dom_string_create_interned(
682 (const uint8_t *) lwc_string_data(qname
->name
),
683 lwc_string_length(qname
->name
),
685 if (err
!= DOM_NO_ERR
) {
689 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
690 if (err
!= DOM_NO_ERR
) {
691 dom_string_unref(name
);
695 dom_string_unref(name
);
701 * Test the given node for an attribute with a specific value
703 * This will return true (via the "match" pointer) if the libdom node
704 * has an attribute with the given name and value. The comparison is
705 * case-sensitive. It corresponds to node[attr=value] in CSS.
707 * \param pw Pointer to the current SVG parser state
708 * \param node Libdom SVG node to test
709 * \param qname Attribute name to check for
710 * \param value Attribute value to check for
711 * \param match Pointer to the test result
713 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
714 * intern the attribute name (which usually indicates memory
717 css_error
node_has_attribute_equal(void *pw
, void *node
,
718 const css_qname
*qname
, lwc_string
*value
,
721 /* Implementation note: NetSurf always returns "no match" when
722 * the value is empty (length zero). We allow it, because why
727 dom_string
*attr_val
;
730 /* Intern the attribute name as a dom_string so we can
731 * use dom_element_get_attribute() */
732 err
= dom_string_create_interned(
733 (const uint8_t *) lwc_string_data(qname
->name
),
734 lwc_string_length(qname
->name
),
736 if (err
!= DOM_NO_ERR
) {
740 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
741 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
742 /* There was an error getting the attribute's value or
743 * the attribute doesn't exist. So, no match? */
744 dom_string_unref(name
);
749 /* Otherwise, we have the attribute value from the given node
750 * and all we need to do is compare. */
751 dom_string_unref(name
);
752 *match
= dom_string_lwc_isequal(attr_val
, value
);
753 dom_string_unref(attr_val
);
760 * Test the given node for an attribute with a specific value,
761 * possibly followed by a single hyphen
763 * This will return true (via the "match" pointer) if the libdom node
764 * has an attribute with the given name and value or with the given
765 * name and a value that is followed by exactly one hyphen. The
766 * comparison is case-sensitive. This corresponds to [attr|=value]
769 * \param pw Pointer to the current SVG parser state
770 * \param node Libdom SVG node to test
771 * \param qname Attribute name to check for
772 * \param value Attribute value to check for
773 * \param match Pointer to the test result
775 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
776 * intern the attribute name (which usually indicates memory
779 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
780 const css_qname
*qname
, lwc_string
*value
,
783 /* Implementation note: NetSurf always returns "no match" when
784 * the value is empty (length zero). We allow it, because why
789 dom_string
*attr_val
;
792 const char *vdata
; /* to hold the data underlying "value" */
794 const char *avdata
; /* to hold the found attribute value data */
797 /* Intern the attribute name as a dom_string so we can
798 * use dom_element_get_attribute() */
799 err
= dom_string_create_interned(
800 (const uint8_t *) lwc_string_data(qname
->name
),
801 lwc_string_length(qname
->name
),
803 if (err
!= DOM_NO_ERR
) {
807 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
808 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
809 /* There was an error getting the attribute's value or
810 * the attribute doesn't exist. So, no match? */
811 dom_string_unref(name
);
816 /* Otherwise, we have the attribute value from the given node
817 * and all we need to do is compare. */
818 dom_string_unref(name
);
819 *match
= dom_string_lwc_isequal(attr_val
, value
);
821 /* Exact match, we're done */
822 dom_string_unref(attr_val
);
826 /* No exact match, try it with a hyphen on the end */
827 vdata
= lwc_string_data(value
); /* needle */
828 vdata_len
= lwc_string_length(value
);
829 avdata
= dom_string_data(attr_val
); /* haystack */
830 avdata_len
= dom_string_byte_length(attr_val
);
831 dom_string_unref(attr_val
);
833 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
834 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
835 /* If there's a hyphen in the right position,
836 * it suffices to compare the strings only up
847 * Test the given node for an attribute whose value is a
848 * space-separated list of words, one of which is the given word
850 * This will return true (via the "match" pointer) if the libdom node
851 * has an attribute with the given name and whose value when
852 * considered as a space-separated list of words contains the given
853 * word. The comparison is case-sensitive. This corresponds to
854 * [attr~=value] in CSS.
856 * \param pw Pointer to the current SVG parser state
857 * \param node Libdom SVG node to test
858 * \param qname Attribute name to check for
859 * \param word Value word to check for
860 * \param match Pointer to the test result
862 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
863 * intern the attribute name (which usually indicates memory
866 css_error
node_has_attribute_includes(void *pw
, void *node
,
867 const css_qname
*qname
, lwc_string
*word
,
873 dom_string
*attr_val
;
875 size_t wordlen
; /* length of "word" */
877 /* pointers used to parse a space-separated list of words */
882 *match
= false; /* default to no match */
884 wordlen
= lwc_string_length(word
);
886 /* In this case, the spec says that "if 'val' is the
887 * empty string, it will never represent anything." */
891 /* Intern the attribute name as a dom_string so we can
892 * use dom_element_get_attribute() */
893 err
= dom_string_create_interned(
894 (const uint8_t *) lwc_string_data(qname
->name
),
895 lwc_string_length(qname
->name
),
897 if (err
!= DOM_NO_ERR
) {
901 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
902 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
903 /* There was an error getting the attribute's value or
904 * the attribute doesn't exist. So, no match? */
905 dom_string_unref(name
);
909 /* Parse the list comparing each word against "word" */
910 start
= dom_string_data(attr_val
);
911 end
= start
+ dom_string_byte_length(attr_val
);
912 dom_string_unref(attr_val
);
914 for (p
= start
; p
<= end
; p
++) {
915 /* Move forward until we find the end of the first word */
916 if (*p
== ' ' || *p
== '\0') {
917 /* If the length of that word is the length of the
918 * word we're looking for, do the comparison. */
919 if ((size_t) (p
- start
) == wordlen
&&
921 lwc_string_data(word
),
926 /* No match? Set "start" to the beginning of
927 * the next word and loop. */
937 * Test the given node for an attribute whose value begins with the
940 * This will return true (via the "match" pointer) if the libdom node
941 * has an attribute with the given name and whose value begins with
942 * the given prefix string. The comparison is case-sensitive. This
943 * corresponds to [attr^=value] in CSS.
945 * \param pw Pointer to the current SVG parser state
946 * \param node Libdom SVG node to test
947 * \param qname Attribute name to check for
948 * \param prefix Value prefix to check for
949 * \param match Pointer to the test result
951 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
952 * intern the attribute name (which usually indicates memory
955 css_error
node_has_attribute_prefix(void *pw
, void *node
,
956 const css_qname
*qname
, lwc_string
*prefix
,
961 dom_string
*attr_val
;
963 const char *avdata
; /* attribute value data */
964 size_t avdata_len
; /* length of that attribute value data */
965 size_t prefixlen
; /* length of "prefix" */
967 prefixlen
= lwc_string_length(prefix
);
968 if (prefixlen
== 0) {
969 /* In this case, the spec says that "if 'val' is the
970 * empty string, it will never represent anything." */
974 /* Intern the attribute name as a dom_string so we can
975 * use dom_element_get_attribute() */
976 err
= dom_string_create_interned(
977 (const uint8_t *) lwc_string_data(qname
->name
),
978 lwc_string_length(qname
->name
),
980 if (err
!= DOM_NO_ERR
) {
984 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
985 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
986 /* There was an error getting the attribute's value or
987 * the attribute doesn't exist. So, no match? */
988 dom_string_unref(name
);
993 /* Otherwise, we have the attribute value from the given node,
994 * and the first thing we want to do is check to see if the
995 * whole thing matches the prefix. */
996 dom_string_unref(name
);
997 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
999 /* If not, check to see if an, uh, prefix matches the
1001 if (*match
== false) {
1002 avdata
= dom_string_data(attr_val
);
1003 avdata_len
= dom_string_byte_length(attr_val
);
1004 if ((avdata_len
>= prefixlen
) &&
1005 (strncasecmp(avdata
,
1006 lwc_string_data(prefix
),
1008 /* Use strncasecmp to compare only the first
1009 * "n" characters, where "n" is the length of
1015 dom_string_unref(attr_val
);
1022 * Test the given node for an attribute whose value end with the
1025 * This will return true (via the "match" pointer) if the libdom node
1026 * has an attribute with the given name and whose value ends with
1027 * the given suffix string. The comparison is case-sensitive. This
1028 * corresponds to [attr$=value] in CSS.
1030 * \param pw Pointer to the current SVG parser state
1031 * \param node Libdom SVG node to test
1032 * \param qname Attribute name to check for
1033 * \param suffix Value suffix to check for
1034 * \param match Pointer to the test result
1036 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1037 * intern the attribute name (which usually indicates memory
1040 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1041 const css_qname
*qname
, lwc_string
*suffix
,
1046 dom_string
*attr_val
;
1048 const char *avdata
; /* attribute value data */
1049 size_t avdata_len
; /* length of that attribute value data */
1050 size_t suffixlen
; /* length of "suffix" */
1052 /* convenience pointer we'll use when matching the suffix */
1053 const char *suffix_start
;
1055 suffixlen
= lwc_string_length(suffix
);
1056 if (suffixlen
== 0) {
1057 /* In this case, the spec says that "if 'val' is the
1058 * empty string, it will never represent anything." */
1062 /* Intern the attribute name as a dom_string so we can
1063 * use dom_element_get_attribute() */
1064 err
= dom_string_create_interned(
1065 (const uint8_t *) lwc_string_data(qname
->name
),
1066 lwc_string_length(qname
->name
),
1068 if (err
!= DOM_NO_ERR
) {
1072 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1073 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1074 /* There was an error getting the attribute's value or
1075 * the attribute doesn't exist. So, no match? */
1076 dom_string_unref(name
);
1081 /* Otherwise, we have the attribute value from the given node,
1082 * and the first thing we want to do is check to see if the
1083 * whole thing matches the suffix. */
1084 dom_string_unref(name
);
1085 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1087 /* If not, check to see if an, uh, suffix matches the
1089 if (*match
== false) {
1090 avdata
= dom_string_data(attr_val
);
1091 avdata_len
= dom_string_byte_length(attr_val
);
1093 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1095 if ((avdata_len
>= suffixlen
) &&
1096 (strncasecmp(suffix_start
,
1097 lwc_string_data(suffix
),
1099 /* Use strncasecmp to compare only the last
1100 * "n" characters, where "n" is the length of
1106 dom_string_unref(attr_val
);
1113 * Test the given node for an attribute whose value contains the
1116 * This will return true (via the "match" pointer) if the libdom node
1117 * has an attribute with the given name and whose value contains the
1118 * given substring. The comparison is case-sensitive. This corresponds
1119 * to [attr*=value] in CSS.
1121 * \param pw Pointer to the current SVG parser state
1122 * \param node Libdom SVG node to test
1123 * \param qname Attribute name to check for
1124 * \param substring Value substring to check for
1125 * \param match Pointer to the test result
1127 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1128 * intern the attribute name (which usually indicates memory
1131 css_error
node_has_attribute_substring(void *pw
, void *node
,
1132 const css_qname
*qname
, lwc_string
*substring
,
1137 dom_string
*attr_val
;
1139 size_t attr_len
; /* length of attr_val */
1140 size_t substrlen
; /* length of "substring" */
1142 /* Convenience pointers we use when comparing substrings */
1146 substrlen
= lwc_string_length(substring
);
1147 if (substrlen
== 0) {
1148 /* In this case, the spec says that "if 'val' is the
1149 * empty string, it will never represent anything." */
1153 /* Intern the attribute name as a dom_string so we can
1154 * use dom_element_get_attribute() */
1155 err
= dom_string_create_interned(
1156 (const uint8_t *) lwc_string_data(qname
->name
),
1157 lwc_string_length(qname
->name
),
1159 if (err
!= DOM_NO_ERR
) {
1163 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1164 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1165 /* There was an error getting the attribute's value or
1166 * the attribute doesn't exist. So, no match? */
1167 dom_string_unref(name
);
1172 /* Otherwise, we have the attribute value from the given node,
1173 * and the first thing we want to do is check to see if the
1174 * whole thing matches the substring. */
1175 dom_string_unref(name
);
1176 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1178 /* If not, check to see if an, uh, substring matches the
1180 if (*match
== false) {
1181 p
= dom_string_data(attr_val
);
1183 /* Check every long-enough suffix for a prefix match */
1184 attr_len
= dom_string_byte_length(attr_val
);
1185 if (attr_len
>= substrlen
) {
1186 p_max
= p
+ attr_len
- substrlen
;
1187 while (p
<= p_max
) {
1189 lwc_string_data(substring
),
1199 dom_string_unref(attr_val
);
1206 * Test whether or not the given node is the document's root element
1207 * This corresponds to the CSS :root pseudo-selector.
1209 * \param pw Pointer to the current SVG parser state
1210 * \param node Libdom SVG node to test
1211 * \param match Pointer to the test result
1213 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1215 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1222 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1223 if (err
!= DOM_NO_ERR
) {
1227 /* It's the root element if it doesn't have a parent element */
1228 if (parent
!= NULL
) {
1229 err
= dom_node_get_node_type(parent
, &type
);
1230 dom_node_unref(parent
);
1231 if (err
!= DOM_NO_ERR
) {
1234 if (type
!= DOM_DOCUMENT_NODE
) {
1235 /* DOM_DOCUMENT_NODE is the only allowable
1236 * type of parent node for the root element */
1248 * Used internally in node_count_siblings() to "count" the given
1249 * sibling node. It factors out the node type and name checks.
1251 static int node_count_siblings_check(dom_node
*dnode
,
1258 dom_string
*dnode_name
;
1260 /* We flip this to 1 if/when we count this node */
1263 if (dnode
== NULL
) {
1267 exc
= dom_node_get_node_type(dnode
, &type
);
1268 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1269 /* We only count element siblings */
1273 /* ... with the right name */
1276 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1278 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1279 if (dom_string_isequal(name
,
1283 dom_string_unref(dnode_name
);
1294 * Count the given node's sibling elements
1296 * This counts the given node's sibling elements in one direction,
1297 * either forwards or backwards, in the DOM. Keep in mind that the
1298 * libdom tree is upside-down compared to the CSS one; so "next" and
1299 * "previous" are actually reversed; the default is to count preceding
1300 * libdom siblings which correspond to subsequent CSS siblings.
1302 * This operation is central to the CSS :first-child, :nth-child, and
1303 * :last-child (et cetera) pseudo-selectors.
1305 * If same_name is true, then only nodes having the same
1306 * (case-sensitive) name as the given node are counted.
1308 * \param pw Pointer to the current SVG parser state
1309 * \param node Libdom SVG node whose siblings we're counting
1310 * \param same_name Whether or not to count only siblings having
1311 * the same name as the given node
1312 * \param after Count subsequent siblings rather than precedent
1313 * ones (the default)
1314 * \param count Pointer to the return value, the number of sibling
1317 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1319 css_error
node_count_siblings(void *pw
, void *node
,
1320 bool same_name
, bool after
, int32_t *count
)
1324 dom_node
*dnode
; /* node, but with the right type */
1325 dom_string
*dnode_name
;
1326 dom_node
*next
; /* "next" sibling (depends on direction) */
1328 /* Pointer to the "next sibling" function */
1329 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1334 dnode
= (dom_node
*)node
;
1336 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1337 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1342 /* Increment the reference counter for dnode for as long as
1343 * we retain a reference to it. */
1344 dnode
= dom_node_ref(dnode
);
1346 next_func
= dom_node_get_previous_sibling
;
1348 next_func
= dom_node_get_next_sibling
;
1352 exc
= next_func(dnode
, &next
);
1353 if (exc
!= DOM_NO_ERR
) {
1357 /* If next_func worked, we're about to swap "next"
1358 * with "dnode" meaning that we will no longer retain
1359 * a reference to the current dnode. */
1360 dom_node_unref(dnode
);
1363 *count
+= node_count_siblings_check(dnode
,
1366 } while (dnode
!= NULL
);
1368 if (dnode_name
!= NULL
) {
1369 dom_string_unref(dnode_name
);
1377 * Determine whether or not the given element is empty
1379 * An element is "nonempty" if it has a child that is either an
1380 * element node or a text node.
1382 * \param pw Pointer to the current SVG parser state
1383 * \param node Libdom SVG node to check for emptiness
1384 * \param is_empty Pointer to the return value
1386 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1388 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1391 dom_node
*child
; /* current child node pointer */
1392 dom_node
*next
; /* next child node pointer */
1393 dom_node_type type
; /* what type of node is "child" */
1396 /* Assume that it's empty by default */
1399 /* Get the given node's first child. Implementation detail:
1400 * this increments the reference counter on the child node. */
1401 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1402 if (err
!= DOM_NO_ERR
) {
1406 /* And now loop through all children looking for a
1407 * text/element node. If we find one, the original
1408 * node is "nonempty" */
1409 while (child
!= NULL
) {
1410 err
= dom_node_get_node_type(child
, &type
);
1411 if (err
!= DOM_NO_ERR
) {
1412 dom_node_unref(child
);
1416 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1418 dom_node_unref(child
);
1422 err
= dom_node_get_next_sibling(child
, &next
);
1423 if (err
!= DOM_NO_ERR
) {
1424 dom_node_unref(child
);
1428 /* If we're moving to the next node, we can release
1429 * the reference to the current one */
1430 dom_node_unref(child
);
1439 * Determine whether or not the given node is a link
1441 * A node is a link if it is an element node whose name is "a" and if
1442 * it has an "href" attribute (case-sensitive). This selector
1443 * corresponds to node:link pseudo-class in CSS.
1445 * This pseudo-class is a bit awkward because the two standards (HTML5
1446 * and CSS) disagree on what it means, and because libsvgtiny does not
1447 * have enough information to determine if a link has been "visited"
1448 * yet -- that's a UI property. CSS says that :link is for unvisited
1449 * links, which we can't determine. HTML5 says that each link must
1450 * be either a :link or :visited. Since we can't decide either way,
1451 * It seems less wrong to declare that all links are unvisited; i.e.
1452 * that they match :link.
1454 * \param pw Pointer to the current SVG parser state
1455 * \param node Libdom SVG node to check
1456 * \param is_link Pointer to the boolean return value
1458 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1460 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1463 dom_node
*dnode
; /* node, but with the right type */
1464 dom_string
*dnode_name
;
1466 struct svgtiny_parse_state
* state
;
1468 dnode
= (dom_node
*)node
;
1470 has_href
= false; /* assume no href attribute */
1471 *is_link
= false; /* assume that it's not a link */
1473 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1474 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1478 state
= (struct svgtiny_parse_state
*)pw
;
1479 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1480 exc
= dom_element_has_attribute(node
,
1481 state
->interned_href
,
1483 if (exc
== DOM_NO_ERR
&& has_href
) {
1488 dom_string_unref(dnode_name
);
1494 * Check if the given node is being "hovered" over
1496 * This check always fails because the SVG DOM does not have the
1497 * necessary information (it's a UI property).
1499 * \param pw Pointer to the current SVG parser state; unused
1500 * \param node Libdom SVG node to check; unused
1501 * \param is_hover Pointer to the boolean return value
1503 * \return Always returns CSS_OK
1505 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1515 * Check if the given node is "active"
1517 * This check always fails because the SVG DOM does not have the
1518 * necessary information (it's a UI property).
1520 * \param pw Pointer to the current SVG parser state; unused
1521 * \param node Libdom SVG node to check; unused
1522 * \param is_active Pointer to the boolean return value
1524 * \return Always returns CSS_OK
1526 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1536 * Check if the given node has the focus
1538 * This check always fails because the SVG DOM does not have the
1539 * necessary information (it's a UI property).
1541 * \param pw Pointer to the current SVG parser state; unused
1542 * \param node Libdom SVG node to check; unused
1543 * \param is_focus Pointer to the boolean return value
1545 * \return Always returns CSS_OK
1547 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1557 * Check if the given node is enabled
1559 * This check always fails because the SVG DOM does not have the
1560 * necessary information (it's a UI property).
1562 * \param pw Pointer to the current SVG parser state; unused
1563 * \param node Libdom SVG node to check; unused
1564 * \param is_enabled Pointer to the boolean return value
1566 * \return Always returns CSS_OK
1568 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1572 *is_enabled
= false;
1578 * Check if the given node is disabled
1580 * This check always fails because the SVG DOM does not have the
1581 * necessary information (it's a UI property). Beware, until they are
1582 * implemented, this is NOT the logical negation of node_is_enabled!
1584 * \param pw Pointer to the current SVG parser state; unused
1585 * \param node Libdom SVG node to check; unused
1586 * \param is_disabled Pointer to the boolean return value
1588 * \return Always returns CSS_OK
1590 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1594 *is_disabled
= false;
1600 * Test whether or not the given node is "checked"
1602 * This test always fails because the SVG DOM does not have the
1603 * necessary information (it's a UI property).
1605 * \param pw Pointer to the current SVG parser state; unused
1606 * \param node Libdom SVG node to check; unused
1607 * \param is_checked Pointer to the boolean return value
1609 * \return Always returns CSS_OK
1611 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1615 *is_checked
= false;