]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement parent_node() select handler
[libsvgtiny.git] / src / svgtiny_css.c
1 #include <libcss/libcss.h>
2
3 #include "svgtiny.h"
4 #include "svgtiny_internal.h"
5
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
18
19 /**
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
22 * parsing CSS.
23 */
24 css_error svgtiny_resolve_url(void *pw,
25 const char *base, lwc_string *rel, lwc_string **abs)
26 {
27 UNUSED(pw);
28 UNUSED(base);
29
30 /* Copy the relative URL to the absolute one (the return
31 value) */
32 *abs = lwc_string_ref(rel);
33 return CSS_OK;
34 }
35
36 /**
37 * Create a stylesheet with the default set of params.
38 *
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.
43 *
44 * \return The return value from css_stylesheet_create() is returned.
45 */
46 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
47 bool inline_style)
48 {
49 css_stylesheet_params params;
50
51 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
52 params.level = CSS_LEVEL_DEFAULT;
53 params.charset = NULL;
54 params.url = "";
55 params.title = NULL;
56 params.allow_quirks = false;
57 params.inline_style = inline_style;
58 params.resolve = svgtiny_resolve_url;
59 params.resolve_pw = NULL;
60 params.import = NULL;
61 params.import_pw = NULL;
62 params.color = NULL;
63 params.color_pw = NULL;
64 params.font = NULL;
65 params.font_pw = NULL;
66
67 return css_stylesheet_create(&params, sheet);
68 }
69
70
71 /**************************/
72 /* libcss select handlers */
73 /**************************/
74 /*
75 * From here on we implement the "select handler "API defined in
76 * libcss's include/libcss/select.h and discussed briefly in its
77 * docs/API document.
78 */
79
80
81 /**
82 * Retrieve the given node's name
83 *
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
87 *
88 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
89 */
90 css_error node_name(void *pw, void *node, css_qname *qname)
91 {
92 dom_string *name;
93 dom_exception err;
94 struct svgtiny_parse_state *state;
95
96 err = dom_node_get_node_name((dom_node *)node, &name);
97 if (err != DOM_NO_ERR) {
98 return CSS_NOMEM;
99 }
100
101 state = (struct svgtiny_parse_state *)pw;
102 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
103
104 err = dom_string_intern(name, &qname->name);
105 if (err != DOM_NO_ERR) {
106 dom_string_unref(name);
107 return CSS_NOMEM;
108 }
109
110 dom_string_unref(name);
111
112 return CSS_OK;
113 }
114
115
116 /**
117 * Retrieve the given node's classes
118 *
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
123 * name array
124 *
125 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
126 *
127 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
128 * because the underlying libdom function never fails
129 */
130 css_error node_classes(void *pw, void *node,
131 lwc_string ***classes, uint32_t *n_classes)
132 {
133 UNUSED(pw);
134 dom_exception err;
135
136 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
137
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) {
142 return CSS_NOMEM;
143 }
144
145 return CSS_OK;
146 }
147
148
149 /**
150 * Retrieve the given node's id
151 *
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
155 *
156 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
157 */
158 css_error node_id(void *pw, void *node, lwc_string **id)
159 {
160 dom_string *attr;
161 dom_exception err;
162 struct svgtiny_parse_state *state;
163
164 /* Begin with the assumption that this node has no id */
165 *id = NULL;
166
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) {
171 return CSS_NOMEM;
172 }
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 */
176 return CSS_OK;
177 }
178
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
181 dom_string. */
182 err = dom_string_intern(attr, id);
183 if (err != DOM_NO_ERR) {
184 dom_string_unref(attr);
185 return CSS_NOMEM;
186 }
187 dom_string_unref(attr);
188 return CSS_OK;
189 }
190
191
192
193 /**
194 * Find the first parent of the given element having the given name
195 *
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
200 *
201 * \return Always returns CSS_OK
202 *
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
206 */
207 css_error named_parent_node(void *pw, void *node,
208 const css_qname *qname, void **parent)
209 {
210 UNUSED(pw);
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
214 * for it. */
215 dom_element_named_parent_node((dom_element *)node,
216 qname->name,
217 (struct dom_element **)parent);
218
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);
233
234 return CSS_OK;
235 }
236
237
238 /**
239 * Find the "next-sibling" of the given element having the given name
240 *
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.
246 *
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
251 *
252 * \return Always returns CSS_OK
253 *
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
257 */
258 css_error named_sibling_node(void *pw, void *node,
259 const css_qname *qname, void **sibling)
260 {
261 UNUSED(pw);
262 dom_node *n = node; /* the current node */
263 dom_node *prev; /* the previous node */
264 dom_exception err;
265 dom_node_type type;
266 dom_string *name;
267
268 *sibling = NULL; /* default to nothing found */
269
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) {
277 return CSS_OK;
278 }
279
280 while (n != NULL) {
281 /* We're looking for the first ELEMENT sibling */
282 err = dom_node_get_node_type(n, &type);
283 if (err != DOM_NO_ERR) {
284 dom_node_unref(n);
285 return CSS_OK;
286 }
287
288 if (type == DOM_ELEMENT_NODE) {
289 /* We found an element node, does it have the
290 * right name? */
291 err = dom_node_get_node_name(n, &name);
292 if (err != DOM_NO_ERR) {
293 dom_node_unref(n);
294 return CSS_OK;
295 }
296
297 if (dom_string_lwc_isequal(name,
298 qname->name)) {
299 /* The name is right, return it */
300 *sibling = n;
301 }
302
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
306 * of NULL below */
307 dom_string_unref(name);
308 dom_node_unref(n);
309 return CSS_OK;
310 }
311
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) {
316 dom_node_unref(n);
317 return CSS_OK;
318 }
319
320 dom_node_unref(n);
321 n = prev;
322 }
323
324 return CSS_OK;
325 }
326
327
328 /**
329 * Find the first "subsequent-sibling" of the given element having the
330 * given name
331 *
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.
337 *
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
342 *
343 * \return Always returns CSS_OK
344 *
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
348 */
349 css_error named_generic_sibling_node(void *pw, void *node,
350 const css_qname *qname, void **sibling)
351 {
352 UNUSED(pw);
353 dom_node *n = node; /* the current node */
354 dom_node *prev; /* the previous node */
355 dom_exception err;
356 dom_node_type type;
357 dom_string *name;
358
359
360 *sibling = NULL; /* default to nothing found */
361
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) {
369 return CSS_OK;
370 }
371
372 while (n != NULL) {
373 err = dom_node_get_node_type(n, &type);
374 if (err != DOM_NO_ERR) {
375 dom_node_unref(n);
376 return CSS_OK;
377 }
378
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) {
383 dom_node_unref(n);
384 return CSS_OK;
385 }
386
387 if (dom_string_lwc_isequal(name,
388 qname->name)) {
389 /* Found one. Save it and stop the search */
390 dom_string_unref(name);
391 dom_node_unref(n);
392 *sibling = n;
393 return CSS_OK;
394 }
395
396 dom_string_unref(name);
397 }
398
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) {
403 dom_node_unref(n);
404 return CSS_OK;
405 }
406
407 dom_node_unref(n);
408 n = prev;
409 }
410
411 return CSS_OK;
412 }
413
414
415 /**
416 * Return a pointer to the given node's parent
417 *
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
421 *
422 * \return Always returns CSS_OK
423 */
424 css_error parent_node(void *pw, void *node, void **parent)
425 {
426 UNUSED(pw);
427 /* Libdom basically implements this for us */
428 dom_element_parent_node(node, (struct dom_element **)parent);
429
430 /* See the comment in named_parent_node() for why we decrement
431 * this reference counter here. */
432 dom_node_unref(*parent);
433
434 return CSS_OK;
435 }