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_visited(void *pw
, void *node
, bool *is_visited
);
51 static css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
);
52 static css_error
node_is_active(void *pw
, void *node
, bool *is_active
);
53 static css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
);
54 static css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
);
55 static css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
);
56 static css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
);
57 static css_error
node_is_target(void *pw
, void *node
, bool *is_target
);
58 static css_error
node_is_lang(void *pw
, void *node
,
59 lwc_string
*lang
, bool *is_lang
);
60 static css_error
ua_default_for_property(void *pw
, uint32_t property
,
65 * Resolve a relative URL to an absolute one by doing nothing. This is
66 * the simplest possible implementation of a URL resolver, needed for
69 css_error
svgtiny_resolve_url(void *pw
,
70 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
75 /* Copy the relative URL to the absolute one (the return
77 *abs
= lwc_string_ref(rel
);
82 * Create a stylesheet with the default set of params.
84 * \param sheet A stylesheet pointer, passed in by reference, that
85 * we use to store the newly-created stylesheet.
86 * \param inline_style True if this stylesheet represents an inline
87 * style, and false otherwise.
89 * \return The return value from css_stylesheet_create() is returned.
91 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
94 css_stylesheet_params params
;
96 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
97 params
.level
= CSS_LEVEL_DEFAULT
;
98 params
.charset
= NULL
;
101 params
.allow_quirks
= false;
102 params
.inline_style
= inline_style
;
103 params
.resolve
= svgtiny_resolve_url
;
104 params
.resolve_pw
= NULL
;
105 params
.import = NULL
;
106 params
.import_pw
= NULL
;
108 params
.color_pw
= NULL
;
110 params
.font_pw
= NULL
;
112 return css_stylesheet_create(¶ms
, sheet
);
116 /**************************/
117 /* libcss select handlers */
118 /**************************/
120 * From here on we implement the "select handler "API defined in
121 * libcss's include/libcss/select.h and discussed briefly in its
127 * Retrieve the given node's name
129 * \param pw Pointer to the current SVG parser state
130 * \param node Libdom SVG node
131 * \param qname Address at which to store the node name
133 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
135 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
139 struct svgtiny_parse_state
*state
;
141 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
142 if (err
!= DOM_NO_ERR
) {
146 state
= (struct svgtiny_parse_state
*)pw
;
147 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
149 err
= dom_string_intern(name
, &qname
->name
);
150 if (err
!= DOM_NO_ERR
) {
151 dom_string_unref(name
);
155 dom_string_unref(name
);
162 * Retrieve the given node's classes
164 * \param pw Pointer to the current SVG parser state
165 * \param node Libdom SVG node
166 * \param classes Address at which to store the class name array
167 * \param n_classes Address at which to store the length of the class
170 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
172 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
173 * because the underlying libdom function never fails
175 css_error
node_classes(void *pw
, void *node
,
176 lwc_string
***classes
, uint32_t *n_classes
)
181 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
183 /* The implementation does not do it, but the documentation
184 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
185 possible here, so we handle it to be on the safe side. */
186 if (err
!= DOM_NO_ERR
) {
195 * Retrieve the given node's id
197 * \param pw Pointer to the current SVG parser state
198 * \param node Libdom SVG node
199 * \param id Address at which to store the id
201 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
203 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
207 struct svgtiny_parse_state
*state
;
209 /* Begin with the assumption that this node has no id */
212 state
= (struct svgtiny_parse_state
*)pw
;
213 err
= dom_element_get_attribute((dom_node
*)node
,
214 state
->interned_id
, &attr
);
215 if (err
!= DOM_NO_ERR
) {
218 else if (attr
== NULL
) {
219 /* The node has no id attribute and our return value
220 is already set to NULL so we're done */
224 /* If we found an id attribute (a dom_string), intern it into
225 an lwc_string that we can return, and then cleanup the
227 err
= dom_string_intern(attr
, id
);
228 if (err
!= DOM_NO_ERR
) {
229 dom_string_unref(attr
);
232 dom_string_unref(attr
);
239 * Find the first parent of the given element having the given name
241 * \param pw Pointer to the current SVG parser state
242 * \param node Libdom SVG node
243 * \param qname Name of the parent node to search for
244 * \param parent Address at which to store the parent node pointer
246 * \return Always returns CSS_OK
248 * \post If a suitable element is found, a pointer to it will be
249 * stored at the address pointed to by \a parent; otherwise,
250 * NULL will be stored at the address pointed to by \a parent
252 css_error
named_parent_node(void *pw
, void *node
,
253 const css_qname
*qname
, void **parent
)
256 /* dom_element_named_parent_node() was invented to implement
257 * this select handler so there isn't much for us to do except
258 * call it. It's OK if node isn't an element, libdom checks
260 dom_element_named_parent_node((dom_element
*)node
,
262 (struct dom_element
**)parent
);
264 /* Implementation detail: dom_element_named_parent_node()
265 * increments the reference count of the parent element before
266 * returning it to us. According to docs/RefCnt in the libdom
267 * repository, this will prevent the parent element from being
268 * destroyed if it is pruned from the DOM. That sounds good,
269 * since we don't want to be using a pointer to an object that
270 * has been destroyed... but we also have no way of later
271 * decrementing the reference count ourselves, and don't want
272 * to make the returned node eternal. Decrementing the
273 * reference counter now allows it to be destroyed when the
274 * DOM no longer needs it, and so long as no other parts of
275 * libsvgtiny are messing with the DOM during parsing, that
276 * shouldn't (ha ha) cause any problems. */
277 dom_node_unref(*parent
);
284 * Find the "next-sibling" of the given element having the given name
286 * This search corresponds to the "+ foo" combinator in CSS and will
287 * find only "foo" element nodes that immediately precede the given
288 * node under the same parent in the DOM. In CSS the tree is viewed
289 * top-down and in libdom it is viewed from the bottom-up; as a result
290 * "next" and "previous" are sometimes backwards. This is case-sensitive.
292 * \param pw Pointer to the current SVG parser state
293 * \param node Libdom SVG node
294 * \param qname Name of the sibling node to search for
295 * \param sibling Address at which to store the sibling node pointer
297 * \return Always returns CSS_OK
299 * \post If a suitable element is found, a pointer to it will be
300 * stored at the address pointed to by \a sibling; otherwise,
301 * NULL will be stored at the address pointed to by \a sibling
303 css_error
named_sibling_node(void *pw
, void *node
,
304 const css_qname
*qname
, void **sibling
)
307 dom_node
*n
= node
; /* the current node */
308 dom_node
*prev
; /* the previous node */
313 *sibling
= NULL
; /* default to nothing found */
315 /* Begin the search; the first iteration we do outside of the
316 * loop. Implementation detil: dom_node_get_previous_sibling()
317 * increments the reference counter on the returned node. A
318 * comment within named_parent_node() explains why we
319 * decrement it ASAP. */
320 err
= dom_node_get_previous_sibling(n
, &n
);
321 if (err
!= DOM_NO_ERR
) {
326 /* We're looking for the first ELEMENT sibling */
327 err
= dom_node_get_node_type(n
, &type
);
328 if (err
!= DOM_NO_ERR
) {
333 if (type
== DOM_ELEMENT_NODE
) {
334 /* We found an element node, does it have the
336 err
= dom_node_get_node_name(n
, &name
);
337 if (err
!= DOM_NO_ERR
) {
342 if (dom_string_lwc_isequal(name
,
344 /* The name is right, return it */
348 /* There's only one next-sibling element node
349 * and we've already found it, so if its name
350 * wasn't right, we return the default value
352 dom_string_unref(name
);
357 /* Not an element node, so we move on the the previous
358 * previous sibling */
359 err
= dom_node_get_previous_sibling(n
, &prev
);
360 if (err
!= DOM_NO_ERR
) {
374 * Find the first "subsequent-sibling" of the given element having the
377 * This search corresponds to the "~ foo" combinator in CSS and will
378 * find only "foo" element nodes that precede the given node (under
379 * the same parent) in the DOM. In CSS the tree is viewed top-down and
380 * in libdom it is viewed from the bottom-up; as a result "next" and
381 * "previous" are sometimes backwards. This is case-sensitive.
383 * \param pw Pointer to the current SVG parser state
384 * \param node Libdom SVG node
385 * \param qname Name of the sibling node to search for
386 * \param sibling Address at which to store the sibling node pointer
388 * \return Always returns CSS_OK
390 * \post If a suitable element is found, a pointer to it will be
391 * stored at the address pointed to by \a sibling; otherwise,
392 * NULL will be stored at the address pointed to by \a sibling
394 css_error
named_generic_sibling_node(void *pw
, void *node
,
395 const css_qname
*qname
, void **sibling
)
398 dom_node
*n
= node
; /* the current node */
399 dom_node
*prev
; /* the previous node */
405 *sibling
= NULL
; /* default to nothing found */
407 /* Begin the search; the first iteration we do outside of the
408 * loop. Implementation detil: dom_node_get_previous_sibling()
409 * increments the reference counter on the returned node. A
410 * comment within named_parent_node() explains why we
411 * decrement it ASAP. */
412 err
= dom_node_get_previous_sibling(n
, &n
);
413 if (err
!= DOM_NO_ERR
) {
418 err
= dom_node_get_node_type(n
, &type
);
419 if (err
!= DOM_NO_ERR
) {
424 if (type
== DOM_ELEMENT_NODE
) {
425 /* We only want ELEMENT nodes */
426 err
= dom_node_get_node_name(n
, &name
);
427 if (err
!= DOM_NO_ERR
) {
432 if (dom_string_lwc_isequal(name
,
434 /* Found one. Save it and stop the search */
435 dom_string_unref(name
);
441 dom_string_unref(name
);
444 /* This sibling wasn't an element with the desired
445 name, so move on to the previous sibling */
446 err
= dom_node_get_previous_sibling(n
, &prev
);
447 if (err
!= DOM_NO_ERR
) {
461 * Return a pointer to the given node's parent
463 * \param pw Pointer to the current SVG parser state
464 * \param node Libdom SVG node
465 * \param parent Address at which to store the node's parent pointer
467 * \return Always returns CSS_OK
469 css_error
parent_node(void *pw
, void *node
, void **parent
)
472 /* Libdom basically implements this for us */
473 dom_element_parent_node(node
, (struct dom_element
**)parent
);
475 /* See the comment in named_parent_node() for why we decrement
476 * this reference counter here. */
477 dom_node_unref(*parent
);
484 * Find the "next-sibling" of the given element
486 * This search corresponds "+ *" in CSS and will find the first
487 * element node that immediately precedes the given node under the
488 * same parent in the DOM. In CSS the tree is viewed top-down and in
489 * libdom it is viewed from the bottom-up; as a result "next" and
490 * "previous" are sometimes backwards.
492 * \param pw Pointer to the current SVG parser state
493 * \param node Libdom SVG node
494 * \param sibling Address at which to store the sibling node pointer
496 * \return Always returns CSS_OK
498 * \post If a suitable element is found, a pointer to it will be
499 * stored at the address pointed to by \a sibling; otherwise,
500 * NULL will be stored at the address pointed to by \a sibling
502 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
505 dom_node
*n
= node
; /* the current node */
506 dom_node
*prev
; /* the previous node */
510 *sibling
= NULL
; /* default to nothing found */
512 /* Begin the search; the first iteration we do outside of the
513 * loop. Implementation detil: dom_node_get_previous_sibling()
514 * increments the reference counter on the returned node. A
515 * comment within named_parent_node() explains why we
516 * decrement it ASAP. */
517 err
= dom_node_get_previous_sibling(n
, &n
);
518 if (err
!= DOM_NO_ERR
) {
523 err
= dom_node_get_node_type(n
, &type
);
524 if (err
!= DOM_NO_ERR
) {
529 if (type
== DOM_ELEMENT_NODE
) {
530 /* We found a sibling node that is also an
531 element and that's all we wanted. */
537 /* This sibling node was not an element; move on to
538 the previous sibling */
539 err
= dom_node_get_previous_sibling(n
, &prev
);
540 if (err
!= DOM_NO_ERR
) {
554 * Test the given node for the given name
556 * This will return true (via the "match" pointer) if the libdom node
557 * has the given name or if that name is the universal selector;
558 * otherwise it returns false. The comparison is case-sensitive. It
559 * corresponds to a rule like "body { ... }" in CSS.
561 * \param pw Pointer to the current SVG parser state
562 * \param node Libdom SVG node to test
563 * \param qname Name to check for
564 * \param match Pointer to the test result
566 * \return Always returns CSS_OK
568 css_error
node_has_name(void *pw
, void *node
,
569 const css_qname
*qname
, bool *match
)
571 struct svgtiny_parse_state
*state
;
575 /* Start by checking to see if qname is the universal selector */
576 state
= (struct svgtiny_parse_state
*)pw
;
577 if (lwc_string_isequal(qname
->name
,
578 state
->interned_universal
, match
) == lwc_error_ok
) {
580 /* It's the universal selector. In NetSurf, all node
581 * names match the universal selector, and nothing in
582 * the libcss documentation suggests another approach,
583 * so we follow NetSurf here. */
588 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
589 if (err
!= DOM_NO_ERR
) {
593 /* Unlike with HTML, SVG element names are case-sensitive */
594 *match
= dom_string_lwc_isequal(name
, qname
->name
);
595 dom_string_unref(name
);
602 * Test the given node for the given class
604 * This will return true (via the "match" pointer) if the libdom node
605 * has the given class. The comparison is case-sensitive. It
606 * corresponds to node.class in CSS.
608 * \param pw Pointer to the current SVG parser state
609 * \param node Libdom SVG node to test
610 * \param name Class name to check for
611 * \param match Pointer to the test result
613 * \return Always returns CSS_OK
615 css_error
node_has_class(void *pw
, void *node
,
616 lwc_string
*name
, bool *match
)
619 /* libdom implements this for us and apparently it cannot fail */
620 dom_element_has_class((dom_node
*)node
, name
, match
);
626 * Test the given node for the given id
628 * This will return true (via the "match" pointer) if the libdom node
629 * has the given id. The comparison is case-sensitive. It corresponds
632 * \param pw Pointer to the current SVG parser state
633 * \param node Libdom SVG node to test
634 * \param name Id to check for
635 * \param match Pointer to the test result
637 * \return Always returns CSS_OK
639 css_error
node_has_id(void *pw
, void *node
,
640 lwc_string
*name
, bool *match
)
644 struct svgtiny_parse_state
*state
;
646 attr
= NULL
; /* a priori the "id" attribute may not exist */
647 *match
= false; /* default to no match */
649 state
= (struct svgtiny_parse_state
*)pw
;
650 err
= dom_element_get_attribute((dom_node
*)node
,
651 state
->interned_id
, &attr
);
652 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
656 *match
= dom_string_lwc_isequal(attr
, name
);
657 dom_string_unref(attr
);
664 * Test the given node for the given attribute
666 * This will return true (via the "match" pointer) if the libdom node
667 * has an attribute with the given name. The comparison is
668 * case-sensitive. It corresponds to node[attr] in CSS.
670 * \param pw Pointer to the current SVG parser state
671 * \param node Libdom SVG node to test
672 * \param qname Attribute name to check for
673 * \param match Pointer to the test result
675 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
678 css_error
node_has_attribute(void *pw
, void *node
,
679 const css_qname
*qname
, bool *match
)
685 /* intern the attribute name as a dom_string so we can
686 * delegate to dom_element_has_attribute() */
687 err
= dom_string_create_interned(
688 (const uint8_t *) lwc_string_data(qname
->name
),
689 lwc_string_length(qname
->name
),
691 if (err
!= DOM_NO_ERR
) {
695 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
696 if (err
!= DOM_NO_ERR
) {
697 dom_string_unref(name
);
701 dom_string_unref(name
);
707 * Test the given node for an attribute with a specific value
709 * This will return true (via the "match" pointer) if the libdom node
710 * has an attribute with the given name and value. The comparison is
711 * case-sensitive. It corresponds to node[attr=value] in CSS.
713 * \param pw Pointer to the current SVG parser state
714 * \param node Libdom SVG node to test
715 * \param qname Attribute name to check for
716 * \param value Attribute value to check for
717 * \param match Pointer to the test result
719 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
720 * intern the attribute name (which usually indicates memory
723 css_error
node_has_attribute_equal(void *pw
, void *node
,
724 const css_qname
*qname
, lwc_string
*value
,
727 /* Implementation note: NetSurf always returns "no match" when
728 * the value is empty (length zero). We allow it, because why
733 dom_string
*attr_val
;
736 /* Intern the attribute name as a dom_string so we can
737 * use dom_element_get_attribute() */
738 err
= dom_string_create_interned(
739 (const uint8_t *) lwc_string_data(qname
->name
),
740 lwc_string_length(qname
->name
),
742 if (err
!= DOM_NO_ERR
) {
746 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
747 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
748 /* There was an error getting the attribute's value or
749 * the attribute doesn't exist. So, no match? */
750 dom_string_unref(name
);
755 /* Otherwise, we have the attribute value from the given node
756 * and all we need to do is compare. */
757 dom_string_unref(name
);
758 *match
= dom_string_lwc_isequal(attr_val
, value
);
759 dom_string_unref(attr_val
);
766 * Test the given node for an attribute with a specific value,
767 * possibly followed by a single hyphen
769 * This will return true (via the "match" pointer) if the libdom node
770 * has an attribute with the given name and value or with the given
771 * name and a value that is followed by exactly one hyphen. The
772 * comparison is case-sensitive. This corresponds to [attr|=value]
775 * \param pw Pointer to the current SVG parser state
776 * \param node Libdom SVG node to test
777 * \param qname Attribute name to check for
778 * \param value Attribute value to check for
779 * \param match Pointer to the test result
781 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
782 * intern the attribute name (which usually indicates memory
785 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
786 const css_qname
*qname
, lwc_string
*value
,
789 /* Implementation note: NetSurf always returns "no match" when
790 * the value is empty (length zero). We allow it, because why
795 dom_string
*attr_val
;
798 const char *vdata
; /* to hold the data underlying "value" */
800 const char *avdata
; /* to hold the found attribute value data */
803 /* Intern the attribute name as a dom_string so we can
804 * use dom_element_get_attribute() */
805 err
= dom_string_create_interned(
806 (const uint8_t *) lwc_string_data(qname
->name
),
807 lwc_string_length(qname
->name
),
809 if (err
!= DOM_NO_ERR
) {
813 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
814 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
815 /* There was an error getting the attribute's value or
816 * the attribute doesn't exist. So, no match? */
817 dom_string_unref(name
);
822 /* Otherwise, we have the attribute value from the given node
823 * and all we need to do is compare. */
824 dom_string_unref(name
);
825 *match
= dom_string_lwc_isequal(attr_val
, value
);
827 /* Exact match, we're done */
828 dom_string_unref(attr_val
);
832 /* No exact match, try it with a hyphen on the end */
833 vdata
= lwc_string_data(value
); /* needle */
834 vdata_len
= lwc_string_length(value
);
835 avdata
= dom_string_data(attr_val
); /* haystack */
836 avdata_len
= dom_string_byte_length(attr_val
);
837 dom_string_unref(attr_val
);
839 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
840 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
841 /* If there's a hyphen in the right position,
842 * it suffices to compare the strings only up
853 * Test the given node for an attribute whose value is a
854 * space-separated list of words, one of which is the given word
856 * This will return true (via the "match" pointer) if the libdom node
857 * has an attribute with the given name and whose value when
858 * considered as a space-separated list of words contains the given
859 * word. The comparison is case-sensitive. This corresponds to
860 * [attr~=value] in CSS.
862 * \param pw Pointer to the current SVG parser state
863 * \param node Libdom SVG node to test
864 * \param qname Attribute name to check for
865 * \param word Value word to check for
866 * \param match Pointer to the test result
868 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
869 * intern the attribute name (which usually indicates memory
872 css_error
node_has_attribute_includes(void *pw
, void *node
,
873 const css_qname
*qname
, lwc_string
*word
,
879 dom_string
*attr_val
;
881 size_t wordlen
; /* length of "word" */
883 /* pointers used to parse a space-separated list of words */
888 *match
= false; /* default to no match */
890 wordlen
= lwc_string_length(word
);
892 /* In this case, the spec says that "if 'val' is the
893 * empty string, it will never represent anything." */
897 /* Intern the attribute name as a dom_string so we can
898 * use dom_element_get_attribute() */
899 err
= dom_string_create_interned(
900 (const uint8_t *) lwc_string_data(qname
->name
),
901 lwc_string_length(qname
->name
),
903 if (err
!= DOM_NO_ERR
) {
907 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
908 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
909 /* There was an error getting the attribute's value or
910 * the attribute doesn't exist. So, no match? */
911 dom_string_unref(name
);
915 /* Parse the list comparing each word against "word" */
916 start
= dom_string_data(attr_val
);
917 end
= start
+ dom_string_byte_length(attr_val
);
918 dom_string_unref(attr_val
);
920 for (p
= start
; p
<= end
; p
++) {
921 /* Move forward until we find the end of the first word */
922 if (*p
== ' ' || *p
== '\0') {
923 /* If the length of that word is the length of the
924 * word we're looking for, do the comparison. */
925 if ((size_t) (p
- start
) == wordlen
&&
927 lwc_string_data(word
),
932 /* No match? Set "start" to the beginning of
933 * the next word and loop. */
943 * Test the given node for an attribute whose value begins with the
946 * This will return true (via the "match" pointer) if the libdom node
947 * has an attribute with the given name and whose value begins with
948 * the given prefix string. The comparison is case-sensitive. This
949 * corresponds to [attr^=value] in CSS.
951 * \param pw Pointer to the current SVG parser state
952 * \param node Libdom SVG node to test
953 * \param qname Attribute name to check for
954 * \param prefix Value prefix to check for
955 * \param match Pointer to the test result
957 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
958 * intern the attribute name (which usually indicates memory
961 css_error
node_has_attribute_prefix(void *pw
, void *node
,
962 const css_qname
*qname
, lwc_string
*prefix
,
967 dom_string
*attr_val
;
969 const char *avdata
; /* attribute value data */
970 size_t avdata_len
; /* length of that attribute value data */
971 size_t prefixlen
; /* length of "prefix" */
973 prefixlen
= lwc_string_length(prefix
);
974 if (prefixlen
== 0) {
975 /* In this case, the spec says that "if 'val' is the
976 * empty string, it will never represent anything." */
980 /* Intern the attribute name as a dom_string so we can
981 * use dom_element_get_attribute() */
982 err
= dom_string_create_interned(
983 (const uint8_t *) lwc_string_data(qname
->name
),
984 lwc_string_length(qname
->name
),
986 if (err
!= DOM_NO_ERR
) {
990 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
991 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
992 /* There was an error getting the attribute's value or
993 * the attribute doesn't exist. So, no match? */
994 dom_string_unref(name
);
999 /* Otherwise, we have the attribute value from the given node,
1000 * and the first thing we want to do is check to see if the
1001 * whole thing matches the prefix. */
1002 dom_string_unref(name
);
1003 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
1005 /* If not, check to see if an, uh, prefix matches the
1007 if (*match
== false) {
1008 avdata
= dom_string_data(attr_val
);
1009 avdata_len
= dom_string_byte_length(attr_val
);
1010 if ((avdata_len
>= prefixlen
) &&
1011 (strncasecmp(avdata
,
1012 lwc_string_data(prefix
),
1014 /* Use strncasecmp to compare only the first
1015 * "n" characters, where "n" is the length of
1021 dom_string_unref(attr_val
);
1028 * Test the given node for an attribute whose value end with the
1031 * This will return true (via the "match" pointer) if the libdom node
1032 * has an attribute with the given name and whose value ends with
1033 * the given suffix string. The comparison is case-sensitive. This
1034 * corresponds to [attr$=value] in CSS.
1036 * \param pw Pointer to the current SVG parser state
1037 * \param node Libdom SVG node to test
1038 * \param qname Attribute name to check for
1039 * \param suffix Value suffix to check for
1040 * \param match Pointer to the test result
1042 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1043 * intern the attribute name (which usually indicates memory
1046 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1047 const css_qname
*qname
, lwc_string
*suffix
,
1052 dom_string
*attr_val
;
1054 const char *avdata
; /* attribute value data */
1055 size_t avdata_len
; /* length of that attribute value data */
1056 size_t suffixlen
; /* length of "suffix" */
1058 /* convenience pointer we'll use when matching the suffix */
1059 const char *suffix_start
;
1061 suffixlen
= lwc_string_length(suffix
);
1062 if (suffixlen
== 0) {
1063 /* In this case, the spec says that "if 'val' is the
1064 * empty string, it will never represent anything." */
1068 /* Intern the attribute name as a dom_string so we can
1069 * use dom_element_get_attribute() */
1070 err
= dom_string_create_interned(
1071 (const uint8_t *) lwc_string_data(qname
->name
),
1072 lwc_string_length(qname
->name
),
1074 if (err
!= DOM_NO_ERR
) {
1078 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1079 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1080 /* There was an error getting the attribute's value or
1081 * the attribute doesn't exist. So, no match? */
1082 dom_string_unref(name
);
1087 /* Otherwise, we have the attribute value from the given node,
1088 * and the first thing we want to do is check to see if the
1089 * whole thing matches the suffix. */
1090 dom_string_unref(name
);
1091 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1093 /* If not, check to see if an, uh, suffix matches the
1095 if (*match
== false) {
1096 avdata
= dom_string_data(attr_val
);
1097 avdata_len
= dom_string_byte_length(attr_val
);
1099 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1101 if ((avdata_len
>= suffixlen
) &&
1102 (strncasecmp(suffix_start
,
1103 lwc_string_data(suffix
),
1105 /* Use strncasecmp to compare only the last
1106 * "n" characters, where "n" is the length of
1112 dom_string_unref(attr_val
);
1119 * Implement node_has_attribute_substring() with optional case-
1120 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1121 * not supported by libcss yet, but it allows us to factor out some
1124 static css_error
_node_has_attribute_substring(void *pw
, void *node
,
1125 const css_qname
*qname
, lwc_string
*substring
,
1126 bool *match
, bool insensitive
)
1130 dom_string
*attr_val
;
1132 size_t attr_len
; /* length of attr_val */
1133 size_t substrlen
; /* length of "substring" */
1135 /* Convenience pointers we use when comparing substrings */
1139 substrlen
= lwc_string_length(substring
);
1140 if (substrlen
== 0) {
1141 /* In this case, the spec says that "if 'val' is the
1142 * empty string, it will never represent anything." */
1146 /* Intern the attribute name as a dom_string so we can
1147 * use dom_element_get_attribute() */
1148 err
= dom_string_create_interned(
1149 (const uint8_t *) lwc_string_data(qname
->name
),
1150 lwc_string_length(qname
->name
),
1152 if (err
!= DOM_NO_ERR
) {
1156 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1157 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1158 /* There was an error getting the attribute's value or
1159 * the attribute doesn't exist. So, no match? */
1160 dom_string_unref(name
);
1165 /* Otherwise, we have the attribute value from the given node,
1166 * and the first thing we want to do is check to see if the
1167 * whole thing matches the substring. */
1168 dom_string_unref(name
);
1171 *match
= dom_string_caseless_lwc_isequal(attr_val
, substring
);
1174 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1177 /* If not, check to see if an, uh, substring matches the
1179 if (*match
== false) {
1180 p
= dom_string_data(attr_val
);
1182 /* Check every long-enough suffix for a prefix match */
1183 attr_len
= dom_string_byte_length(attr_val
);
1184 if (attr_len
>= substrlen
) {
1185 p_max
= p
+ attr_len
- substrlen
;
1186 while (p
<= p_max
) {
1188 lwc_string_data(substring
),
1198 dom_string_unref(attr_val
);
1204 * Test the given node for an attribute whose value contains the
1207 * This will return true (via the "match" pointer) if the libdom node
1208 * has an attribute with the given name and whose value contains the
1209 * given substring. The comparison is case-sensitive. This corresponds
1210 * to [attr*=value] in CSS.
1212 * \param pw Pointer to the current SVG parser state
1213 * \param node Libdom SVG node to test
1214 * \param qname Attribute name to check for
1215 * \param substring Value substring to check for
1216 * \param match Pointer to the test result
1218 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1219 * intern the attribute name (which usually indicates memory
1222 css_error
node_has_attribute_substring(void *pw
, void *node
,
1223 const css_qname
*qname
, lwc_string
*substring
,
1226 return _node_has_attribute_substring(pw
, node
, qname
, substring
,
1232 * Test whether or not the given node is the document's root element
1233 * This corresponds to the CSS :root pseudo-selector.
1235 * \param pw Pointer to the current SVG parser state
1236 * \param node Libdom SVG node to test
1237 * \param match Pointer to the test result
1239 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1241 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1248 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1249 if (err
!= DOM_NO_ERR
) {
1253 /* It's the root element if it doesn't have a parent element */
1254 if (parent
!= NULL
) {
1255 err
= dom_node_get_node_type(parent
, &type
);
1256 dom_node_unref(parent
);
1257 if (err
!= DOM_NO_ERR
) {
1260 if (type
!= DOM_DOCUMENT_NODE
) {
1261 /* DOM_DOCUMENT_NODE is the only allowable
1262 * type of parent node for the root element */
1274 * Used internally in node_count_siblings() to "count" the given
1275 * sibling node. It factors out the node type and name checks.
1277 static int node_count_siblings_check(dom_node
*dnode
,
1284 dom_string
*dnode_name
;
1286 /* We flip this to 1 if/when we count this node */
1289 if (dnode
== NULL
) {
1293 exc
= dom_node_get_node_type(dnode
, &type
);
1294 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1295 /* We only count element siblings */
1299 /* ... with the right name */
1302 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1304 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1305 if (dom_string_isequal(name
,
1309 dom_string_unref(dnode_name
);
1320 * Count the given node's sibling elements
1322 * This counts the given node's sibling elements in one direction,
1323 * either forwards or backwards, in the DOM. Keep in mind that the
1324 * libdom tree is upside-down compared to the CSS one; so "next" and
1325 * "previous" are actually reversed; the default is to count preceding
1326 * libdom siblings which correspond to subsequent CSS siblings.
1328 * This operation is central to the CSS :first-child, :nth-child, and
1329 * :last-child (et cetera) pseudo-selectors.
1331 * If same_name is true, then only nodes having the same
1332 * (case-sensitive) name as the given node are counted.
1334 * \param pw Pointer to the current SVG parser state
1335 * \param node Libdom SVG node whose siblings we're counting
1336 * \param same_name Whether or not to count only siblings having
1337 * the same name as the given node
1338 * \param after Count subsequent siblings rather than precedent
1339 * ones (the default)
1340 * \param count Pointer to the return value, the number of sibling
1343 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1345 css_error
node_count_siblings(void *pw
, void *node
,
1346 bool same_name
, bool after
, int32_t *count
)
1350 dom_node
*dnode
; /* node, but with the right type */
1351 dom_string
*dnode_name
;
1352 dom_node
*next
; /* "next" sibling (depends on direction) */
1354 /* Pointer to the "next sibling" function */
1355 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1360 dnode
= (dom_node
*)node
;
1362 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1363 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1368 /* Increment the reference counter for dnode for as long as
1369 * we retain a reference to it. */
1370 dnode
= dom_node_ref(dnode
);
1372 next_func
= dom_node_get_previous_sibling
;
1374 next_func
= dom_node_get_next_sibling
;
1378 exc
= next_func(dnode
, &next
);
1379 if (exc
!= DOM_NO_ERR
) {
1383 /* If next_func worked, we're about to swap "next"
1384 * with "dnode" meaning that we will no longer retain
1385 * a reference to the current dnode. */
1386 dom_node_unref(dnode
);
1389 *count
+= node_count_siblings_check(dnode
,
1392 } while (dnode
!= NULL
);
1394 if (dnode_name
!= NULL
) {
1395 dom_string_unref(dnode_name
);
1403 * Determine whether or not the given element is empty
1405 * An element is "nonempty" if it has a child that is either an
1406 * element node or a text node.
1408 * \param pw Pointer to the current SVG parser state
1409 * \param node Libdom SVG node to check for emptiness
1410 * \param is_empty Pointer to the return value
1412 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1414 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1417 dom_node
*child
; /* current child node pointer */
1418 dom_node
*next
; /* next child node pointer */
1419 dom_node_type type
; /* what type of node is "child" */
1422 /* Assume that it's empty by default */
1425 /* Get the given node's first child. Implementation detail:
1426 * this increments the reference counter on the child node. */
1427 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1428 if (err
!= DOM_NO_ERR
) {
1432 /* And now loop through all children looking for a
1433 * text/element node. If we find one, the original
1434 * node is "nonempty" */
1435 while (child
!= NULL
) {
1436 err
= dom_node_get_node_type(child
, &type
);
1437 if (err
!= DOM_NO_ERR
) {
1438 dom_node_unref(child
);
1442 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1444 dom_node_unref(child
);
1448 err
= dom_node_get_next_sibling(child
, &next
);
1449 if (err
!= DOM_NO_ERR
) {
1450 dom_node_unref(child
);
1454 /* If we're moving to the next node, we can release
1455 * the reference to the current one */
1456 dom_node_unref(child
);
1465 * Determine whether or not the given node is a link
1467 * A node is a link if it is an element node whose name is "a" and if
1468 * it has an "href" attribute (case-sensitive). This selector
1469 * corresponds to node:link pseudo-class in CSS.
1471 * This pseudo-class is a bit awkward because the two standards (HTML5
1472 * and CSS) disagree on what it means, and because libsvgtiny does not
1473 * have enough information to determine if a link has been "visited"
1474 * yet -- that's a UI property. CSS says that :link is for unvisited
1475 * links, which we can't determine. HTML5 says that each link must
1476 * be either a :link or :visited. Since we can't decide either way,
1477 * It seems less wrong to declare that all links are unvisited; i.e.
1478 * that they match :link.
1480 * \param pw Pointer to the current SVG parser state
1481 * \param node Libdom SVG node to check
1482 * \param is_link Pointer to the boolean return value
1484 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1486 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1489 dom_node
*dnode
; /* node, but with the right type */
1490 dom_string
*dnode_name
;
1492 struct svgtiny_parse_state
* state
;
1494 dnode
= (dom_node
*)node
;
1496 has_href
= false; /* assume no href attribute */
1497 *is_link
= false; /* assume that it's not a link */
1499 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1500 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1504 state
= (struct svgtiny_parse_state
*)pw
;
1505 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1506 exc
= dom_element_has_attribute(node
,
1507 state
->interned_href
,
1509 if (exc
== DOM_NO_ERR
&& has_href
) {
1514 dom_string_unref(dnode_name
);
1519 * Check if the given node is a link that has been visited already
1521 * This check always fails because the SVG DOM does not have the
1522 * necessary information (it's a UI property).
1524 * \param pw Pointer to the current SVG parser state; unused
1525 * \param node Libdom SVG node to check; unused
1526 * \param is_visited Pointer to the boolean return value
1528 * \return Always returns CSS_OK
1530 css_error
node_is_visited(void *pw
, void *node
, bool *is_visited
)
1534 *is_visited
= false;
1540 * Check if the given node is being "hovered" over
1542 * This check always fails because the SVG DOM does not have the
1543 * necessary information (it's a UI property).
1545 * \param pw Pointer to the current SVG parser state; unused
1546 * \param node Libdom SVG node to check; unused
1547 * \param is_hover Pointer to the boolean return value
1549 * \return Always returns CSS_OK
1551 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1561 * Check if the given node is "active"
1563 * This check always fails because the SVG DOM does not have the
1564 * necessary information (it's a UI property).
1566 * \param pw Pointer to the current SVG parser state; unused
1567 * \param node Libdom SVG node to check; unused
1568 * \param is_active Pointer to the boolean return value
1570 * \return Always returns CSS_OK
1572 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1582 * Check if the given node has the focus
1584 * This check always fails because the SVG DOM does not have the
1585 * necessary information (it's a UI property).
1587 * \param pw Pointer to the current SVG parser state; unused
1588 * \param node Libdom SVG node to check; unused
1589 * \param is_focus Pointer to the boolean return value
1591 * \return Always returns CSS_OK
1593 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1603 * Check if the given node is enabled
1605 * This check always fails because the SVG DOM does not have the
1606 * necessary information (it's a UI property).
1608 * \param pw Pointer to the current SVG parser state; unused
1609 * \param node Libdom SVG node to check; unused
1610 * \param is_enabled Pointer to the boolean return value
1612 * \return Always returns CSS_OK
1614 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1618 *is_enabled
= false;
1624 * Check if the given node is disabled
1626 * This check always fails because the SVG DOM does not have the
1627 * necessary information (it's a UI property). Beware, until they are
1628 * implemented, this is NOT the logical negation of node_is_enabled!
1630 * \param pw Pointer to the current SVG parser state; unused
1631 * \param node Libdom SVG node to check; unused
1632 * \param is_disabled Pointer to the boolean return value
1634 * \return Always returns CSS_OK
1636 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1640 *is_disabled
= false;
1646 * Test whether or not the given node is "checked"
1648 * This test always fails because the SVG DOM does not have the
1649 * necessary information (it's a UI property).
1651 * \param pw Pointer to the current SVG parser state; unused
1652 * \param node Libdom SVG node to check; unused
1653 * \param is_checked Pointer to the boolean return value
1655 * \return Always returns CSS_OK
1657 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1661 *is_checked
= false;
1667 * Check if the given node is the "target" of the document URL
1669 * This test always fails because the SVG DOM does not have the
1670 * necessary information (it's a UI property).
1672 * \param pw Pointer to the current SVG parser state; unused
1673 * \param node Libdom SVG node to check; unused
1674 * \param is_target Pointer to the boolean return value
1676 * \return Always returns CSS_OK
1678 css_error
node_is_target(void *pw
, void *node
, bool *is_target
)
1688 * Check if the given node is the given language
1690 * This test is corresponds to the CSS :lang() selector and is not
1691 * fully implemented yet: it looks only for "lang" attributes on the
1692 * given element and its parents, and performs a simple substring
1693 * check. This results in a partial implementation of CSS Level 3 for
1694 * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
1695 * favor of the "lang" attribute that is defined only in SVG 2.0.
1697 * \param pw Pointer to the current SVG parser state; unused
1698 * \param node Libdom SVG node to check
1699 * \param lang The language to match
1700 * \param is_lang Pointer to the boolean return value
1702 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1704 static css_error
node_is_lang(void *pw
, void *node
,
1705 lwc_string
*lang
, bool *is_lang
)
1708 /* SVG2 elements support both "lang" and "xml:lang"
1709 * attributes; earlier versions have only the XML
1710 * attribute. It would not be too hard to add support for
1711 * xml:lang" here. The main difficulty standing in the way of
1712 * a full Level 4 implementation is the complexity of the
1715 * https://www.w3.org/TR/selectors-4/#the-lang-pseudo
1720 dom_exception d_err
;
1721 dom_node
*n
; /* current node */
1722 dom_node
*p
; /* parent node */
1723 bool match
; /* retval from node_has_attribute_substring() */
1725 /* Define the attribute name "lang" that we're looking for.
1726 * We only use a css_qname here because that's what the
1727 * node_has_attribute_substring() takes; the namespace
1728 * portion of it is irrelevant. */
1732 if (lwc_intern_string("lang", 4, &attr
.name
) != lwc_error_ok
) {
1736 *is_lang
= false; /* default to no match */
1737 n
= (dom_node
*)node
;
1739 /* Loop through all parents of the given node looking for a
1740 * substring match */
1742 c_err
= _node_has_attribute_substring(pw
, (void *)n
, &attr
,
1743 lang
, &match
, true);
1744 if (c_err
!= CSS_OK
) {
1745 lwc_string_destroy(attr
.name
);
1749 /* matched this element; we're done */
1750 lwc_string_destroy(attr
.name
);
1755 /* no match on this element, try its parent */
1756 d_err
= dom_node_get_parent_node(n
, &p
);
1757 if (d_err
!= DOM_NO_ERR
) {
1758 lwc_string_destroy(attr
.name
);
1764 /* If we never find a match we may wind up here */
1765 lwc_string_destroy(attr
.name
);
1771 * User-agent defaults for CSS properties
1773 * For the moment, we provide no defaults, because libsvgtiny does not
1774 * yet support any CSS properties that might need them.
1776 * \param pw Pointer to the current SVG parser state; unused
1777 * \param property LibCSS property identifier; unused
1778 * \param hint Pointer to hint object (a return value); unused
1780 * \return Always returns CSS_INVALID
1782 css_error
ua_default_for_property(void *pw
, uint32_t property
,