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
,
39 * Resolve a relative URL to an absolute one by doing nothing. This is
40 * the simplest possible implementation of a URL resolver, needed for
43 css_error
svgtiny_resolve_url(void *pw
,
44 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
49 /* Copy the relative URL to the absolute one (the return
51 *abs
= lwc_string_ref(rel
);
56 * Create a stylesheet with the default set of params.
58 * \param sheet A stylesheet pointer, passed in by reference, that
59 * we use to store the newly-created stylesheet.
60 * \param inline_style True if this stylesheet represents an inline
61 * style, and false otherwise.
63 * \return The return value from css_stylesheet_create() is returned.
65 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
68 css_stylesheet_params params
;
70 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
71 params
.level
= CSS_LEVEL_DEFAULT
;
72 params
.charset
= NULL
;
75 params
.allow_quirks
= false;
76 params
.inline_style
= inline_style
;
77 params
.resolve
= svgtiny_resolve_url
;
78 params
.resolve_pw
= NULL
;
80 params
.import_pw
= NULL
;
82 params
.color_pw
= NULL
;
84 params
.font_pw
= NULL
;
86 return css_stylesheet_create(¶ms
, sheet
);
90 /**************************/
91 /* libcss select handlers */
92 /**************************/
94 * From here on we implement the "select handler "API defined in
95 * libcss's include/libcss/select.h and discussed briefly in its
101 * Retrieve the given node's name
103 * \param pw Pointer to the current SVG parser state
104 * \param node Libdom SVG node
105 * \param qname Address at which to store the node name
107 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
109 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
113 struct svgtiny_parse_state
*state
;
115 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
116 if (err
!= DOM_NO_ERR
) {
120 state
= (struct svgtiny_parse_state
*)pw
;
121 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
123 err
= dom_string_intern(name
, &qname
->name
);
124 if (err
!= DOM_NO_ERR
) {
125 dom_string_unref(name
);
129 dom_string_unref(name
);
136 * Retrieve the given node's classes
138 * \param pw Pointer to the current SVG parser state
139 * \param node Libdom SVG node
140 * \param classes Address at which to store the class name array
141 * \param n_classes Address at which to store the length of the class
144 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
146 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
147 * because the underlying libdom function never fails
149 css_error
node_classes(void *pw
, void *node
,
150 lwc_string
***classes
, uint32_t *n_classes
)
155 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
157 /* The implementation does not do it, but the documentation
158 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
159 possible here, so we handle it to be on the safe side. */
160 if (err
!= DOM_NO_ERR
) {
169 * Retrieve the given node's id
171 * \param pw Pointer to the current SVG parser state
172 * \param node Libdom SVG node
173 * \param id Address at which to store the id
175 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
177 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
181 struct svgtiny_parse_state
*state
;
183 /* Begin with the assumption that this node has no id */
186 state
= (struct svgtiny_parse_state
*)pw
;
187 err
= dom_element_get_attribute((dom_node
*)node
,
188 state
->interned_id
, &attr
);
189 if (err
!= DOM_NO_ERR
) {
192 else if (attr
== NULL
) {
193 /* The node has no id attribute and our return value
194 is already set to NULL so we're done */
198 /* If we found an id attribute (a dom_string), intern it into
199 an lwc_string that we can return, and then cleanup the
201 err
= dom_string_intern(attr
, id
);
202 if (err
!= DOM_NO_ERR
) {
203 dom_string_unref(attr
);
206 dom_string_unref(attr
);
213 * Find the first parent of the given element having the given name
215 * \param pw Pointer to the current SVG parser state
216 * \param node Libdom SVG node
217 * \param qname Name of the parent node to search for
218 * \param parent Address at which to store the parent node pointer
220 * \return Always returns CSS_OK
222 * \post If a suitable element is found, a pointer to it will be
223 * stored at the address pointed to by \a parent; otherwise,
224 * NULL will be stored at the address pointed to by \a parent
226 css_error
named_parent_node(void *pw
, void *node
,
227 const css_qname
*qname
, void **parent
)
230 /* dom_element_named_parent_node() was invented to implement
231 * this select handler so there isn't much for us to do except
232 * call it. It's OK if node isn't an element, libdom checks
234 dom_element_named_parent_node((dom_element
*)node
,
236 (struct dom_element
**)parent
);
238 /* Implementation detail: dom_element_named_parent_node()
239 * increments the reference count of the parent element before
240 * returning it to us. According to docs/RefCnt in the libdom
241 * repository, this will prevent the parent element from being
242 * destroyed if it is pruned from the DOM. That sounds good,
243 * since we don't want to be using a pointer to an object that
244 * has been destroyed... but we also have no way of later
245 * decrementing the reference count ourselves, and don't want
246 * to make the returned node eternal. Decrementing the
247 * reference counter now allows it to be destroyed when the
248 * DOM no longer needs it, and so long as no other parts of
249 * libsvgtiny are messing with the DOM during parsing, that
250 * shouldn't (ha ha) cause any problems. */
251 dom_node_unref(*parent
);
258 * Find the "next-sibling" of the given element having the given name
260 * This search corresponds to the "+ foo" combinator in CSS and will
261 * find only "foo" element nodes that immediately precede the given
262 * node under the same parent in the DOM. In CSS the tree is viewed
263 * top-down and in libdom it is viewed from the bottom-up; as a result
264 * "next" and "previous" are sometimes backwards. This is case-sensitive.
266 * \param pw Pointer to the current SVG parser state
267 * \param node Libdom SVG node
268 * \param qname Name of the sibling node to search for
269 * \param sibling Address at which to store the sibling node pointer
271 * \return Always returns CSS_OK
273 * \post If a suitable element is found, a pointer to it will be
274 * stored at the address pointed to by \a sibling; otherwise,
275 * NULL will be stored at the address pointed to by \a sibling
277 css_error
named_sibling_node(void *pw
, void *node
,
278 const css_qname
*qname
, void **sibling
)
281 dom_node
*n
= node
; /* the current node */
282 dom_node
*prev
; /* the previous node */
287 *sibling
= NULL
; /* default to nothing found */
289 /* Begin the search; the first iteration we do outside of the
290 * loop. Implementation detil: dom_node_get_previous_sibling()
291 * increments the reference counter on the returned node. A
292 * comment within named_parent_node() explains why we
293 * decrement it ASAP. */
294 err
= dom_node_get_previous_sibling(n
, &n
);
295 if (err
!= DOM_NO_ERR
) {
300 /* We're looking for the first ELEMENT sibling */
301 err
= dom_node_get_node_type(n
, &type
);
302 if (err
!= DOM_NO_ERR
) {
307 if (type
== DOM_ELEMENT_NODE
) {
308 /* We found an element node, does it have the
310 err
= dom_node_get_node_name(n
, &name
);
311 if (err
!= DOM_NO_ERR
) {
316 if (dom_string_lwc_isequal(name
,
318 /* The name is right, return it */
322 /* There's only one next-sibling element node
323 * and we've already found it, so if its name
324 * wasn't right, we return the default value
326 dom_string_unref(name
);
331 /* Not an element node, so we move on the the previous
332 * previous sibling */
333 err
= dom_node_get_previous_sibling(n
, &prev
);
334 if (err
!= DOM_NO_ERR
) {
348 * Find the first "subsequent-sibling" of the given element having the
351 * This search corresponds to the "~ foo" combinator in CSS and will
352 * find only "foo" element nodes that precede the given node (under
353 * the same parent) in the DOM. In CSS the tree is viewed top-down and
354 * in libdom it is viewed from the bottom-up; as a result "next" and
355 * "previous" are sometimes backwards. This is case-sensitive.
357 * \param pw Pointer to the current SVG parser state
358 * \param node Libdom SVG node
359 * \param qname Name of the sibling node to search for
360 * \param sibling Address at which to store the sibling node pointer
362 * \return Always returns CSS_OK
364 * \post If a suitable element is found, a pointer to it will be
365 * stored at the address pointed to by \a sibling; otherwise,
366 * NULL will be stored at the address pointed to by \a sibling
368 css_error
named_generic_sibling_node(void *pw
, void *node
,
369 const css_qname
*qname
, void **sibling
)
372 dom_node
*n
= node
; /* the current node */
373 dom_node
*prev
; /* the previous node */
379 *sibling
= NULL
; /* default to nothing found */
381 /* Begin the search; the first iteration we do outside of the
382 * loop. Implementation detil: dom_node_get_previous_sibling()
383 * increments the reference counter on the returned node. A
384 * comment within named_parent_node() explains why we
385 * decrement it ASAP. */
386 err
= dom_node_get_previous_sibling(n
, &n
);
387 if (err
!= DOM_NO_ERR
) {
392 err
= dom_node_get_node_type(n
, &type
);
393 if (err
!= DOM_NO_ERR
) {
398 if (type
== DOM_ELEMENT_NODE
) {
399 /* We only want ELEMENT nodes */
400 err
= dom_node_get_node_name(n
, &name
);
401 if (err
!= DOM_NO_ERR
) {
406 if (dom_string_lwc_isequal(name
,
408 /* Found one. Save it and stop the search */
409 dom_string_unref(name
);
415 dom_string_unref(name
);
418 /* This sibling wasn't an element with the desired
419 name, so move on to the previous sibling */
420 err
= dom_node_get_previous_sibling(n
, &prev
);
421 if (err
!= DOM_NO_ERR
) {
435 * Return a pointer to the given node's parent
437 * \param pw Pointer to the current SVG parser state
438 * \param node Libdom SVG node
439 * \param parent Address at which to store the node's parent pointer
441 * \return Always returns CSS_OK
443 css_error
parent_node(void *pw
, void *node
, void **parent
)
446 /* Libdom basically implements this for us */
447 dom_element_parent_node(node
, (struct dom_element
**)parent
);
449 /* See the comment in named_parent_node() for why we decrement
450 * this reference counter here. */
451 dom_node_unref(*parent
);
458 * Find the "next-sibling" of the given element
460 * This search corresponds "+ *" in CSS and will find the first
461 * element node that immediately precedes the given node under the
462 * same parent in the DOM. In CSS the tree is viewed top-down and in
463 * libdom it is viewed from the bottom-up; as a result "next" and
464 * "previous" are sometimes backwards.
466 * \param pw Pointer to the current SVG parser state
467 * \param node Libdom SVG node
468 * \param sibling Address at which to store the sibling node pointer
470 * \return Always returns CSS_OK
472 * \post If a suitable element is found, a pointer to it will be
473 * stored at the address pointed to by \a sibling; otherwise,
474 * NULL will be stored at the address pointed to by \a sibling
476 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
479 dom_node
*n
= node
; /* the current node */
480 dom_node
*prev
; /* the previous node */
484 *sibling
= NULL
; /* default to nothing found */
486 /* Begin the search; the first iteration we do outside of the
487 * loop. Implementation detil: dom_node_get_previous_sibling()
488 * increments the reference counter on the returned node. A
489 * comment within named_parent_node() explains why we
490 * decrement it ASAP. */
491 err
= dom_node_get_previous_sibling(n
, &n
);
492 if (err
!= DOM_NO_ERR
) {
497 err
= dom_node_get_node_type(n
, &type
);
498 if (err
!= DOM_NO_ERR
) {
503 if (type
== DOM_ELEMENT_NODE
) {
504 /* We found a sibling node that is also an
505 element and that's all we wanted. */
511 /* This sibling node was not an element; move on to
512 the previous sibling */
513 err
= dom_node_get_previous_sibling(n
, &prev
);
514 if (err
!= DOM_NO_ERR
) {
528 * Test the given node for the given name
530 * This will return true (via the "match" pointer) if the libdom node
531 * has the given name or if that name is the universal selector;
532 * otherwise it returns false. The comparison is case-sensitive. It
533 * corresponds to a rule like "body { ... }" in CSS.
535 * \param pw Pointer to the current SVG parser state
536 * \param node Libdom SVG node to test
537 * \param qname Name to check for
538 * \param match Pointer to the test result
540 * \return Always returns CSS_OK
542 css_error
node_has_name(void *pw
, void *node
,
543 const css_qname
*qname
, bool *match
)
545 struct svgtiny_parse_state
*state
;
549 /* Start by checking to see if qname is the universal selector */
550 state
= (struct svgtiny_parse_state
*)pw
;
551 if (lwc_string_isequal(qname
->name
,
552 state
->interned_universal
, match
) == lwc_error_ok
) {
554 /* It's the universal selector. In NetSurf, all node
555 * names match the universal selector, and nothing in
556 * the libcss documentation suggests another approach,
557 * so we follow NetSurf here. */
562 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
563 if (err
!= DOM_NO_ERR
) {
567 /* Unlike with HTML, SVG element names are case-sensitive */
568 *match
= dom_string_lwc_isequal(name
, qname
->name
);
569 dom_string_unref(name
);
576 * Test the given node for the given class
578 * This will return true (via the "match" pointer) if the libdom node
579 * has the given class. The comparison is case-sensitive. It
580 * corresponds to node.class in CSS.
582 * \param pw Pointer to the current SVG parser state
583 * \param node Libdom SVG node to test
584 * \param name Class name to check for
585 * \param match Pointer to the test result
587 * \return Always returns CSS_OK
589 css_error
node_has_class(void *pw
, void *node
,
590 lwc_string
*name
, bool *match
)
593 /* libdom implements this for us and apparently it cannot fail */
594 dom_element_has_class((dom_node
*)node
, name
, match
);
600 * Test the given node for the given id
602 * This will return true (via the "match" pointer) if the libdom node
603 * has the given id. The comparison is case-sensitive. It corresponds
606 * \param pw Pointer to the current SVG parser state
607 * \param node Libdom SVG node to test
608 * \param name Id to check for
609 * \param match Pointer to the test result
611 * \return Always returns CSS_OK
613 css_error
node_has_id(void *pw
, void *node
,
614 lwc_string
*name
, bool *match
)
618 struct svgtiny_parse_state
*state
;
620 attr
= NULL
; /* a priori the "id" attribute may not exist */
621 *match
= false; /* default to no match */
623 state
= (struct svgtiny_parse_state
*)pw
;
624 err
= dom_element_get_attribute((dom_node
*)node
,
625 state
->interned_id
, &attr
);
626 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
630 *match
= dom_string_lwc_isequal(attr
, name
);
631 dom_string_unref(attr
);
638 * Test the given node for the given attribute
640 * This will return true (via the "match" pointer) if the libdom node
641 * has an attribute with the given name. The comparison is
642 * case-sensitive. It corresponds to node[attr] in CSS.
644 * \param pw Pointer to the current SVG parser state
645 * \param node Libdom SVG node to test
646 * \param qname Attribute name to check for
647 * \param match Pointer to the test result
649 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
652 css_error
node_has_attribute(void *pw
, void *node
,
653 const css_qname
*qname
, bool *match
)
659 /* intern the attribute name as a dom_string so we can
660 * delegate to dom_element_has_attribute() */
661 err
= dom_string_create_interned(
662 (const uint8_t *) lwc_string_data(qname
->name
),
663 lwc_string_length(qname
->name
),
665 if (err
!= DOM_NO_ERR
) {
669 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
670 if (err
!= DOM_NO_ERR
) {
671 dom_string_unref(name
);
675 dom_string_unref(name
);
681 * Test the given node for an attribute with a specific value
683 * This will return true (via the "match" pointer) if the libdom node
684 * has an attribute with the given name and value. The comparison is
685 * case-sensitive. It corresponds to node[attr=value] in CSS.
687 * \param pw Pointer to the current SVG parser state
688 * \param node Libdom SVG node to test
689 * \param qname Attribute name to check for
690 * \param value Attribute value to check for
691 * \param match Pointer to the test result
693 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
694 * intern the attribute name (which usually indicates memory
697 css_error
node_has_attribute_equal(void *pw
, void *node
,
698 const css_qname
*qname
, lwc_string
*value
,
701 /* Implementation note: NetSurf always returns "no match" when
702 * the value is empty (length zero). We allow it, because why
707 dom_string
*attr_val
;
710 /* Intern the attribute name as a dom_string so we can
711 * use dom_element_get_attribute() */
712 err
= dom_string_create_interned(
713 (const uint8_t *) lwc_string_data(qname
->name
),
714 lwc_string_length(qname
->name
),
716 if (err
!= DOM_NO_ERR
) {
720 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
721 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
722 /* There was an error getting the attribute's value or
723 * the attribute doesn't exist. So, no match? */
724 dom_string_unref(name
);
729 /* Otherwise, we have the attribute value from the given node
730 * and all we need to do is compare. */
731 dom_string_unref(name
);
732 *match
= dom_string_lwc_isequal(attr_val
, value
);
733 dom_string_unref(attr_val
);
740 * Test the given node for an attribute with a specific value,
741 * possibly followed by a single hyphen
743 * This will return true (via the "match" pointer) if the libdom node
744 * has an attribute with the given name and value or with the given
745 * name and a value that is followed by exactly one hyphen. The
746 * comparison is case-sensitive. This corresponds to [attr|=value]
749 * \param pw Pointer to the current SVG parser state
750 * \param node Libdom SVG node to test
751 * \param qname Attribute name to check for
752 * \param value Attribute value to check for
753 * \param match Pointer to the test result
755 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
756 * intern the attribute name (which usually indicates memory
759 css_error
node_has_attribute_dashmatch(void *pw
, void *node
,
760 const css_qname
*qname
, lwc_string
*value
,
763 /* Implementation note: NetSurf always returns "no match" when
764 * the value is empty (length zero). We allow it, because why
769 dom_string
*attr_val
;
772 const char *vdata
; /* to hold the data underlying "value" */
774 const char *avdata
; /* to hold the found attribute value data */
777 /* Intern the attribute name as a dom_string so we can
778 * use dom_element_get_attribute() */
779 err
= dom_string_create_interned(
780 (const uint8_t *) lwc_string_data(qname
->name
),
781 lwc_string_length(qname
->name
),
783 if (err
!= DOM_NO_ERR
) {
787 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
788 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
789 /* There was an error getting the attribute's value or
790 * the attribute doesn't exist. So, no match? */
791 dom_string_unref(name
);
796 /* Otherwise, we have the attribute value from the given node
797 * and all we need to do is compare. */
798 dom_string_unref(name
);
799 *match
= dom_string_lwc_isequal(attr_val
, value
);
801 /* Exact match, we're done */
802 dom_string_unref(attr_val
);
806 /* No exact match, try it with a hyphen on the end */
807 vdata
= lwc_string_data(value
); /* needle */
808 vdata_len
= lwc_string_length(value
);
809 avdata
= dom_string_data(attr_val
); /* haystack */
810 avdata_len
= dom_string_byte_length(attr_val
);
811 dom_string_unref(attr_val
);
813 if (avdata_len
> vdata_len
&& avdata
[vdata_len
] == '-') {
814 if (strncasecmp(avdata
, vdata
, vdata_len
) == 0) {
815 /* If there's a hyphen in the right position,
816 * it suffices to compare the strings only up
827 * Test the given node for an attribute whose value is a
828 * space-separated list of words, one of which is the given word
830 * This will return true (via the "match" pointer) if the libdom node
831 * has an attribute with the given name and whose value when
832 * considered as a space-separated list of words contains the given
833 * word. The comparison is case-sensitive. This corresponds to
834 * [attr~=value] in CSS.
836 * \param pw Pointer to the current SVG parser state
837 * \param node Libdom SVG node to test
838 * \param qname Attribute name to check for
839 * \param word Value word to check for
840 * \param match Pointer to the test result
842 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
843 * intern the attribute name (which usually indicates memory
846 css_error
node_has_attribute_includes(void *pw
, void *node
,
847 const css_qname
*qname
, lwc_string
*word
,
853 dom_string
*attr_val
;
855 size_t wordlen
; /* length of "word" */
857 /* pointers used to parse a space-separated list of words */
862 *match
= false; /* default to no match */
864 wordlen
= lwc_string_length(word
);
866 /* In this case, the spec says that "if 'val' is the
867 * empty string, it will never represent anything." */
871 /* Intern the attribute name as a dom_string so we can
872 * use dom_element_get_attribute() */
873 err
= dom_string_create_interned(
874 (const uint8_t *) lwc_string_data(qname
->name
),
875 lwc_string_length(qname
->name
),
877 if (err
!= DOM_NO_ERR
) {
881 err
= dom_element_get_attribute((dom_node
*)node
, name
, &attr_val
);
882 if ((err
!= DOM_NO_ERR
) || (attr_val
== NULL
)) {
883 /* There was an error getting the attribute's value or
884 * the attribute doesn't exist. So, no match? */
885 dom_string_unref(name
);
889 /* Parse the list comparing each word against "word" */
890 start
= dom_string_data(attr_val
);
891 end
= start
+ dom_string_byte_length(attr_val
);
892 dom_string_unref(attr_val
);
894 for (p
= start
; p
<= end
; p
++) {
895 /* Move forward until we find the end of the first word */
896 if (*p
== ' ' || *p
== '\0') {
897 /* If the length of that word is the length of the
898 * word we're looking for, do the comparison. */
899 if ((size_t) (p
- start
) == wordlen
&&
901 lwc_string_data(word
),
906 /* No match? Set "start" to the beginning of
907 * the next word and loop. */