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
,
42 * Resolve a relative URL to an absolute one by doing nothing. This is
43 * the simplest possible implementation of a URL resolver, needed for
46 css_error
svgtiny_resolve_url(void *pw
,
47 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
52 /* Copy the relative URL to the absolute one (the return
54 *abs
= lwc_string_ref(rel
);
59 * Create a stylesheet with the default set of params.
61 * \param sheet A stylesheet pointer, passed in by reference, that
62 * we use to store the newly-created stylesheet.
63 * \param inline_style True if this stylesheet represents an inline
64 * style, and false otherwise.
66 * \return The return value from css_stylesheet_create() is returned.
68 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
71 css_stylesheet_params params
;
73 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
74 params
.level
= CSS_LEVEL_DEFAULT
;
75 params
.charset
= NULL
;
78 params
.allow_quirks
= false;
79 params
.inline_style
= inline_style
;
80 params
.resolve
= svgtiny_resolve_url
;
81 params
.resolve_pw
= NULL
;
83 params
.import_pw
= NULL
;
85 params
.color_pw
= NULL
;
87 params
.font_pw
= NULL
;
89 return css_stylesheet_create(¶ms
, sheet
);
93 /**************************/
94 /* libcss select handlers */
95 /**************************/
97 * From here on we implement the "select handler "API defined in
98 * libcss's include/libcss/select.h and discussed briefly in its
104 * Retrieve the given node's name
106 * \param pw Pointer to the current SVG parser state
107 * \param node Libdom SVG node
108 * \param qname Address at which to store the node name
110 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
112 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
116 struct svgtiny_parse_state
*state
;
118 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
119 if (err
!= DOM_NO_ERR
) {
123 state
= (struct svgtiny_parse_state
*)pw
;
124 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
126 err
= dom_string_intern(name
, &qname
->name
);
127 if (err
!= DOM_NO_ERR
) {
128 dom_string_unref(name
);
132 dom_string_unref(name
);
139 * Retrieve the given node's classes
141 * \param pw Pointer to the current SVG parser state
142 * \param node Libdom SVG node
143 * \param classes Address at which to store the class name array
144 * \param n_classes Address at which to store the length of the class
147 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
149 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
150 * because the underlying libdom function never fails
152 css_error
node_classes(void *pw
, void *node
,
153 lwc_string
***classes
, uint32_t *n_classes
)
158 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
160 /* The implementation does not do it, but the documentation
161 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
162 possible here, so we handle it to be on the safe side. */
163 if (err
!= DOM_NO_ERR
) {
172 * Retrieve the given node's id
174 * \param pw Pointer to the current SVG parser state
175 * \param node Libdom SVG node
176 * \param id Address at which to store the id
178 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
180 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
184 struct svgtiny_parse_state
*state
;
186 /* Begin with the assumption that this node has no id */
189 state
= (struct svgtiny_parse_state
*)pw
;
190 err
= dom_element_get_attribute((dom_node
*)node
,
191 state
->interned_id
, &attr
);
192 if (err
!= DOM_NO_ERR
) {
195 else if (attr
== NULL
) {
196 /* The node has no id attribute and our return value
197 is already set to NULL so we're done */
201 /* If we found an id attribute (a dom_string), intern it into
202 an lwc_string that we can return, and then cleanup the
204 err
= dom_string_intern(attr
, id
);
205 if (err
!= DOM_NO_ERR
) {
206 dom_string_unref(attr
);
209 dom_string_unref(attr
);
216 * Find the first parent of the given element having the given name
218 * \param pw Pointer to the current SVG parser state
219 * \param node Libdom SVG node
220 * \param qname Name of the parent node to search for
221 * \param parent Address at which to store the parent node pointer
223 * \return Always returns CSS_OK
225 * \post If a suitable element is found, a pointer to it will be
226 * stored at the address pointed to by \a parent; otherwise,
227 * NULL will be stored at the address pointed to by \a parent
229 css_error
named_parent_node(void *pw
, void *node
,
230 const css_qname
*qname
, void **parent
)
233 /* dom_element_named_parent_node() was invented to implement
234 * this select handler so there isn't much for us to do except
235 * call it. It's OK if node isn't an element, libdom checks
237 dom_element_named_parent_node((dom_element
*)node
,
239 (struct dom_element
**)parent
);
241 /* Implementation detail: dom_element_named_parent_node()
242 * increments the reference count of the parent element before
243 * returning it to us. According to docs/RefCnt in the libdom
244 * repository, this will prevent the parent element from being
245 * destroyed if it is pruned from the DOM. That sounds good,
246 * since we don't want to be using a pointer to an object that
247 * has been destroyed... but we also have no way of later
248 * decrementing the reference count ourselves, and don't want
249 * to make the returned node eternal. Decrementing the
250 * reference counter now allows it to be destroyed when the
251 * DOM no longer needs it, and so long as no other parts of
252 * libsvgtiny are messing with the DOM during parsing, that
253 * shouldn't (ha ha) cause any problems. */
254 dom_node_unref(*parent
);
261 * Find the "next-sibling" of the given element having the given name
263 * This search corresponds to the "+ foo" combinator in CSS and will
264 * find only "foo" element nodes that immediately precede the given
265 * node under the same parent in the DOM. In CSS the tree is viewed
266 * top-down and in libdom it is viewed from the bottom-up; as a result
267 * "next" and "previous" are sometimes backwards. This is case-sensitive.
269 * \param pw Pointer to the current SVG parser state
270 * \param node Libdom SVG node
271 * \param qname Name of the sibling node to search for
272 * \param sibling Address at which to store the sibling node pointer
274 * \return Always returns CSS_OK
276 * \post If a suitable element is found, a pointer to it will be
277 * stored at the address pointed to by \a sibling; otherwise,
278 * NULL will be stored at the address pointed to by \a sibling
280 css_error
named_sibling_node(void *pw
, void *node
,
281 const css_qname
*qname
, void **sibling
)
284 dom_node
*n
= node
; /* the current node */
285 dom_node
*prev
; /* the previous node */
290 *sibling
= NULL
; /* default to nothing found */
292 /* Begin the search; the first iteration we do outside of the
293 * loop. Implementation detil: dom_node_get_previous_sibling()
294 * increments the reference counter on the returned node. A
295 * comment within named_parent_node() explains why we
296 * decrement it ASAP. */
297 err
= dom_node_get_previous_sibling(n
, &n
);
298 if (err
!= DOM_NO_ERR
) {
303 /* We're looking for the first ELEMENT sibling */
304 err
= dom_node_get_node_type(n
, &type
);
305 if (err
!= DOM_NO_ERR
) {
310 if (type
== DOM_ELEMENT_NODE
) {
311 /* We found an element node, does it have the
313 err
= dom_node_get_node_name(n
, &name
);
314 if (err
!= DOM_NO_ERR
) {
319 if (dom_string_lwc_isequal(name
,
321 /* The name is right, return it */
325 /* There's only one next-sibling element node
326 * and we've already found it, so if its name
327 * wasn't right, we return the default value
329 dom_string_unref(name
);
334 /* Not an element node, so we move on the the previous
335 * previous sibling */
336 err
= dom_node_get_previous_sibling(n
, &prev
);
337 if (err
!= DOM_NO_ERR
) {
351 * Find the first "subsequent-sibling" of the given element having the
354 * This search corresponds to the "~ foo" combinator in CSS and will
355 * find only "foo" element nodes that precede the given node (under
356 * the same parent) in the DOM. In CSS the tree is viewed top-down and
357 * in libdom it is viewed from the bottom-up; as a result "next" and
358 * "previous" are sometimes backwards. This is case-sensitive.
360 * \param pw Pointer to the current SVG parser state
361 * \param node Libdom SVG node
362 * \param qname Name of the sibling node to search for
363 * \param sibling Address at which to store the sibling node pointer
365 * \return Always returns CSS_OK
367 * \post If a suitable element is found, a pointer to it will be
368 * stored at the address pointed to by \a sibling; otherwise,
369 * NULL will be stored at the address pointed to by \a sibling
371 css_error
named_generic_sibling_node(void *pw
, void *node
,
372 const css_qname
*qname
, void **sibling
)
375 dom_node
*n
= node
; /* the current node */
376 dom_node
*prev
; /* the previous node */
382 *sibling
= NULL
; /* default to nothing found */
384 /* Begin the search; the first iteration we do outside of the
385 * loop. Implementation detil: dom_node_get_previous_sibling()
386 * increments the reference counter on the returned node. A
387 * comment within named_parent_node() explains why we
388 * decrement it ASAP. */
389 err
= dom_node_get_previous_sibling(n
, &n
);
390 if (err
!= DOM_NO_ERR
) {
395 err
= dom_node_get_node_type(n
, &type
);
396 if (err
!= DOM_NO_ERR
) {
401 if (type
== DOM_ELEMENT_NODE
) {
402 /* We only want ELEMENT nodes */
403 err
= dom_node_get_node_name(n
, &name
);
404 if (err
!= DOM_NO_ERR
) {
409 if (dom_string_lwc_isequal(name
,
411 /* Found one. Save it and stop the search */
412 dom_string_unref(name
);
418 dom_string_unref(name
);
421 /* This sibling wasn't an element with the desired
422 name, so move on to the previous sibling */
423 err
= dom_node_get_previous_sibling(n
, &prev
);
424 if (err
!= DOM_NO_ERR
) {
438 * Return a pointer to the given node's parent
440 * \param pw Pointer to the current SVG parser state
441 * \param node Libdom SVG node
442 * \param parent Address at which to store the node's parent pointer
444 * \return Always returns CSS_OK
446 css_error
parent_node(void *pw
, void *node
, void **parent
)
449 /* Libdom basically implements this for us */
450 dom_element_parent_node(node
, (struct dom_element
**)parent
);
452 /* See the comment in named_parent_node() for why we decrement
453 * this reference counter here. */
454 dom_node_unref(*parent
);
461 * Find the "next-sibling" of the given element
463 * This search corresponds "+ *" in CSS and will find the first
464 * element node that immediately precedes the given node under the
465 * same parent in the DOM. In CSS the tree is viewed top-down and in
466 * libdom it is viewed from the bottom-up; as a result "next" and
467 * "previous" are sometimes backwards.
469 * \param pw Pointer to the current SVG parser state
470 * \param node Libdom SVG node
471 * \param sibling Address at which to store the sibling node pointer
473 * \return Always returns CSS_OK
475 * \post If a suitable element is found, a pointer to it will be
476 * stored at the address pointed to by \a sibling; otherwise,
477 * NULL will be stored at the address pointed to by \a sibling
479 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
482 dom_node
*n
= node
; /* the current node */
483 dom_node
*prev
; /* the previous node */
487 *sibling
= NULL
; /* default to nothing found */
489 /* Begin the search; the first iteration we do outside of the
490 * loop. Implementation detil: dom_node_get_previous_sibling()
491 * increments the reference counter on the returned node. A
492 * comment within named_parent_node() explains why we
493 * decrement it ASAP. */
494 err
= dom_node_get_previous_sibling(n
, &n
);
495 if (err
!= DOM_NO_ERR
) {
500 err
= dom_node_get_node_type(n
, &type
);
501 if (err
!= DOM_NO_ERR
) {
506 if (type
== DOM_ELEMENT_NODE
) {
507 /* We found a sibling node that is also an
508 element and that's all we wanted. */
514 /* This sibling node was not an element; move on to
515 the previous sibling */
516 err
= dom_node_get_previous_sibling(n
, &prev
);
517 if (err
!= DOM_NO_ERR
) {
531 * Test the given node for the given name
533 * This will return true (via the "match" pointer) if the libdom node
534 * has the given name or if that name is the universal selector;
535 * otherwise it returns false. The comparison is case-sensitive. It
536 * corresponds to a rule like "body { ... }" in CSS.
538 * \param pw Pointer to the current SVG parser state
539 * \param node Libdom SVG node to test
540 * \param qname Name to check for
541 * \param match Pointer to the test result
543 * \return Always returns CSS_OK
545 css_error
node_has_name(void *pw
, void *node
,
546 const css_qname
*qname
, bool *match
)
548 struct svgtiny_parse_state
*state
;
552 /* Start by checking to see if qname is the universal selector */
553 state
= (struct svgtiny_parse_state
*)pw
;
554 if (lwc_string_isequal(qname
->name
,
555 state
->interned_universal
, match
) == lwc_error_ok
) {
557 /* It's the universal selector. In NetSurf, all node
558 * names match the universal selector, and nothing in
559 * the libcss documentation suggests another approach,
560 * so we follow NetSurf here. */
565 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
566 if (err
!= DOM_NO_ERR
) {
570 /* Unlike with HTML, SVG element names are case-sensitive */
571 *match
= dom_string_lwc_isequal(name
, qname
->name
);
572 dom_string_unref(name
);
579 * Test the given node for the given class
581 * This will return true (via the "match" pointer) if the libdom node
582 * has the given class. The comparison is case-sensitive. It
583 * corresponds to node.class in CSS.
585 * \param pw Pointer to the current SVG parser state
586 * \param node Libdom SVG node to test
587 * \param name Class name to check for
588 * \param match Pointer to the test result
590 * \return Always returns CSS_OK
592 css_error
node_has_class(void *pw
, void *node
,
593 lwc_string
*name
, bool *match
)
596 /* libdom implements this for us and apparently it cannot fail */
597 dom_element_has_class((dom_node
*)node
, name
, match
);
603 * Test the given node for the given id
605 * This will return true (via the "match" pointer) if the libdom node
606 * has the given id. The comparison is case-sensitive. It corresponds
609 * \param pw Pointer to the current SVG parser state
610 * \param node Libdom SVG node to test
611 * \param name Id to check for
612 * \param match Pointer to the test result
614 * \return Always returns CSS_OK
616 css_error
node_has_id(void *pw
, void *node
,
617 lwc_string
*name
, bool *match
)
621 struct svgtiny_parse_state
*state
;
623 attr
= NULL
; /* a priori the "id" attribute may not exist */
624 *match
= false; /* default to no match */
626 state
= (struct svgtiny_parse_state
*)pw
;
627 err
= dom_element_get_attribute((dom_node
*)node
,
628 state
->interned_id
, &attr
);
629 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
633 *match
= dom_string_lwc_isequal(attr
, name
);
634 dom_string_unref(attr
);
641 * Test the given node for the given attribute
643 * This will return true (via the "match" pointer) if the libdom node
644 * has an attribute with the given name. The comparison is
645 * case-sensitive. It corresponds to node[attr] in CSS.
647 * \param pw Pointer to the current SVG parser state
648 * \param node Libdom SVG node to test
649 * \param qname Attribute name to check for
650 * \param match Pointer to the test result
652 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
655 css_error
node_has_attribute(void *pw
, void *node
,
656 const css_qname
*qname
, bool *match
)
662 /* intern the attribute name as a dom_string so we can
663 * delegate to dom_element_has_attribute() */
664 err
= dom_string_create_interned(
665 (const uint8_t *) lwc_string_data(qname
->name
),
666 lwc_string_length(qname
->name
),
668 if (err
!= DOM_NO_ERR
) {
672 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
673 if (err
!= DOM_NO_ERR
) {
674 dom_string_unref(name
);
678 dom_string_unref(name
);
684 * Test the given node for an attribute with a specific value
686 * This will return true (via the "match" pointer) if the libdom node
687 * has an attribute with the given name and value. The comparison is
688 * case-sensitive. It corresponds to node[attr=value] in CSS.
690 * \param pw Pointer to the current SVG parser state
691 * \param node Libdom SVG node to test
692 * \param qname Attribute name to check for
693 * \param value Attribute value to check for
694 * \param match Pointer to the test result
696 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
697 * intern the attribute name (which usually indicates memory
700 css_error
node_has_attribute_equal(void *pw
, void *node
,
701 const css_qname
*qname
, lwc_string
*value
,
704 /* Implementation note: NetSurf always returns "no match" when
705 * the value is empty (length zero). We allow it, because why
710 dom_string
*attr_val
;
713 /* Intern the attribute name as a dom_string so we can
714 * use dom_element_get_attribute() */
715 err
= dom_string_create_interned(
716 (const uint8_t *) lwc_string_data(qname
->name
),
717 lwc_string_length(qname
->name
),
719 if (err
!= DOM_NO_ERR
) {
723 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
724 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
725 /* There was an error getting the attribute's value or
726 * the attribute doesn't exist. So, no match? */
727 dom_string_unref(name
);
732 /* Otherwise, we have the attribute value from the given node
733 * and all we need to do is compare. */
734 dom_string_unref(name
);
735 *match
= dom_string_lwc_isequal(attr_val
, value
);
736 dom_string_unref(attr_val
);
743 * Test the given node for an attribute with a specific value,
744 * possibly followed by a single hyphen
746 * This will return true (via the "match" pointer) if the libdom node
747 * has an attribute with the given name and value or with the given
748 * name and a value that is followed by exactly one hyphen. The
749 * comparison is case-sensitive. This corresponds to [attr|=value]
752 * \param pw Pointer to the current SVG parser state
753 * \param node Libdom SVG node to test
754 * \param qname Attribute name to check for
755 * \param value Attribute value to check for
756 * \param match Pointer to the test result
758 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
759 * intern the attribute name (which usually indicates memory
762 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
763 const css_qname
*qname
, lwc_string
*value
,
766 /* Implementation note: NetSurf always returns "no match" when
767 * the value is empty (length zero). We allow it, because why
772 dom_string
*attr_val
;
775 const char *vdata
; /* to hold the data underlying "value" */
777 const char *avdata
; /* to hold the found attribute value data */
780 /* Intern the attribute name as a dom_string so we can
781 * use dom_element_get_attribute() */
782 err
= dom_string_create_interned(
783 (const uint8_t *) lwc_string_data(qname
->name
),
784 lwc_string_length(qname
->name
),
786 if (err
!= DOM_NO_ERR
) {
790 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
791 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
792 /* There was an error getting the attribute's value or
793 * the attribute doesn't exist. So, no match? */
794 dom_string_unref(name
);
799 /* Otherwise, we have the attribute value from the given node
800 * and all we need to do is compare. */
801 dom_string_unref(name
);
802 *match
= dom_string_lwc_isequal(attr_val
, value
);
804 /* Exact match, we're done */
805 dom_string_unref(attr_val
);
809 /* No exact match, try it with a hyphen on the end */
810 vdata
= lwc_string_data(value
); /* needle */
811 vdata_len
= lwc_string_length(value
);
812 avdata
= dom_string_data(attr_val
); /* haystack */
813 avdata_len
= dom_string_byte_length(attr_val
);
814 dom_string_unref(attr_val
);
816 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
817 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
818 /* If there's a hyphen in the right position,
819 * it suffices to compare the strings only up
830 * Test the given node for an attribute whose value is a
831 * space-separated list of words, one of which is the given word
833 * This will return true (via the "match" pointer) if the libdom node
834 * has an attribute with the given name and whose value when
835 * considered as a space-separated list of words contains the given
836 * word. The comparison is case-sensitive. This corresponds to
837 * [attr~=value] in CSS.
839 * \param pw Pointer to the current SVG parser state
840 * \param node Libdom SVG node to test
841 * \param qname Attribute name to check for
842 * \param word Value word to check for
843 * \param match Pointer to the test result
845 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
846 * intern the attribute name (which usually indicates memory
849 css_error
node_has_attribute_includes(void *pw
, void *node
,
850 const css_qname
*qname
, lwc_string
*word
,
856 dom_string
*attr_val
;
858 size_t wordlen
; /* length of "word" */
860 /* pointers used to parse a space-separated list of words */
865 *match
= false; /* default to no match */
867 wordlen
= lwc_string_length(word
);
869 /* In this case, the spec says that "if 'val' is the
870 * empty string, it will never represent anything." */
874 /* Intern the attribute name as a dom_string so we can
875 * use dom_element_get_attribute() */
876 err
= dom_string_create_interned(
877 (const uint8_t *) lwc_string_data(qname
->name
),
878 lwc_string_length(qname
->name
),
880 if (err
!= DOM_NO_ERR
) {
884 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
885 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
886 /* There was an error getting the attribute's value or
887 * the attribute doesn't exist. So, no match? */
888 dom_string_unref(name
);
892 /* Parse the list comparing each word against "word" */
893 start
= dom_string_data(attr_val
);
894 end
= start
+ dom_string_byte_length(attr_val
);
895 dom_string_unref(attr_val
);
897 for (p
= start
; p
<= end
; p
++) {
898 /* Move forward until we find the end of the first word */
899 if (*p
== ' ' || *p
== '\0') {
900 /* If the length of that word is the length of the
901 * word we're looking for, do the comparison. */
902 if ((size_t) (p
- start
) == wordlen
&&
904 lwc_string_data(word
),
909 /* No match? Set "start" to the beginning of
910 * the next word and loop. */
920 * Test the given node for an attribute whose value begins with the
923 * This will return true (via the "match" pointer) if the libdom node
924 * has an attribute with the given name and whose value begins with
925 * the given prefix string. The comparison is case-sensitive. This
926 * corresponds to [attr^=value] in CSS.
928 * \param pw Pointer to the current SVG parser state
929 * \param node Libdom SVG node to test
930 * \param qname Attribute name to check for
931 * \param prefix Value prefix to check for
932 * \param match Pointer to the test result
934 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
935 * intern the attribute name (which usually indicates memory
938 css_error
node_has_attribute_prefix(void *pw
, void *node
,
939 const css_qname
*qname
, lwc_string
*prefix
,
944 dom_string
*attr_val
;
946 const char *avdata
; /* attribute value data */
947 size_t avdata_len
; /* length of that attribute value data */
948 size_t prefixlen
; /* length of "prefix" */
950 prefixlen
= lwc_string_length(prefix
);
951 if (prefixlen
== 0) {
952 /* In this case, the spec says that "if 'val' is the
953 * empty string, it will never represent anything." */
957 /* Intern the attribute name as a dom_string so we can
958 * use dom_element_get_attribute() */
959 err
= dom_string_create_interned(
960 (const uint8_t *) lwc_string_data(qname
->name
),
961 lwc_string_length(qname
->name
),
963 if (err
!= DOM_NO_ERR
) {
967 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
968 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
969 /* There was an error getting the attribute's value or
970 * the attribute doesn't exist. So, no match? */
971 dom_string_unref(name
);
976 /* Otherwise, we have the attribute value from the given node,
977 * and the first thing we want to do is check to see if the
978 * whole thing matches the prefix. */
979 dom_string_unref(name
);
980 *match
= dom_string_lwc_isequal(attr_val
, prefix
);
982 /* If not, check to see if an, uh, prefix matches the
984 if (*match
== false) {
985 avdata
= dom_string_data(attr_val
);
986 avdata_len
= dom_string_byte_length(attr_val
);
987 if ((avdata_len
>= prefixlen
) &&
989 lwc_string_data(prefix
),
991 /* Use strncasecmp to compare only the first
992 * "n" characters, where "n" is the length of
998 dom_string_unref(attr_val
);