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