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
);
19 * Resolve a relative URL to an absolute one by doing nothing. This is
20 * the simplest possible implementation of a URL resolver, needed for
23 css_error
svgtiny_resolve_url(void *pw
,
24 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
29 /* Copy the relative URL to the absolute one (the return
31 *abs
= lwc_string_ref(rel
);
36 * Create a stylesheet with the default set of params.
38 * \param sheet A stylesheet pointer, passed in by reference, that
39 * we use to store the newly-created stylesheet.
40 * \param inline_style True if this stylesheet represents an inline
41 * style, and false otherwise.
43 * \return The return value from css_stylesheet_create() is returned.
45 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
48 css_stylesheet_params params
;
50 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
51 params
.level
= CSS_LEVEL_DEFAULT
;
52 params
.charset
= NULL
;
55 params
.allow_quirks
= false;
56 params
.inline_style
= inline_style
;
57 params
.resolve
= svgtiny_resolve_url
;
58 params
.resolve_pw
= NULL
;
60 params
.import_pw
= NULL
;
62 params
.color_pw
= NULL
;
64 params
.font_pw
= NULL
;
66 return css_stylesheet_create(¶ms
, sheet
);
70 /**************************/
71 /* libcss select handlers */
72 /**************************/
74 * From here on we implement the "select handler "API defined in
75 * libcss's include/libcss/select.h and discussed briefly in its
81 * Retrieve the given node's name
83 * \param pw Pointer to the current SVG parser state
84 * \param node Libdom SVG node
85 * \param qname Address at which to store the node name
87 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
89 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
93 struct svgtiny_parse_state
*state
;
95 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
96 if (err
!= DOM_NO_ERR
) {
100 state
= (struct svgtiny_parse_state
*)pw
;
101 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
103 err
= dom_string_intern(name
, &qname
->name
);
104 if (err
!= DOM_NO_ERR
) {
105 dom_string_unref(name
);
109 dom_string_unref(name
);
116 * Retrieve the given node's classes
118 * \param pw Pointer to the current SVG parser state
119 * \param node Libdom SVG node
120 * \param classes Address at which to store the class name array
121 * \param n_classes Address at which to store the length of the class
124 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
126 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
127 * because the underlying libdom function never fails
129 css_error
node_classes(void *pw
, void *node
,
130 lwc_string
***classes
, uint32_t *n_classes
)
135 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
137 /* The implementation does not do it, but the documentation
138 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
139 possible here, so we handle it to be on the safe side. */
140 if (err
!= DOM_NO_ERR
) {
149 * Retrieve the given node's id
151 * \param pw Pointer to the current SVG parser state
152 * \param node Libdom SVG node
153 * \param id Address at which to store the id
155 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
157 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
161 struct svgtiny_parse_state
*state
;
163 /* Begin with the assumption that this node has no id */
166 state
= (struct svgtiny_parse_state
*)pw
;
167 err
= dom_element_get_attribute((dom_node
*)node
,
168 state
->interned_id
, &attr
);
169 if (err
!= DOM_NO_ERR
) {
172 else if (attr
== NULL
) {
173 /* The node has no id attribute and our return value
174 is already set to NULL so we're done */
178 /* If we found an id attribute (a dom_string), intern it into
179 an lwc_string that we can return, and then cleanup the
181 err
= dom_string_intern(attr
, id
);
182 if (err
!= DOM_NO_ERR
) {
183 dom_string_unref(attr
);
186 dom_string_unref(attr
);
193 * Find the first parent of the given element having the given name
195 * \param pw Pointer to the current SVG parser state
196 * \param node Libdom SVG node
197 * \param qname Name of the parent node to search for
198 * \param parent Address at which to store the parent node pointer
200 * \return Always returns CSS_OK
202 * \post If a suitable element is found, a pointer to it will be
203 * stored at the address pointed to by \a parent; otherwise,
204 * NULL will be stored at the address pointed to by \a parent
206 css_error
named_parent_node(void *pw
, void *node
,
207 const css_qname
*qname
, void **parent
)
210 /* dom_element_named_parent_node() was invented to implement
211 * this select handler so there isn't much for us to do except
212 * call it. It's OK if node isn't an element, libdom checks
214 dom_element_named_parent_node((dom_element
*)node
,
216 (struct dom_element
**)parent
);
218 /* Implementation detail: dom_element_named_parent_node()
219 * increments the reference count of the parent element before
220 * returning it to us. According to docs/RefCnt in the libdom
221 * repository, this will prevent the parent element from being
222 * destroyed if it is pruned from the DOM. That sounds good,
223 * since we don't want to be using a pointer to an object that
224 * has been destroyed... but we also have no way of later
225 * decrementing the reference count ourselves, and don't want
226 * to make the returned node eternal. Decrementing the
227 * reference counter now allows it to be destroyed when the
228 * DOM no longer needs it, and so long as no other parts of
229 * libsvgtiny are messing with the DOM during parsing, that
230 * shouldn't (ha ha) cause any problems. */
231 dom_node_unref(*parent
);
238 * Find the "next-sibling" of the given element having the given name
240 * This search corresponds to the "+ foo" combinator in CSS and will
241 * find only "foo" element nodes that immediately precede the given
242 * node under the same parent in the DOM. In CSS the tree is viewed
243 * top-down and in libdom it is viewed from the bottom-up; as a result
244 * "next" and "previous" are sometimes backwards. This is case-sensitive.
246 * \param pw Pointer to the current SVG parser state
247 * \param node Libdom SVG node
248 * \param qname Name of the sibling node to search for
249 * \param sibling Address at which to store the sibling node pointer
251 * \return Always returns CSS_OK
253 * \post If a suitable element is found, a pointer to it will be
254 * stored at the address pointed to by \a sibling; otherwise,
255 * NULL will be stored at the address pointed to by \a sibling
257 css_error
named_sibling_node(void *pw
, void *node
,
258 const css_qname
*qname
, void **sibling
)
261 dom_node
*n
= node
; /* the current node */
262 dom_node
*prev
; /* the previous node */
267 *sibling
= NULL
; /* default to nothing found */
269 /* Begin the search; the first iteration we do outside of the
270 * loop. Implementation detil: dom_node_get_previous_sibling()
271 * increments the reference counter on the returned node. A
272 * comment within named_parent_node() explains why we
273 * decrement it ASAP. */
274 err
= dom_node_get_previous_sibling(n
, &n
);
275 if (err
!= DOM_NO_ERR
) {
280 /* We're looking for the first ELEMENT sibling */
281 err
= dom_node_get_node_type(n
, &type
);
282 if (err
!= DOM_NO_ERR
) {
287 if (type
== DOM_ELEMENT_NODE
) {
288 /* We found an element node, does it have the
290 err
= dom_node_get_node_name(n
, &name
);
291 if (err
!= DOM_NO_ERR
) {
296 if (dom_string_lwc_isequal(name
,
298 /* The name is right, return it */
302 /* There's only one next-sibling element node
303 * and we've already found it, so if its name
304 * wasn't right, we return the default value
306 dom_string_unref(name
);
311 /* Not an element node, so we move on the the previous
312 * previous sibling */
313 err
= dom_node_get_previous_sibling(n
, &prev
);
314 if (err
!= DOM_NO_ERR
) {
328 * Find the first "subsequent-sibling" of the given element having the
331 * This search corresponds to the "~ foo" combinator in CSS and will
332 * find only "foo" element nodes that precede the given node (under
333 * the same parent) in the DOM. In CSS the tree is viewed top-down and
334 * in libdom it is viewed from the bottom-up; as a result "next" and
335 * "previous" are sometimes backwards. This is case-sensitive.
337 * \param pw Pointer to the current SVG parser state
338 * \param node Libdom SVG node
339 * \param qname Name of the sibling node to search for
340 * \param sibling Address at which to store the sibling node pointer
342 * \return Always returns CSS_OK
344 * \post If a suitable element is found, a pointer to it will be
345 * stored at the address pointed to by \a sibling; otherwise,
346 * NULL will be stored at the address pointed to by \a sibling
348 css_error
named_generic_sibling_node(void *pw
, void *node
,
349 const css_qname
*qname
, void **sibling
)
352 dom_node
*n
= node
; /* the current node */
353 dom_node
*prev
; /* the previous node */
359 *sibling
= NULL
; /* default to nothing found */
361 /* Begin the search; the first iteration we do outside of the
362 * loop. Implementation detil: dom_node_get_previous_sibling()
363 * increments the reference counter on the returned node. A
364 * comment within named_parent_node() explains why we
365 * decrement it ASAP. */
366 err
= dom_node_get_previous_sibling(n
, &n
);
367 if (err
!= DOM_NO_ERR
) {
372 err
= dom_node_get_node_type(n
, &type
);
373 if (err
!= DOM_NO_ERR
) {
378 if (type
== DOM_ELEMENT_NODE
) {
379 /* We only want ELEMENT nodes */
380 err
= dom_node_get_node_name(n
, &name
);
381 if (err
!= DOM_NO_ERR
) {
386 if (dom_string_lwc_isequal(name
,
388 /* Found one. Save it and stop the search */
389 dom_string_unref(name
);
395 dom_string_unref(name
);
398 /* This sibling wasn't an element with the desired
399 name, so move on to the previous sibling */
400 err
= dom_node_get_previous_sibling(n
, &prev
);
401 if (err
!= DOM_NO_ERR
) {