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
);
49 * Resolve a relative URL to an absolute one by doing nothing. This is
50 * the simplest possible implementation of a URL resolver, needed for
53 css_error
svgtiny_resolve_url(void *pw
,
54 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
59 /* Copy the relative URL to the absolute one (the return
61 *abs
= lwc_string_ref(rel
);
66 * Create a stylesheet with the default set of params.
68 * \param sheet A stylesheet pointer, passed in by reference, that
69 * we use to store the newly-created stylesheet.
70 * \param inline_style True if this stylesheet represents an inline
71 * style, and false otherwise.
73 * \return The return value from css_stylesheet_create() is returned.
75 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
78 css_stylesheet_params params
;
80 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
81 params
.level
= CSS_LEVEL_DEFAULT
;
82 params
.charset
= NULL
;
85 params
.allow_quirks
= false;
86 params
.inline_style
= inline_style
;
87 params
.resolve
= svgtiny_resolve_url
;
88 params
.resolve_pw
= NULL
;
90 params
.import_pw
= NULL
;
92 params
.color_pw
= NULL
;
94 params
.font_pw
= NULL
;
96 return css_stylesheet_create(¶ms
, sheet
);
100 /**************************/
101 /* libcss select handlers */
102 /**************************/
104 * From here on we implement the "select handler "API defined in
105 * libcss's include/libcss/select.h and discussed briefly in its
111 * Retrieve the given node's name
113 * \param pw Pointer to the current SVG parser state
114 * \param node Libdom SVG node
115 * \param qname Address at which to store the node name
117 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
119 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
123 struct svgtiny_parse_state
*state
;
125 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
126 if (err
!= DOM_NO_ERR
) {
130 state
= (struct svgtiny_parse_state
*)pw
;
131 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
133 err
= dom_string_intern(name
, &qname
->name
);
134 if (err
!= DOM_NO_ERR
) {
135 dom_string_unref(name
);
139 dom_string_unref(name
);
146 * Retrieve the given node's classes
148 * \param pw Pointer to the current SVG parser state
149 * \param node Libdom SVG node
150 * \param classes Address at which to store the class name array
151 * \param n_classes Address at which to store the length of the class
154 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
156 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
157 * because the underlying libdom function never fails
159 css_error
node_classes(void *pw
, void *node
,
160 lwc_string
***classes
, uint32_t *n_classes
)
165 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
167 /* The implementation does not do it, but the documentation
168 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
169 possible here, so we handle it to be on the safe side. */
170 if (err
!= DOM_NO_ERR
) {
179 * Retrieve the given node's id
181 * \param pw Pointer to the current SVG parser state
182 * \param node Libdom SVG node
183 * \param id Address at which to store the id
185 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
187 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
191 struct svgtiny_parse_state
*state
;
193 /* Begin with the assumption that this node has no id */
196 state
= (struct svgtiny_parse_state
*)pw
;
197 err
= dom_element_get_attribute((dom_node
*)node
,
198 state
->interned_id
, &attr
);
199 if (err
!= DOM_NO_ERR
) {
202 else if (attr
== NULL
) {
203 /* The node has no id attribute and our return value
204 is already set to NULL so we're done */
208 /* If we found an id attribute (a dom_string), intern it into
209 an lwc_string that we can return, and then cleanup the
211 err
= dom_string_intern(attr
, id
);
212 if (err
!= DOM_NO_ERR
) {
213 dom_string_unref(attr
);
216 dom_string_unref(attr
);
223 * Find the first parent of the given element having the given name
225 * \param pw Pointer to the current SVG parser state
226 * \param node Libdom SVG node
227 * \param qname Name of the parent node to search for
228 * \param parent Address at which to store the parent node pointer
230 * \return Always returns CSS_OK
232 * \post If a suitable element is found, a pointer to it will be
233 * stored at the address pointed to by \a parent; otherwise,
234 * NULL will be stored at the address pointed to by \a parent
236 css_error
named_parent_node(void *pw
, void *node
,
237 const css_qname
*qname
, void **parent
)
240 /* dom_element_named_parent_node() was invented to implement
241 * this select handler so there isn't much for us to do except
242 * call it. It's OK if node isn't an element, libdom checks
244 dom_element_named_parent_node((dom_element
*)node
,
246 (struct dom_element
**)parent
);
248 /* Implementation detail: dom_element_named_parent_node()
249 * increments the reference count of the parent element before
250 * returning it to us. According to docs/RefCnt in the libdom
251 * repository, this will prevent the parent element from being
252 * destroyed if it is pruned from the DOM. That sounds good,
253 * since we don't want to be using a pointer to an object that
254 * has been destroyed... but we also have no way of later
255 * decrementing the reference count ourselves, and don't want
256 * to make the returned node eternal. Decrementing the
257 * reference counter now allows it to be destroyed when the
258 * DOM no longer needs it, and so long as no other parts of
259 * libsvgtiny are messing with the DOM during parsing, that
260 * shouldn't (ha ha) cause any problems. */
261 dom_node_unref(*parent
);
268 * Find the "next-sibling" of the given element having the given name
270 * This search corresponds to the "+ foo" combinator in CSS and will
271 * find only "foo" element nodes that immediately precede the given
272 * node under the same parent in the DOM. In CSS the tree is viewed
273 * top-down and in libdom it is viewed from the bottom-up; as a result
274 * "next" and "previous" are sometimes backwards. This is case-sensitive.
276 * \param pw Pointer to the current SVG parser state
277 * \param node Libdom SVG node
278 * \param qname Name of the sibling node to search for
279 * \param sibling Address at which to store the sibling node pointer
281 * \return Always returns CSS_OK
283 * \post If a suitable element is found, a pointer to it will be
284 * stored at the address pointed to by \a sibling; otherwise,
285 * NULL will be stored at the address pointed to by \a sibling
287 css_error
named_sibling_node(void *pw
, void *node
,
288 const css_qname
*qname
, void **sibling
)
291 dom_node
*n
= node
; /* the current node */
292 dom_node
*prev
; /* the previous node */
297 *sibling
= NULL
; /* default to nothing found */
299 /* Begin the search; the first iteration we do outside of the
300 * loop. Implementation detil: dom_node_get_previous_sibling()
301 * increments the reference counter on the returned node. A
302 * comment within named_parent_node() explains why we
303 * decrement it ASAP. */
304 err
= dom_node_get_previous_sibling(n
, &n
);
305 if (err
!= DOM_NO_ERR
) {
310 /* We're looking for the first ELEMENT sibling */
311 err
= dom_node_get_node_type(n
, &type
);
312 if (err
!= DOM_NO_ERR
) {
317 if (type
== DOM_ELEMENT_NODE
) {
318 /* We found an element node, does it have the
320 err
= dom_node_get_node_name(n
, &name
);
321 if (err
!= DOM_NO_ERR
) {
326 if (dom_string_lwc_isequal(name
,
328 /* The name is right, return it */
332 /* There's only one next-sibling element node
333 * and we've already found it, so if its name
334 * wasn't right, we return the default value
336 dom_string_unref(name
);
341 /* Not an element node, so we move on the the previous
342 * previous sibling */
343 err
= dom_node_get_previous_sibling(n
, &prev
);
344 if (err
!= DOM_NO_ERR
) {
358 * Find the first "subsequent-sibling" of the given element having the
361 * This search corresponds to the "~ foo" combinator in CSS and will
362 * find only "foo" element nodes that precede the given node (under
363 * the same parent) in the DOM. In CSS the tree is viewed top-down and
364 * in libdom it is viewed from the bottom-up; as a result "next" and
365 * "previous" are sometimes backwards. This is case-sensitive.
367 * \param pw Pointer to the current SVG parser state
368 * \param node Libdom SVG node
369 * \param qname Name of the sibling node to search for
370 * \param sibling Address at which to store the sibling node pointer
372 * \return Always returns CSS_OK
374 * \post If a suitable element is found, a pointer to it will be
375 * stored at the address pointed to by \a sibling; otherwise,
376 * NULL will be stored at the address pointed to by \a sibling
378 css_error
named_generic_sibling_node(void *pw
, void *node
,
379 const css_qname
*qname
, void **sibling
)
382 dom_node
*n
= node
; /* the current node */
383 dom_node
*prev
; /* the previous node */
389 *sibling
= NULL
; /* default to nothing found */
391 /* Begin the search; the first iteration we do outside of the
392 * loop. Implementation detil: dom_node_get_previous_sibling()
393 * increments the reference counter on the returned node. A
394 * comment within named_parent_node() explains why we
395 * decrement it ASAP. */
396 err
= dom_node_get_previous_sibling(n
, &n
);
397 if (err
!= DOM_NO_ERR
) {
402 err
= dom_node_get_node_type(n
, &type
);
403 if (err
!= DOM_NO_ERR
) {
408 if (type
== DOM_ELEMENT_NODE
) {
409 /* We only want ELEMENT nodes */
410 err
= dom_node_get_node_name(n
, &name
);
411 if (err
!= DOM_NO_ERR
) {
416 if (dom_string_lwc_isequal(name
,
418 /* Found one. Save it and stop the search */
419 dom_string_unref(name
);
425 dom_string_unref(name
);
428 /* This sibling wasn't an element with the desired
429 name, so move on to the previous sibling */
430 err
= dom_node_get_previous_sibling(n
, &prev
);
431 if (err
!= DOM_NO_ERR
) {
445 * Return a pointer to the given node's parent
447 * \param pw Pointer to the current SVG parser state
448 * \param node Libdom SVG node
449 * \param parent Address at which to store the node's parent pointer
451 * \return Always returns CSS_OK
453 css_error
parent_node(void *pw
, void *node
, void **parent
)
456 /* Libdom basically implements this for us */
457 dom_element_parent_node(node
, (struct dom_element
**)parent
);
459 /* See the comment in named_parent_node() for why we decrement
460 * this reference counter here. */
461 dom_node_unref(*parent
);
468 * Find the "next-sibling" of the given element
470 * This search corresponds "+ *" in CSS and will find the first
471 * element node that immediately precedes the given node under the
472 * same parent in the DOM. In CSS the tree is viewed top-down and in
473 * libdom it is viewed from the bottom-up; as a result "next" and
474 * "previous" are sometimes backwards.
476 * \param pw Pointer to the current SVG parser state
477 * \param node Libdom SVG node
478 * \param sibling Address at which to store the sibling node pointer
480 * \return Always returns CSS_OK
482 * \post If a suitable element is found, a pointer to it will be
483 * stored at the address pointed to by \a sibling; otherwise,
484 * NULL will be stored at the address pointed to by \a sibling
486 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
489 dom_node
*n
= node
; /* the current node */
490 dom_node
*prev
; /* the previous node */
494 *sibling
= NULL
; /* default to nothing found */
496 /* Begin the search; the first iteration we do outside of the
497 * loop. Implementation detil: dom_node_get_previous_sibling()
498 * increments the reference counter on the returned node. A
499 * comment within named_parent_node() explains why we
500 * decrement it ASAP. */
501 err
= dom_node_get_previous_sibling(n
, &n
);
502 if (err
!= DOM_NO_ERR
) {
507 err
= dom_node_get_node_type(n
, &type
);
508 if (err
!= DOM_NO_ERR
) {
513 if (type
== DOM_ELEMENT_NODE
) {
514 /* We found a sibling node that is also an
515 element and that's all we wanted. */
521 /* This sibling node was not an element; move on to
522 the previous sibling */
523 err
= dom_node_get_previous_sibling(n
, &prev
);
524 if (err
!= DOM_NO_ERR
) {
538 * Test the given node for the given name
540 * This will return true (via the "match" pointer) if the libdom node
541 * has the given name or if that name is the universal selector;
542 * otherwise it returns false. The comparison is case-sensitive. It
543 * corresponds to a rule like "body { ... }" in CSS.
545 * \param pw Pointer to the current SVG parser state
546 * \param node Libdom SVG node to test
547 * \param qname Name to check for
548 * \param match Pointer to the test result
550 * \return Always returns CSS_OK
552 css_error
node_has_name(void *pw
, void *node
,
553 const css_qname
*qname
, bool *match
)
555 struct svgtiny_parse_state
*state
;
559 /* Start by checking to see if qname is the universal selector */
560 state
= (struct svgtiny_parse_state
*)pw
;
561 if (lwc_string_isequal(qname
->name
,
562 state
->interned_universal
, match
) == lwc_error_ok
) {
564 /* It's the universal selector. In NetSurf, all node
565 * names match the universal selector, and nothing in
566 * the libcss documentation suggests another approach,
567 * so we follow NetSurf here. */
572 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
573 if (err
!= DOM_NO_ERR
) {
577 /* Unlike with HTML, SVG element names are case-sensitive */
578 *match
= dom_string_lwc_isequal(name
, qname
->name
);
579 dom_string_unref(name
);
586 * Test the given node for the given class
588 * This will return true (via the "match" pointer) if the libdom node
589 * has the given class. The comparison is case-sensitive. It
590 * corresponds to node.class in CSS.
592 * \param pw Pointer to the current SVG parser state
593 * \param node Libdom SVG node to test
594 * \param name Class name to check for
595 * \param match Pointer to the test result
597 * \return Always returns CSS_OK
599 css_error
node_has_class(void *pw
, void *node
,
600 lwc_string
*name
, bool *match
)
603 /* libdom implements this for us and apparently it cannot fail */
604 dom_element_has_class((dom_node
*)node
, name
, match
);
610 * Test the given node for the given id
612 * This will return true (via the "match" pointer) if the libdom node
613 * has the given id. The comparison is case-sensitive. It corresponds
616 * \param pw Pointer to the current SVG parser state
617 * \param node Libdom SVG node to test
618 * \param name Id to check for
619 * \param match Pointer to the test result
621 * \return Always returns CSS_OK
623 css_error
node_has_id(void *pw
, void *node
,
624 lwc_string
*name
, bool *match
)
628 struct svgtiny_parse_state
*state
;
630 attr
= NULL
; /* a priori the "id" attribute may not exist */
631 *match
= false; /* default to no match */
633 state
= (struct svgtiny_parse_state
*)pw
;
634 err
= dom_element_get_attribute((dom_node
*)node
,
635 state
->interned_id
, &attr
);
636 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
640 *match
= dom_string_lwc_isequal(attr
, name
);
641 dom_string_unref(attr
);
648 * Test the given node for the given attribute
650 * This will return true (via the "match" pointer) if the libdom node
651 * has an attribute with the given name. The comparison is
652 * case-sensitive. It corresponds to node[attr] in CSS.
654 * \param pw Pointer to the current SVG parser state
655 * \param node Libdom SVG node to test
656 * \param qname Attribute name to check for
657 * \param match Pointer to the test result
659 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
662 css_error
node_has_attribute(void *pw
, void *node
,
663 const css_qname
*qname
, bool *match
)
669 /* intern the attribute name as a dom_string so we can
670 * delegate to dom_element_has_attribute() */
671 err
= dom_string_create_interned(
672 (const uint8_t *) lwc_string_data(qname
->name
),
673 lwc_string_length(qname
->name
),
675 if (err
!= DOM_NO_ERR
) {
679 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
680 if (err
!= DOM_NO_ERR
) {
681 dom_string_unref(name
);
685 dom_string_unref(name
);
691 * Test the given node for an attribute with a specific value
693 * This will return true (via the "match" pointer) if the libdom node
694 * has an attribute with the given name and value. The comparison is
695 * case-sensitive. It corresponds to node[attr=value] in CSS.
697 * \param pw Pointer to the current SVG parser state
698 * \param node Libdom SVG node to test
699 * \param qname Attribute name to check for
700 * \param value Attribute value to check for
701 * \param match Pointer to the test result
703 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
704 * intern the attribute name (which usually indicates memory
707 css_error
node_has_attribute_equal(void *pw
, void *node
,
708 const css_qname
*qname
, lwc_string
*value
,
711 /* Implementation note: NetSurf always returns "no match" when
712 * the value is empty (length zero). We allow it, because why
717 dom_string
*attr_val
;
720 /* Intern the attribute name as a dom_string so we can
721 * use dom_element_get_attribute() */
722 err
= dom_string_create_interned(
723 (const uint8_t *) lwc_string_data(qname
->name
),
724 lwc_string_length(qname
->name
),
726 if (err
!= DOM_NO_ERR
) {
730 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
731 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
732 /* There was an error getting the attribute's value or
733 * the attribute doesn't exist. So, no match? */
734 dom_string_unref(name
);
739 /* Otherwise, we have the attribute value from the given node
740 * and all we need to do is compare. */
741 dom_string_unref(name
);
742 *match
= dom_string_lwc_isequal(attr_val
, value
);
743 dom_string_unref(attr_val
);
750 * Test the given node for an attribute with a specific value,
751 * possibly followed by a single hyphen
753 * This will return true (via the "match" pointer) if the libdom node
754 * has an attribute with the given name and value or with the given
755 * name and a value that is followed by exactly one hyphen. The
756 * comparison is case-sensitive. This corresponds to [attr|=value]
759 * \param pw Pointer to the current SVG parser state
760 * \param node Libdom SVG node to test
761 * \param qname Attribute name to check for
762 * \param value Attribute value to check for
763 * \param match Pointer to the test result
765 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
766 * intern the attribute name (which usually indicates memory
769 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
770 const css_qname
*qname
, lwc_string
*value
,
773 /* Implementation note: NetSurf always returns "no match" when
774 * the value is empty (length zero). We allow it, because why
779 dom_string
*attr_val
;
782 const char *vdata
; /* to hold the data underlying "value" */
784 const char *avdata
; /* to hold the found attribute value data */
787 /* Intern the attribute name as a dom_string so we can
788 * use dom_element_get_attribute() */
789 err
= dom_string_create_interned(
790 (const uint8_t *) lwc_string_data(qname
->name
),
791 lwc_string_length(qname
->name
),
793 if (err
!= DOM_NO_ERR
) {
797 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
798 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
799 /* There was an error getting the attribute's value or
800 * the attribute doesn't exist. So, no match? */
801 dom_string_unref(name
);
806 /* Otherwise, we have the attribute value from the given node
807 * and all we need to do is compare. */
808 dom_string_unref(name
);
809 *match
= dom_string_lwc_isequal(attr_val
, value
);
811 /* Exact match, we're done */
812 dom_string_unref(attr_val
);
816 /* No exact match, try it with a hyphen on the end */
817 vdata
= lwc_string_data(value
); /* needle */
818 vdata_len
= lwc_string_length(value
);
819 avdata
= dom_string_data(attr_val
); /* haystack */
820 avdata_len
= dom_string_byte_length(attr_val
);
821 dom_string_unref(attr_val
);
823 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
824 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
825 /* If there's a hyphen in the right position,
826 * it suffices to compare the strings only up
837 * Test the given node for an attribute whose value is a
838 * space-separated list of words, one of which is the given word
840 * This will return true (via the "match" pointer) if the libdom node
841 * has an attribute with the given name and whose value when
842 * considered as a space-separated list of words contains the given
843 * word. The comparison is case-sensitive. This corresponds to
844 * [attr~=value] in CSS.
846 * \param pw Pointer to the current SVG parser state
847 * \param node Libdom SVG node to test
848 * \param qname Attribute name to check for
849 * \param word Value word to check for
850 * \param match Pointer to the test result
852 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
853 * intern the attribute name (which usually indicates memory
856 css_error
node_has_attribute_includes(void *pw
, void *node
,
857 const css_qname
*qname
, lwc_string
*word
,
863 dom_string
*attr_val
;
865 size_t wordlen
; /* length of "word" */
867 /* pointers used to parse a space-separated list of words */
872 *match
= false; /* default to no match */
874 wordlen
= lwc_string_length(word
);
876 /* In this case, the spec says that "if 'val' is the
877 * empty string, it will never represent anything." */
881 /* Intern the attribute name as a dom_string so we can
882 * use dom_element_get_attribute() */
883 err
= dom_string_create_interned(
884 (const uint8_t *) lwc_string_data(qname
->name
),
885 lwc_string_length(qname
->name
),
887 if (err
!= DOM_NO_ERR
) {
891 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
892 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
893 /* There was an error getting the attribute's value or
894 * the attribute doesn't exist. So, no match? */
895 dom_string_unref(name
);
899 /* Parse the list comparing each word against "word" */
900 start
= dom_string_data(attr_val
);
901 end
= start
+ dom_string_byte_length(attr_val
);
902 dom_string_unref(attr_val
);
904 for (p
= start
; p
<= end
; p
++) {
905 /* Move forward until we find the end of the first word */
906 if (*p
== ' ' || *p
== '\0') {
907 /* If the length of that word is the length of the
908 * word we're looking for, do the comparison. */
909 if ((size_t) (p
- start
) == wordlen
&&
911 lwc_string_data(word
),
916 /* No match? Set "start" to the beginning of
917 * the next word and loop. */
927 * Test the given node for an attribute whose value begins with the
930 * This will return true (via the "match" pointer) if the libdom node
931 * has an attribute with the given name and whose value begins with
932 * the given prefix string. The comparison is case-sensitive. This
933 * corresponds to [attr^=value] in CSS.
935 * \param pw Pointer to the current SVG parser state
936 * \param node Libdom SVG node to test
937 * \param qname Attribute name to check for
938 * \param prefix Value prefix to check for
939 * \param match Pointer to the test result
941 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
942 * intern the attribute name (which usually indicates memory
945 css_error
node_has_attribute_prefix(void *pw
, void *node
,
946 const css_qname
*qname
, lwc_string
*prefix
,
951 dom_string
*attr_val
;
953 const char *avdata
; /* attribute value data */
954 size_t avdata_len
; /* length of that attribute value data */
955 size_t prefixlen
; /* length of "prefix" */
957 prefixlen
= lwc_string_length(prefix
);
958 if (prefixlen
== 0) {
959 /* In this case, the spec says that "if 'val' is the
960 * empty string, it will never represent anything." */
964 /* Intern the attribute name as a dom_string so we can
965 * use dom_element_get_attribute() */
966 err
= dom_string_create_interned(
967 (const uint8_t *) lwc_string_data(qname
->name
),
968 lwc_string_length(qname
->name
),
970 if (err
!= DOM_NO_ERR
) {
974 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
975 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
976 /* There was an error getting the attribute's value or
977 * the attribute doesn't exist. So, no match? */
978 dom_string_unref(name
);
983 /* Otherwise, we have the attribute value from the given node,
984 * and the first thing we want to do is check to see if the
985 * whole thing matches the prefix. */
986 dom_string_unref(name
);
987 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
989 /* If not, check to see if an, uh, prefix matches the
991 if (*match
== false) {
992 avdata
= dom_string_data(attr_val
);
993 avdata_len
= dom_string_byte_length(attr_val
);
994 if ((avdata_len
>= prefixlen
) &&
996 lwc_string_data(prefix
),
998 /* Use strncasecmp to compare only the first
999 * "n" characters, where "n" is the length of
1005 dom_string_unref(attr_val
);
1012 * Test the given node for an attribute whose value end with the
1015 * This will return true (via the "match" pointer) if the libdom node
1016 * has an attribute with the given name and whose value ends with
1017 * the given suffix string. The comparison is case-sensitive. This
1018 * corresponds to [attr$=value] in CSS.
1020 * \param pw Pointer to the current SVG parser state
1021 * \param node Libdom SVG node to test
1022 * \param qname Attribute name to check for
1023 * \param suffix Value suffix to check for
1024 * \param match Pointer to the test result
1026 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1027 * intern the attribute name (which usually indicates memory
1030 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1031 const css_qname
*qname
, lwc_string
*suffix
,
1036 dom_string
*attr_val
;
1038 const char *avdata
; /* attribute value data */
1039 size_t avdata_len
; /* length of that attribute value data */
1040 size_t suffixlen
; /* length of "suffix" */
1042 /* convenience pointer we'll use when matching the suffix */
1043 const char *suffix_start
;
1045 suffixlen
= lwc_string_length(suffix
);
1046 if (suffixlen
== 0) {
1047 /* In this case, the spec says that "if 'val' is the
1048 * empty string, it will never represent anything." */
1052 /* Intern the attribute name as a dom_string so we can
1053 * use dom_element_get_attribute() */
1054 err
= dom_string_create_interned(
1055 (const uint8_t *) lwc_string_data(qname
->name
),
1056 lwc_string_length(qname
->name
),
1058 if (err
!= DOM_NO_ERR
) {
1062 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1063 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1064 /* There was an error getting the attribute's value or
1065 * the attribute doesn't exist. So, no match? */
1066 dom_string_unref(name
);
1071 /* Otherwise, we have the attribute value from the given node,
1072 * and the first thing we want to do is check to see if the
1073 * whole thing matches the suffix. */
1074 dom_string_unref(name
);
1075 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1077 /* If not, check to see if an, uh, suffix matches the
1079 if (*match
== false) {
1080 avdata
= dom_string_data(attr_val
);
1081 avdata_len
= dom_string_byte_length(attr_val
);
1083 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1085 if ((avdata_len
>= suffixlen
) &&
1086 (strncasecmp(suffix_start
,
1087 lwc_string_data(suffix
),
1089 /* Use strncasecmp to compare only the last
1090 * "n" characters, where "n" is the length of
1096 dom_string_unref(attr_val
);
1103 * Test the given node for an attribute whose value contains the
1106 * This will return true (via the "match" pointer) if the libdom node
1107 * has an attribute with the given name and whose value contains the
1108 * given substring. The comparison is case-sensitive. This corresponds
1109 * to [attr*=value] in CSS.
1111 * \param pw Pointer to the current SVG parser state
1112 * \param node Libdom SVG node to test
1113 * \param qname Attribute name to check for
1114 * \param substring Value substring to check for
1115 * \param match Pointer to the test result
1117 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1118 * intern the attribute name (which usually indicates memory
1121 css_error
node_has_attribute_substring(void *pw
, void *node
,
1122 const css_qname
*qname
, lwc_string
*substring
,
1127 dom_string
*attr_val
;
1129 size_t attr_len
; /* length of attr_val */
1130 size_t substrlen
; /* length of "substring" */
1132 /* Convenience pointers we use when comparing substrings */
1136 substrlen
= lwc_string_length(substring
);
1137 if (substrlen
== 0) {
1138 /* In this case, the spec says that "if 'val' is the
1139 * empty string, it will never represent anything." */
1143 /* Intern the attribute name as a dom_string so we can
1144 * use dom_element_get_attribute() */
1145 err
= dom_string_create_interned(
1146 (const uint8_t *) lwc_string_data(qname
->name
),
1147 lwc_string_length(qname
->name
),
1149 if (err
!= DOM_NO_ERR
) {
1153 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1154 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1155 /* There was an error getting the attribute's value or
1156 * the attribute doesn't exist. So, no match? */
1157 dom_string_unref(name
);
1162 /* Otherwise, we have the attribute value from the given node,
1163 * and the first thing we want to do is check to see if the
1164 * whole thing matches the substring. */
1165 dom_string_unref(name
);
1166 *match
= dom_string_lwc_isequal(attr_val
, substring
);
1168 /* If not, check to see if an, uh, substring matches the
1170 if (*match
== false) {
1171 p
= dom_string_data(attr_val
);
1173 /* Check every long-enough suffix for a prefix match */
1174 attr_len
= dom_string_byte_length(attr_val
);
1175 if (attr_len
>= substrlen
) {
1176 p_max
= p
+ attr_len
- substrlen
;
1177 while (p
<= p_max
) {
1179 lwc_string_data(substring
),
1189 dom_string_unref(attr_val
);
1196 * Test whether or not the given node is the document's root element
1197 * This corresponds to the CSS :root pseudo-selector.
1199 * \param pw Pointer to the current SVG parser state
1200 * \param node Libdom SVG node to test
1201 * \param match Pointer to the test result
1203 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1205 css_error
node_is_root(void *pw
, void *node
, bool *match
)
1212 err
= dom_node_get_parent_node((dom_node
*)node
, &parent
);
1213 if (err
!= DOM_NO_ERR
) {
1217 /* It's the root element if it doesn't have a parent element */
1218 if (parent
!= NULL
) {
1219 err
= dom_node_get_node_type(parent
, &type
);
1220 dom_node_unref(parent
);
1221 if (err
!= DOM_NO_ERR
) {
1224 if (type
!= DOM_DOCUMENT_NODE
) {
1225 /* DOM_DOCUMENT_NODE is the only allowable
1226 * type of parent node for the root element */