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
);
59 static css_error
ua_default_for_property(void *pw
, uint32_t property
,
64 * Resolve a relative URL to an absolute one by doing nothing. This is
65 * the simplest possible implementation of a URL resolver, needed for
68 css_error
svgtiny_resolve_url(void *pw
,
69 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
74 /* Copy the relative URL to the absolute one (the return
76 *abs
= lwc_string_ref(rel
);
81 * Create a stylesheet with the default set of params.
83 * \param sheet A stylesheet pointer, passed in by reference, that
84 * we use to store the newly-created stylesheet.
85 * \param inline_style True if this stylesheet represents an inline
86 * style, and false otherwise.
88 * \return The return value from css_stylesheet_create() is returned.
90 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
93 css_stylesheet_params params
;
95 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
96 params
.level
= CSS_LEVEL_DEFAULT
;
97 params
.charset
= NULL
;
100 params
.allow_quirks
= false;
101 params
.inline_style
= inline_style
;
102 params
.resolve
= svgtiny_resolve_url
;
103 params
.resolve_pw
= NULL
;
104 params
.import = NULL
;
105 params
.import_pw
= NULL
;
107 params
.color_pw
= NULL
;
109 params
.font_pw
= NULL
;
111 return css_stylesheet_create(¶ms
, sheet
);
115 /**************************/
116 /* libcss select handlers */
117 /**************************/
119 * From here on we implement the "select handler "API defined in
120 * libcss's include/libcss/select.h and discussed briefly in its
126 * Retrieve the given node's name
128 * \param pw Pointer to the current SVG parser state
129 * \param node Libdom SVG node
130 * \param qname Address at which to store the node name
132 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
134 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
138 struct svgtiny_parse_state
*state
;
140 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
141 if (err
!= DOM_NO_ERR
) {
145 state
= (struct svgtiny_parse_state
*)pw
;
146 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
148 err
= dom_string_intern(name
, &qname
->name
);
149 if (err
!= DOM_NO_ERR
) {
150 dom_string_unref(name
);
154 dom_string_unref(name
);
161 * Retrieve the given node's classes
163 * \param pw Pointer to the current SVG parser state
164 * \param node Libdom SVG node
165 * \param classes Address at which to store the class name array
166 * \param n_classes Address at which to store the length of the class
169 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
171 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
172 * because the underlying libdom function never fails
174 css_error
node_classes(void *pw
, void *node
,
175 lwc_string
***classes
, uint32_t *n_classes
)
180 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
182 /* The implementation does not do it, but the documentation
183 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
184 possible here, so we handle it to be on the safe side. */
185 if (err
!= DOM_NO_ERR
) {
194 * Retrieve the given node's id
196 * \param pw Pointer to the current SVG parser state
197 * \param node Libdom SVG node
198 * \param id Address at which to store the id
200 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
202 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
206 struct svgtiny_parse_state
*state
;
208 /* Begin with the assumption that this node has no id */
211 state
= (struct svgtiny_parse_state
*)pw
;
212 err
= dom_element_get_attribute((dom_node
*)node
,
213 state
->interned_id
, &attr
);
214 if (err
!= DOM_NO_ERR
) {
217 else if (attr
== NULL
) {
218 /* The node has no id attribute and our return value
219 is already set to NULL so we're done */
223 /* If we found an id attribute (a dom_string), intern it into
224 an lwc_string that we can return, and then cleanup the
226 err
= dom_string_intern(attr
, id
);
227 if (err
!= DOM_NO_ERR
) {
228 dom_string_unref(attr
);
231 dom_string_unref(attr
);
238 * Find the first parent of the given element having the given name
240 * \param pw Pointer to the current SVG parser state
241 * \param node Libdom SVG node
242 * \param qname Name of the parent node to search for
243 * \param parent Address at which to store the parent node pointer
245 * \return Always returns CSS_OK
247 * \post If a suitable element is found, a pointer to it will be
248 * stored at the address pointed to by \a parent; otherwise,
249 * NULL will be stored at the address pointed to by \a parent
251 css_error
named_parent_node(void *pw
, void *node
,
252 const css_qname
*qname
, void **parent
)
255 /* dom_element_named_parent_node() was invented to implement
256 * this select handler so there isn't much for us to do except
257 * call it. It's OK if node isn't an element, libdom checks
259 dom_element_named_parent_node((dom_element
*)node
,
261 (struct dom_element
**)parent
);
263 /* Implementation detail: dom_element_named_parent_node()
264 * increments the reference count of the parent element before
265 * returning it to us. According to docs/RefCnt in the libdom
266 * repository, this will prevent the parent element from being
267 * destroyed if it is pruned from the DOM. That sounds good,
268 * since we don't want to be using a pointer to an object that
269 * has been destroyed... but we also have no way of later
270 * decrementing the reference count ourselves, and don't want
271 * to make the returned node eternal. Decrementing the
272 * reference counter now allows it to be destroyed when the
273 * DOM no longer needs it, and so long as no other parts of
274 * libsvgtiny are messing with the DOM during parsing, that
275 * shouldn't (ha ha) cause any problems. */
276 dom_node_unref(*parent
);
283 * Find the "next-sibling" of the given element having the given name
285 * This search corresponds to the "+ foo" combinator in CSS and will
286 * find only "foo" element nodes that immediately precede the given
287 * node under the same parent in the DOM. In CSS the tree is viewed
288 * top-down and in libdom it is viewed from the bottom-up; as a result
289 * "next" and "previous" are sometimes backwards. This is case-sensitive.
291 * \param pw Pointer to the current SVG parser state
292 * \param node Libdom SVG node
293 * \param qname Name of the sibling node to search for
294 * \param sibling Address at which to store the sibling node pointer
296 * \return Always returns CSS_OK
298 * \post If a suitable element is found, a pointer to it will be
299 * stored at the address pointed to by \a sibling; otherwise,
300 * NULL will be stored at the address pointed to by \a sibling
302 css_error
named_sibling_node(void *pw
, void *node
,
303 const css_qname
*qname
, void **sibling
)
306 dom_node
*n
= node
; /* the current node */
307 dom_node
*prev
; /* the previous node */
312 *sibling
= NULL
; /* default to nothing found */
314 /* Begin the search; the first iteration we do outside of the
315 * loop. Implementation detil: dom_node_get_previous_sibling()
316 * increments the reference counter on the returned node. A
317 * comment within named_parent_node() explains why we
318 * decrement it ASAP. */
319 err
= dom_node_get_previous_sibling(n
, &n
);
320 if (err
!= DOM_NO_ERR
) {
325 /* We're looking for the first ELEMENT sibling */
326 err
= dom_node_get_node_type(n
, &type
);
327 if (err
!= DOM_NO_ERR
) {
332 if (type
== DOM_ELEMENT_NODE
) {
333 /* We found an element node, does it have the
335 err
= dom_node_get_node_name(n
, &name
);
336 if (err
!= DOM_NO_ERR
) {
341 if (dom_string_lwc_isequal(name
,
343 /* The name is right, return it */
347 /* There's only one next-sibling element node
348 * and we've already found it, so if its name
349 * wasn't right, we return the default value
351 dom_string_unref(name
);
356 /* Not an element node, so we move on the the previous
357 * previous sibling */
358 err
= dom_node_get_previous_sibling(n
, &prev
);
359 if (err
!= DOM_NO_ERR
) {
373 * Find the first "subsequent-sibling" of the given element having the
376 * This search corresponds to the "~ foo" combinator in CSS and will
377 * find only "foo" element nodes that precede the given node (under
378 * the same parent) in the DOM. In CSS the tree is viewed top-down and
379 * in libdom it is viewed from the bottom-up; as a result "next" and
380 * "previous" are sometimes backwards. This is case-sensitive.
382 * \param pw Pointer to the current SVG parser state
383 * \param node Libdom SVG node
384 * \param qname Name of the sibling node to search for
385 * \param sibling Address at which to store the sibling node pointer
387 * \return Always returns CSS_OK
389 * \post If a suitable element is found, a pointer to it will be
390 * stored at the address pointed to by \a sibling; otherwise,
391 * NULL will be stored at the address pointed to by \a sibling
393 css_error
named_generic_sibling_node(void *pw
, void *node
,
394 const css_qname
*qname
, void **sibling
)
397 dom_node
*n
= node
; /* the current node */
398 dom_node
*prev
; /* the previous node */
404 *sibling
= NULL
; /* default to nothing found */
406 /* Begin the search; the first iteration we do outside of the
407 * loop. Implementation detil: dom_node_get_previous_sibling()
408 * increments the reference counter on the returned node. A
409 * comment within named_parent_node() explains why we
410 * decrement it ASAP. */
411 err
= dom_node_get_previous_sibling(n
, &n
);
412 if (err
!= DOM_NO_ERR
) {
417 err
= dom_node_get_node_type(n
, &type
);
418 if (err
!= DOM_NO_ERR
) {
423 if (type
== DOM_ELEMENT_NODE
) {
424 /* We only want ELEMENT nodes */
425 err
= dom_node_get_node_name(n
, &name
);
426 if (err
!= DOM_NO_ERR
) {
431 if (dom_string_lwc_isequal(name
,
433 /* Found one. Save it and stop the search */
434 dom_string_unref(name
);
440 dom_string_unref(name
);
443 /* This sibling wasn't an element with the desired
444 name, so move on to the previous sibling */
445 err
= dom_node_get_previous_sibling(n
, &prev
);
446 if (err
!= DOM_NO_ERR
) {
460 * Return a pointer to the given node's parent
462 * \param pw Pointer to the current SVG parser state
463 * \param node Libdom SVG node
464 * \param parent Address at which to store the node's parent pointer
466 * \return Always returns CSS_OK
468 css_error
parent_node(void *pw
, void *node
, void **parent
)
471 /* Libdom basically implements this for us */
472 dom_element_parent_node(node
, (struct dom_element
**)parent
);
474 /* See the comment in named_parent_node() for why we decrement
475 * this reference counter here. */
476 dom_node_unref(*parent
);
483 * Find the "next-sibling" of the given element
485 * This search corresponds "+ *" in CSS and will find the first
486 * element node that immediately precedes the given node under the
487 * same parent in the DOM. In CSS the tree is viewed top-down and in
488 * libdom it is viewed from the bottom-up; as a result "next" and
489 * "previous" are sometimes backwards.
491 * \param pw Pointer to the current SVG parser state
492 * \param node Libdom SVG node
493 * \param sibling Address at which to store the sibling node pointer
495 * \return Always returns CSS_OK
497 * \post If a suitable element is found, a pointer to it will be
498 * stored at the address pointed to by \a sibling; otherwise,
499 * NULL will be stored at the address pointed to by \a sibling
501 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
504 dom_node
*n
= node
; /* the current node */
505 dom_node
*prev
; /* the previous node */
509 *sibling
= NULL
; /* default to nothing found */
511 /* Begin the search; the first iteration we do outside of the
512 * loop. Implementation detil: dom_node_get_previous_sibling()
513 * increments the reference counter on the returned node. A
514 * comment within named_parent_node() explains why we
515 * decrement it ASAP. */
516 err
= dom_node_get_previous_sibling(n
, &n
);
517 if (err
!= DOM_NO_ERR
) {
522 err
= dom_node_get_node_type(n
, &type
);
523 if (err
!= DOM_NO_ERR
) {
528 if (type
== DOM_ELEMENT_NODE
) {
529 /* We found a sibling node that is also an
530 element and that's all we wanted. */
536 /* This sibling node was not an element; move on to
537 the previous sibling */
538 err
= dom_node_get_previous_sibling(n
, &prev
);
539 if (err
!= DOM_NO_ERR
) {
553 * Test the given node for the given name
555 * This will return true (via the "match" pointer) if the libdom node
556 * has the given name or if that name is the universal selector;
557 * otherwise it returns false. The comparison is case-sensitive. It
558 * corresponds to a rule like "body { ... }" in CSS.
560 * \param pw Pointer to the current SVG parser state
561 * \param node Libdom SVG node to test
562 * \param qname Name to check for
563 * \param match Pointer to the test result
565 * \return Always returns CSS_OK
567 css_error
node_has_name(void *pw
, void *node
,
568 const css_qname
*qname
, bool *match
)
570 struct svgtiny_parse_state
*state
;
574 /* Start by checking to see if qname is the universal selector */
575 state
= (struct svgtiny_parse_state
*)pw
;
576 if (lwc_string_isequal(qname
->name
,
577 state
->interned_universal
, match
) == lwc_error_ok
) {
579 /* It's the universal selector. In NetSurf, all node
580 * names match the universal selector, and nothing in
581 * the libcss documentation suggests another approach,
582 * so we follow NetSurf here. */
587 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
588 if (err
!= DOM_NO_ERR
) {
592 /* Unlike with HTML, SVG element names are case-sensitive */
593 *match
= dom_string_lwc_isequal(name
, qname
->name
);
594 dom_string_unref(name
);
601 * Test the given node for the given class
603 * This will return true (via the "match" pointer) if the libdom node
604 * has the given class. The comparison is case-sensitive. It
605 * corresponds to node.class in CSS.
607 * \param pw Pointer to the current SVG parser state
608 * \param node Libdom SVG node to test
609 * \param name Class name to check for
610 * \param match Pointer to the test result
612 * \return Always returns CSS_OK
614 css_error
node_has_class(void *pw
, void *node
,
615 lwc_string
*name
, bool *match
)
618 /* libdom implements this for us and apparently it cannot fail */
619 dom_element_has_class((dom_node
*)node
, name
, match
);
625 * Test the given node for the given id
627 * This will return true (via the "match" pointer) if the libdom node
628 * has the given id. The comparison is case-sensitive. It corresponds
631 * \param pw Pointer to the current SVG parser state
632 * \param node Libdom SVG node to test
633 * \param name Id to check for
634 * \param match Pointer to the test result
636 * \return Always returns CSS_OK
638 css_error
node_has_id(void *pw
, void *node
,
639 lwc_string
*name
, bool *match
)
643 struct svgtiny_parse_state
*state
;
645 attr
= NULL
; /* a priori the "id" attribute may not exist */
646 *match
= false; /* default to no match */
648 state
= (struct svgtiny_parse_state
*)pw
;
649 err
= dom_element_get_attribute((dom_node
*)node
,
650 state
->interned_id
, &attr
);
651 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
655 *match
= dom_string_lwc_isequal(attr
, name
);
656 dom_string_unref(attr
);
663 * Test the given node for the given attribute
665 * This will return true (via the "match" pointer) if the libdom node
666 * has an attribute with the given name. The comparison is
667 * case-sensitive. It corresponds to node[attr] in CSS.
669 * \param pw Pointer to the current SVG parser state
670 * \param node Libdom SVG node to test
671 * \param qname Attribute name to check for
672 * \param match Pointer to the test result
674 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
677 css_error
node_has_attribute(void *pw
, void *node
,
678 const css_qname
*qname
, bool *match
)
684 /* intern the attribute name as a dom_string so we can
685 * delegate to dom_element_has_attribute() */
686 err
= dom_string_create_interned(
687 (const uint8_t *) lwc_string_data(qname
->name
),
688 lwc_string_length(qname
->name
),
690 if (err
!= DOM_NO_ERR
) {
694 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
695 if (err
!= DOM_NO_ERR
) {
696 dom_string_unref(name
);
700 dom_string_unref(name
);
706 * Test the given node for an attribute with a specific value
708 * This will return true (via the "match" pointer) if the libdom node
709 * has an attribute with the given name and value. The comparison is
710 * case-sensitive. It corresponds to node[attr=value] in CSS.
712 * \param pw Pointer to the current SVG parser state
713 * \param node Libdom SVG node to test
714 * \param qname Attribute name to check for
715 * \param value Attribute value to check for
716 * \param match Pointer to the test result
718 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
719 * intern the attribute name (which usually indicates memory
722 css_error
node_has_attribute_equal(void *pw
, void *node
,
723 const css_qname
*qname
, lwc_string
*value
,
726 /* Implementation note: NetSurf always returns "no match" when
727 * the value is empty (length zero). We allow it, because why
732 dom_string
*attr_val
;
735 /* Intern the attribute name as a dom_string so we can
736 * use dom_element_get_attribute() */
737 err
= dom_string_create_interned(
738 (const uint8_t *) lwc_string_data(qname
->name
),
739 lwc_string_length(qname
->name
),
741 if (err
!= DOM_NO_ERR
) {
745 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
746 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
747 /* There was an error getting the attribute's value or
748 * the attribute doesn't exist. So, no match? */
749 dom_string_unref(name
);
754 /* Otherwise, we have the attribute value from the given node
755 * and all we need to do is compare. */
756 dom_string_unref(name
);
757 *match
= dom_string_lwc_isequal(attr_val
, value
);
758 dom_string_unref(attr_val
);
765 * Test the given node for an attribute with a specific value,
766 * possibly followed by a single hyphen
768 * This will return true (via the "match" pointer) if the libdom node
769 * has an attribute with the given name and value or with the given
770 * name and a value that is followed by exactly one hyphen. The
771 * comparison is case-sensitive. This corresponds to [attr|=value]
774 * \param pw Pointer to the current SVG parser state
775 * \param node Libdom SVG node to test
776 * \param qname Attribute name to check for
777 * \param value Attribute value to check for
778 * \param match Pointer to the test result
780 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
781 * intern the attribute name (which usually indicates memory
784 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
785 const css_qname
*qname
, lwc_string
*value
,
788 /* Implementation note: NetSurf always returns "no match" when
789 * the value is empty (length zero). We allow it, because why
794 dom_string
*attr_val
;
797 const char *vdata
; /* to hold the data underlying "value" */
799 const char *avdata
; /* to hold the found attribute value data */
802 /* Intern the attribute name as a dom_string so we can
803 * use dom_element_get_attribute() */
804 err
= dom_string_create_interned(
805 (const uint8_t *) lwc_string_data(qname
->name
),
806 lwc_string_length(qname
->name
),
808 if (err
!= DOM_NO_ERR
) {
812 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
813 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
814 /* There was an error getting the attribute's value or
815 * the attribute doesn't exist. So, no match? */
816 dom_string_unref(name
);
821 /* Otherwise, we have the attribute value from the given node
822 * and all we need to do is compare. */
823 dom_string_unref(name
);
824 *match
= dom_string_lwc_isequal(attr_val
, value
);
826 /* Exact match, we're done */
827 dom_string_unref(attr_val
);
831 /* No exact match, try it with a hyphen on the end */
832 vdata
= lwc_string_data(value
); /* needle */
833 vdata_len
= lwc_string_length(value
);
834 avdata
= dom_string_data(attr_val
); /* haystack */
835 avdata_len
= dom_string_byte_length(attr_val
);
836 dom_string_unref(attr_val
);
838 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
839 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
840 /* If there's a hyphen in the right position,
841 * it suffices to compare the strings only up
852 * Test the given node for an attribute whose value is a
853 * space-separated list of words, one of which is the given word
855 * This will return true (via the "match" pointer) if the libdom node
856 * has an attribute with the given name and whose value when
857 * considered as a space-separated list of words contains the given
858 * word. The comparison is case-sensitive. This corresponds to
859 * [attr~=value] in CSS.
861 * \param pw Pointer to the current SVG parser state
862 * \param node Libdom SVG node to test
863 * \param qname Attribute name to check for
864 * \param word Value word to check for
865 * \param match Pointer to the test result
867 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
868 * intern the attribute name (which usually indicates memory
871 css_error
node_has_attribute_includes(void *pw
, void *node
,
872 const css_qname
*qname
, lwc_string
*word
,
878 dom_string
*attr_val
;
880 size_t wordlen
; /* length of "word" */
882 /* pointers used to parse a space-separated list of words */
887 *match
= false; /* default to no match */
889 wordlen
= lwc_string_length(word
);
891 /* In this case, the spec says that "if 'val' is the
892 * empty string, it will never represent anything." */
896 /* Intern the attribute name as a dom_string so we can
897 * use dom_element_get_attribute() */
898 err
= dom_string_create_interned(
899 (const uint8_t *) lwc_string_data(qname
->name
),
900 lwc_string_length(qname
->name
),
902 if (err
!= DOM_NO_ERR
) {
906 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
907 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
908 /* There was an error getting the attribute's value or
909 * the attribute doesn't exist. So, no match? */
910 dom_string_unref(name
);
914 /* Parse the list comparing each word against "word" */
915 start
= dom_string_data(attr_val
);
916 end
= start
+ dom_string_byte_length(attr_val
);
917 dom_string_unref(attr_val
);
919 for (p
= start
; p
<= end
; p
++) {
920 /* Move forward until we find the end of the first word */
921 if (*p
== ' ' || *p
== '\0') {
922 /* If the length of that word is the length of the
923 * word we're looking for, do the comparison. */
924 if ((size_t) (p
- start
) == wordlen
&&
926 lwc_string_data(word
),
931 /* No match? Set "start" to the beginning of
932 * the next word and loop. */
942 * Test the given node for an attribute whose value begins with the
945 * This will return true (via the "match" pointer) if the libdom node
946 * has an attribute with the given name and whose value begins with
947 * the given prefix string. The comparison is case-sensitive. This
948 * corresponds to [attr^=value] in CSS.
950 * \param pw Pointer to the current SVG parser state
951 * \param node Libdom SVG node to test
952 * \param qname Attribute name to check for
953 * \param prefix Value prefix to check for
954 * \param match Pointer to the test result
956 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
957 * intern the attribute name (which usually indicates memory
960 css_error
node_has_attribute_prefix(void *pw
, void *node
,
961 const css_qname
*qname
, lwc_string
*prefix
,
966 dom_string
*attr_val
;
968 const char *avdata
; /* attribute value data */
969 size_t avdata_len
; /* length of that attribute value data */
970 size_t prefixlen
; /* length of "prefix" */
972 prefixlen
= lwc_string_length(prefix
);
973 if (prefixlen
== 0) {
974 /* In this case, the spec says that "if 'val' is the
975 * empty string, it will never represent anything." */
979 /* Intern the attribute name as a dom_string so we can
980 * use dom_element_get_attribute() */
981 err
= dom_string_create_interned(
982 (const uint8_t *) lwc_string_data(qname
->name
),
983 lwc_string_length(qname
->name
),
985 if (err
!= DOM_NO_ERR
) {
989 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
990 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
991 /* There was an error getting the attribute's value or
992 * the attribute doesn't exist. So, no match? */
993 dom_string_unref(name
);
998 /* Otherwise, we have the attribute value from the given node,
999 * and the first thing we want to do is check to see if the
1000 * whole thing matches the prefix. */
1001 dom_string_unref(name
);
1002 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
1004 /* If not, check to see if an, uh, prefix matches the
1006 if (*match
== false) {
1007 avdata
= dom_string_data(attr_val
);
1008 avdata_len
= dom_string_byte_length(attr_val
);
1009 if ((avdata_len
>= prefixlen
) &&
1010 (strncasecmp(avdata
,
1011 lwc_string_data(prefix
),
1013 /* Use strncasecmp to compare only the first
1014 * "n" characters, where "n" is the length of
1020 dom_string_unref(attr_val
);
1027 * Test the given node for an attribute whose value end with the
1030 * This will return true (via the "match" pointer) if the libdom node
1031 * has an attribute with the given name and whose value ends with
1032 * the given suffix string. The comparison is case-sensitive. This
1033 * corresponds to [attr$=value] in CSS.
1035 * \param pw Pointer to the current SVG parser state
1036 * \param node Libdom SVG node to test
1037 * \param qname Attribute name to check for
1038 * \param suffix Value suffix to check for
1039 * \param match Pointer to the test result
1041 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1042 * intern the attribute name (which usually indicates memory
1045 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1046 const css_qname
*qname
, lwc_string
*suffix
,
1051 dom_string
*attr_val
;
1053 const char *avdata
; /* attribute value data */
1054 size_t avdata_len
; /* length of that attribute value data */
1055 size_t suffixlen
; /* length of "suffix" */
1057 /* convenience pointer we'll use when matching the suffix */
1058 const char *suffix_start
;
1060 suffixlen
= lwc_string_length(suffix
);
1061 if (suffixlen
== 0) {
1062 /* In this case, the spec says that "if 'val' is the
1063 * empty string, it will never represent anything." */
1067 /* Intern the attribute name as a dom_string so we can
1068 * use dom_element_get_attribute() */
1069 err
= dom_string_create_interned(
1070 (const uint8_t *) lwc_string_data(qname
->name
),
1071 lwc_string_length(qname
->name
),
1073 if (err
!= DOM_NO_ERR
) {
1077 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1078 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1079 /* There was an error getting the attribute's value or
1080 * the attribute doesn't exist. So, no match? */
1081 dom_string_unref(name
);
1086 /* Otherwise, we have the attribute value from the given node,
1087 * and the first thing we want to do is check to see if the
1088 * whole thing matches the suffix. */
1089 dom_string_unref(name
);
1090 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1092 /* If not, check to see if an, uh, suffix matches the
1094 if (*match
== false) {
1095 avdata
= dom_string_data(attr_val
);
1096 avdata_len
= dom_string_byte_length(attr_val
);
1098 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1100 if ((avdata_len
>= suffixlen
) &&
1101 (strncasecmp(suffix_start
,
1102 lwc_string_data(suffix
),
1104 /* Use strncasecmp to compare only the last
1105 * "n" characters, where "n" is the length of
1111 dom_string_unref(attr_val
);
1118 * Implement node_has_attribute_substring() with optional case-
1119 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1120 * not supported by libcss yet, but it allows us to factor out some
1123 static css_error
_node_has_attribute_substring(void *pw
, void *node
,
1124 const css_qname
*qname
, lwc_string
*substring
,
1125 bool *match
, bool insensitive
)
1129 dom_string
*attr_val
;
1131 size_t attr_len
; /* length of attr_val */
1132 size_t substrlen
; /* length of "substring" */
1134 /* Convenience pointers we use when comparing substrings */
1138 substrlen
= lwc_string_length(substring
);
1139 if (substrlen
== 0) {
1140 /* In this case, the spec says that "if 'val' is the
1141 * empty string, it will never represent anything." */
1145 /* Intern the attribute name as a dom_string so we can
1146 * use dom_element_get_attribute() */
1147 err
= dom_string_create_interned(
1148 (const uint8_t *) lwc_string_data(qname
->name
),
1149 lwc_string_length(qname
->name
),
1151 if (err
!= DOM_NO_ERR
) {
1155 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1156 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1157 /* There was an error getting the attribute's value or
1158 * the attribute doesn't exist. So, no match? */
1159 dom_string_unref(name
);
1164 /* Otherwise, we have the attribute value from the given node,
1165 * and the first thing we want to do is check to see if the
1166 * whole thing matches the substring. */
1167 dom_string_unref(name
);
1170 *match
= dom_string_caseless_lwc_isequal(attr_val
, substring
);
1173 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1176 /* If not, check to see if an, uh, substring matches the
1178 if (*match
== false) {
1179 p
= dom_string_data(attr_val
);
1181 /* Check every long-enough suffix for a prefix match */
1182 attr_len
= dom_string_byte_length(attr_val
);
1183 if (attr_len
>= substrlen
) {
1184 p_max
= p
+ attr_len
- substrlen
;
1185 while (p
<= p_max
) {
1187 lwc_string_data(substring
),
1197 dom_string_unref(attr_val
);
1203 * Test the given node for an attribute whose value contains the
1206 * This will return true (via the "match" pointer) if the libdom node
1207 * has an attribute with the given name and whose value contains the
1208 * given substring. The comparison is case-sensitive. This corresponds
1209 * to [attr*=value] in CSS.
1211 * \param pw Pointer to the current SVG parser state
1212 * \param node Libdom SVG node to test
1213 * \param qname Attribute name to check for
1214 * \param substring Value substring to check for
1215 * \param match Pointer to the test result
1217 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1218 * intern the attribute name (which usually indicates memory
1221 css_error
node_has_attribute_substring(void *pw
, void *node
,
1222 const css_qname
*qname
, lwc_string
*substring
,
1225 return _node_has_attribute_substring(pw
, node
, qname
, substring
,
1231 * Test whether or not the given node is the document's root element
1232 * This corresponds to the CSS :root pseudo-selector.
1234 * \param pw Pointer to the current SVG parser state
1235 * \param node Libdom SVG node to test
1236 * \param match Pointer to the test result
1238 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1240 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1247 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1248 if (err
!= DOM_NO_ERR
) {
1252 /* It's the root element if it doesn't have a parent element */
1253 if (parent
!= NULL
) {
1254 err
= dom_node_get_node_type(parent
, &type
);
1255 dom_node_unref(parent
);
1256 if (err
!= DOM_NO_ERR
) {
1259 if (type
!= DOM_DOCUMENT_NODE
) {
1260 /* DOM_DOCUMENT_NODE is the only allowable
1261 * type of parent node for the root element */
1273 * Used internally in node_count_siblings() to "count" the given
1274 * sibling node. It factors out the node type and name checks.
1276 static int node_count_siblings_check(dom_node
*dnode
,
1283 dom_string
*dnode_name
;
1285 /* We flip this to 1 if/when we count this node */
1288 if (dnode
== NULL
) {
1292 exc
= dom_node_get_node_type(dnode
, &type
);
1293 if ((exc
!= DOM_NO_ERR
) || (type
!= DOM_ELEMENT_NODE
)) {
1294 /* We only count element siblings */
1298 /* ... with the right name */
1301 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1303 if ((exc
== DOM_NO_ERR
) && (dnode_name
!= NULL
)) {
1304 if (dom_string_isequal(name
,
1308 dom_string_unref(dnode_name
);
1319 * Count the given node's sibling elements
1321 * This counts the given node's sibling elements in one direction,
1322 * either forwards or backwards, in the DOM. Keep in mind that the
1323 * libdom tree is upside-down compared to the CSS one; so "next" and
1324 * "previous" are actually reversed; the default is to count preceding
1325 * libdom siblings which correspond to subsequent CSS siblings.
1327 * This operation is central to the CSS :first-child, :nth-child, and
1328 * :last-child (et cetera) pseudo-selectors.
1330 * If same_name is true, then only nodes having the same
1331 * (case-sensitive) name as the given node are counted.
1333 * \param pw Pointer to the current SVG parser state
1334 * \param node Libdom SVG node whose siblings we're counting
1335 * \param same_name Whether or not to count only siblings having
1336 * the same name as the given node
1337 * \param after Count subsequent siblings rather than precedent
1338 * ones (the default)
1339 * \param count Pointer to the return value, the number of sibling
1342 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1344 css_error
node_count_siblings(void *pw
, void *node
,
1345 bool same_name
, bool after
, int32_t *count
)
1349 dom_node
*dnode
; /* node, but with the right type */
1350 dom_string
*dnode_name
;
1351 dom_node
*next
; /* "next" sibling (depends on direction) */
1353 /* Pointer to the "next sibling" function */
1354 dom_exception (*next_func
)(dom_node
*, dom_node
**);
1359 dnode
= (dom_node
*)node
;
1361 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1362 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1367 /* Increment the reference counter for dnode for as long as
1368 * we retain a reference to it. */
1369 dnode
= dom_node_ref(dnode
);
1371 next_func
= dom_node_get_previous_sibling
;
1373 next_func
= dom_node_get_next_sibling
;
1377 exc
= next_func(dnode
, &next
);
1378 if (exc
!= DOM_NO_ERR
) {
1382 /* If next_func worked, we're about to swap "next"
1383 * with "dnode" meaning that we will no longer retain
1384 * a reference to the current dnode. */
1385 dom_node_unref(dnode
);
1388 *count
+= node_count_siblings_check(dnode
,
1391 } while (dnode
!= NULL
);
1393 if (dnode_name
!= NULL
) {
1394 dom_string_unref(dnode_name
);
1402 * Determine whether or not the given element is empty
1404 * An element is "nonempty" if it has a child that is either an
1405 * element node or a text node.
1407 * \param pw Pointer to the current SVG parser state
1408 * \param node Libdom SVG node to check for emptiness
1409 * \param is_empty Pointer to the return value
1411 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1413 css_error
node_is_empty(void *pw
, void *node
, bool *is_empty
)
1416 dom_node
*child
; /* current child node pointer */
1417 dom_node
*next
; /* next child node pointer */
1418 dom_node_type type
; /* what type of node is "child" */
1421 /* Assume that it's empty by default */
1424 /* Get the given node's first child. Implementation detail:
1425 * this increments the reference counter on the child node. */
1426 err
= dom_node_get_first_child((dom_node
*)node
, &child
);
1427 if (err
!= DOM_NO_ERR
) {
1431 /* And now loop through all children looking for a
1432 * text/element node. If we find one, the original
1433 * node is "nonempty" */
1434 while (child
!= NULL
) {
1435 err
= dom_node_get_node_type(child
, &type
);
1436 if (err
!= DOM_NO_ERR
) {
1437 dom_node_unref(child
);
1441 if (type
== DOM_ELEMENT_NODE
|| type
== DOM_TEXT_NODE
) {
1443 dom_node_unref(child
);
1447 err
= dom_node_get_next_sibling(child
, &next
);
1448 if (err
!= DOM_NO_ERR
) {
1449 dom_node_unref(child
);
1453 /* If we're moving to the next node, we can release
1454 * the reference to the current one */
1455 dom_node_unref(child
);
1464 * Determine whether or not the given node is a link
1466 * A node is a link if it is an element node whose name is "a" and if
1467 * it has an "href" attribute (case-sensitive). This selector
1468 * corresponds to node:link pseudo-class in CSS.
1470 * This pseudo-class is a bit awkward because the two standards (HTML5
1471 * and CSS) disagree on what it means, and because libsvgtiny does not
1472 * have enough information to determine if a link has been "visited"
1473 * yet -- that's a UI property. CSS says that :link is for unvisited
1474 * links, which we can't determine. HTML5 says that each link must
1475 * be either a :link or :visited. Since we can't decide either way,
1476 * It seems less wrong to declare that all links are unvisited; i.e.
1477 * that they match :link.
1479 * \param pw Pointer to the current SVG parser state
1480 * \param node Libdom SVG node to check
1481 * \param is_link Pointer to the boolean return value
1483 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1485 css_error
node_is_link(void *pw
, void *node
, bool *is_link
)
1488 dom_node
*dnode
; /* node, but with the right type */
1489 dom_string
*dnode_name
;
1491 struct svgtiny_parse_state
* state
;
1493 dnode
= (dom_node
*)node
;
1495 has_href
= false; /* assume no href attribute */
1496 *is_link
= false; /* assume that it's not a link */
1498 exc
= dom_node_get_node_name(dnode
, &dnode_name
);
1499 if ((exc
!= DOM_NO_ERR
) || (dnode_name
== NULL
)) {
1503 state
= (struct svgtiny_parse_state
*)pw
;
1504 if (dom_string_isequal(dnode_name
, state
->interned_a
)) {
1505 exc
= dom_element_has_attribute(node
,
1506 state
->interned_href
,
1508 if (exc
== DOM_NO_ERR
&& has_href
) {
1513 dom_string_unref(dnode_name
);
1519 * Check if the given node is being "hovered" over
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_hover Pointer to the boolean return value
1528 * \return Always returns CSS_OK
1530 css_error
node_is_hover(void *pw
, void *node
, bool *is_hover
)
1540 * Check if the given node is "active"
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_active Pointer to the boolean return value
1549 * \return Always returns CSS_OK
1551 css_error
node_is_active(void *pw
, void *node
, bool *is_active
)
1561 * Check if the given node has the focus
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_focus Pointer to the boolean return value
1570 * \return Always returns CSS_OK
1572 css_error
node_is_focus(void *pw
, void *node
, bool *is_focus
)
1582 * Check if the given node is enabled
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_enabled Pointer to the boolean return value
1591 * \return Always returns CSS_OK
1593 css_error
node_is_enabled(void *pw
, void *node
, bool *is_enabled
)
1597 *is_enabled
= false;
1603 * Check if the given node is disabled
1605 * This check always fails because the SVG DOM does not have the
1606 * necessary information (it's a UI property). Beware, until they are
1607 * implemented, this is NOT the logical negation of node_is_enabled!
1609 * \param pw Pointer to the current SVG parser state; unused
1610 * \param node Libdom SVG node to check; unused
1611 * \param is_disabled Pointer to the boolean return value
1613 * \return Always returns CSS_OK
1615 css_error
node_is_disabled(void *pw
, void *node
, bool *is_disabled
)
1619 *is_disabled
= false;
1625 * Test whether or not the given node is "checked"
1627 * This test always fails because the SVG DOM does not have the
1628 * necessary information (it's a UI property).
1630 * \param pw Pointer to the current SVG parser state; unused
1631 * \param node Libdom SVG node to check; unused
1632 * \param is_checked Pointer to the boolean return value
1634 * \return Always returns CSS_OK
1636 css_error
node_is_checked(void *pw
, void *node
, bool *is_checked
)
1640 *is_checked
= false;
1646 * Check if the given node is the "target" of the document URL
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_target Pointer to the boolean return value
1655 * \return Always returns CSS_OK
1657 css_error
node_is_target(void *pw
, void *node
, bool *is_target
)
1667 * Check if the given node is the given language
1669 * This test is corresponds to the CSS :lang() selector and is not
1670 * fully implemented yet: it looks only for "lang" attributes on the
1671 * given element and its parents, and performs a simple substring
1672 * check. This results in a partial implementation of CSS Level 3 for
1673 * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
1674 * favor of the "lang" attribute that is defined only in SVG 2.0.
1676 * \param pw Pointer to the current SVG parser state; unused
1677 * \param node Libdom SVG node to check
1678 * \param lang The language to match
1679 * \param is_lang Pointer to the boolean return value
1681 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1683 static css_error
node_is_lang(void *pw
, void *node
,
1684 lwc_string
*lang
, bool *is_lang
)
1687 /* SVG2 elements support both "lang" and "xml:lang"
1688 * attributes; earlier versions have only the XML
1689 * attribute. It would not be too hard to add support for
1690 * xml:lang" here. The main difficulty standing in the way of
1691 * a full Level 4 implementation is the complexity of the
1694 * https://www.w3.org/TR/selectors-4/#the-lang-pseudo
1699 dom_exception d_err
;
1700 dom_node
*n
; /* current node */
1701 dom_node
*p
; /* parent node */
1702 bool match
; /* retval from node_has_attribute_substring() */
1704 /* Define the attribute name "lang" that we're looking for.
1705 * We only use a css_qname here because that's what the
1706 * node_has_attribute_substring() takes; the namespace
1707 * portion of it is irrelevant. */
1711 if (lwc_intern_string("lang", 4, &attr
.name
) != lwc_error_ok
) {
1715 *is_lang
= false; /* default to no match */
1716 n
= (dom_node
*)node
;
1718 /* Loop through all parents of the given node looking for a
1719 * substring match */
1721 c_err
= _node_has_attribute_substring(pw
, (void *)n
, &attr
,
1722 lang
, &match
, true);
1723 if (c_err
!= CSS_OK
) {
1724 lwc_string_destroy(attr
.name
);
1728 /* matched this element; we're done */
1729 lwc_string_destroy(attr
.name
);
1734 /* no match on this element, try its parent */
1735 d_err
= dom_node_get_parent_node(n
, &p
);
1736 if (d_err
!= DOM_NO_ERR
) {
1737 lwc_string_destroy(attr
.name
);
1743 /* If we never find a match we may wind up here */
1744 lwc_string_destroy(attr
.name
);
1750 * User-agent defaults for CSS properties
1752 * For the moment, we provide no defaults, because libsvgtiny does not
1753 * yet support any CSS properties that might need them.
1755 * \param pw Pointer to the current SVG parser state; unused
1756 * \param property LibCSS property identifier; unused
1757 * \param hint Pointer to hint object (a return value); unused
1759 * \return Always returns CSS_INVALID
1761 css_error
ua_default_for_property(void *pw
, uint32_t property
,