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