]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement node_has_name() 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 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
21
22 /**
23 * Resolve a relative URL to an absolute one by doing nothing. This is
24 * the simplest possible implementation of a URL resolver, needed for
25 * parsing CSS.
26 */
27 css_error svgtiny_resolve_url(void *pw,
28 const char *base, lwc_string *rel, lwc_string **abs)
29 {
30 UNUSED(pw);
31 UNUSED(base);
32
33 /* Copy the relative URL to the absolute one (the return
34 value) */
35 *abs = lwc_string_ref(rel);
36 return CSS_OK;
37 }
38
39 /**
40 * Create a stylesheet with the default set of params.
41 *
42 * \param sheet A stylesheet pointer, passed in by reference, that
43 * we use to store the newly-created stylesheet.
44 * \param inline_style True if this stylesheet represents an inline
45 * style, and false otherwise.
46 *
47 * \return The return value from css_stylesheet_create() is returned.
48 */
49 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
50 bool inline_style)
51 {
52 css_stylesheet_params params;
53
54 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
55 params.level = CSS_LEVEL_DEFAULT;
56 params.charset = NULL;
57 params.url = "";
58 params.title = NULL;
59 params.allow_quirks = false;
60 params.inline_style = inline_style;
61 params.resolve = svgtiny_resolve_url;
62 params.resolve_pw = NULL;
63 params.import = NULL;
64 params.import_pw = NULL;
65 params.color = NULL;
66 params.color_pw = NULL;
67 params.font = NULL;
68 params.font_pw = NULL;
69
70 return css_stylesheet_create(&params, sheet);
71 }
72
73
74 /**************************/
75 /* libcss select handlers */
76 /**************************/
77 /*
78 * From here on we implement the "select handler "API defined in
79 * libcss's include/libcss/select.h and discussed briefly in its
80 * docs/API document.
81 */
82
83
84 /**
85 * Retrieve the given node's name
86 *
87 * \param pw Pointer to the current SVG parser state
88 * \param node Libdom SVG node
89 * \param qname Address at which to store the node name
90 *
91 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
92 */
93 css_error node_name(void *pw, void *node, css_qname *qname)
94 {
95 dom_string *name;
96 dom_exception err;
97 struct svgtiny_parse_state *state;
98
99 err = dom_node_get_node_name((dom_node *)node, &name);
100 if (err != DOM_NO_ERR) {
101 return CSS_NOMEM;
102 }
103
104 state = (struct svgtiny_parse_state *)pw;
105 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
106
107 err = dom_string_intern(name, &qname->name);
108 if (err != DOM_NO_ERR) {
109 dom_string_unref(name);
110 return CSS_NOMEM;
111 }
112
113 dom_string_unref(name);
114
115 return CSS_OK;
116 }
117
118
119 /**
120 * Retrieve the given node's classes
121 *
122 * \param pw Pointer to the current SVG parser state
123 * \param node Libdom SVG node
124 * \param classes Address at which to store the class name array
125 * \param n_classes Address at which to store the length of the class
126 * name array
127 *
128 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
129 *
130 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
131 * because the underlying libdom function never fails
132 */
133 css_error node_classes(void *pw, void *node,
134 lwc_string ***classes, uint32_t *n_classes)
135 {
136 UNUSED(pw);
137 dom_exception err;
138
139 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
140
141 /* The implementation does not do it, but the documentation
142 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
143 possible here, so we handle it to be on the safe side. */
144 if (err != DOM_NO_ERR) {
145 return CSS_NOMEM;
146 }
147
148 return CSS_OK;
149 }
150
151
152 /**
153 * Retrieve the given node's id
154 *
155 * \param pw Pointer to the current SVG parser state
156 * \param node Libdom SVG node
157 * \param id Address at which to store the id
158 *
159 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
160 */
161 css_error node_id(void *pw, void *node, lwc_string **id)
162 {
163 dom_string *attr;
164 dom_exception err;
165 struct svgtiny_parse_state *state;
166
167 /* Begin with the assumption that this node has no id */
168 *id = NULL;
169
170 state = (struct svgtiny_parse_state *)pw;
171 err = dom_element_get_attribute((dom_node *)node,
172 state->interned_id, &attr);
173 if (err != DOM_NO_ERR) {
174 return CSS_NOMEM;
175 }
176 else if (attr == NULL) {
177 /* The node has no id attribute and our return value
178 is already set to NULL so we're done */
179 return CSS_OK;
180 }
181
182 /* If we found an id attribute (a dom_string), intern it into
183 an lwc_string that we can return, and then cleanup the
184 dom_string. */
185 err = dom_string_intern(attr, id);
186 if (err != DOM_NO_ERR) {
187 dom_string_unref(attr);
188 return CSS_NOMEM;
189 }
190 dom_string_unref(attr);
191 return CSS_OK;
192 }
193
194
195
196 /**
197 * Find the first parent of the given element having the given name
198 *
199 * \param pw Pointer to the current SVG parser state
200 * \param node Libdom SVG node
201 * \param qname Name of the parent node to search for
202 * \param parent Address at which to store the parent node pointer
203 *
204 * \return Always returns CSS_OK
205 *
206 * \post If a suitable element is found, a pointer to it will be
207 * stored at the address pointed to by \a parent; otherwise,
208 * NULL will be stored at the address pointed to by \a parent
209 */
210 css_error named_parent_node(void *pw, void *node,
211 const css_qname *qname, void **parent)
212 {
213 UNUSED(pw);
214 /* dom_element_named_parent_node() was invented to implement
215 * this select handler so there isn't much for us to do except
216 * call it. It's OK if node isn't an element, libdom checks
217 * for it. */
218 dom_element_named_parent_node((dom_element *)node,
219 qname->name,
220 (struct dom_element **)parent);
221
222 /* Implementation detail: dom_element_named_parent_node()
223 * increments the reference count of the parent element before
224 * returning it to us. According to docs/RefCnt in the libdom
225 * repository, this will prevent the parent element from being
226 * destroyed if it is pruned from the DOM. That sounds good,
227 * since we don't want to be using a pointer to an object that
228 * has been destroyed... but we also have no way of later
229 * decrementing the reference count ourselves, and don't want
230 * to make the returned node eternal. Decrementing the
231 * reference counter now allows it to be destroyed when the
232 * DOM no longer needs it, and so long as no other parts of
233 * libsvgtiny are messing with the DOM during parsing, that
234 * shouldn't (ha ha) cause any problems. */
235 dom_node_unref(*parent);
236
237 return CSS_OK;
238 }
239
240
241 /**
242 * Find the "next-sibling" of the given element having the given name
243 *
244 * This search corresponds to the "+ foo" combinator in CSS and will
245 * find only "foo" element nodes that immediately precede the given
246 * node under the same parent in the DOM. In CSS the tree is viewed
247 * top-down and in libdom it is viewed from the bottom-up; as a result
248 * "next" and "previous" are sometimes backwards. This is case-sensitive.
249 *
250 * \param pw Pointer to the current SVG parser state
251 * \param node Libdom SVG node
252 * \param qname Name of the sibling node to search for
253 * \param sibling Address at which to store the sibling node pointer
254 *
255 * \return Always returns CSS_OK
256 *
257 * \post If a suitable element is found, a pointer to it will be
258 * stored at the address pointed to by \a sibling; otherwise,
259 * NULL will be stored at the address pointed to by \a sibling
260 */
261 css_error named_sibling_node(void *pw, void *node,
262 const css_qname *qname, void **sibling)
263 {
264 UNUSED(pw);
265 dom_node *n = node; /* the current node */
266 dom_node *prev; /* the previous node */
267 dom_exception err;
268 dom_node_type type;
269 dom_string *name;
270
271 *sibling = NULL; /* default to nothing found */
272
273 /* Begin the search; the first iteration we do outside of the
274 * loop. Implementation detil: dom_node_get_previous_sibling()
275 * increments the reference counter on the returned node. A
276 * comment within named_parent_node() explains why we
277 * decrement it ASAP. */
278 err = dom_node_get_previous_sibling(n, &n);
279 if (err != DOM_NO_ERR) {
280 return CSS_OK;
281 }
282
283 while (n != NULL) {
284 /* We're looking for the first ELEMENT sibling */
285 err = dom_node_get_node_type(n, &type);
286 if (err != DOM_NO_ERR) {
287 dom_node_unref(n);
288 return CSS_OK;
289 }
290
291 if (type == DOM_ELEMENT_NODE) {
292 /* We found an element node, does it have the
293 * right name? */
294 err = dom_node_get_node_name(n, &name);
295 if (err != DOM_NO_ERR) {
296 dom_node_unref(n);
297 return CSS_OK;
298 }
299
300 if (dom_string_lwc_isequal(name,
301 qname->name)) {
302 /* The name is right, return it */
303 *sibling = n;
304 }
305
306 /* There's only one next-sibling element node
307 * and we've already found it, so if its name
308 * wasn't right, we return the default value
309 * of NULL below */
310 dom_string_unref(name);
311 dom_node_unref(n);
312 return CSS_OK;
313 }
314
315 /* Not an element node, so we move on the the previous
316 * previous sibling */
317 err = dom_node_get_previous_sibling(n, &prev);
318 if (err != DOM_NO_ERR) {
319 dom_node_unref(n);
320 return CSS_OK;
321 }
322
323 dom_node_unref(n);
324 n = prev;
325 }
326
327 return CSS_OK;
328 }
329
330
331 /**
332 * Find the first "subsequent-sibling" of the given element having the
333 * given name
334 *
335 * This search corresponds to the "~ foo" combinator in CSS and will
336 * find only "foo" element nodes that precede the given node (under
337 * the same parent) in the DOM. In CSS the tree is viewed top-down and
338 * in libdom it is viewed from the bottom-up; as a result "next" and
339 * "previous" are sometimes backwards. This is case-sensitive.
340 *
341 * \param pw Pointer to the current SVG parser state
342 * \param node Libdom SVG node
343 * \param qname Name of the sibling node to search for
344 * \param sibling Address at which to store the sibling node pointer
345 *
346 * \return Always returns CSS_OK
347 *
348 * \post If a suitable element is found, a pointer to it will be
349 * stored at the address pointed to by \a sibling; otherwise,
350 * NULL will be stored at the address pointed to by \a sibling
351 */
352 css_error named_generic_sibling_node(void *pw, void *node,
353 const css_qname *qname, void **sibling)
354 {
355 UNUSED(pw);
356 dom_node *n = node; /* the current node */
357 dom_node *prev; /* the previous node */
358 dom_exception err;
359 dom_node_type type;
360 dom_string *name;
361
362
363 *sibling = NULL; /* default to nothing found */
364
365 /* Begin the search; the first iteration we do outside of the
366 * loop. Implementation detil: dom_node_get_previous_sibling()
367 * increments the reference counter on the returned node. A
368 * comment within named_parent_node() explains why we
369 * decrement it ASAP. */
370 err = dom_node_get_previous_sibling(n, &n);
371 if (err != DOM_NO_ERR) {
372 return CSS_OK;
373 }
374
375 while (n != NULL) {
376 err = dom_node_get_node_type(n, &type);
377 if (err != DOM_NO_ERR) {
378 dom_node_unref(n);
379 return CSS_OK;
380 }
381
382 if (type == DOM_ELEMENT_NODE) {
383 /* We only want ELEMENT nodes */
384 err = dom_node_get_node_name(n, &name);
385 if (err != DOM_NO_ERR) {
386 dom_node_unref(n);
387 return CSS_OK;
388 }
389
390 if (dom_string_lwc_isequal(name,
391 qname->name)) {
392 /* Found one. Save it and stop the search */
393 dom_string_unref(name);
394 dom_node_unref(n);
395 *sibling = n;
396 return CSS_OK;
397 }
398
399 dom_string_unref(name);
400 }
401
402 /* This sibling wasn't an element with the desired
403 name, so move on to the previous sibling */
404 err = dom_node_get_previous_sibling(n, &prev);
405 if (err != DOM_NO_ERR) {
406 dom_node_unref(n);
407 return CSS_OK;
408 }
409
410 dom_node_unref(n);
411 n = prev;
412 }
413
414 return CSS_OK;
415 }
416
417
418 /**
419 * Return a pointer to the given node's parent
420 *
421 * \param pw Pointer to the current SVG parser state
422 * \param node Libdom SVG node
423 * \param parent Address at which to store the node's parent pointer
424 *
425 * \return Always returns CSS_OK
426 */
427 css_error parent_node(void *pw, void *node, void **parent)
428 {
429 UNUSED(pw);
430 /* Libdom basically implements this for us */
431 dom_element_parent_node(node, (struct dom_element **)parent);
432
433 /* See the comment in named_parent_node() for why we decrement
434 * this reference counter here. */
435 dom_node_unref(*parent);
436
437 return CSS_OK;
438 }
439
440
441 /**
442 * Find the "next-sibling" of the given element
443 *
444 * This search corresponds "+ *" in CSS and will find the first
445 * element node that immediately precedes the given node under the
446 * same parent in the DOM. In CSS the tree is viewed top-down and in
447 * libdom it is viewed from the bottom-up; as a result "next" and
448 * "previous" are sometimes backwards.
449 *
450 * \param pw Pointer to the current SVG parser state
451 * \param node Libdom SVG node
452 * \param sibling Address at which to store the sibling node pointer
453 *
454 * \return Always returns CSS_OK
455 *
456 * \post If a suitable element is found, a pointer to it will be
457 * stored at the address pointed to by \a sibling; otherwise,
458 * NULL will be stored at the address pointed to by \a sibling
459 */
460 css_error sibling_node(void *pw, void *node, void **sibling)
461 {
462 UNUSED(pw);
463 dom_node *n = node; /* the current node */
464 dom_node *prev; /* the previous node */
465 dom_exception err;
466 dom_node_type type;
467
468 *sibling = NULL; /* default to nothing found */
469
470 /* Begin the search; the first iteration we do outside of the
471 * loop. Implementation detil: dom_node_get_previous_sibling()
472 * increments the reference counter on the returned node. A
473 * comment within named_parent_node() explains why we
474 * decrement it ASAP. */
475 err = dom_node_get_previous_sibling(n, &n);
476 if (err != DOM_NO_ERR) {
477 return CSS_OK;
478 }
479
480 while (n != NULL) {
481 err = dom_node_get_node_type(n, &type);
482 if (err != DOM_NO_ERR) {
483 dom_node_unref(n);
484 return CSS_OK;
485 }
486
487 if (type == DOM_ELEMENT_NODE) {
488 /* We found a sibling node that is also an
489 element and that's all we wanted. */
490 *sibling = n;
491 dom_node_unref(n);
492 return CSS_OK;
493 }
494
495 /* This sibling node was not an element; move on to
496 the previous sibling */
497 err = dom_node_get_previous_sibling(n, &prev);
498 if (err != DOM_NO_ERR) {
499 dom_node_unref(n);
500 return CSS_OK;
501 }
502
503 dom_node_unref(n);
504 n = prev;
505 }
506
507 return CSS_OK;
508 }
509
510
511 /**
512 * Test the given node for the given name
513 *
514 * This will return true (via the "match" pointer) if the libdom node
515 * has the given name or if that name is the universal selector;
516 * otherwise it returns false. The comparison is case-sensitive. It
517 * corresponds to a rule like "body { ... }" in CSS.
518 *
519 * \param pw Pointer to the current SVG parser state
520 * \param node Libdom SVG node to test
521 * \param qname Name to check for
522 * \param match Pointer to the test result
523 *
524 * \return Always returns CSS_OK
525 */
526 css_error node_has_name(void *pw, void *node,
527 const css_qname *qname, bool *match)
528 {
529 struct svgtiny_parse_state *state;
530 dom_string *name;
531 dom_exception err;
532
533 /* Start by checking to see if qname is the universal selector */
534 state = (struct svgtiny_parse_state *)pw;
535 if (lwc_string_isequal(qname->name,
536 state->interned_universal, match) == lwc_error_ok) {
537 if (*match) {
538 /* It's the universal selector. In NetSurf, all node
539 * names match the universal selector, and nothing in
540 * the libcss documentation suggests another approach,
541 * so we follow NetSurf here. */
542 return CSS_OK;
543 }
544 }
545
546 err = dom_node_get_node_name((dom_node *)node, &name);
547 if (err != DOM_NO_ERR) {
548 return CSS_OK;
549 }
550
551 /* Unlike with HTML, SVG element names are case-sensitive */
552 *match = dom_string_lwc_isequal(name, qname->name);
553 dom_string_unref(name);
554
555 return CSS_OK;
556 }