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
);
20 * Resolve a relative URL to an absolute one by doing nothing. This is
21 * the simplest possible implementation of a URL resolver, needed for
24 css_error
svgtiny_resolve_url(void *pw
,
25 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
30 /* Copy the relative URL to the absolute one (the return
32 *abs
= lwc_string_ref(rel
);
37 * Create a stylesheet with the default set of params.
39 * \param sheet A stylesheet pointer, passed in by reference, that
40 * we use to store the newly-created stylesheet.
41 * \param inline_style True if this stylesheet represents an inline
42 * style, and false otherwise.
44 * \return The return value from css_stylesheet_create() is returned.
46 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
49 css_stylesheet_params params
;
51 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
52 params
.level
= CSS_LEVEL_DEFAULT
;
53 params
.charset
= NULL
;
56 params
.allow_quirks
= false;
57 params
.inline_style
= inline_style
;
58 params
.resolve
= svgtiny_resolve_url
;
59 params
.resolve_pw
= NULL
;
61 params
.import_pw
= NULL
;
63 params
.color_pw
= NULL
;
65 params
.font_pw
= NULL
;
67 return css_stylesheet_create(¶ms
, sheet
);
71 /**************************/
72 /* libcss select handlers */
73 /**************************/
75 * From here on we implement the "select handler "API defined in
76 * libcss's include/libcss/select.h and discussed briefly in its
82 * Retrieve the given node's name
84 * \param pw Pointer to the current SVG parser state
85 * \param node Libdom SVG node
86 * \param qname Address at which to store the node name
88 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
90 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
94 struct svgtiny_parse_state
*state
;
96 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
97 if (err
!= DOM_NO_ERR
) {
101 state
= (struct svgtiny_parse_state
*)pw
;
102 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
104 err
= dom_string_intern(name
, &qname
->name
);
105 if (err
!= DOM_NO_ERR
) {
106 dom_string_unref(name
);
110 dom_string_unref(name
);
117 * Retrieve the given node's classes
119 * \param pw Pointer to the current SVG parser state
120 * \param node Libdom SVG node
121 * \param classes Address at which to store the class name array
122 * \param n_classes Address at which to store the length of the class
125 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
127 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
128 * because the underlying libdom function never fails
130 css_error
node_classes(void *pw
, void *node
,
131 lwc_string
***classes
, uint32_t *n_classes
)
136 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
138 /* The implementation does not do it, but the documentation
139 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
140 possible here, so we handle it to be on the safe side. */
141 if (err
!= DOM_NO_ERR
) {
150 * Retrieve the given node's id
152 * \param pw Pointer to the current SVG parser state
153 * \param node Libdom SVG node
154 * \param id Address at which to store the id
156 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
158 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
162 struct svgtiny_parse_state
*state
;
164 /* Begin with the assumption that this node has no id */
167 state
= (struct svgtiny_parse_state
*)pw
;
168 err
= dom_element_get_attribute((dom_node
*)node
,
169 state
->interned_id
, &attr
);
170 if (err
!= DOM_NO_ERR
) {
173 else if (attr
== NULL
) {
174 /* The node has no id attribute and our return value
175 is already set to NULL so we're done */
179 /* If we found an id attribute (a dom_string), intern it into
180 an lwc_string that we can return, and then cleanup the
182 err
= dom_string_intern(attr
, id
);
183 if (err
!= DOM_NO_ERR
) {
184 dom_string_unref(attr
);
187 dom_string_unref(attr
);
194 * Find the first parent of the given element having the given name
196 * \param pw Pointer to the current SVG parser state
197 * \param node Libdom SVG node
198 * \param qname Name of the parent node to search for
199 * \param parent Address at which to store the parent node pointer
201 * \return Always returns CSS_OK
203 * \post If a suitable element is found, a pointer to it will be
204 * stored at the address pointed to by \a parent; otherwise,
205 * NULL will be stored at the address pointed to by \a parent
207 css_error
named_parent_node(void *pw
, void *node
,
208 const css_qname
*qname
, void **parent
)
211 /* dom_element_named_parent_node() was invented to implement
212 * this select handler so there isn't much for us to do except
213 * call it. It's OK if node isn't an element, libdom checks
215 dom_element_named_parent_node((dom_element
*)node
,
217 (struct dom_element
**)parent
);
219 /* Implementation detail: dom_element_named_parent_node()
220 * increments the reference count of the parent element before
221 * returning it to us. According to docs/RefCnt in the libdom
222 * repository, this will prevent the parent element from being
223 * destroyed if it is pruned from the DOM. That sounds good,
224 * since we don't want to be using a pointer to an object that
225 * has been destroyed... but we also have no way of later
226 * decrementing the reference count ourselves, and don't want
227 * to make the returned node eternal. Decrementing the
228 * reference counter now allows it to be destroyed when the
229 * DOM no longer needs it, and so long as no other parts of
230 * libsvgtiny are messing with the DOM during parsing, that
231 * shouldn't (ha ha) cause any problems. */
232 dom_node_unref(*parent
);
239 * Find the "next-sibling" of the given element having the given name
241 * This search corresponds to the "+ foo" combinator in CSS and will
242 * find only "foo" element nodes that immediately precede the given
243 * node under the same parent in the DOM. In CSS the tree is viewed
244 * top-down and in libdom it is viewed from the bottom-up; as a result
245 * "next" and "previous" are sometimes backwards. This is case-sensitive.
247 * \param pw Pointer to the current SVG parser state
248 * \param node Libdom SVG node
249 * \param qname Name of the sibling node to search for
250 * \param sibling Address at which to store the sibling node pointer
252 * \return Always returns CSS_OK
254 * \post If a suitable element is found, a pointer to it will be
255 * stored at the address pointed to by \a sibling; otherwise,
256 * NULL will be stored at the address pointed to by \a sibling
258 css_error
named_sibling_node(void *pw
, void *node
,
259 const css_qname
*qname
, void **sibling
)
262 dom_node
*n
= node
; /* the current node */
263 dom_node
*prev
; /* the previous node */
268 *sibling
= NULL
; /* default to nothing found */
270 /* Begin the search; the first iteration we do outside of the
271 * loop. Implementation detil: dom_node_get_previous_sibling()
272 * increments the reference counter on the returned node. A
273 * comment within named_parent_node() explains why we
274 * decrement it ASAP. */
275 err
= dom_node_get_previous_sibling(n
, &n
);
276 if (err
!= DOM_NO_ERR
) {
281 /* We're looking for the first ELEMENT sibling */
282 err
= dom_node_get_node_type(n
, &type
);
283 if (err
!= DOM_NO_ERR
) {
288 if (type
== DOM_ELEMENT_NODE
) {
289 /* We found an element node, does it have the
291 err
= dom_node_get_node_name(n
, &name
);
292 if (err
!= DOM_NO_ERR
) {
297 if (dom_string_lwc_isequal(name
,
299 /* The name is right, return it */
303 /* There's only one next-sibling element node
304 * and we've already found it, so if its name
305 * wasn't right, we return the default value
307 dom_string_unref(name
);
312 /* Not an element node, so we move on the the previous
313 * previous sibling */
314 err
= dom_node_get_previous_sibling(n
, &prev
);
315 if (err
!= DOM_NO_ERR
) {
329 * Find the first "subsequent-sibling" of the given element having the
332 * This search corresponds to the "~ foo" combinator in CSS and will
333 * find only "foo" element nodes that precede the given node (under
334 * the same parent) in the DOM. In CSS the tree is viewed top-down and
335 * in libdom it is viewed from the bottom-up; as a result "next" and
336 * "previous" are sometimes backwards. This is case-sensitive.
338 * \param pw Pointer to the current SVG parser state
339 * \param node Libdom SVG node
340 * \param qname Name of the sibling node to search for
341 * \param sibling Address at which to store the sibling node pointer
343 * \return Always returns CSS_OK
345 * \post If a suitable element is found, a pointer to it will be
346 * stored at the address pointed to by \a sibling; otherwise,
347 * NULL will be stored at the address pointed to by \a sibling
349 css_error
named_generic_sibling_node(void *pw
, void *node
,
350 const css_qname
*qname
, void **sibling
)
353 dom_node
*n
= node
; /* the current node */
354 dom_node
*prev
; /* the previous node */
360 *sibling
= NULL
; /* default to nothing found */
362 /* Begin the search; the first iteration we do outside of the
363 * loop. Implementation detil: dom_node_get_previous_sibling()
364 * increments the reference counter on the returned node. A
365 * comment within named_parent_node() explains why we
366 * decrement it ASAP. */
367 err
= dom_node_get_previous_sibling(n
, &n
);
368 if (err
!= DOM_NO_ERR
) {
373 err
= dom_node_get_node_type(n
, &type
);
374 if (err
!= DOM_NO_ERR
) {
379 if (type
== DOM_ELEMENT_NODE
) {
380 /* We only want ELEMENT nodes */
381 err
= dom_node_get_node_name(n
, &name
);
382 if (err
!= DOM_NO_ERR
) {
387 if (dom_string_lwc_isequal(name
,
389 /* Found one. Save it and stop the search */
390 dom_string_unref(name
);
396 dom_string_unref(name
);
399 /* This sibling wasn't an element with the desired
400 name, so move on to the previous sibling */
401 err
= dom_node_get_previous_sibling(n
, &prev
);
402 if (err
!= DOM_NO_ERR
) {
416 * Return a pointer to the given node's parent
418 * \param pw Pointer to the current SVG parser state
419 * \param node Libdom SVG node
420 * \param parent Address at which to store the node's parent pointer
422 * \return Always returns CSS_OK
424 css_error
parent_node(void *pw
, void *node
, void **parent
)
427 /* Libdom basically implements this for us */
428 dom_element_parent_node(node
, (struct dom_element
**)parent
);
430 /* See the comment in named_parent_node() for why we decrement
431 * this reference counter here. */
432 dom_node_unref(*parent
);