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
);
56 static css_error
node_is_target(void *pw
, void *node
, bool *is_target
);
60 * Resolve a relative URL to an absolute one by doing nothing. This is
61 * the simplest possible implementation of a URL resolver, needed for
64 css_error
svgtiny_resolve_url(void *pw
,
65 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
70 /* Copy the relative URL to the absolute one (the return
72 *abs
= lwc_string_ref(rel
);
77 * Create a stylesheet with the default set of params.
79 * \param sheet A stylesheet pointer, passed in by reference, that
80 * we use to store the newly-created stylesheet.
81 * \param inline_style True if this stylesheet represents an inline
82 * style, and false otherwise.
84 * \return The return value from css_stylesheet_create() is returned.
86 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
89 css_stylesheet_params params
;
91 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
92 params
.level
= CSS_LEVEL_DEFAULT
;
93 params
.charset
= NULL
;
96 params
.allow_quirks
= false;
97 params
.inline_style
= inline_style
;
98 params
.resolve
= svgtiny_resolve_url
;
99 params
.resolve_pw
= NULL
;
100 params
.import = NULL
;
101 params
.import_pw
= NULL
;
103 params
.color_pw
= NULL
;
105 params
.font_pw
= NULL
;
107 return css_stylesheet_create(¶ms
, sheet
);
111 /**************************/
112 /* libcss select handlers */
113 /**************************/
115 * From here on we implement the "select handler "API defined in
116 * libcss's include/libcss/select.h and discussed briefly in its
122 * Retrieve the given node's name
124 * \param pw Pointer to the current SVG parser state
125 * \param node Libdom SVG node
126 * \param qname Address at which to store the node name
128 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
130 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
134 struct svgtiny_parse_state
*state
;
136 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
137 if (err
!= DOM_NO_ERR
) {
141 state
= (struct svgtiny_parse_state
*)pw
;
142 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
144 err
= dom_string_intern(name
, &qname
->name
);
145 if (err
!= DOM_NO_ERR
) {
146 dom_string_unref(name
);
150 dom_string_unref(name
);
157 * Retrieve the given node's classes
159 * \param pw Pointer to the current SVG parser state
160 * \param node Libdom SVG node
161 * \param classes Address at which to store the class name array
162 * \param n_classes Address at which to store the length of the class
165 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
167 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
168 * because the underlying libdom function never fails
170 css_error
node_classes(void *pw
, void *node
,
171 lwc_string
***classes
, uint32_t *n_classes
)
176 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
178 /* The implementation does not do it, but the documentation
179 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
180 possible here, so we handle it to be on the safe side. */
181 if (err
!= DOM_NO_ERR
) {
190 * Retrieve the given node's id
192 * \param pw Pointer to the current SVG parser state
193 * \param node Libdom SVG node
194 * \param id Address at which to store the id
196 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
198 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
202 struct svgtiny_parse_state
*state
;
204 /* Begin with the assumption that this node has no id */
207 state
= (struct svgtiny_parse_state
*)pw
;
208 err
= dom_element_get_attribute((dom_node
*)node
,
209 state
->interned_id
, &attr
);
210 if (err
!= DOM_NO_ERR
) {
213 else if (attr
== NULL
) {
214 /* The node has no id attribute and our return value
215 is already set to NULL so we're done */
219 /* If we found an id attribute (a dom_string), intern it into
220 an lwc_string that we can return, and then cleanup the
222 err
= dom_string_intern(attr
, id
);
223 if (err
!= DOM_NO_ERR
) {
224 dom_string_unref(attr
);
227 dom_string_unref(attr
);
234 * Find the first parent of the given element having the given name
236 * \param pw Pointer to the current SVG parser state
237 * \param node Libdom SVG node
238 * \param qname Name of the parent node to search for
239 * \param parent Address at which to store the parent node pointer
241 * \return Always returns CSS_OK
243 * \post If a suitable element is found, a pointer to it will be
244 * stored at the address pointed to by \a parent; otherwise,
245 * NULL will be stored at the address pointed to by \a parent
247 css_error
named_parent_node(void *pw
, void *node
,
248 const css_qname
*qname
, void **parent
)
251 /* dom_element_named_parent_node() was invented to implement
252 * this select handler so there isn't much for us to do except
253 * call it. It's OK if node isn't an element, libdom checks
255 dom_element_named_parent_node((dom_element
*)node
,
257 (struct dom_element
**)parent
);
259 /* Implementation detail: dom_element_named_parent_node()
260 * increments the reference count of the parent element before
261 * returning it to us. According to docs/RefCnt in the libdom
262 * repository, this will prevent the parent element from being
263 * destroyed if it is pruned from the DOM. That sounds good,
264 * since we don't want to be using a pointer to an object that
265 * has been destroyed... but we also have no way of later
266 * decrementing the reference count ourselves, and don't want
267 * to make the returned node eternal. Decrementing the
268 * reference counter now allows it to be destroyed when the
269 * DOM no longer needs it, and so long as no other parts of
270 * libsvgtiny are messing with the DOM during parsing, that
271 * shouldn't (ha ha) cause any problems. */
272 dom_node_unref(*parent
);
279 * Find the "next-sibling" of the given element having the given name
281 * This search corresponds to the "+ foo" combinator in CSS and will
282 * find only "foo" element nodes that immediately precede the given
283 * node under the same parent in the DOM. In CSS the tree is viewed
284 * top-down and in libdom it is viewed from the bottom-up; as a result
285 * "next" and "previous" are sometimes backwards. This is case-sensitive.
287 * \param pw Pointer to the current SVG parser state
288 * \param node Libdom SVG node
289 * \param qname Name of the sibling node to search for
290 * \param sibling Address at which to store the sibling node pointer
292 * \return Always returns CSS_OK
294 * \post If a suitable element is found, a pointer to it will be
295 * stored at the address pointed to by \a sibling; otherwise,
296 * NULL will be stored at the address pointed to by \a sibling
298 css_error
named_sibling_node(void *pw
, void *node
,
299 const css_qname
*qname
, void **sibling
)
302 dom_node
*n
= node
; /* the current node */
303 dom_node
*prev
; /* the previous node */
308 *sibling
= NULL
; /* default to nothing found */
310 /* Begin the search; the first iteration we do outside of the
311 * loop. Implementation detil: dom_node_get_previous_sibling()
312 * increments the reference counter on the returned node. A
313 * comment within named_parent_node() explains why we
314 * decrement it ASAP. */
315 err
= dom_node_get_previous_sibling(n
, &n
);
316 if (err
!= DOM_NO_ERR
) {
321 /* We're looking for the first ELEMENT sibling */
322 err
= dom_node_get_node_type(n
, &type
);
323 if (err
!= DOM_NO_ERR
) {
328 if (type
== DOM_ELEMENT_NODE
) {
329 /* We found an element node, does it have the
331 err
= dom_node_get_node_name(n
, &name
);
332 if (err
!= DOM_NO_ERR
) {
337 if (dom_string_lwc_isequal(name
,
339 /* The name is right, return it */
343 /* There's only one next-sibling element node
344 * and we've already found it, so if its name
345 * wasn't right, we return the default value
347 dom_string_unref(name
);
352 /* Not an element node, so we move on the the previous
353 * previous sibling */
354 err
= dom_node_get_previous_sibling(n
, &prev
);
355 if (err
!= DOM_NO_ERR
) {
369 * Find the first "subsequent-sibling" of the given element having the
372 * This search corresponds to the "~ foo" combinator in CSS and will
373 * find only "foo" element nodes that precede the given node (under
374 * the same parent) in the DOM. In CSS the tree is viewed top-down and
375 * in libdom it is viewed from the bottom-up; as a result "next" and
376 * "previous" are sometimes backwards. This is case-sensitive.
378 * \param pw Pointer to the current SVG parser state
379 * \param node Libdom SVG node
380 * \param qname Name of the sibling node to search for
381 * \param sibling Address at which to store the sibling node pointer
383 * \return Always returns CSS_OK
385 * \post If a suitable element is found, a pointer to it will be
386 * stored at the address pointed to by \a sibling; otherwise,
387 * NULL will be stored at the address pointed to by \a sibling
389 css_error
named_generic_sibling_node(void *pw
, void *node
,
390 const css_qname
*qname
, void **sibling
)
393 dom_node
*n
= node
; /* the current node */
394 dom_node
*prev
; /* the previous node */
400 *sibling
= NULL
; /* default to nothing found */
402 /* Begin the search; the first iteration we do outside of the
403 * loop. Implementation detil: dom_node_get_previous_sibling()
404 * increments the reference counter on the returned node. A
405 * comment within named_parent_node() explains why we
406 * decrement it ASAP. */
407 err
= dom_node_get_previous_sibling(n
, &n
);
408 if (err
!= DOM_NO_ERR
) {
413 err
= dom_node_get_node_type(n
, &type
);
414 if (err
!= DOM_NO_ERR
) {
419 if (type
== DOM_ELEMENT_NODE
) {
420 /* We only want ELEMENT nodes */
421 err
= dom_node_get_node_name(n
, &name
);
422 if (err
!= DOM_NO_ERR
) {
427 if (dom_string_lwc_isequal(name
,
429 /* Found one. Save it and stop the search */
430 dom_string_unref(name
);
436 dom_string_unref(name
);
439 /* This sibling wasn't an element with the desired
440 name, so move on to the previous sibling */
441 err
= dom_node_get_previous_sibling(n
, &prev
);
442 if (err
!= DOM_NO_ERR
) {
456 * Return a pointer to the given node's parent
458 * \param pw Pointer to the current SVG parser state
459 * \param node Libdom SVG node
460 * \param parent Address at which to store the node's parent pointer
462 * \return Always returns CSS_OK
464 css_error
parent_node(void *pw
, void *node
, void **parent
)
467 /* Libdom basically implements this for us */
468 dom_element_parent_node(node
, (struct dom_element
**)parent
);
470 /* See the comment in named_parent_node() for why we decrement
471 * this reference counter here. */
472 dom_node_unref(*parent
);
479 * Find the "next-sibling" of the given element
481 * This search corresponds "+ *" in CSS and will find the first
482 * element node that immediately precedes the given node under the
483 * same parent in the DOM. In CSS the tree is viewed top-down and in
484 * libdom it is viewed from the bottom-up; as a result "next" and
485 * "previous" are sometimes backwards.
487 * \param pw Pointer to the current SVG parser state
488 * \param node Libdom SVG node
489 * \param sibling Address at which to store the sibling node pointer
491 * \return Always returns CSS_OK
493 * \post If a suitable element is found, a pointer to it will be
494 * stored at the address pointed to by \a sibling; otherwise,
495 * NULL will be stored at the address pointed to by \a sibling
497 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
500 dom_node
*n
= node
; /* the current node */
501 dom_node
*prev
; /* the previous node */
505 *sibling
= NULL
; /* default to nothing found */
507 /* Begin the search; the first iteration we do outside of the
508 * loop. Implementation detil: dom_node_get_previous_sibling()
509 * increments the reference counter on the returned node. A
510 * comment within named_parent_node() explains why we
511 * decrement it ASAP. */
512 err
= dom_node_get_previous_sibling(n
, &n
);
513 if (err
!= DOM_NO_ERR
) {
518 err
= dom_node_get_node_type(n
, &type
);
519 if (err
!= DOM_NO_ERR
) {
524 if (type
== DOM_ELEMENT_NODE
) {
525 /* We found a sibling node that is also an
526 element and that's all we wanted. */
532 /* This sibling node was not an element; move on to
533 the previous sibling */
534 err
= dom_node_get_previous_sibling(n
, &prev
);
535 if (err
!= DOM_NO_ERR
) {
549 * Test the given node for the given name
551 * This will return true (via the "match" pointer) if the libdom node
552 * has the given name or if that name is the universal selector;
553 * otherwise it returns false. The comparison is case-sensitive. It
554 * corresponds to a rule like "body { ... }" in CSS.
556 * \param pw Pointer to the current SVG parser state
557 * \param node Libdom SVG node to test
558 * \param qname Name to check for
559 * \param match Pointer to the test result
561 * \return Always returns CSS_OK
563 css_error
node_has_name(void *pw
, void *node
,
564 const css_qname
*qname
, bool *match
)
566 struct svgtiny_parse_state
*state
;
570 /* Start by checking to see if qname is the universal selector */
571 state
= (struct svgtiny_parse_state
*)pw
;
572 if (lwc_string_isequal(qname
->name
,
573 state
->interned_universal
, match
) == lwc_error_ok
) {
575 /* It's the universal selector. In NetSurf, all node
576 * names match the universal selector, and nothing in
577 * the libcss documentation suggests another approach,
578 * so we follow NetSurf here. */
583 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
584 if (err
!= DOM_NO_ERR
) {
588 /* Unlike with HTML, SVG element names are case-sensitive */
589 *match
= dom_string_lwc_isequal(name
, qname
->name
);
590 dom_string_unref(name
);
597 * Test the given node for the given class
599 * This will return true (via the "match" pointer) if the libdom node
600 * has the given class. The comparison is case-sensitive. It
601 * corresponds to node.class in CSS.
603 * \param pw Pointer to the current SVG parser state
604 * \param node Libdom SVG node to test
605 * \param name Class name to check for
606 * \param match Pointer to the test result
608 * \return Always returns CSS_OK
610 css_error
node_has_class(void *pw
, void *node
,
611 lwc_string
*name
, bool *match
)
614 /* libdom implements this for us and apparently it cannot fail */
615 dom_element_has_class((dom_node
*)node
, name
, match
);
621 * Test the given node for the given id
623 * This will return true (via the "match" pointer) if the libdom node
624 * has the given id. The comparison is case-sensitive. It corresponds
627 * \param pw Pointer to the current SVG parser state
628 * \param node Libdom SVG node to test
629 * \param name Id to check for
630 * \param match Pointer to the test result
632 * \return Always returns CSS_OK
634 css_error
node_has_id(void *pw
, void *node
,
635 lwc_string
*name
, bool *match
)
639 struct svgtiny_parse_state
*state
;
641 attr
= NULL
; /* a priori the "id" attribute may not exist */
642 *match
= false; /* default to no match */
644 state
= (struct svgtiny_parse_state
*)pw
;
645 err
= dom_element_get_attribute((dom_node
*)node
,
646 state
->interned_id
, &attr
);
647 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
651 *match
= dom_string_lwc_isequal(attr
, name
);
652 dom_string_unref(attr
);
659 * Test the given node for the given attribute
661 * This will return true (via the "match" pointer) if the libdom node
662 * has an attribute with the given name. The comparison is
663 * case-sensitive. It corresponds to node[attr] in CSS.
665 * \param pw Pointer to the current SVG parser state
666 * \param node Libdom SVG node to test
667 * \param qname Attribute name to check for
668 * \param match Pointer to the test result
670 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
673 css_error
node_has_attribute(void *pw
, void *node
,
674 const css_qname
*qname
, bool *match
)
680 /* intern the attribute name as a dom_string so we can
681 * delegate to dom_element_has_attribute() */
682 err
= dom_string_create_interned(
683 (const uint8_t *) lwc_string_data(qname
->name
),
684 lwc_string_length(qname
->name
),
686 if (err
!= DOM_NO_ERR
) {
690 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
691 if (err
!= DOM_NO_ERR
) {
692 dom_string_unref(name
);
696 dom_string_unref(name
);
702 * Test the given node for an attribute with a specific value
704 * This will return true (via the "match" pointer) if the libdom node
705 * has an attribute with the given name and value. The comparison is
706 * case-sensitive. It corresponds to node[attr=value] in CSS.
708 * \param pw Pointer to the current SVG parser state
709 * \param node Libdom SVG node to test
710 * \param qname Attribute name to check for
711 * \param value Attribute value to check for
712 * \param match Pointer to the test result
714 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
715 * intern the attribute name (which usually indicates memory
718 css_error
node_has_attribute_equal(void *pw
, void *node
,
719 const css_qname
*qname
, lwc_string
*value
,
722 /* Implementation note: NetSurf always returns "no match" when
723 * the value is empty (length zero). We allow it, because why
728 dom_string
*attr_val
;
731 /* Intern the attribute name as a dom_string so we can
732 * use dom_element_get_attribute() */
733 err
= dom_string_create_interned(
734 (const uint8_t *) lwc_string_data(qname
->name
),
735 lwc_string_length(qname
->name
),
737 if (err
!= DOM_NO_ERR
) {
741 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
742 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
743 /* There was an error getting the attribute's value or
744 * the attribute doesn't exist. So, no match? */
745 dom_string_unref(name
);
750 /* Otherwise, we have the attribute value from the given node
751 * and all we need to do is compare. */
752 dom_string_unref(name
);
753 *match
= dom_string_lwc_isequal(attr_val
, value
);
754 dom_string_unref(attr_val
);
761 * Test the given node for an attribute with a specific value,
762 * possibly followed by a single hyphen
764 * This will return true (via the "match" pointer) if the libdom node
765 * has an attribute with the given name and value or with the given
766 * name and a value that is followed by exactly one hyphen. The
767 * comparison is case-sensitive. This corresponds to [attr|=value]
770 * \param pw Pointer to the current SVG parser state
771 * \param node Libdom SVG node to test
772 * \param qname Attribute name to check for
773 * \param value Attribute value to check for
774 * \param match Pointer to the test result
776 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
777 * intern the attribute name (which usually indicates memory
780 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
781 const css_qname
*qname
, lwc_string
*value
,
784 /* Implementation note: NetSurf always returns "no match" when
785 * the value is empty (length zero). We allow it, because why
790 dom_string
*attr_val
;
793 const char *vdata
; /* to hold the data underlying "value" */
795 const char *avdata
; /* to hold the found attribute value data */
798 /* Intern the attribute name as a dom_string so we can
799 * use dom_element_get_attribute() */
800 err
= dom_string_create_interned(
801 (const uint8_t *) lwc_string_data(qname
->name
),
802 lwc_string_length(qname
->name
),
804 if (err
!= DOM_NO_ERR
) {
808 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
809 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
810 /* There was an error getting the attribute's value or
811 * the attribute doesn't exist. So, no match? */
812 dom_string_unref(name
);
817 /* Otherwise, we have the attribute value from the given node
818 * and all we need to do is compare. */
819 dom_string_unref(name
);
820 *match
= dom_string_lwc_isequal(attr_val
, value
);
822 /* Exact match, we're done */
823 dom_string_unref(attr_val
);
827 /* No exact match, try it with a hyphen on the end */
828 vdata
= lwc_string_data(value
); /* needle */
829 vdata_len
= lwc_string_length(value
);
830 avdata
= dom_string_data(attr_val
); /* haystack */
831 avdata_len
= dom_string_byte_length(attr_val
);
832 dom_string_unref(attr_val
);
834 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
835 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
836 /* If there's a hyphen in the right position,
837 * it suffices to compare the strings only up
848 * Test the given node for an attribute whose value is a
849 * space-separated list of words, one of which is the given word
851 * This will return true (via the "match" pointer) if the libdom node
852 * has an attribute with the given name and whose value when
853 * considered as a space-separated list of words contains the given
854 * word. The comparison is case-sensitive. This corresponds to
855 * [attr~=value] in CSS.
857 * \param pw Pointer to the current SVG parser state
858 * \param node Libdom SVG node to test
859 * \param qname Attribute name to check for
860 * \param word Value word to check for
861 * \param match Pointer to the test result
863 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
864 * intern the attribute name (which usually indicates memory
867 css_error
node_has_attribute_includes(void *pw
, void *node
,
868 const css_qname
*qname
, lwc_string
*word
,
874 dom_string
*attr_val
;
876 size_t wordlen
; /* length of "word" */
878 /* pointers used to parse a space-separated list of words */
883 *match
= false; /* default to no match */
885 wordlen
= lwc_string_length(word
);
887 /* In this case, the spec says that "if 'val' is the
888 * empty string, it will never represent anything." */
892 /* Intern the attribute name as a dom_string so we can
893 * use dom_element_get_attribute() */
894 err
= dom_string_create_interned(
895 (const uint8_t *) lwc_string_data(qname
->name
),
896 lwc_string_length(qname
->name
),
898 if (err
!= DOM_NO_ERR
) {
902 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
903 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
904 /* There was an error getting the attribute's value or
905 * the attribute doesn't exist. So, no match? */
906 dom_string_unref(name
);
910 /* Parse the list comparing each word against "word" */
911 start
= dom_string_data(attr_val
);
912 end
= start
+ dom_string_byte_length(attr_val
);
913 dom_string_unref(attr_val
);
915 for (p
= start
; p
<= end
; p
++) {
916 /* Move forward until we find the end of the first word */
917 if (*p
== ' ' || *p
== '\0') {
918 /* If the length of that word is the length of the
919 * word we're looking for, do the comparison. */
920 if ((size_t) (p
- start
) == wordlen
&&
922 lwc_string_data(word
),
927 /* No match? Set "start" to the beginning of
928 * the next word and loop. */
938 * Test the given node for an attribute whose value begins with the
941 * This will return true (via the "match" pointer) if the libdom node
942 * has an attribute with the given name and whose value begins with
943 * the given prefix string. The comparison is case-sensitive. This
944 * corresponds to [attr^=value] in CSS.
946 * \param pw Pointer to the current SVG parser state
947 * \param node Libdom SVG node to test
948 * \param qname Attribute name to check for
949 * \param prefix Value prefix to check for
950 * \param match Pointer to the test result
952 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
953 * intern the attribute name (which usually indicates memory
956 css_error
node_has_attribute_prefix(void *pw
, void *node
,
957 const css_qname
*qname
, lwc_string
*prefix
,
962 dom_string
*attr_val
;
964 const char *avdata
; /* attribute value data */
965 size_t avdata_len
; /* length of that attribute value data */
966 size_t prefixlen
; /* length of "prefix" */
968 prefixlen
= lwc_string_length(prefix
);
969 if (prefixlen
== 0) {
970 /* In this case, the spec says that "if 'val' is the
971 * empty string, it will never represent anything." */
975 /* Intern the attribute name as a dom_string so we can
976 * use dom_element_get_attribute() */
977 err
= dom_string_create_interned(
978 (const uint8_t *) lwc_string_data(qname
->name
),
979 lwc_string_length(qname
->name
),
981 if (err
!= DOM_NO_ERR
) {
985 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
986 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
987 /* There was an error getting the attribute's value or
988 * the attribute doesn't exist. So, no match? */
989 dom_string_unref(name
);
994 /* Otherwise, we have the attribute value from the given node,
995 * and the first thing we want to do is check to see if the
996 * whole thing matches the prefix. */
997 dom_string_unref(name
);
998 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
1000 /* If not, check to see if an, uh, prefix matches the
1002 if (*match
== false) {
1003 avdata
= dom_string_data(attr_val
);
1004 avdata_len
= dom_string_byte_length(attr_val
);
1005 if ((avdata_len
>= prefixlen
) &&
1006 (strncasecmp(avdata
,
1007 lwc_string_data(prefix
),
1009 /* Use strncasecmp to compare only the first
1010 * "n" characters, where "n" is the length of
1016 dom_string_unref(attr_val
);
1023 * Test the given node for an attribute whose value end with the
1026 * This will return true (via the "match" pointer) if the libdom node
1027 * has an attribute with the given name and whose value ends with
1028 * the given suffix string. The comparison is case-sensitive. This
1029 * corresponds to [attr$=value] in CSS.
1031 * \param pw Pointer to the current SVG parser state
1032 * \param node Libdom SVG node to test
1033 * \param qname Attribute name to check for
1034 * \param suffix Value suffix to check for
1035 * \param match Pointer to the test result
1037 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1038 * intern the attribute name (which usually indicates memory
1041 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1042 const css_qname
*qname
, lwc_string
*suffix
,
1047 dom_string
*attr_val
;
1049 const char *avdata
; /* attribute value data */
1050 size_t avdata_len
; /* length of that attribute value data */
1051 size_t suffixlen
; /* length of "suffix" */
1053 /* convenience pointer we'll use when matching the suffix */
1054 const char *suffix_start
;
1056 suffixlen
= lwc_string_length(suffix
);
1057 if (suffixlen
== 0) {
1058 /* In this case, the spec says that "if 'val' is the
1059 * empty string, it will never represent anything." */
1063 /* Intern the attribute name as a dom_string so we can
1064 * use dom_element_get_attribute() */
1065 err
= dom_string_create_interned(
1066 (const uint8_t *) lwc_string_data(qname
->name
),
1067 lwc_string_length(qname
->name
),
1069 if (err
!= DOM_NO_ERR
) {
1073 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1074 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1075 /* There was an error getting the attribute's value or
1076 * the attribute doesn't exist. So, no match? */
1077 dom_string_unref(name
);
1082 /* Otherwise, we have the attribute value from the given node,
1083 * and the first thing we want to do is check to see if the
1084 * whole thing matches the suffix. */
1085 dom_string_unref(name
);
1086 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1088 /* If not, check to see if an, uh, suffix matches the
1090 if (*match
== false) {
1091 avdata
= dom_string_data(attr_val
);
1092 avdata_len
= dom_string_byte_length(attr_val
);
1094 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1096 if ((avdata_len
>= suffixlen
) &&
1097 (strncasecmp(suffix_start
,
1098 lwc_string_data(suffix
),
1100 /* Use strncasecmp to compare only the last
1101 * "n" characters, where "n" is the length of
1107 dom_string_unref(attr_val
);
1114 * Implement node_has_attribute_substring() with optional case-
1115 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1116 * not supported by libcss yet, but it allows us to factor out some
1119 static css_error
_node_has_attribute_substring(void *pw
, void *node
,
1120 const css_qname
*qname
, lwc_string
*substring
,
1121 bool *match
, bool insensitive
)
1125 dom_string
*attr_val
;
1127 size_t attr_len
; /* length of attr_val */
1128 size_t substrlen
; /* length of "substring" */
1130 /* Convenience pointers we use when comparing substrings */
1134 substrlen
= lwc_string_length(substring
);
1135 if (substrlen
== 0) {
1136 /* In this case, the spec says that "if 'val' is the
1137 * empty string, it will never represent anything." */
1141 /* Intern the attribute name as a dom_string so we can
1142 * use dom_element_get_attribute() */
1143 err
= dom_string_create_interned(
1144 (const uint8_t *) lwc_string_data(qname
->name
),
1145 lwc_string_length(qname
->name
),
1147 if (err
!= DOM_NO_ERR
) {
1151 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1152 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1153 /* There was an error getting the attribute's value or
1154 * the attribute doesn't exist. So, no match? */
1155 dom_string_unref(name
);
1160 /* Otherwise, we have the attribute value from the given node,
1161 * and the first thing we want to do is check to see if the
1162 * whole thing matches the substring. */
1163 dom_string_unref(name
);
1166 *match
= dom_string_caseless_lwc_isequal(attr_val
, substring
);
1169 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1172 /* If not, check to see if an, uh, substring matches the
1174 if (*match
== false) {
1175 p
= dom_string_data(attr_val
);
1177 /* Check every long-enough suffix for a prefix match */
1178 attr_len
= dom_string_byte_length(attr_val
);
1179 if (attr_len
>= substrlen
) {
1180 p_max
= p
+ attr_len
- substrlen
;
1181 while (p
<= p_max
) {
1183 lwc_string_data(substring
),
1193 dom_string_unref(attr_val
);
1199 * Test the given node for an attribute whose value contains the
1202 * This will return true (via the "match" pointer) if the libdom node
1203 * has an attribute with the given name and whose value contains the
1204 * given substring. The comparison is case-sensitive. This corresponds
1205 * to [attr*=value] in CSS.
1207 * \param pw Pointer to the current SVG parser state
1208 * \param node Libdom SVG node to test
1209 * \param qname Attribute name to check for
1210 * \param substring Value substring to check for
1211 * \param match Pointer to the test result
1213 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1214 * intern the attribute name (which usually indicates memory
1217 css_error
node_has_attribute_substring(void *pw
, void *node
,
1218 const css_qname
*qname
, lwc_string
*substring
,
1221 return _node_has_attribute_substring(pw
, node
, qname
, substring
,
1227 * Test whether or not the given node is the document's root element
1228 * This corresponds to the CSS :root pseudo-selector.
1230 * \param pw Pointer to the current SVG parser state
1231 * \param node Libdom SVG node to test
1232 * \param match Pointer to the test result
1234 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1236 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1243 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1244 if (err
!= DOM_NO_ERR
) {
1248 /* It's the root element if it doesn't have a parent element */
1249 if (parent
!= NULL
) {
1250 err
= dom_node_get_node_type(parent
, &type
);
1251 dom_node_unref(parent
);
1252 if (err
!= DOM_NO_ERR
) {
1255 if (type
!= DOM_DOCUMENT_NODE
) {
1256 /* DOM_DOCUMENT_NODE is the only allowable
1257 * type of parent node for the root element */
1269 * Used internally in node_count_siblings() to "count" the given
1270 * sibling node. It factors out the node type and name checks.
1272 static int node_count_siblings_check(dom_node
*dnode
,
1279 dom_string
*dnode_name
;
1281 /* We flip this to 1 if/when we count this node */
1284 if (dnode
== NULL
) {
1288 exc
= dom_node_get_node_type(dnode
, &type
);
1289 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1290 /* We only count element siblings */
1294 /* ... with the right name */
1297 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1299 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1300 if (dom_string_isequal(name
,
1304 dom_string_unref(dnode_name
);
1315 * Count the given node's sibling elements
1317 * This counts the given node's sibling elements in one direction,
1318 * either forwards or backwards, in the DOM. Keep in mind that the
1319 * libdom tree is upside-down compared to the CSS one; so "next" and
1320 * "previous" are actually reversed; the default is to count preceding
1321 * libdom siblings which correspond to subsequent CSS siblings.
1323 * This operation is central to the CSS :first-child, :nth-child, and
1324 * :last-child (et cetera) pseudo-selectors.
1326 * If same_name is true, then only nodes having the same
1327 * (case-sensitive) name as the given node are counted.
1329 * \param pw Pointer to the current SVG parser state
1330 * \param node Libdom SVG node whose siblings we're counting
1331 * \param same_name Whether or not to count only siblings having
1332 * the same name as the given node
1333 * \param after Count subsequent siblings rather than precedent
1334 * ones (the default)
1335 * \param count Pointer to the return value, the number of sibling
1338 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1340 css_error
node_count_siblings(void *pw
, void *node
,
1341 bool same_name
, bool after
, int32_t *count
)
1345 dom_node
*dnode
; /* node, but with the right type */
1346 dom_string
*dnode_name
;
1347 dom_node
*next
; /* "next" sibling (depends on direction) */
1349 /* Pointer to the "next sibling" function */
1350 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1355 dnode
= (dom_node
*)node
;
1357 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1358 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1363 /* Increment the reference counter for dnode for as long as
1364 * we retain a reference to it. */
1365 dnode
= dom_node_ref(dnode
);
1367 next_func
= dom_node_get_previous_sibling
;
1369 next_func
= dom_node_get_next_sibling
;
1373 exc
= next_func(dnode
, &next
);
1374 if (exc
!= DOM_NO_ERR
) {
1378 /* If next_func worked, we're about to swap "next"
1379 * with "dnode" meaning that we will no longer retain
1380 * a reference to the current dnode. */
1381 dom_node_unref(dnode
);
1384 *count
+= node_count_siblings_check(dnode
,
1387 } while (dnode
!= NULL
);
1389 if (dnode_name
!= NULL
) {
1390 dom_string_unref(dnode_name
);
1398 * Determine whether or not the given element is empty
1400 * An element is "nonempty" if it has a child that is either an
1401 * element node or a text node.
1403 * \param pw Pointer to the current SVG parser state
1404 * \param node Libdom SVG node to check for emptiness
1405 * \param is_empty Pointer to the return value
1407 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1409 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1412 dom_node
*child
; /* current child node pointer */
1413 dom_node
*next
; /* next child node pointer */
1414 dom_node_type type
; /* what type of node is "child" */
1417 /* Assume that it's empty by default */
1420 /* Get the given node's first child. Implementation detail:
1421 * this increments the reference counter on the child node. */
1422 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1423 if (err
!= DOM_NO_ERR
) {
1427 /* And now loop through all children looking for a
1428 * text/element node. If we find one, the original
1429 * node is "nonempty" */
1430 while (child
!= NULL
) {
1431 err
= dom_node_get_node_type(child
, &type
);
1432 if (err
!= DOM_NO_ERR
) {
1433 dom_node_unref(child
);
1437 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1439 dom_node_unref(child
);
1443 err
= dom_node_get_next_sibling(child
, &next
);
1444 if (err
!= DOM_NO_ERR
) {
1445 dom_node_unref(child
);
1449 /* If we're moving to the next node, we can release
1450 * the reference to the current one */
1451 dom_node_unref(child
);
1460 * Determine whether or not the given node is a link
1462 * A node is a link if it is an element node whose name is "a" and if
1463 * it has an "href" attribute (case-sensitive). This selector
1464 * corresponds to node:link pseudo-class in CSS.
1466 * This pseudo-class is a bit awkward because the two standards (HTML5
1467 * and CSS) disagree on what it means, and because libsvgtiny does not
1468 * have enough information to determine if a link has been "visited"
1469 * yet -- that's a UI property. CSS says that :link is for unvisited
1470 * links, which we can't determine. HTML5 says that each link must
1471 * be either a :link or :visited. Since we can't decide either way,
1472 * It seems less wrong to declare that all links are unvisited; i.e.
1473 * that they match :link.
1475 * \param pw Pointer to the current SVG parser state
1476 * \param node Libdom SVG node to check
1477 * \param is_link Pointer to the boolean return value
1479 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1481 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1484 dom_node
*dnode
; /* node, but with the right type */
1485 dom_string
*dnode_name
;
1487 struct svgtiny_parse_state
* state
;
1489 dnode
= (dom_node
*)node
;
1491 has_href
= false; /* assume no href attribute */
1492 *is_link
= false; /* assume that it's not a link */
1494 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1495 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1499 state
= (struct svgtiny_parse_state
*)pw
;
1500 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1501 exc
= dom_element_has_attribute(node
,
1502 state
->interned_href
,
1504 if (exc
== DOM_NO_ERR
&& has_href
) {
1509 dom_string_unref(dnode_name
);
1515 * Check if the given node is being "hovered" over
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_hover Pointer to the boolean return value
1524 * \return Always returns CSS_OK
1526 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1536 * Check if the given node is "active"
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_active Pointer to the boolean return value
1545 * \return Always returns CSS_OK
1547 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1557 * Check if the given node has the focus
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_focus Pointer to the boolean return value
1566 * \return Always returns CSS_OK
1568 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1578 * Check if the given node is enabled
1580 * This check always fails because the SVG DOM does not have the
1581 * necessary information (it's a UI property).
1583 * \param pw Pointer to the current SVG parser state; unused
1584 * \param node Libdom SVG node to check; unused
1585 * \param is_enabled Pointer to the boolean return value
1587 * \return Always returns CSS_OK
1589 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1593 *is_enabled
= false;
1599 * Check if the given node is disabled
1601 * This check always fails because the SVG DOM does not have the
1602 * necessary information (it's a UI property). Beware, until they are
1603 * implemented, this is NOT the logical negation of node_is_enabled!
1605 * \param pw Pointer to the current SVG parser state; unused
1606 * \param node Libdom SVG node to check; unused
1607 * \param is_disabled Pointer to the boolean return value
1609 * \return Always returns CSS_OK
1611 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1615 *is_disabled
= false;
1621 * Test whether or not the given node is "checked"
1623 * This test always fails because the SVG DOM does not have the
1624 * necessary information (it's a UI property).
1626 * \param pw Pointer to the current SVG parser state; unused
1627 * \param node Libdom SVG node to check; unused
1628 * \param is_checked Pointer to the boolean return value
1630 * \return Always returns CSS_OK
1632 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1636 *is_checked
= false;
1642 * Check if the given node is the "target" of the document URL
1644 * This test always fails because the SVG DOM does not have the
1645 * necessary information (it's a UI property).
1647 * \param pw Pointer to the current SVG parser state; unused
1648 * \param node Libdom SVG node to check; unused
1649 * \param is_target Pointer to the boolean return value
1651 * \return Always returns CSS_OK
1653 css_error
node_is_target(void *pw
, void *node
, bool *is_target
)