1 #include <libcss/libcss.h>
4 #include "svgtiny_internal.h"
6 static css_error
node_name(void *pw
, void *node
, css_qname
*qname
);
7 static css_error
node_classes(void *pw
, void *node
,
8 lwc_string
***classes
, uint32_t *n_classes
);
9 static css_error
node_id(void *pw
, void *node
, lwc_string
**id
);
10 static css_error
named_parent_node(void *pw
, void *node
,
11 const css_qname
*qname
, void **parent
);
12 static css_error
named_sibling_node(void *pw
, void *node
,
13 const css_qname
*qname
, void **sibling
);
14 static css_error
named_generic_sibling_node(void *pw
, void *node
,
15 const css_qname
*qname
, void **sibling
);
16 static css_error
parent_node(void *pw
, void *node
, void **parent
);
17 static css_error
sibling_node(void *pw
, void *node
, void **sibling
);
18 static css_error
node_has_name(void *pw
, void *node
,
19 const css_qname
*qname
, bool *match
);
20 static css_error
node_has_class(void *pw
, void *node
,
21 lwc_string
*name
, bool *match
);
22 static css_error
node_has_id(void *pw
, void *node
,
23 lwc_string
*name
, bool *match
);
24 static css_error
node_has_attribute(void *pw
, void *node
,
25 const css_qname
*qname
, bool *match
);
29 * Resolve a relative URL to an absolute one by doing nothing. This is
30 * the simplest possible implementation of a URL resolver, needed for
33 css_error
svgtiny_resolve_url(void *pw
,
34 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
39 /* Copy the relative URL to the absolute one (the return
41 *abs
= lwc_string_ref(rel
);
46 * Create a stylesheet with the default set of params.
48 * \param sheet A stylesheet pointer, passed in by reference, that
49 * we use to store the newly-created stylesheet.
50 * \param inline_style True if this stylesheet represents an inline
51 * style, and false otherwise.
53 * \return The return value from css_stylesheet_create() is returned.
55 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
58 css_stylesheet_params params
;
60 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
61 params
.level
= CSS_LEVEL_DEFAULT
;
62 params
.charset
= NULL
;
65 params
.allow_quirks
= false;
66 params
.inline_style
= inline_style
;
67 params
.resolve
= svgtiny_resolve_url
;
68 params
.resolve_pw
= NULL
;
70 params
.import_pw
= NULL
;
72 params
.color_pw
= NULL
;
74 params
.font_pw
= NULL
;
76 return css_stylesheet_create(¶ms
, sheet
);
80 /**************************/
81 /* libcss select handlers */
82 /**************************/
84 * From here on we implement the "select handler "API defined in
85 * libcss's include/libcss/select.h and discussed briefly in its
91 * Retrieve the given node's name
93 * \param pw Pointer to the current SVG parser state
94 * \param node Libdom SVG node
95 * \param qname Address at which to store the node name
97 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
99 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
103 struct svgtiny_parse_state
*state
;
105 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
106 if (err
!= DOM_NO_ERR
) {
110 state
= (struct svgtiny_parse_state
*)pw
;
111 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
113 err
= dom_string_intern(name
, &qname
->name
);
114 if (err
!= DOM_NO_ERR
) {
115 dom_string_unref(name
);
119 dom_string_unref(name
);
126 * Retrieve the given node's classes
128 * \param pw Pointer to the current SVG parser state
129 * \param node Libdom SVG node
130 * \param classes Address at which to store the class name array
131 * \param n_classes Address at which to store the length of the class
134 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
136 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
137 * because the underlying libdom function never fails
139 css_error
node_classes(void *pw
, void *node
,
140 lwc_string
***classes
, uint32_t *n_classes
)
145 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
147 /* The implementation does not do it, but the documentation
148 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
149 possible here, so we handle it to be on the safe side. */
150 if (err
!= DOM_NO_ERR
) {
159 * Retrieve the given node's id
161 * \param pw Pointer to the current SVG parser state
162 * \param node Libdom SVG node
163 * \param id Address at which to store the id
165 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
167 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
171 struct svgtiny_parse_state
*state
;
173 /* Begin with the assumption that this node has no id */
176 state
= (struct svgtiny_parse_state
*)pw
;
177 err
= dom_element_get_attribute((dom_node
*)node
,
178 state
->interned_id
, &attr
);
179 if (err
!= DOM_NO_ERR
) {
182 else if (attr
== NULL
) {
183 /* The node has no id attribute and our return value
184 is already set to NULL so we're done */
188 /* If we found an id attribute (a dom_string), intern it into
189 an lwc_string that we can return, and then cleanup the
191 err
= dom_string_intern(attr
, id
);
192 if (err
!= DOM_NO_ERR
) {
193 dom_string_unref(attr
);
196 dom_string_unref(attr
);
203 * Find the first parent of the given element having the given name
205 * \param pw Pointer to the current SVG parser state
206 * \param node Libdom SVG node
207 * \param qname Name of the parent node to search for
208 * \param parent Address at which to store the parent node pointer
210 * \return Always returns CSS_OK
212 * \post If a suitable element is found, a pointer to it will be
213 * stored at the address pointed to by \a parent; otherwise,
214 * NULL will be stored at the address pointed to by \a parent
216 css_error
named_parent_node(void *pw
, void *node
,
217 const css_qname
*qname
, void **parent
)
220 /* dom_element_named_parent_node() was invented to implement
221 * this select handler so there isn't much for us to do except
222 * call it. It's OK if node isn't an element, libdom checks
224 dom_element_named_parent_node((dom_element
*)node
,
226 (struct dom_element
**)parent
);
228 /* Implementation detail: dom_element_named_parent_node()
229 * increments the reference count of the parent element before
230 * returning it to us. According to docs/RefCnt in the libdom
231 * repository, this will prevent the parent element from being
232 * destroyed if it is pruned from the DOM. That sounds good,
233 * since we don't want to be using a pointer to an object that
234 * has been destroyed... but we also have no way of later
235 * decrementing the reference count ourselves, and don't want
236 * to make the returned node eternal. Decrementing the
237 * reference counter now allows it to be destroyed when the
238 * DOM no longer needs it, and so long as no other parts of
239 * libsvgtiny are messing with the DOM during parsing, that
240 * shouldn't (ha ha) cause any problems. */
241 dom_node_unref(*parent
);
248 * Find the "next-sibling" of the given element having the given name
250 * This search corresponds to the "+ foo" combinator in CSS and will
251 * find only "foo" element nodes that immediately precede the given
252 * node under the same parent in the DOM. In CSS the tree is viewed
253 * top-down and in libdom it is viewed from the bottom-up; as a result
254 * "next" and "previous" are sometimes backwards. This is case-sensitive.
256 * \param pw Pointer to the current SVG parser state
257 * \param node Libdom SVG node
258 * \param qname Name of the sibling node to search for
259 * \param sibling Address at which to store the sibling node pointer
261 * \return Always returns CSS_OK
263 * \post If a suitable element is found, a pointer to it will be
264 * stored at the address pointed to by \a sibling; otherwise,
265 * NULL will be stored at the address pointed to by \a sibling
267 css_error
named_sibling_node(void *pw
, void *node
,
268 const css_qname
*qname
, void **sibling
)
271 dom_node
*n
= node
; /* the current node */
272 dom_node
*prev
; /* the previous node */
277 *sibling
= NULL
; /* default to nothing found */
279 /* Begin the search; the first iteration we do outside of the
280 * loop. Implementation detil: dom_node_get_previous_sibling()
281 * increments the reference counter on the returned node. A
282 * comment within named_parent_node() explains why we
283 * decrement it ASAP. */
284 err
= dom_node_get_previous_sibling(n
, &n
);
285 if (err
!= DOM_NO_ERR
) {
290 /* We're looking for the first ELEMENT sibling */
291 err
= dom_node_get_node_type(n
, &type
);
292 if (err
!= DOM_NO_ERR
) {
297 if (type
== DOM_ELEMENT_NODE
) {
298 /* We found an element node, does it have the
300 err
= dom_node_get_node_name(n
, &name
);
301 if (err
!= DOM_NO_ERR
) {
306 if (dom_string_lwc_isequal(name
,
308 /* The name is right, return it */
312 /* There's only one next-sibling element node
313 * and we've already found it, so if its name
314 * wasn't right, we return the default value
316 dom_string_unref(name
);
321 /* Not an element node, so we move on the the previous
322 * previous sibling */
323 err
= dom_node_get_previous_sibling(n
, &prev
);
324 if (err
!= DOM_NO_ERR
) {
338 * Find the first "subsequent-sibling" of the given element having the
341 * This search corresponds to the "~ foo" combinator in CSS and will
342 * find only "foo" element nodes that precede the given node (under
343 * the same parent) in the DOM. In CSS the tree is viewed top-down and
344 * in libdom it is viewed from the bottom-up; as a result "next" and
345 * "previous" are sometimes backwards. This is case-sensitive.
347 * \param pw Pointer to the current SVG parser state
348 * \param node Libdom SVG node
349 * \param qname Name of the sibling node to search for
350 * \param sibling Address at which to store the sibling node pointer
352 * \return Always returns CSS_OK
354 * \post If a suitable element is found, a pointer to it will be
355 * stored at the address pointed to by \a sibling; otherwise,
356 * NULL will be stored at the address pointed to by \a sibling
358 css_error
named_generic_sibling_node(void *pw
, void *node
,
359 const css_qname
*qname
, void **sibling
)
362 dom_node
*n
= node
; /* the current node */
363 dom_node
*prev
; /* the previous node */
369 *sibling
= NULL
; /* default to nothing found */
371 /* Begin the search; the first iteration we do outside of the
372 * loop. Implementation detil: dom_node_get_previous_sibling()
373 * increments the reference counter on the returned node. A
374 * comment within named_parent_node() explains why we
375 * decrement it ASAP. */
376 err
= dom_node_get_previous_sibling(n
, &n
);
377 if (err
!= DOM_NO_ERR
) {
382 err
= dom_node_get_node_type(n
, &type
);
383 if (err
!= DOM_NO_ERR
) {
388 if (type
== DOM_ELEMENT_NODE
) {
389 /* We only want ELEMENT nodes */
390 err
= dom_node_get_node_name(n
, &name
);
391 if (err
!= DOM_NO_ERR
) {
396 if (dom_string_lwc_isequal(name
,
398 /* Found one. Save it and stop the search */
399 dom_string_unref(name
);
405 dom_string_unref(name
);
408 /* This sibling wasn't an element with the desired
409 name, so move on to the previous sibling */
410 err
= dom_node_get_previous_sibling(n
, &prev
);
411 if (err
!= DOM_NO_ERR
) {
425 * Return a pointer to the given node's parent
427 * \param pw Pointer to the current SVG parser state
428 * \param node Libdom SVG node
429 * \param parent Address at which to store the node's parent pointer
431 * \return Always returns CSS_OK
433 css_error
parent_node(void *pw
, void *node
, void **parent
)
436 /* Libdom basically implements this for us */
437 dom_element_parent_node(node
, (struct dom_element
**)parent
);
439 /* See the comment in named_parent_node() for why we decrement
440 * this reference counter here. */
441 dom_node_unref(*parent
);
448 * Find the "next-sibling" of the given element
450 * This search corresponds "+ *" in CSS and will find the first
451 * element node that immediately precedes the given node under the
452 * same parent in the DOM. In CSS the tree is viewed top-down and in
453 * libdom it is viewed from the bottom-up; as a result "next" and
454 * "previous" are sometimes backwards.
456 * \param pw Pointer to the current SVG parser state
457 * \param node Libdom SVG node
458 * \param sibling Address at which to store the sibling node pointer
460 * \return Always returns CSS_OK
462 * \post If a suitable element is found, a pointer to it will be
463 * stored at the address pointed to by \a sibling; otherwise,
464 * NULL will be stored at the address pointed to by \a sibling
466 css_error
sibling_node(void *pw
, void *node
, void **sibling
)
469 dom_node
*n
= node
; /* the current node */
470 dom_node
*prev
; /* the previous node */
474 *sibling
= NULL
; /* default to nothing found */
476 /* Begin the search; the first iteration we do outside of the
477 * loop. Implementation detil: dom_node_get_previous_sibling()
478 * increments the reference counter on the returned node. A
479 * comment within named_parent_node() explains why we
480 * decrement it ASAP. */
481 err
= dom_node_get_previous_sibling(n
, &n
);
482 if (err
!= DOM_NO_ERR
) {
487 err
= dom_node_get_node_type(n
, &type
);
488 if (err
!= DOM_NO_ERR
) {
493 if (type
== DOM_ELEMENT_NODE
) {
494 /* We found a sibling node that is also an
495 element and that's all we wanted. */
501 /* This sibling node was not an element; move on to
502 the previous sibling */
503 err
= dom_node_get_previous_sibling(n
, &prev
);
504 if (err
!= DOM_NO_ERR
) {
518 * Test the given node for the given name
520 * This will return true (via the "match" pointer) if the libdom node
521 * has the given name or if that name is the universal selector;
522 * otherwise it returns false. The comparison is case-sensitive. It
523 * corresponds to a rule like "body { ... }" in CSS.
525 * \param pw Pointer to the current SVG parser state
526 * \param node Libdom SVG node to test
527 * \param qname Name to check for
528 * \param match Pointer to the test result
530 * \return Always returns CSS_OK
532 css_error
node_has_name(void *pw
, void *node
,
533 const css_qname
*qname
, bool *match
)
535 struct svgtiny_parse_state
*state
;
539 /* Start by checking to see if qname is the universal selector */
540 state
= (struct svgtiny_parse_state
*)pw
;
541 if (lwc_string_isequal(qname
->name
,
542 state
->interned_universal
, match
) == lwc_error_ok
) {
544 /* It's the universal selector. In NetSurf, all node
545 * names match the universal selector, and nothing in
546 * the libcss documentation suggests another approach,
547 * so we follow NetSurf here. */
552 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
553 if (err
!= DOM_NO_ERR
) {
557 /* Unlike with HTML, SVG element names are case-sensitive */
558 *match
= dom_string_lwc_isequal(name
, qname
->name
);
559 dom_string_unref(name
);
566 * Test the given node for the given class
568 * This will return true (via the "match" pointer) if the libdom node
569 * has the given class. The comparison is case-sensitive. It
570 * corresponds to node.class in CSS.
572 * \param pw Pointer to the current SVG parser state
573 * \param node Libdom SVG node to test
574 * \param name Class name to check for
575 * \param match Pointer to the test result
577 * \return Always returns CSS_OK
579 css_error
node_has_class(void *pw
, void *node
,
580 lwc_string
*name
, bool *match
)
583 /* libdom implements this for us and apparently it cannot fail */
584 dom_element_has_class((dom_node
*)node
, name
, match
);
590 * Test the given node for the given id
592 * This will return true (via the "match" pointer) if the libdom node
593 * has the given id. The comparison is case-sensitive. It corresponds
596 * \param pw Pointer to the current SVG parser state
597 * \param node Libdom SVG node to test
598 * \param name Id to check for
599 * \param match Pointer to the test result
601 * \return Always returns CSS_OK
603 css_error
node_has_id(void *pw
, void *node
,
604 lwc_string
*name
, bool *match
)
608 struct svgtiny_parse_state
*state
;
610 attr
= NULL
; /* a priori the "id" attribute may not exist */
611 *match
= false; /* default to no match */
613 state
= (struct svgtiny_parse_state
*)pw
;
614 err
= dom_element_get_attribute((dom_node
*)node
,
615 state
->interned_id
, &attr
);
616 if (err
!= DOM_NO_ERR
|| attr
== NULL
) {
620 *match
= dom_string_lwc_isequal(attr
, name
);
621 dom_string_unref(attr
);
628 * Test the given node for the given attribute
630 * This will return true (via the "match" pointer) if the libdom node
631 * has an attribute with the given name. The comparison is
632 * case-sensitive. It corresponds to node[attr] in CSS.
634 * \param pw Pointer to the current SVG parser state
635 * \param node Libdom SVG node to test
636 * \param qname Attribute name to check for
637 * \param match Pointer to the test result
639 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
642 css_error
node_has_attribute(void *pw
, void *node
,
643 const css_qname
*qname
, bool *match
)
649 /* intern the attribute name as a dom_string so we can
650 * delegate to dom_element_has_attribute() */
651 err
= dom_string_create_interned(
652 (const uint8_t *) lwc_string_data(qname
->name
),
653 lwc_string_length(qname
->name
),
655 if (err
!= DOM_NO_ERR
) {
659 err
= dom_element_has_attribute((dom_node
*)node
, name
, match
);
660 if (err
!= DOM_NO_ERR
) {
661 dom_string_unref(name
);
665 dom_string_unref(name
);