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
,
45 * Resolve a relative URL to an absolute one by doing nothing. This is
46 * the simplest possible implementation of a URL resolver, needed for
49 css_error
svgtiny_resolve_url(void *pw
,
50 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
55 /* Copy the relative URL to the absolute one (the return
57 *abs
= lwc_string_ref(rel
);
62 * Create a stylesheet with the default set of params.
64 * \param sheet A stylesheet pointer, passed in by reference, that
65 * we use to store the newly-created stylesheet.
66 * \param inline_style True if this stylesheet represents an inline
67 * style, and false otherwise.
69 * \return The return value from css_stylesheet_create() is returned.
71 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
74 css_stylesheet_params params
;
76 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
77 params
.level
= CSS_LEVEL_DEFAULT
;
78 params
.charset
= NULL
;
81 params
.allow_quirks
= false;
82 params
.inline_style
= inline_style
;
83 params
.resolve
= svgtiny_resolve_url
;
84 params
.resolve_pw
= NULL
;
86 params
.import_pw
= NULL
;
88 params
.color_pw
= NULL
;
90 params
.font_pw
= NULL
;
92 return css_stylesheet_create(¶ms
, sheet
);
96 /**************************/
97 /* libcss select handlers */
98 /**************************/
100 * From here on we implement the "select handler "API defined in
101 * libcss's include/libcss/select.h and discussed briefly in its
107 * Retrieve the given node's name
109 * \param pw Pointer to the current SVG parser state
110 * \param node Libdom SVG node
111 * \param qname Address at which to store the node name
113 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
115 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
119 struct svgtiny_parse_state
*state
;
121 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
122 if (err
!= DOM_NO_ERR
) {
126 state
= (struct svgtiny_parse_state
*)pw
;
127 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
129 err
= dom_string_intern(name
, &qname
->name
);
130 if (err
!= DOM_NO_ERR
) {
131 dom_string_unref(name
);
135 dom_string_unref(name
);
142 * Retrieve the given node's classes
144 * \param pw Pointer to the current SVG parser state
145 * \param node Libdom SVG node
146 * \param classes Address at which to store the class name array
147 * \param n_classes Address at which to store the length of the class
150 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
152 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
153 * because the underlying libdom function never fails
155 css_error
node_classes(void *pw
, void *node
,
156 lwc_string
***classes
, uint32_t *n_classes
)
161 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
163 /* The implementation does not do it, but the documentation
164 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
165 possible here, so we handle it to be on the safe side. */
166 if (err
!= DOM_NO_ERR
) {
175 * Retrieve the given node's id
177 * \param pw Pointer to the current SVG parser state
178 * \param node Libdom SVG node
179 * \param id Address at which to store the id
181 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
183 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
187 struct svgtiny_parse_state
*state
;
189 /* Begin with the assumption that this node has no id */
192 state
= (struct svgtiny_parse_state
*)pw
;
193 err
= dom_element_get_attribute((dom_node
*)node
,
194 state
->interned_id
, &attr
);
195 if (err
!= DOM_NO_ERR
) {
198 else if (attr
== NULL
) {
199 /* The node has no id attribute and our return value
200 is already set to NULL so we're done */
204 /* If we found an id attribute (a dom_string), intern it into
205 an lwc_string that we can return, and then cleanup the
207 err
= dom_string_intern(attr
, id
);
208 if (err
!= DOM_NO_ERR
) {
209 dom_string_unref(attr
);
212 dom_string_unref(attr
);
219 * Find the first parent of the given element having the given name
221 * \param pw Pointer to the current SVG parser state
222 * \param node Libdom SVG node
223 * \param qname Name of the parent node to search for
224 * \param parent Address at which to store the parent node pointer
226 * \return Always returns CSS_OK
228 * \post If a suitable element is found, a pointer to it will be
229 * stored at the address pointed to by \a parent; otherwise,
230 * NULL will be stored at the address pointed to by \a parent
232 css_error
named_parent_node(void *pw
, void *node
,
233 const css_qname
*qname
, void **parent
)
236 /* dom_element_named_parent_node() was invented to implement
237 * this select handler so there isn't much for us to do except
238 * call it. It's OK if node isn't an element, libdom checks
240 dom_element_named_parent_node((dom_element
*)node
,
242 (struct dom_element
**)parent
);
244 /* Implementation detail: dom_element_named_parent_node()
245 * increments the reference count of the parent element before
246 * returning it to us. According to docs/RefCnt in the libdom
247 * repository, this will prevent the parent element from being
248 * destroyed if it is pruned from the DOM. That sounds good,
249 * since we don't want to be using a pointer to an object that
250 * has been destroyed... but we also have no way of later
251 * decrementing the reference count ourselves, and don't want
252 * to make the returned node eternal. Decrementing the
253 * reference counter now allows it to be destroyed when the
254 * DOM no longer needs it, and so long as no other parts of
255 * libsvgtiny are messing with the DOM during parsing, that
256 * shouldn't (ha ha) cause any problems. */
257 dom_node_unref(*parent
);
264 * Find the "next-sibling" of the given element having the given name
266 * This search corresponds to the "+ foo" combinator in CSS and will
267 * find only "foo" element nodes that immediately precede the given
268 * node under the same parent in the DOM. In CSS the tree is viewed
269 * top-down and in libdom it is viewed from the bottom-up; as a result
270 * "next" and "previous" are sometimes backwards. This is case-sensitive.
272 * \param pw Pointer to the current SVG parser state
273 * \param node Libdom SVG node
274 * \param qname Name of the sibling node to search for
275 * \param sibling Address at which to store the sibling node pointer
277 * \return Always returns CSS_OK
279 * \post If a suitable element is found, a pointer to it will be
280 * stored at the address pointed to by \a sibling; otherwise,
281 * NULL will be stored at the address pointed to by \a sibling
283 css_error
named_sibling_node(void *pw
, void *node
,
284 const css_qname
*qname
, void **sibling
)
287 dom_node
*n
= node
; /* the current node */
288 dom_node
*prev
; /* the previous node */
293 *sibling
= NULL
; /* default to nothing found */
295 /* Begin the search; the first iteration we do outside of the
296 * loop. Implementation detil: dom_node_get_previous_sibling()
297 * increments the reference counter on the returned node. A
298 * comment within named_parent_node() explains why we
299 * decrement it ASAP. */
300 err
= dom_node_get_previous_sibling(n
, &n
);
301 if (err
!= DOM_NO_ERR
) {
306 /* We're looking for the first ELEMENT sibling */
307 err
= dom_node_get_node_type(n
, &type
);
308 if (err
!= DOM_NO_ERR
) {
313 if (type
== DOM_ELEMENT_NODE
) {
314 /* We found an element node, does it have the
316 err
= dom_node_get_node_name(n
, &name
);
317 if (err
!= DOM_NO_ERR
) {
322 if (dom_string_lwc_isequal(name
,
324 /* The name is right, return it */
328 /* There's only one next-sibling element node
329 * and we've already found it, so if its name
330 * wasn't right, we return the default value
332 dom_string_unref(name
);
337 /* Not an element node, so we move on the the previous
338 * previous sibling */
339 err
= dom_node_get_previous_sibling(n
, &prev
);
340 if (err
!= DOM_NO_ERR
) {
354 * Find the first "subsequent-sibling" of the given element having the
357 * This search corresponds to the "~ foo" combinator in CSS and will
358 * find only "foo" element nodes that precede the given node (under
359 * the same parent) in the DOM. In CSS the tree is viewed top-down and
360 * in libdom it is viewed from the bottom-up; as a result "next" and
361 * "previous" are sometimes backwards. This is case-sensitive.
363 * \param pw Pointer to the current SVG parser state
364 * \param node Libdom SVG node
365 * \param qname Name of the sibling node to search for
366 * \param sibling Address at which to store the sibling node pointer
368 * \return Always returns CSS_OK
370 * \post If a suitable element is found, a pointer to it will be
371 * stored at the address pointed to by \a sibling; otherwise,
372 * NULL will be stored at the address pointed to by \a sibling
374 css_error
named_generic_sibling_node(void *pw
, void *node
,
375 const css_qname
*qname
, void **sibling
)
378 dom_node
*n
= node
; /* the current node */
379 dom_node
*prev
; /* the previous node */
385 *sibling
= NULL
; /* default to nothing found */
387 /* Begin the search; the first iteration we do outside of the
388 * loop. Implementation detil: dom_node_get_previous_sibling()
389 * increments the reference counter on the returned node. A
390 * comment within named_parent_node() explains why we
391 * decrement it ASAP. */
392 err
= dom_node_get_previous_sibling(n
, &n
);
393 if (err
!= DOM_NO_ERR
) {
398 err
= dom_node_get_node_type(n
, &type
);
399 if (err
!= DOM_NO_ERR
) {
404 if (type
== DOM_ELEMENT_NODE
) {
405 /* We only want ELEMENT nodes */
406 err
= dom_node_get_node_name(n
, &name
);
407 if (err
!= DOM_NO_ERR
) {
412 if (dom_string_lwc_isequal(name
,
414 /* Found one. Save it and stop the search */
415 dom_string_unref(name
);
421 dom_string_unref(name
);
424 /* This sibling wasn't an element with the desired
425 name, so move on to the previous sibling */
426 err
= dom_node_get_previous_sibling(n
, &prev
);
427 if (err
!= DOM_NO_ERR
) {
441 * Return a pointer to the given node's parent
443 * \param pw Pointer to the current SVG parser state
444 * \param node Libdom SVG node
445 * \param parent Address at which to store the node's parent pointer
447 * \return Always returns CSS_OK
449 css_error
parent_node(void *pw
, void *node
, void **parent
)
452 /* Libdom basically implements this for us */
453 dom_element_parent_node(node
, (struct dom_element
**)parent
);
455 /* See the comment in named_parent_node() for why we decrement
456 * this reference counter here. */
457 dom_node_unref(*parent
);
464 * Find the "next-sibling" of the given element
466 * This search corresponds "+ *" in CSS and will find the first
467 * element node that immediately precedes the given node under the
468 * same parent in the DOM. In CSS the tree is viewed top-down and in
469 * libdom it is viewed from the bottom-up; as a result "next" and
470 * "previous" are sometimes backwards.
472 * \param pw Pointer to the current SVG parser state
473 * \param node Libdom SVG node
474 * \param sibling Address at which to store the sibling node pointer
476 * \return Always returns CSS_OK
478 * \post If a suitable element is found, a pointer to it will be
479 * stored at the address pointed to by \a sibling; otherwise,
480 * NULL will be stored at the address pointed to by \a sibling
482 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
485 dom_node
*n
= node
; /* the current node */
486 dom_node
*prev
; /* the previous node */
490 *sibling
= NULL
; /* default to nothing found */
492 /* Begin the search; the first iteration we do outside of the
493 * loop. Implementation detil: dom_node_get_previous_sibling()
494 * increments the reference counter on the returned node. A
495 * comment within named_parent_node() explains why we
496 * decrement it ASAP. */
497 err
= dom_node_get_previous_sibling(n
, &n
);
498 if (err
!= DOM_NO_ERR
) {
503 err
= dom_node_get_node_type(n
, &type
);
504 if (err
!= DOM_NO_ERR
) {
509 if (type
== DOM_ELEMENT_NODE
) {
510 /* We found a sibling node that is also an
511 element and that's all we wanted. */
517 /* This sibling node was not an element; move on to
518 the previous sibling */
519 err
= dom_node_get_previous_sibling(n
, &prev
);
520 if (err
!= DOM_NO_ERR
) {
534 * Test the given node for the given name
536 * This will return true (via the "match" pointer) if the libdom node
537 * has the given name or if that name is the universal selector;
538 * otherwise it returns false. The comparison is case-sensitive. It
539 * corresponds to a rule like "body { ... }" in CSS.
541 * \param pw Pointer to the current SVG parser state
542 * \param node Libdom SVG node to test
543 * \param qname Name to check for
544 * \param match Pointer to the test result
546 * \return Always returns CSS_OK
548 css_error
node_has_name(void *pw
, void *node
,
549 const css_qname
*qname
, bool *match
)
551 struct svgtiny_parse_state
*state
;
555 /* Start by checking to see if qname is the universal selector */
556 state
= (struct svgtiny_parse_state
*)pw
;
557 if (lwc_string_isequal(qname
->name
,
558 state
->interned_universal
, match
) == lwc_error_ok
) {
560 /* It's the universal selector. In NetSurf, all node
561 * names match the universal selector, and nothing in
562 * the libcss documentation suggests another approach,
563 * so we follow NetSurf here. */
568 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
569 if (err
!= DOM_NO_ERR
) {
573 /* Unlike with HTML, SVG element names are case-sensitive */
574 *match
= dom_string_lwc_isequal(name
, qname
->name
);
575 dom_string_unref(name
);
582 * Test the given node for the given class
584 * This will return true (via the "match" pointer) if the libdom node
585 * has the given class. The comparison is case-sensitive. It
586 * corresponds to node.class in CSS.
588 * \param pw Pointer to the current SVG parser state
589 * \param node Libdom SVG node to test
590 * \param name Class name to check for
591 * \param match Pointer to the test result
593 * \return Always returns CSS_OK
595 css_error
node_has_class(void *pw
, void *node
,
596 lwc_string
*name
, bool *match
)
599 /* libdom implements this for us and apparently it cannot fail */
600 dom_element_has_class((dom_node
*)node
, name
, match
);
606 * Test the given node for the given id
608 * This will return true (via the "match" pointer) if the libdom node
609 * has the given id. The comparison is case-sensitive. It corresponds
612 * \param pw Pointer to the current SVG parser state
613 * \param node Libdom SVG node to test
614 * \param name Id to check for
615 * \param match Pointer to the test result
617 * \return Always returns CSS_OK
619 css_error
node_has_id(void *pw
, void *node
,
620 lwc_string
*name
, bool *match
)
624 struct svgtiny_parse_state
*state
;
626 attr
= NULL
; /* a priori the "id" attribute may not exist */
627 *match
= false; /* default to no match */
629 state
= (struct svgtiny_parse_state
*)pw
;
630 err
= dom_element_get_attribute((dom_node
*)node
,
631 state
->interned_id
, &attr
);
632 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
636 *match
= dom_string_lwc_isequal(attr
, name
);
637 dom_string_unref(attr
);
644 * Test the given node for the given attribute
646 * This will return true (via the "match" pointer) if the libdom node
647 * has an attribute with the given name. The comparison is
648 * case-sensitive. It corresponds to node[attr] in CSS.
650 * \param pw Pointer to the current SVG parser state
651 * \param node Libdom SVG node to test
652 * \param qname Attribute name to check for
653 * \param match Pointer to the test result
655 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
658 css_error
node_has_attribute(void *pw
, void *node
,
659 const css_qname
*qname
, bool *match
)
665 /* intern the attribute name as a dom_string so we can
666 * delegate to dom_element_has_attribute() */
667 err
= dom_string_create_interned(
668 (const uint8_t *) lwc_string_data(qname
->name
),
669 lwc_string_length(qname
->name
),
671 if (err
!= DOM_NO_ERR
) {
675 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
676 if (err
!= DOM_NO_ERR
) {
677 dom_string_unref(name
);
681 dom_string_unref(name
);
687 * Test the given node for an attribute with a specific value
689 * This will return true (via the "match" pointer) if the libdom node
690 * has an attribute with the given name and value. The comparison is
691 * case-sensitive. It corresponds to node[attr=value] in CSS.
693 * \param pw Pointer to the current SVG parser state
694 * \param node Libdom SVG node to test
695 * \param qname Attribute name to check for
696 * \param value Attribute value to check for
697 * \param match Pointer to the test result
699 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
700 * intern the attribute name (which usually indicates memory
703 css_error
node_has_attribute_equal(void *pw
, void *node
,
704 const css_qname
*qname
, lwc_string
*value
,
707 /* Implementation note: NetSurf always returns "no match" when
708 * the value is empty (length zero). We allow it, because why
713 dom_string
*attr_val
;
716 /* Intern the attribute name as a dom_string so we can
717 * use dom_element_get_attribute() */
718 err
= dom_string_create_interned(
719 (const uint8_t *) lwc_string_data(qname
->name
),
720 lwc_string_length(qname
->name
),
722 if (err
!= DOM_NO_ERR
) {
726 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
727 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
728 /* There was an error getting the attribute's value or
729 * the attribute doesn't exist. So, no match? */
730 dom_string_unref(name
);
735 /* Otherwise, we have the attribute value from the given node
736 * and all we need to do is compare. */
737 dom_string_unref(name
);
738 *match
= dom_string_lwc_isequal(attr_val
, value
);
739 dom_string_unref(attr_val
);
746 * Test the given node for an attribute with a specific value,
747 * possibly followed by a single hyphen
749 * This will return true (via the "match" pointer) if the libdom node
750 * has an attribute with the given name and value or with the given
751 * name and a value that is followed by exactly one hyphen. The
752 * comparison is case-sensitive. This corresponds to [attr|=value]
755 * \param pw Pointer to the current SVG parser state
756 * \param node Libdom SVG node to test
757 * \param qname Attribute name to check for
758 * \param value Attribute value to check for
759 * \param match Pointer to the test result
761 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
762 * intern the attribute name (which usually indicates memory
765 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
766 const css_qname
*qname
, lwc_string
*value
,
769 /* Implementation note: NetSurf always returns "no match" when
770 * the value is empty (length zero). We allow it, because why
775 dom_string
*attr_val
;
778 const char *vdata
; /* to hold the data underlying "value" */
780 const char *avdata
; /* to hold the found attribute value data */
783 /* Intern the attribute name as a dom_string so we can
784 * use dom_element_get_attribute() */
785 err
= dom_string_create_interned(
786 (const uint8_t *) lwc_string_data(qname
->name
),
787 lwc_string_length(qname
->name
),
789 if (err
!= DOM_NO_ERR
) {
793 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
794 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
795 /* There was an error getting the attribute's value or
796 * the attribute doesn't exist. So, no match? */
797 dom_string_unref(name
);
802 /* Otherwise, we have the attribute value from the given node
803 * and all we need to do is compare. */
804 dom_string_unref(name
);
805 *match
= dom_string_lwc_isequal(attr_val
, value
);
807 /* Exact match, we're done */
808 dom_string_unref(attr_val
);
812 /* No exact match, try it with a hyphen on the end */
813 vdata
= lwc_string_data(value
); /* needle */
814 vdata_len
= lwc_string_length(value
);
815 avdata
= dom_string_data(attr_val
); /* haystack */
816 avdata_len
= dom_string_byte_length(attr_val
);
817 dom_string_unref(attr_val
);
819 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
820 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
821 /* If there's a hyphen in the right position,
822 * it suffices to compare the strings only up
833 * Test the given node for an attribute whose value is a
834 * space-separated list of words, one of which is the given word
836 * This will return true (via the "match" pointer) if the libdom node
837 * has an attribute with the given name and whose value when
838 * considered as a space-separated list of words contains the given
839 * word. The comparison is case-sensitive. This corresponds to
840 * [attr~=value] in CSS.
842 * \param pw Pointer to the current SVG parser state
843 * \param node Libdom SVG node to test
844 * \param qname Attribute name to check for
845 * \param word Value word to check for
846 * \param match Pointer to the test result
848 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
849 * intern the attribute name (which usually indicates memory
852 css_error
node_has_attribute_includes(void *pw
, void *node
,
853 const css_qname
*qname
, lwc_string
*word
,
859 dom_string
*attr_val
;
861 size_t wordlen
; /* length of "word" */
863 /* pointers used to parse a space-separated list of words */
868 *match
= false; /* default to no match */
870 wordlen
= lwc_string_length(word
);
872 /* In this case, the spec says that "if 'val' is the
873 * empty string, it will never represent anything." */
877 /* Intern the attribute name as a dom_string so we can
878 * use dom_element_get_attribute() */
879 err
= dom_string_create_interned(
880 (const uint8_t *) lwc_string_data(qname
->name
),
881 lwc_string_length(qname
->name
),
883 if (err
!= DOM_NO_ERR
) {
887 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
888 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
889 /* There was an error getting the attribute's value or
890 * the attribute doesn't exist. So, no match? */
891 dom_string_unref(name
);
895 /* Parse the list comparing each word against "word" */
896 start
= dom_string_data(attr_val
);
897 end
= start
+ dom_string_byte_length(attr_val
);
898 dom_string_unref(attr_val
);
900 for (p
= start
; p
<= end
; p
++) {
901 /* Move forward until we find the end of the first word */
902 if (*p
== ' ' || *p
== '\0') {
903 /* If the length of that word is the length of the
904 * word we're looking for, do the comparison. */
905 if ((size_t) (p
- start
) == wordlen
&&
907 lwc_string_data(word
),
912 /* No match? Set "start" to the beginning of
913 * the next word and loop. */
923 * Test the given node for an attribute whose value begins with the
926 * This will return true (via the "match" pointer) if the libdom node
927 * has an attribute with the given name and whose value begins with
928 * the given prefix string. The comparison is case-sensitive. This
929 * corresponds to [attr^=value] in CSS.
931 * \param pw Pointer to the current SVG parser state
932 * \param node Libdom SVG node to test
933 * \param qname Attribute name to check for
934 * \param prefix Value prefix to check for
935 * \param match Pointer to the test result
937 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
938 * intern the attribute name (which usually indicates memory
941 css_error
node_has_attribute_prefix(void *pw
, void *node
,
942 const css_qname
*qname
, lwc_string
*prefix
,
947 dom_string
*attr_val
;
949 const char *avdata
; /* attribute value data */
950 size_t avdata_len
; /* length of that attribute value data */
951 size_t prefixlen
; /* length of "prefix" */
953 prefixlen
= lwc_string_length(prefix
);
954 if (prefixlen
== 0) {
955 /* In this case, the spec says that "if 'val' is the
956 * empty string, it will never represent anything." */
960 /* Intern the attribute name as a dom_string so we can
961 * use dom_element_get_attribute() */
962 err
= dom_string_create_interned(
963 (const uint8_t *) lwc_string_data(qname
->name
),
964 lwc_string_length(qname
->name
),
966 if (err
!= DOM_NO_ERR
) {
970 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
971 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
972 /* There was an error getting the attribute's value or
973 * the attribute doesn't exist. So, no match? */
974 dom_string_unref(name
);
979 /* Otherwise, we have the attribute value from the given node,
980 * and the first thing we want to do is check to see if the
981 * whole thing matches the prefix. */
982 dom_string_unref(name
);
983 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
985 /* If not, check to see if an, uh, prefix matches the
987 if (*match
== false) {
988 avdata
= dom_string_data(attr_val
);
989 avdata_len
= dom_string_byte_length(attr_val
);
990 if ((avdata_len
>= prefixlen
) &&
992 lwc_string_data(prefix
),
994 /* Use strncasecmp to compare only the first
995 * "n" characters, where "n" is the length of
1001 dom_string_unref(attr_val
);
1008 * Test the given node for an attribute whose value end with the
1011 * This will return true (via the "match" pointer) if the libdom node
1012 * has an attribute with the given name and whose value ends with
1013 * the given suffix string. The comparison is case-sensitive. This
1014 * corresponds to [attr$=value] in CSS.
1016 * \param pw Pointer to the current SVG parser state
1017 * \param node Libdom SVG node to test
1018 * \param qname Attribute name to check for
1019 * \param suffix Value suffix to check for
1020 * \param match Pointer to the test result
1022 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1023 * intern the attribute name (which usually indicates memory
1026 css_error
node_has_attribute_suffix(void *pw
, void *node
,
1027 const css_qname
*qname
, lwc_string
*suffix
,
1032 dom_string
*attr_val
;
1034 const char *avdata
; /* attribute value data */
1035 size_t avdata_len
; /* length of that attribute value data */
1036 size_t suffixlen
; /* length of "suffix" */
1038 /* convenience pointer we'll use when matching the suffix */
1039 const char *suffix_start
;
1041 suffixlen
= lwc_string_length(suffix
);
1042 if (suffixlen
== 0) {
1043 /* In this case, the spec says that "if 'val' is the
1044 * empty string, it will never represent anything." */
1048 /* Intern the attribute name as a dom_string so we can
1049 * use dom_element_get_attribute() */
1050 err
= dom_string_create_interned(
1051 (const uint8_t *) lwc_string_data(qname
->name
),
1052 lwc_string_length(qname
->name
),
1054 if (err
!= DOM_NO_ERR
) {
1058 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
1059 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
1060 /* There was an error getting the attribute's value or
1061 * the attribute doesn't exist. So, no match? */
1062 dom_string_unref(name
);
1067 /* Otherwise, we have the attribute value from the given node,
1068 * and the first thing we want to do is check to see if the
1069 * whole thing matches the suffix. */
1070 dom_string_unref(name
);
1071 *match
= dom_string_lwc_isequal(attr_val
, suffix
);
1073 /* If not, check to see if an, uh, suffix matches the
1075 if (*match
== false) {
1076 avdata
= dom_string_data(attr_val
);
1077 avdata_len
= dom_string_byte_length(attr_val
);
1079 suffix_start
= (char *)(avdata
+ avdata_len
- suffixlen
);
1081 if ((avdata_len
>= suffixlen
) &&
1082 (strncasecmp(suffix_start
,
1083 lwc_string_data(suffix
),
1085 /* Use strncasecmp to compare only the last
1086 * "n" characters, where "n" is the length of
1092 dom_string_unref(attr_val
);