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
);
57 static css_error
node_is_lang(void *pw
, void *node
,
58 lwc_string
*lang
, bool *is_lang
);
62 * Resolve a relative URL to an absolute one by doing nothing. This is
63 * the simplest possible implementation of a URL resolver, needed for
66 css_error
svgtiny_resolve_url(void *pw
,
67 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
72 /* Copy the relative URL to the absolute one (the return
74 *abs
= lwc_string_ref(rel
);
79 * Create a stylesheet with the default set of params.
81 * \param sheet A stylesheet pointer, passed in by reference, that
82 * we use to store the newly-created stylesheet.
83 * \param inline_style True if this stylesheet represents an inline
84 * style, and false otherwise.
86 * \return The return value from css_stylesheet_create() is returned.
88 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
91 css_stylesheet_params params
;
93 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
94 params
.level
= CSS_LEVEL_DEFAULT
;
95 params
.charset
= NULL
;
98 params
.allow_quirks
= false;
99 params
.inline_style
= inline_style
;
100 params
.resolve
= svgtiny_resolve_url
;
101 params
.resolve_pw
= NULL
;
102 params
.import = NULL
;
103 params
.import_pw
= NULL
;
105 params
.color_pw
= NULL
;
107 params
.font_pw
= NULL
;
109 return css_stylesheet_create(¶ms
, sheet
);
113 /**************************/
114 /* libcss select handlers */
115 /**************************/
117 * From here on we implement the "select handler "API defined in
118 * libcss's include/libcss/select.h and discussed briefly in its
124 * Retrieve the given node's name
126 * \param pw Pointer to the current SVG parser state
127 * \param node Libdom SVG node
128 * \param qname Address at which to store the node name
130 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
132 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
136 struct svgtiny_parse_state
*state
;
138 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
139 if (err
!= DOM_NO_ERR
) {
143 state
= (struct svgtiny_parse_state
*)pw
;
144 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
146 err
= dom_string_intern(name
, &qname
->name
);
147 if (err
!= DOM_NO_ERR
) {
148 dom_string_unref(name
);
152 dom_string_unref(name
);
159 * Retrieve the given node's classes
161 * \param pw Pointer to the current SVG parser state
162 * \param node Libdom SVG node
163 * \param classes Address at which to store the class name array
164 * \param n_classes Address at which to store the length of the class
167 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
169 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
170 * because the underlying libdom function never fails
172 css_error
node_classes(void *pw
, void *node
,
173 lwc_string
***classes
, uint32_t *n_classes
)
178 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
180 /* The implementation does not do it, but the documentation
181 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
182 possible here, so we handle it to be on the safe side. */
183 if (err
!= DOM_NO_ERR
) {
192 * Retrieve the given node's id
194 * \param pw Pointer to the current SVG parser state
195 * \param node Libdom SVG node
196 * \param id Address at which to store the id
198 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
200 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
204 struct svgtiny_parse_state
*state
;
206 /* Begin with the assumption that this node has no id */
209 state
= (struct svgtiny_parse_state
*)pw
;
210 err
= dom_element_get_attribute((dom_node
*)node
,
211 state
->interned_id
, &attr
);
212 if (err
!= DOM_NO_ERR
) {
215 else if (attr
== NULL
) {
216 /* The node has no id attribute and our return value
217 is already set to NULL so we're done */
221 /* If we found an id attribute (a dom_string), intern it into
222 an lwc_string that we can return, and then cleanup the
224 err
= dom_string_intern(attr
, id
);
225 if (err
!= DOM_NO_ERR
) {
226 dom_string_unref(attr
);
229 dom_string_unref(attr
);
236 * Find the first parent of the given element having the given name
238 * \param pw Pointer to the current SVG parser state
239 * \param node Libdom SVG node
240 * \param qname Name of the parent node to search for
241 * \param parent Address at which to store the parent node pointer
243 * \return Always returns CSS_OK
245 * \post If a suitable element is found, a pointer to it will be
246 * stored at the address pointed to by \a parent; otherwise,
247 * NULL will be stored at the address pointed to by \a parent
249 css_error
named_parent_node(void *pw
, void *node
,
250 const css_qname
*qname
, void **parent
)
253 /* dom_element_named_parent_node() was invented to implement
254 * this select handler so there isn't much for us to do except
255 * call it. It's OK if node isn't an element, libdom checks
257 dom_element_named_parent_node((dom_element
*)node
,
259 (struct dom_element
**)parent
);
261 /* Implementation detail: dom_element_named_parent_node()
262 * increments the reference count of the parent element before
263 * returning it to us. According to docs/RefCnt in the libdom
264 * repository, this will prevent the parent element from being
265 * destroyed if it is pruned from the DOM. That sounds good,
266 * since we don't want to be using a pointer to an object that
267 * has been destroyed... but we also have no way of later
268 * decrementing the reference count ourselves, and don't want
269 * to make the returned node eternal. Decrementing the
270 * reference counter now allows it to be destroyed when the
271 * DOM no longer needs it, and so long as no other parts of
272 * libsvgtiny are messing with the DOM during parsing, that
273 * shouldn't (ha ha) cause any problems. */
274 dom_node_unref(*parent
);
281 * Find the "next-sibling" of the given element having the given name
283 * This search corresponds to the "+ foo" combinator in CSS and will
284 * find only "foo" element nodes that immediately precede the given
285 * node under the same parent in the DOM. In CSS the tree is viewed
286 * top-down and in libdom it is viewed from the bottom-up; as a result
287 * "next" and "previous" are sometimes backwards. This is case-sensitive.
289 * \param pw Pointer to the current SVG parser state
290 * \param node Libdom SVG node
291 * \param qname Name of the sibling node to search for
292 * \param sibling Address at which to store the sibling node pointer
294 * \return Always returns CSS_OK
296 * \post If a suitable element is found, a pointer to it will be
297 * stored at the address pointed to by \a sibling; otherwise,
298 * NULL will be stored at the address pointed to by \a sibling
300 css_error
named_sibling_node(void *pw
, void *node
,
301 const css_qname
*qname
, void **sibling
)
304 dom_node
*n
= node
; /* the current node */
305 dom_node
*prev
; /* the previous node */
310 *sibling
= NULL
; /* default to nothing found */
312 /* Begin the search; the first iteration we do outside of the
313 * loop. Implementation detil: dom_node_get_previous_sibling()
314 * increments the reference counter on the returned node. A
315 * comment within named_parent_node() explains why we
316 * decrement it ASAP. */
317 err
= dom_node_get_previous_sibling(n
, &n
);
318 if (err
!= DOM_NO_ERR
) {
323 /* We're looking for the first ELEMENT sibling */
324 err
= dom_node_get_node_type(n
, &type
);
325 if (err
!= DOM_NO_ERR
) {
330 if (type
== DOM_ELEMENT_NODE
) {
331 /* We found an element node, does it have the
333 err
= dom_node_get_node_name(n
, &name
);
334 if (err
!= DOM_NO_ERR
) {
339 if (dom_string_lwc_isequal(name
,
341 /* The name is right, return it */
345 /* There's only one next-sibling element node
346 * and we've already found it, so if its name
347 * wasn't right, we return the default value
349 dom_string_unref(name
);
354 /* Not an element node, so we move on the the previous
355 * previous sibling */
356 err
= dom_node_get_previous_sibling(n
, &prev
);
357 if (err
!= DOM_NO_ERR
) {
371 * Find the first "subsequent-sibling" of the given element having the
374 * This search corresponds to the "~ foo" combinator in CSS and will
375 * find only "foo" element nodes that precede the given node (under
376 * the same parent) in the DOM. In CSS the tree is viewed top-down and
377 * in libdom it is viewed from the bottom-up; as a result "next" and
378 * "previous" are sometimes backwards. This is case-sensitive.
380 * \param pw Pointer to the current SVG parser state
381 * \param node Libdom SVG node
382 * \param qname Name of the sibling node to search for
383 * \param sibling Address at which to store the sibling node pointer
385 * \return Always returns CSS_OK
387 * \post If a suitable element is found, a pointer to it will be
388 * stored at the address pointed to by \a sibling; otherwise,
389 * NULL will be stored at the address pointed to by \a sibling
391 css_error
named_generic_sibling_node(void *pw
, void *node
,
392 const css_qname
*qname
, void **sibling
)
395 dom_node
*n
= node
; /* the current node */
396 dom_node
*prev
; /* the previous node */
402 *sibling
= NULL
; /* default to nothing found */
404 /* Begin the search; the first iteration we do outside of the
405 * loop. Implementation detil: dom_node_get_previous_sibling()
406 * increments the reference counter on the returned node. A
407 * comment within named_parent_node() explains why we
408 * decrement it ASAP. */
409 err
= dom_node_get_previous_sibling(n
, &n
);
410 if (err
!= DOM_NO_ERR
) {
415 err
= dom_node_get_node_type(n
, &type
);
416 if (err
!= DOM_NO_ERR
) {
421 if (type
== DOM_ELEMENT_NODE
) {
422 /* We only want ELEMENT nodes */
423 err
= dom_node_get_node_name(n
, &name
);
424 if (err
!= DOM_NO_ERR
) {
429 if (dom_string_lwc_isequal(name
,
431 /* Found one. Save it and stop the search */
432 dom_string_unref(name
);
438 dom_string_unref(name
);
441 /* This sibling wasn't an element with the desired
442 name, so move on to the previous sibling */
443 err
= dom_node_get_previous_sibling(n
, &prev
);
444 if (err
!= DOM_NO_ERR
) {
458 * Return a pointer to the given node's parent
460 * \param pw Pointer to the current SVG parser state
461 * \param node Libdom SVG node
462 * \param parent Address at which to store the node's parent pointer
464 * \return Always returns CSS_OK
466 css_error
parent_node(void *pw
, void *node
, void **parent
)
469 /* Libdom basically implements this for us */
470 dom_element_parent_node(node
, (struct dom_element
**)parent
);
472 /* See the comment in named_parent_node() for why we decrement
473 * this reference counter here. */
474 dom_node_unref(*parent
);
481 * Find the "next-sibling" of the given element
483 * This search corresponds "+ *" in CSS and will find the first
484 * element node that immediately precedes the given node under the
485 * same parent in the DOM. In CSS the tree is viewed top-down and in
486 * libdom it is viewed from the bottom-up; as a result "next" and
487 * "previous" are sometimes backwards.
489 * \param pw Pointer to the current SVG parser state
490 * \param node Libdom SVG node
491 * \param sibling Address at which to store the sibling node pointer
493 * \return Always returns CSS_OK
495 * \post If a suitable element is found, a pointer to it will be
496 * stored at the address pointed to by \a sibling; otherwise,
497 * NULL will be stored at the address pointed to by \a sibling
499 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
502 dom_node
*n
= node
; /* the current node */
503 dom_node
*prev
; /* the previous node */
507 *sibling
= NULL
; /* default to nothing found */
509 /* Begin the search; the first iteration we do outside of the
510 * loop. Implementation detil: dom_node_get_previous_sibling()
511 * increments the reference counter on the returned node. A
512 * comment within named_parent_node() explains why we
513 * decrement it ASAP. */
514 err
= dom_node_get_previous_sibling(n
, &n
);
515 if (err
!= DOM_NO_ERR
) {
520 err
= dom_node_get_node_type(n
, &type
);
521 if (err
!= DOM_NO_ERR
) {
526 if (type
== DOM_ELEMENT_NODE
) {
527 /* We found a sibling node that is also an
528 element and that's all we wanted. */
534 /* This sibling node was not an element; move on to
535 the previous sibling */
536 err
= dom_node_get_previous_sibling(n
, &prev
);
537 if (err
!= DOM_NO_ERR
) {
551 * Test the given node for the given name
553 * This will return true (via the "match" pointer) if the libdom node
554 * has the given name or if that name is the universal selector;
555 * otherwise it returns false. The comparison is case-sensitive. It
556 * corresponds to a rule like "body { ... }" in CSS.
558 * \param pw Pointer to the current SVG parser state
559 * \param node Libdom SVG node to test
560 * \param qname Name to check for
561 * \param match Pointer to the test result
563 * \return Always returns CSS_OK
565 css_error
node_has_name(void *pw
, void *node
,
566 const css_qname
*qname
, bool *match
)
568 struct svgtiny_parse_state
*state
;
572 /* Start by checking to see if qname is the universal selector */
573 state
= (struct svgtiny_parse_state
*)pw
;
574 if (lwc_string_isequal(qname
->name
,
575 state
->interned_universal
, match
) == lwc_error_ok
) {
577 /* It's the universal selector. In NetSurf, all node
578 * names match the universal selector, and nothing in
579 * the libcss documentation suggests another approach,
580 * so we follow NetSurf here. */
585 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
586 if (err
!= DOM_NO_ERR
) {
590 /* Unlike with HTML, SVG element names are case-sensitive */
591 *match
= dom_string_lwc_isequal(name
, qname
->name
);
592 dom_string_unref(name
);
599 * Test the given node for the given class
601 * This will return true (via the "match" pointer) if the libdom node
602 * has the given class. The comparison is case-sensitive. It
603 * corresponds to node.class in CSS.
605 * \param pw Pointer to the current SVG parser state
606 * \param node Libdom SVG node to test
607 * \param name Class name to check for
608 * \param match Pointer to the test result
610 * \return Always returns CSS_OK
612 css_error
node_has_class(void *pw
, void *node
,
613 lwc_string
*name
, bool *match
)
616 /* libdom implements this for us and apparently it cannot fail */
617 dom_element_has_class((dom_node
*)node
, name
, match
);
623 * Test the given node for the given id
625 * This will return true (via the "match" pointer) if the libdom node
626 * has the given id. The comparison is case-sensitive. It corresponds
629 * \param pw Pointer to the current SVG parser state
630 * \param node Libdom SVG node to test
631 * \param name Id to check for
632 * \param match Pointer to the test result
634 * \return Always returns CSS_OK
636 css_error
node_has_id(void *pw
, void *node
,
637 lwc_string
*name
, bool *match
)
641 struct svgtiny_parse_state
*state
;
643 attr
= NULL
; /* a priori the "id" attribute may not exist */
644 *match
= false; /* default to no match */
646 state
= (struct svgtiny_parse_state
*)pw
;
647 err
= dom_element_get_attribute((dom_node
*)node
,
648 state
->interned_id
, &attr
);
649 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
653 *match
= dom_string_lwc_isequal(attr
, name
);
654 dom_string_unref(attr
);
661 * Test the given node for the given attribute
663 * This will return true (via the "match" pointer) if the libdom node
664 * has an attribute with the given name. The comparison is
665 * case-sensitive. It corresponds to node[attr] in CSS.
667 * \param pw Pointer to the current SVG parser state
668 * \param node Libdom SVG node to test
669 * \param qname Attribute name to check for
670 * \param match Pointer to the test result
672 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
675 css_error
node_has_attribute(void *pw
, void *node
,
676 const css_qname
*qname
, bool *match
)
682 /* intern the attribute name as a dom_string so we can
683 * delegate to dom_element_has_attribute() */
684 err
= dom_string_create_interned(
685 (const uint8_t *) lwc_string_data(qname
->name
),
686 lwc_string_length(qname
->name
),
688 if (err
!= DOM_NO_ERR
) {
692 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
693 if (err
!= DOM_NO_ERR
) {
694 dom_string_unref(name
);
698 dom_string_unref(name
);
704 * Test the given node for an attribute with a specific value
706 * This will return true (via the "match" pointer) if the libdom node
707 * has an attribute with the given name and value. The comparison is
708 * case-sensitive. It corresponds to node[attr=value] in CSS.
710 * \param pw Pointer to the current SVG parser state
711 * \param node Libdom SVG node to test
712 * \param qname Attribute name to check for
713 * \param value Attribute value to check for
714 * \param match Pointer to the test result
716 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
717 * intern the attribute name (which usually indicates memory
720 css_error
node_has_attribute_equal(void *pw
, void *node
,
721 const css_qname
*qname
, lwc_string
*value
,
724 /* Implementation note: NetSurf always returns "no match" when
725 * the value is empty (length zero). We allow it, because why
730 dom_string
*attr_val
;
733 /* Intern the attribute name as a dom_string so we can
734 * use dom_element_get_attribute() */
735 err
= dom_string_create_interned(
736 (const uint8_t *) lwc_string_data(qname
->name
),
737 lwc_string_length(qname
->name
),
739 if (err
!= DOM_NO_ERR
) {
743 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
744 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
745 /* There was an error getting the attribute's value or
746 * the attribute doesn't exist. So, no match? */
747 dom_string_unref(name
);
752 /* Otherwise, we have the attribute value from the given node
753 * and all we need to do is compare. */
754 dom_string_unref(name
);
755 *match
= dom_string_lwc_isequal(attr_val
, value
);
756 dom_string_unref(attr_val
);
763 * Test the given node for an attribute with a specific value,
764 * possibly followed by a single hyphen
766 * This will return true (via the "match" pointer) if the libdom node
767 * has an attribute with the given name and value or with the given
768 * name and a value that is followed by exactly one hyphen. The
769 * comparison is case-sensitive. This corresponds to [attr|=value]
772 * \param pw Pointer to the current SVG parser state
773 * \param node Libdom SVG node to test
774 * \param qname Attribute name to check for
775 * \param value Attribute value to check for
776 * \param match Pointer to the test result
778 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
779 * intern the attribute name (which usually indicates memory
782 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
783 const css_qname
*qname
, lwc_string
*value
,
786 /* Implementation note: NetSurf always returns "no match" when
787 * the value is empty (length zero). We allow it, because why
792 dom_string
*attr_val
;
795 const char *vdata
; /* to hold the data underlying "value" */
797 const char *avdata
; /* to hold the found attribute value data */
800 /* Intern the attribute name as a dom_string so we can
801 * use dom_element_get_attribute() */
802 err
= dom_string_create_interned(
803 (const uint8_t *) lwc_string_data(qname
->name
),
804 lwc_string_length(qname
->name
),
806 if (err
!= DOM_NO_ERR
) {
810 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
811 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
812 /* There was an error getting the attribute's value or
813 * the attribute doesn't exist. So, no match? */
814 dom_string_unref(name
);
819 /* Otherwise, we have the attribute value from the given node
820 * and all we need to do is compare. */
821 dom_string_unref(name
);
822 *match
= dom_string_lwc_isequal(attr_val
, value
);
824 /* Exact match, we're done */
825 dom_string_unref(attr_val
);
829 /* No exact match, try it with a hyphen on the end */
830 vdata
= lwc_string_data(value
); /* needle */
831 vdata_len
= lwc_string_length(value
);
832 avdata
= dom_string_data(attr_val
); /* haystack */
833 avdata_len
= dom_string_byte_length(attr_val
);
834 dom_string_unref(attr_val
);
836 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
837 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
838 /* If there's a hyphen in the right position,
839 * it suffices to compare the strings only up
850 * Test the given node for an attribute whose value is a
851 * space-separated list of words, one of which is the given word
853 * This will return true (via the "match" pointer) if the libdom node
854 * has an attribute with the given name and whose value when
855 * considered as a space-separated list of words contains the given
856 * word. The comparison is case-sensitive. This corresponds to
857 * [attr~=value] in CSS.
859 * \param pw Pointer to the current SVG parser state
860 * \param node Libdom SVG node to test
861 * \param qname Attribute name to check for
862 * \param word Value word to check for
863 * \param match Pointer to the test result
865 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
866 * intern the attribute name (which usually indicates memory
869 css_error
node_has_attribute_includes(void *pw
, void *node
,
870 const css_qname
*qname
, lwc_string
*word
,
876 dom_string
*attr_val
;
878 size_t wordlen
; /* length of "word" */
880 /* pointers used to parse a space-separated list of words */
885 *match
= false; /* default to no match */
887 wordlen
= lwc_string_length(word
);
889 /* In this case, the spec says that "if 'val' is the
890 * empty string, it will never represent anything." */
894 /* Intern the attribute name as a dom_string so we can
895 * use dom_element_get_attribute() */
896 err
= dom_string_create_interned(
897 (const uint8_t *) lwc_string_data(qname
->name
),
898 lwc_string_length(qname
->name
),
900 if (err
!= DOM_NO_ERR
) {
904 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
905 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
906 /* There was an error getting the attribute's value or
907 * the attribute doesn't exist. So, no match? */
908 dom_string_unref(name
);
912 /* Parse the list comparing each word against "word" */
913 start
= dom_string_data(attr_val
);
914 end
= start
+ dom_string_byte_length(attr_val
);
915 dom_string_unref(attr_val
);
917 for (p
= start
; p
<= end
; p
++) {
918 /* Move forward until we find the end of the first word */
919 if (*p
== ' ' || *p
== '\0') {
920 /* If the length of that word is the length of the
921 * word we're looking for, do the comparison. */
922 if ((size_t) (p
- start
) == wordlen
&&
924 lwc_string_data(word
),
929 /* No match? Set "start" to the beginning of
930 * the next word and loop. */
940 * Test the given node for an attribute whose value begins with the
943 * This will return true (via the "match" pointer) if the libdom node
944 * has an attribute with the given name and whose value begins with
945 * the given prefix string. The comparison is case-sensitive. This
946 * corresponds to [attr^=value] in CSS.
948 * \param pw Pointer to the current SVG parser state
949 * \param node Libdom SVG node to test
950 * \param qname Attribute name to check for
951 * \param prefix Value prefix to check for
952 * \param match Pointer to the test result
954 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
955 * intern the attribute name (which usually indicates memory
958 css_error
node_has_attribute_prefix(void *pw
, void *node
,
959 const css_qname
*qname
, lwc_string
*prefix
,
964 dom_string
*attr_val
;
966 const char *avdata
; /* attribute value data */
967 size_t avdata_len
; /* length of that attribute value data */
968 size_t prefixlen
; /* length of "prefix" */
970 prefixlen
= lwc_string_length(prefix
);
971 if (prefixlen
== 0) {
972 /* In this case, the spec says that "if 'val' is the
973 * empty string, it will never represent anything." */
977 /* Intern the attribute name as a dom_string so we can
978 * use dom_element_get_attribute() */
979 err
= dom_string_create_interned(
980 (const uint8_t *) lwc_string_data(qname
->name
),
981 lwc_string_length(qname
->name
),
983 if (err
!= DOM_NO_ERR
) {
987 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
988 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
989 /* There was an error getting the attribute's value or
990 * the attribute doesn't exist. So, no match? */
991 dom_string_unref(name
);
996 /* Otherwise, we have the attribute value from the given node,
997 * and the first thing we want to do is check to see if the
998 * whole thing matches the prefix. */
999 dom_string_unref(name
);
1000 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
1002 /* If not, check to see if an, uh, prefix matches the
1004 if (*match
== false) {
1005 avdata
= dom_string_data(attr_val
);
1006 avdata_len
= dom_string_byte_length(attr_val
);
1007 if ((avdata_len
>= prefixlen
) &&
1008 (strncasecmp(avdata
,
1009 lwc_string_data(prefix
),
1011 /* Use strncasecmp to compare only the first
1012 * "n" characters, where "n" is the length of
1018 dom_string_unref(attr_val
);
1025 * Test the given node for an attribute whose value end with the
1028 * This will return true (via the "match" pointer) if the libdom node
1029 * has an attribute with the given name and whose value ends with
1030 * the given suffix string. The comparison is case-sensitive. This
1031 * corresponds to [attr$=value] in CSS.
1033 * \param pw Pointer to the current SVG parser state
1034 * \param node Libdom SVG node to test
1035 * \param qname Attribute name to check for
1036 * \param suffix Value suffix to check for
1037 * \param match Pointer to the test result
1039 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1040 * intern the attribute name (which usually indicates memory
1043 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1044 const css_qname
*qname
, lwc_string
*suffix
,
1049 dom_string
*attr_val
;
1051 const char *avdata
; /* attribute value data */
1052 size_t avdata_len
; /* length of that attribute value data */
1053 size_t suffixlen
; /* length of "suffix" */
1055 /* convenience pointer we'll use when matching the suffix */
1056 const char *suffix_start
;
1058 suffixlen
= lwc_string_length(suffix
);
1059 if (suffixlen
== 0) {
1060 /* In this case, the spec says that "if 'val' is the
1061 * empty string, it will never represent anything." */
1065 /* Intern the attribute name as a dom_string so we can
1066 * use dom_element_get_attribute() */
1067 err
= dom_string_create_interned(
1068 (const uint8_t *) lwc_string_data(qname
->name
),
1069 lwc_string_length(qname
->name
),
1071 if (err
!= DOM_NO_ERR
) {
1075 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1076 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1077 /* There was an error getting the attribute's value or
1078 * the attribute doesn't exist. So, no match? */
1079 dom_string_unref(name
);
1084 /* Otherwise, we have the attribute value from the given node,
1085 * and the first thing we want to do is check to see if the
1086 * whole thing matches the suffix. */
1087 dom_string_unref(name
);
1088 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1090 /* If not, check to see if an, uh, suffix matches the
1092 if (*match
== false) {
1093 avdata
= dom_string_data(attr_val
);
1094 avdata_len
= dom_string_byte_length(attr_val
);
1096 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1098 if ((avdata_len
>= suffixlen
) &&
1099 (strncasecmp(suffix_start
,
1100 lwc_string_data(suffix
),
1102 /* Use strncasecmp to compare only the last
1103 * "n" characters, where "n" is the length of
1109 dom_string_unref(attr_val
);
1116 * Implement node_has_attribute_substring() with optional case-
1117 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1118 * not supported by libcss yet, but it allows us to factor out some
1121 static css_error
_node_has_attribute_substring(void *pw
, void *node
,
1122 const css_qname
*qname
, lwc_string
*substring
,
1123 bool *match
, bool insensitive
)
1127 dom_string
*attr_val
;
1129 size_t attr_len
; /* length of attr_val */
1130 size_t substrlen
; /* length of "substring" */
1132 /* Convenience pointers we use when comparing substrings */
1136 substrlen
= lwc_string_length(substring
);
1137 if (substrlen
== 0) {
1138 /* In this case, the spec says that "if 'val' is the
1139 * empty string, it will never represent anything." */
1143 /* Intern the attribute name as a dom_string so we can
1144 * use dom_element_get_attribute() */
1145 err
= dom_string_create_interned(
1146 (const uint8_t *) lwc_string_data(qname
->name
),
1147 lwc_string_length(qname
->name
),
1149 if (err
!= DOM_NO_ERR
) {
1153 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1154 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1155 /* There was an error getting the attribute's value or
1156 * the attribute doesn't exist. So, no match? */
1157 dom_string_unref(name
);
1162 /* Otherwise, we have the attribute value from the given node,
1163 * and the first thing we want to do is check to see if the
1164 * whole thing matches the substring. */
1165 dom_string_unref(name
);
1168 *match
= dom_string_caseless_lwc_isequal(attr_val
, substring
);
1171 *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
);
1201 * Test the given node for an attribute whose value contains the
1204 * This will return true (via the "match" pointer) if the libdom node
1205 * has an attribute with the given name and whose value contains the
1206 * given substring. The comparison is case-sensitive. This corresponds
1207 * to [attr*=value] in CSS.
1209 * \param pw Pointer to the current SVG parser state
1210 * \param node Libdom SVG node to test
1211 * \param qname Attribute name to check for
1212 * \param substring Value substring to check for
1213 * \param match Pointer to the test result
1215 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1216 * intern the attribute name (which usually indicates memory
1219 css_error
node_has_attribute_substring(void *pw
, void *node
,
1220 const css_qname
*qname
, lwc_string
*substring
,
1223 return _node_has_attribute_substring(pw
, node
, qname
, substring
,
1229 * Test whether or not the given node is the document's root element
1230 * This corresponds to the CSS :root pseudo-selector.
1232 * \param pw Pointer to the current SVG parser state
1233 * \param node Libdom SVG node to test
1234 * \param match Pointer to the test result
1236 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1238 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1245 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1246 if (err
!= DOM_NO_ERR
) {
1250 /* It's the root element if it doesn't have a parent element */
1251 if (parent
!= NULL
) {
1252 err
= dom_node_get_node_type(parent
, &type
);
1253 dom_node_unref(parent
);
1254 if (err
!= DOM_NO_ERR
) {
1257 if (type
!= DOM_DOCUMENT_NODE
) {
1258 /* DOM_DOCUMENT_NODE is the only allowable
1259 * type of parent node for the root element */
1271 * Used internally in node_count_siblings() to "count" the given
1272 * sibling node. It factors out the node type and name checks.
1274 static int node_count_siblings_check(dom_node
*dnode
,
1281 dom_string
*dnode_name
;
1283 /* We flip this to 1 if/when we count this node */
1286 if (dnode
== NULL
) {
1290 exc
= dom_node_get_node_type(dnode
, &type
);
1291 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1292 /* We only count element siblings */
1296 /* ... with the right name */
1299 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1301 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1302 if (dom_string_isequal(name
,
1306 dom_string_unref(dnode_name
);
1317 * Count the given node's sibling elements
1319 * This counts the given node's sibling elements in one direction,
1320 * either forwards or backwards, in the DOM. Keep in mind that the
1321 * libdom tree is upside-down compared to the CSS one; so "next" and
1322 * "previous" are actually reversed; the default is to count preceding
1323 * libdom siblings which correspond to subsequent CSS siblings.
1325 * This operation is central to the CSS :first-child, :nth-child, and
1326 * :last-child (et cetera) pseudo-selectors.
1328 * If same_name is true, then only nodes having the same
1329 * (case-sensitive) name as the given node are counted.
1331 * \param pw Pointer to the current SVG parser state
1332 * \param node Libdom SVG node whose siblings we're counting
1333 * \param same_name Whether or not to count only siblings having
1334 * the same name as the given node
1335 * \param after Count subsequent siblings rather than precedent
1336 * ones (the default)
1337 * \param count Pointer to the return value, the number of sibling
1340 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1342 css_error
node_count_siblings(void *pw
, void *node
,
1343 bool same_name
, bool after
, int32_t *count
)
1347 dom_node
*dnode
; /* node, but with the right type */
1348 dom_string
*dnode_name
;
1349 dom_node
*next
; /* "next" sibling (depends on direction) */
1351 /* Pointer to the "next sibling" function */
1352 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1357 dnode
= (dom_node
*)node
;
1359 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1360 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1365 /* Increment the reference counter for dnode for as long as
1366 * we retain a reference to it. */
1367 dnode
= dom_node_ref(dnode
);
1369 next_func
= dom_node_get_previous_sibling
;
1371 next_func
= dom_node_get_next_sibling
;
1375 exc
= next_func(dnode
, &next
);
1376 if (exc
!= DOM_NO_ERR
) {
1380 /* If next_func worked, we're about to swap "next"
1381 * with "dnode" meaning that we will no longer retain
1382 * a reference to the current dnode. */
1383 dom_node_unref(dnode
);
1386 *count
+= node_count_siblings_check(dnode
,
1389 } while (dnode
!= NULL
);
1391 if (dnode_name
!= NULL
) {
1392 dom_string_unref(dnode_name
);
1400 * Determine whether or not the given element is empty
1402 * An element is "nonempty" if it has a child that is either an
1403 * element node or a text node.
1405 * \param pw Pointer to the current SVG parser state
1406 * \param node Libdom SVG node to check for emptiness
1407 * \param is_empty Pointer to the return value
1409 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1411 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1414 dom_node
*child
; /* current child node pointer */
1415 dom_node
*next
; /* next child node pointer */
1416 dom_node_type type
; /* what type of node is "child" */
1419 /* Assume that it's empty by default */
1422 /* Get the given node's first child. Implementation detail:
1423 * this increments the reference counter on the child node. */
1424 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1425 if (err
!= DOM_NO_ERR
) {
1429 /* And now loop through all children looking for a
1430 * text/element node. If we find one, the original
1431 * node is "nonempty" */
1432 while (child
!= NULL
) {
1433 err
= dom_node_get_node_type(child
, &type
);
1434 if (err
!= DOM_NO_ERR
) {
1435 dom_node_unref(child
);
1439 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1441 dom_node_unref(child
);
1445 err
= dom_node_get_next_sibling(child
, &next
);
1446 if (err
!= DOM_NO_ERR
) {
1447 dom_node_unref(child
);
1451 /* If we're moving to the next node, we can release
1452 * the reference to the current one */
1453 dom_node_unref(child
);
1462 * Determine whether or not the given node is a link
1464 * A node is a link if it is an element node whose name is "a" and if
1465 * it has an "href" attribute (case-sensitive). This selector
1466 * corresponds to node:link pseudo-class in CSS.
1468 * This pseudo-class is a bit awkward because the two standards (HTML5
1469 * and CSS) disagree on what it means, and because libsvgtiny does not
1470 * have enough information to determine if a link has been "visited"
1471 * yet -- that's a UI property. CSS says that :link is for unvisited
1472 * links, which we can't determine. HTML5 says that each link must
1473 * be either a :link or :visited. Since we can't decide either way,
1474 * It seems less wrong to declare that all links are unvisited; i.e.
1475 * that they match :link.
1477 * \param pw Pointer to the current SVG parser state
1478 * \param node Libdom SVG node to check
1479 * \param is_link Pointer to the boolean return value
1481 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1483 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1486 dom_node
*dnode
; /* node, but with the right type */
1487 dom_string
*dnode_name
;
1489 struct svgtiny_parse_state
* state
;
1491 dnode
= (dom_node
*)node
;
1493 has_href
= false; /* assume no href attribute */
1494 *is_link
= false; /* assume that it's not a link */
1496 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1497 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1501 state
= (struct svgtiny_parse_state
*)pw
;
1502 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1503 exc
= dom_element_has_attribute(node
,
1504 state
->interned_href
,
1506 if (exc
== DOM_NO_ERR
&& has_href
) {
1511 dom_string_unref(dnode_name
);
1517 * Check if the given node is being "hovered" over
1519 * This check always fails because the SVG DOM does not have the
1520 * necessary information (it's a UI property).
1522 * \param pw Pointer to the current SVG parser state; unused
1523 * \param node Libdom SVG node to check; unused
1524 * \param is_hover Pointer to the boolean return value
1526 * \return Always returns CSS_OK
1528 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1538 * Check if the given node is "active"
1540 * This check always fails because the SVG DOM does not have the
1541 * necessary information (it's a UI property).
1543 * \param pw Pointer to the current SVG parser state; unused
1544 * \param node Libdom SVG node to check; unused
1545 * \param is_active Pointer to the boolean return value
1547 * \return Always returns CSS_OK
1549 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1559 * Check if the given node has the focus
1561 * This check always fails because the SVG DOM does not have the
1562 * necessary information (it's a UI property).
1564 * \param pw Pointer to the current SVG parser state; unused
1565 * \param node Libdom SVG node to check; unused
1566 * \param is_focus Pointer to the boolean return value
1568 * \return Always returns CSS_OK
1570 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1580 * Check if the given node is enabled
1582 * This check always fails because the SVG DOM does not have the
1583 * necessary information (it's a UI property).
1585 * \param pw Pointer to the current SVG parser state; unused
1586 * \param node Libdom SVG node to check; unused
1587 * \param is_enabled Pointer to the boolean return value
1589 * \return Always returns CSS_OK
1591 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1595 *is_enabled
= false;
1601 * Check if the given node is disabled
1603 * This check always fails because the SVG DOM does not have the
1604 * necessary information (it's a UI property). Beware, until they are
1605 * implemented, this is NOT the logical negation of node_is_enabled!
1607 * \param pw Pointer to the current SVG parser state; unused
1608 * \param node Libdom SVG node to check; unused
1609 * \param is_disabled Pointer to the boolean return value
1611 * \return Always returns CSS_OK
1613 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1617 *is_disabled
= false;
1623 * Test whether or not the given node is "checked"
1625 * This test always fails because the SVG DOM does not have the
1626 * necessary information (it's a UI property).
1628 * \param pw Pointer to the current SVG parser state; unused
1629 * \param node Libdom SVG node to check; unused
1630 * \param is_checked Pointer to the boolean return value
1632 * \return Always returns CSS_OK
1634 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1638 *is_checked
= false;
1644 * Check if the given node is the "target" of the document URL
1646 * This test always fails because the SVG DOM does not have the
1647 * necessary information (it's a UI property).
1649 * \param pw Pointer to the current SVG parser state; unused
1650 * \param node Libdom SVG node to check; unused
1651 * \param is_target Pointer to the boolean return value
1653 * \return Always returns CSS_OK
1655 css_error
node_is_target(void *pw
, void *node
, bool *is_target
)
1665 * Check if the given node is the given language
1667 * This test is corresponds to the CSS :lang() selector and is not
1668 * fully implemented yet: it looks only for "lang" attributes on the
1669 * given element and its parents, and performs a simple substring
1670 * check. This results in a partial implementation of CSS Level 3 for
1671 * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
1672 * favor of the "lang" attribute that is defined only in SVG 2.0.
1674 * \param pw Pointer to the current SVG parser state; unused
1675 * \param node Libdom SVG node to check
1676 * \param lang The language to match
1677 * \param is_lang Pointer to the boolean return value
1679 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1681 static css_error
node_is_lang(void *pw
, void *node
,
1682 lwc_string
*lang
, bool *is_lang
)
1685 /* SVG2 elements support both "lang" and "xml:lang"
1686 * attributes; earlier versions have only the XML
1687 * attribute. It would not be too hard to add support for
1688 * xml:lang" here. The main difficulty standing in the way of
1689 * a full Level 4 implementation is the complexity of the
1692 * https://www.w3.org/TR/selectors-4/#the-lang-pseudo
1697 dom_exception d_err
;
1698 dom_node
*n
; /* current node */
1699 dom_node
*p
; /* parent node */
1700 bool match
; /* retval from node_has_attribute_substring() */
1702 /* Define the attribute name "lang" that we're looking for.
1703 * We only use a css_qname here because that's what the
1704 * node_has_attribute_substring() takes; the namespace
1705 * portion of it is irrelevant. */
1709 if (lwc_intern_string("lang", 4, &attr
.name
) != lwc_error_ok
) {
1713 *is_lang
= false; /* default to no match */
1714 n
= (dom_node
*)node
;
1716 /* Loop through all parents of the given node looking for a
1717 * substring match */
1719 c_err
= _node_has_attribute_substring(pw
, (void *)n
, &attr
,
1720 lang
, &match
, true);
1721 if (c_err
!= CSS_OK
) {
1722 lwc_string_destroy(attr
.name
);
1726 /* matched this element; we're done */
1727 lwc_string_destroy(attr
.name
);
1732 /* no match on this element, try its parent */
1733 d_err
= dom_node_get_parent_node(n
, &p
);
1734 if (d_err
!= DOM_NO_ERR
) {
1735 lwc_string_destroy(attr
.name
);
1741 /* If we never find a match we may wind up here */
1742 lwc_string_destroy(attr
.name
);