]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement get_libcss_node_data() select handler
[libsvgtiny.git] / src / svgtiny_css.c
1 #include <libcss/libcss.h>
2 #include <strings.h> /* strncasecmp */
3
4 #include "svgtiny.h"
5 #include "svgtiny_internal.h"
6
7 static css_error node_name(void *pw, void *node, css_qname *qname);
8 static css_error node_classes(void *pw, void *node,
9 lwc_string ***classes, uint32_t *n_classes);
10 static css_error node_id(void *pw, void *node, lwc_string **id);
11 static css_error named_parent_node(void *pw, void *node,
12 const css_qname *qname, void **parent);
13 static css_error named_sibling_node(void *pw, void *node,
14 const css_qname *qname, void **sibling);
15 static css_error named_generic_sibling_node(void *pw, void *node,
16 const css_qname *qname, void **sibling);
17 static css_error parent_node(void *pw, void *node, void **parent);
18 static css_error sibling_node(void *pw, void *node, void **sibling);
19 static css_error node_has_name(void *pw, void *node,
20 const css_qname *qname, bool *match);
21 static css_error node_has_class(void *pw, void *node,
22 lwc_string *name, bool *match);
23 static css_error node_has_id(void *pw, void *node,
24 lwc_string *name, bool *match);
25 static css_error node_has_attribute(void *pw, void *node,
26 const css_qname *qname, bool *match);
27 static css_error node_has_attribute_equal(void *pw, void *node,
28 const css_qname *qname, lwc_string *value,
29 bool *match);
30 static css_error node_has_attribute_dashmatch(void *pw, void *node,
31 const css_qname *qname, lwc_string *value,
32 bool *match);
33 static css_error node_has_attribute_includes(void *pw, void *node,
34 const css_qname *qname, lwc_string *word,
35 bool *match);
36 static css_error node_has_attribute_prefix(void *pw, void *node,
37 const css_qname *qname, lwc_string *prefix,
38 bool *match);
39 static css_error node_has_attribute_suffix(void *pw, void *node,
40 const css_qname *qname, lwc_string *suffix,
41 bool *match);
42 static css_error node_has_attribute_substring(void *pw, void *node,
43 const css_qname *qname, lwc_string *substring,
44 bool *match);
45 static css_error node_is_root(void *pw, void *node, bool *match);
46 static css_error node_count_siblings(void *pw, void *node,
47 bool same_name, bool after, int32_t *count);
48 static css_error node_is_empty(void *pw, void *node, bool *is_empty);
49 static css_error node_is_link(void *pw, void *node, bool *is_link);
50 static css_error node_is_visited(void *pw, void *node, bool *is_visited);
51 static css_error node_is_hover(void *pw, void *node, bool *is_hover);
52 static css_error node_is_active(void *pw, void *node, bool *is_active);
53 static css_error node_is_focus(void *pw, void *node, bool *is_focus);
54 static css_error node_is_enabled(void *pw, void *node, bool *is_enabled);
55 static css_error node_is_disabled(void *pw, void *node, bool *is_disabled);
56 static css_error node_is_checked(void *pw, void *node, bool *is_checked);
57 static css_error node_is_target(void *pw, void *node, bool *is_target);
58 static css_error node_is_lang(void *pw, void *node,
59 lwc_string *lang, bool *is_lang);
60 static css_error ua_default_for_property(void *pw, uint32_t property,
61 css_hint *hint);
62 static css_error set_libcss_node_data(void *pw, void *node,
63 void *libcss_node_data);
64 static css_error get_libcss_node_data(void *pw, void *node,
65 void **libcss_node_data);
66
67
68 /**
69 * Resolve a relative URL to an absolute one by doing nothing. This is
70 * the simplest possible implementation of a URL resolver, needed for
71 * parsing CSS.
72 */
73 css_error svgtiny_resolve_url(void *pw,
74 const char *base, lwc_string *rel, lwc_string **abs)
75 {
76 UNUSED(pw);
77 UNUSED(base);
78
79 /* Copy the relative URL to the absolute one (the return
80 value) */
81 *abs = lwc_string_ref(rel);
82 return CSS_OK;
83 }
84
85 /**
86 * Create a stylesheet with the default set of params.
87 *
88 * \param sheet A stylesheet pointer, passed in by reference, that
89 * we use to store the newly-created stylesheet.
90 * \param inline_style True if this stylesheet represents an inline
91 * style, and false otherwise.
92 *
93 * \return The return value from css_stylesheet_create() is returned.
94 */
95 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
96 bool inline_style)
97 {
98 css_stylesheet_params params;
99
100 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
101 params.level = CSS_LEVEL_DEFAULT;
102 params.charset = NULL;
103 params.url = "";
104 params.title = NULL;
105 params.allow_quirks = false;
106 params.inline_style = inline_style;
107 params.resolve = svgtiny_resolve_url;
108 params.resolve_pw = NULL;
109 params.import = NULL;
110 params.import_pw = NULL;
111 params.color = NULL;
112 params.color_pw = NULL;
113 params.font = NULL;
114 params.font_pw = NULL;
115
116 return css_stylesheet_create(&params, sheet);
117 }
118
119
120 /**************************/
121 /* libcss select handlers */
122 /**************************/
123 /*
124 * From here on we implement the "select handler "API defined in
125 * libcss's include/libcss/select.h and discussed briefly in its
126 * docs/API document.
127 */
128
129
130 /**
131 * Retrieve the given node's name
132 *
133 * \param pw Pointer to the current SVG parser state
134 * \param node Libdom SVG node
135 * \param qname Address at which to store the node name
136 *
137 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
138 */
139 css_error node_name(void *pw, void *node, css_qname *qname)
140 {
141 dom_string *name;
142 dom_exception err;
143 struct svgtiny_parse_state *state;
144
145 err = dom_node_get_node_name((dom_node *)node, &name);
146 if (err != DOM_NO_ERR) {
147 return CSS_NOMEM;
148 }
149
150 state = (struct svgtiny_parse_state *)pw;
151 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
152
153 err = dom_string_intern(name, &qname->name);
154 if (err != DOM_NO_ERR) {
155 dom_string_unref(name);
156 return CSS_NOMEM;
157 }
158
159 dom_string_unref(name);
160
161 return CSS_OK;
162 }
163
164
165 /**
166 * Retrieve the given node's classes
167 *
168 * \param pw Pointer to the current SVG parser state
169 * \param node Libdom SVG node
170 * \param classes Address at which to store the class name array
171 * \param n_classes Address at which to store the length of the class
172 * name array
173 *
174 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
175 *
176 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
177 * because the underlying libdom function never fails
178 */
179 css_error node_classes(void *pw, void *node,
180 lwc_string ***classes, uint32_t *n_classes)
181 {
182 UNUSED(pw);
183 dom_exception err;
184
185 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
186
187 /* The implementation does not do it, but the documentation
188 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
189 possible here, so we handle it to be on the safe side. */
190 if (err != DOM_NO_ERR) {
191 return CSS_NOMEM;
192 }
193
194 return CSS_OK;
195 }
196
197
198 /**
199 * Retrieve the given node's id
200 *
201 * \param pw Pointer to the current SVG parser state
202 * \param node Libdom SVG node
203 * \param id Address at which to store the id
204 *
205 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
206 */
207 css_error node_id(void *pw, void *node, lwc_string **id)
208 {
209 dom_string *attr;
210 dom_exception err;
211 struct svgtiny_parse_state *state;
212
213 /* Begin with the assumption that this node has no id */
214 *id = NULL;
215
216 state = (struct svgtiny_parse_state *)pw;
217 err = dom_element_get_attribute((dom_node *)node,
218 state->interned_id, &attr);
219 if (err != DOM_NO_ERR) {
220 return CSS_NOMEM;
221 }
222 else if (attr == NULL) {
223 /* The node has no id attribute and our return value
224 is already set to NULL so we're done */
225 return CSS_OK;
226 }
227
228 /* If we found an id attribute (a dom_string), intern it into
229 an lwc_string that we can return, and then cleanup the
230 dom_string. */
231 err = dom_string_intern(attr, id);
232 if (err != DOM_NO_ERR) {
233 dom_string_unref(attr);
234 return CSS_NOMEM;
235 }
236 dom_string_unref(attr);
237 return CSS_OK;
238 }
239
240
241
242 /**
243 * Find the first parent of the given element having the given name
244 *
245 * \param pw Pointer to the current SVG parser state
246 * \param node Libdom SVG node
247 * \param qname Name of the parent node to search for
248 * \param parent Address at which to store the parent node pointer
249 *
250 * \return Always returns CSS_OK
251 *
252 * \post If a suitable element is found, a pointer to it will be
253 * stored at the address pointed to by \a parent; otherwise,
254 * NULL will be stored at the address pointed to by \a parent
255 */
256 css_error named_parent_node(void *pw, void *node,
257 const css_qname *qname, void **parent)
258 {
259 UNUSED(pw);
260 /* dom_element_named_parent_node() was invented to implement
261 * this select handler so there isn't much for us to do except
262 * call it. It's OK if node isn't an element, libdom checks
263 * for it. */
264 dom_element_named_parent_node((dom_element *)node,
265 qname->name,
266 (struct dom_element **)parent);
267
268 /* Implementation detail: dom_element_named_parent_node()
269 * increments the reference count of the parent element before
270 * returning it to us. According to docs/RefCnt in the libdom
271 * repository, this will prevent the parent element from being
272 * destroyed if it is pruned from the DOM. That sounds good,
273 * since we don't want to be using a pointer to an object that
274 * has been destroyed... but we also have no way of later
275 * decrementing the reference count ourselves, and don't want
276 * to make the returned node eternal. Decrementing the
277 * reference counter now allows it to be destroyed when the
278 * DOM no longer needs it, and so long as no other parts of
279 * libsvgtiny are messing with the DOM during parsing, that
280 * shouldn't (ha ha) cause any problems. */
281 dom_node_unref(*parent);
282
283 return CSS_OK;
284 }
285
286
287 /**
288 * Find the "next-sibling" of the given element having the given name
289 *
290 * This search corresponds to the "+ foo" combinator in CSS and will
291 * find only "foo" element nodes that immediately precede the given
292 * node under the same parent in the DOM. In CSS the tree is viewed
293 * top-down and in libdom it is viewed from the bottom-up; as a result
294 * "next" and "previous" are sometimes backwards. This is case-sensitive.
295 *
296 * \param pw Pointer to the current SVG parser state
297 * \param node Libdom SVG node
298 * \param qname Name of the sibling node to search for
299 * \param sibling Address at which to store the sibling node pointer
300 *
301 * \return Always returns CSS_OK
302 *
303 * \post If a suitable element is found, a pointer to it will be
304 * stored at the address pointed to by \a sibling; otherwise,
305 * NULL will be stored at the address pointed to by \a sibling
306 */
307 css_error named_sibling_node(void *pw, void *node,
308 const css_qname *qname, void **sibling)
309 {
310 UNUSED(pw);
311 dom_node *n = node; /* the current node */
312 dom_node *prev; /* the previous node */
313 dom_exception err;
314 dom_node_type type;
315 dom_string *name;
316
317 *sibling = NULL; /* default to nothing found */
318
319 /* Begin the search; the first iteration we do outside of the
320 * loop. Implementation detil: dom_node_get_previous_sibling()
321 * increments the reference counter on the returned node. A
322 * comment within named_parent_node() explains why we
323 * decrement it ASAP. */
324 err = dom_node_get_previous_sibling(n, &n);
325 if (err != DOM_NO_ERR) {
326 return CSS_OK;
327 }
328
329 while (n != NULL) {
330 /* We're looking for the first ELEMENT sibling */
331 err = dom_node_get_node_type(n, &type);
332 if (err != DOM_NO_ERR) {
333 dom_node_unref(n);
334 return CSS_OK;
335 }
336
337 if (type == DOM_ELEMENT_NODE) {
338 /* We found an element node, does it have the
339 * right name? */
340 err = dom_node_get_node_name(n, &name);
341 if (err != DOM_NO_ERR) {
342 dom_node_unref(n);
343 return CSS_OK;
344 }
345
346 if (dom_string_lwc_isequal(name,
347 qname->name)) {
348 /* The name is right, return it */
349 *sibling = n;
350 }
351
352 /* There's only one next-sibling element node
353 * and we've already found it, so if its name
354 * wasn't right, we return the default value
355 * of NULL below */
356 dom_string_unref(name);
357 dom_node_unref(n);
358 return CSS_OK;
359 }
360
361 /* Not an element node, so we move on the the previous
362 * previous sibling */
363 err = dom_node_get_previous_sibling(n, &prev);
364 if (err != DOM_NO_ERR) {
365 dom_node_unref(n);
366 return CSS_OK;
367 }
368
369 dom_node_unref(n);
370 n = prev;
371 }
372
373 return CSS_OK;
374 }
375
376
377 /**
378 * Find the first "subsequent-sibling" of the given element having the
379 * given name
380 *
381 * This search corresponds to the "~ foo" combinator in CSS and will
382 * find only "foo" element nodes that precede the given node (under
383 * the same parent) in the DOM. In CSS the tree is viewed top-down and
384 * in libdom it is viewed from the bottom-up; as a result "next" and
385 * "previous" are sometimes backwards. This is case-sensitive.
386 *
387 * \param pw Pointer to the current SVG parser state
388 * \param node Libdom SVG node
389 * \param qname Name of the sibling node to search for
390 * \param sibling Address at which to store the sibling node pointer
391 *
392 * \return Always returns CSS_OK
393 *
394 * \post If a suitable element is found, a pointer to it will be
395 * stored at the address pointed to by \a sibling; otherwise,
396 * NULL will be stored at the address pointed to by \a sibling
397 */
398 css_error named_generic_sibling_node(void *pw, void *node,
399 const css_qname *qname, void **sibling)
400 {
401 UNUSED(pw);
402 dom_node *n = node; /* the current node */
403 dom_node *prev; /* the previous node */
404 dom_exception err;
405 dom_node_type type;
406 dom_string *name;
407
408
409 *sibling = NULL; /* default to nothing found */
410
411 /* Begin the search; the first iteration we do outside of the
412 * loop. Implementation detil: dom_node_get_previous_sibling()
413 * increments the reference counter on the returned node. A
414 * comment within named_parent_node() explains why we
415 * decrement it ASAP. */
416 err = dom_node_get_previous_sibling(n, &n);
417 if (err != DOM_NO_ERR) {
418 return CSS_OK;
419 }
420
421 while (n != NULL) {
422 err = dom_node_get_node_type(n, &type);
423 if (err != DOM_NO_ERR) {
424 dom_node_unref(n);
425 return CSS_OK;
426 }
427
428 if (type == DOM_ELEMENT_NODE) {
429 /* We only want ELEMENT nodes */
430 err = dom_node_get_node_name(n, &name);
431 if (err != DOM_NO_ERR) {
432 dom_node_unref(n);
433 return CSS_OK;
434 }
435
436 if (dom_string_lwc_isequal(name,
437 qname->name)) {
438 /* Found one. Save it and stop the search */
439 dom_string_unref(name);
440 dom_node_unref(n);
441 *sibling = n;
442 return CSS_OK;
443 }
444
445 dom_string_unref(name);
446 }
447
448 /* This sibling wasn't an element with the desired
449 name, so move on to the previous sibling */
450 err = dom_node_get_previous_sibling(n, &prev);
451 if (err != DOM_NO_ERR) {
452 dom_node_unref(n);
453 return CSS_OK;
454 }
455
456 dom_node_unref(n);
457 n = prev;
458 }
459
460 return CSS_OK;
461 }
462
463
464 /**
465 * Return a pointer to the given node's parent
466 *
467 * \param pw Pointer to the current SVG parser state
468 * \param node Libdom SVG node
469 * \param parent Address at which to store the node's parent pointer
470 *
471 * \return Always returns CSS_OK
472 */
473 css_error parent_node(void *pw, void *node, void **parent)
474 {
475 UNUSED(pw);
476 /* Libdom basically implements this for us */
477 dom_element_parent_node(node, (struct dom_element **)parent);
478
479 /* See the comment in named_parent_node() for why we decrement
480 * this reference counter here. */
481 dom_node_unref(*parent);
482
483 return CSS_OK;
484 }
485
486
487 /**
488 * Find the "next-sibling" of the given element
489 *
490 * This search corresponds "+ *" in CSS and will find the first
491 * element node that immediately precedes the given node under the
492 * same parent in the DOM. In CSS the tree is viewed top-down and in
493 * libdom it is viewed from the bottom-up; as a result "next" and
494 * "previous" are sometimes backwards.
495 *
496 * \param pw Pointer to the current SVG parser state
497 * \param node Libdom SVG node
498 * \param sibling Address at which to store the sibling node pointer
499 *
500 * \return Always returns CSS_OK
501 *
502 * \post If a suitable element is found, a pointer to it will be
503 * stored at the address pointed to by \a sibling; otherwise,
504 * NULL will be stored at the address pointed to by \a sibling
505 */
506 css_error sibling_node(void *pw, void *node, void **sibling)
507 {
508 UNUSED(pw);
509 dom_node *n = node; /* the current node */
510 dom_node *prev; /* the previous node */
511 dom_exception err;
512 dom_node_type type;
513
514 *sibling = NULL; /* default to nothing found */
515
516 /* Begin the search; the first iteration we do outside of the
517 * loop. Implementation detil: dom_node_get_previous_sibling()
518 * increments the reference counter on the returned node. A
519 * comment within named_parent_node() explains why we
520 * decrement it ASAP. */
521 err = dom_node_get_previous_sibling(n, &n);
522 if (err != DOM_NO_ERR) {
523 return CSS_OK;
524 }
525
526 while (n != NULL) {
527 err = dom_node_get_node_type(n, &type);
528 if (err != DOM_NO_ERR) {
529 dom_node_unref(n);
530 return CSS_OK;
531 }
532
533 if (type == DOM_ELEMENT_NODE) {
534 /* We found a sibling node that is also an
535 element and that's all we wanted. */
536 *sibling = n;
537 dom_node_unref(n);
538 return CSS_OK;
539 }
540
541 /* This sibling node was not an element; move on to
542 the previous sibling */
543 err = dom_node_get_previous_sibling(n, &prev);
544 if (err != DOM_NO_ERR) {
545 dom_node_unref(n);
546 return CSS_OK;
547 }
548
549 dom_node_unref(n);
550 n = prev;
551 }
552
553 return CSS_OK;
554 }
555
556
557 /**
558 * Test the given node for the given name
559 *
560 * This will return true (via the "match" pointer) if the libdom node
561 * has the given name or if that name is the universal selector;
562 * otherwise it returns false. The comparison is case-sensitive. It
563 * corresponds to a rule like "body { ... }" in CSS.
564 *
565 * \param pw Pointer to the current SVG parser state
566 * \param node Libdom SVG node to test
567 * \param qname Name to check for
568 * \param match Pointer to the test result
569 *
570 * \return Always returns CSS_OK
571 */
572 css_error node_has_name(void *pw, void *node,
573 const css_qname *qname, bool *match)
574 {
575 struct svgtiny_parse_state *state;
576 dom_string *name;
577 dom_exception err;
578
579 /* Start by checking to see if qname is the universal selector */
580 state = (struct svgtiny_parse_state *)pw;
581 if (lwc_string_isequal(qname->name,
582 state->interned_universal, match) == lwc_error_ok) {
583 if (*match) {
584 /* It's the universal selector. In NetSurf, all node
585 * names match the universal selector, and nothing in
586 * the libcss documentation suggests another approach,
587 * so we follow NetSurf here. */
588 return CSS_OK;
589 }
590 }
591
592 err = dom_node_get_node_name((dom_node *)node, &name);
593 if (err != DOM_NO_ERR) {
594 return CSS_OK;
595 }
596
597 /* Unlike with HTML, SVG element names are case-sensitive */
598 *match = dom_string_lwc_isequal(name, qname->name);
599 dom_string_unref(name);
600
601 return CSS_OK;
602 }
603
604
605 /**
606 * Test the given node for the given class
607 *
608 * This will return true (via the "match" pointer) if the libdom node
609 * has the given class. The comparison is case-sensitive. It
610 * corresponds to node.class in CSS.
611 *
612 * \param pw Pointer to the current SVG parser state
613 * \param node Libdom SVG node to test
614 * \param name Class name to check for
615 * \param match Pointer to the test result
616 *
617 * \return Always returns CSS_OK
618 */
619 css_error node_has_class(void *pw, void *node,
620 lwc_string *name, bool *match)
621 {
622 UNUSED(pw);
623 /* libdom implements this for us and apparently it cannot fail */
624 dom_element_has_class((dom_node *)node, name, match);
625 return CSS_OK;
626 }
627
628
629 /**
630 * Test the given node for the given id
631 *
632 * This will return true (via the "match" pointer) if the libdom node
633 * has the given id. The comparison is case-sensitive. It corresponds
634 * to node#id in CSS.
635 *
636 * \param pw Pointer to the current SVG parser state
637 * \param node Libdom SVG node to test
638 * \param name Id to check for
639 * \param match Pointer to the test result
640 *
641 * \return Always returns CSS_OK
642 */
643 css_error node_has_id(void *pw, void *node,
644 lwc_string *name, bool *match)
645 {
646 dom_string *attr;
647 dom_exception err;
648 struct svgtiny_parse_state *state;
649
650 attr = NULL; /* a priori the "id" attribute may not exist */
651 *match = false; /* default to no match */
652
653 state = (struct svgtiny_parse_state *)pw;
654 err = dom_element_get_attribute((dom_node *)node,
655 state->interned_id, &attr);
656 if (err != DOM_NO_ERR || attr == NULL) {
657 return CSS_OK;
658 }
659
660 *match = dom_string_lwc_isequal(attr, name);
661 dom_string_unref(attr);
662
663 return CSS_OK;
664 }
665
666
667 /**
668 * Test the given node for the given attribute
669 *
670 * This will return true (via the "match" pointer) if the libdom node
671 * has an attribute with the given name. The comparison is
672 * case-sensitive. It corresponds to node[attr] in CSS.
673 *
674 * \param pw Pointer to the current SVG parser state
675 * \param node Libdom SVG node to test
676 * \param qname Attribute name to check for
677 * \param match Pointer to the test result
678 *
679 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
680 * goes wrong
681 */
682 css_error node_has_attribute(void *pw, void *node,
683 const css_qname *qname, bool *match)
684 {
685 UNUSED(pw);
686 dom_string *name;
687 dom_exception err;
688
689 /* intern the attribute name as a dom_string so we can
690 * delegate to dom_element_has_attribute() */
691 err = dom_string_create_interned(
692 (const uint8_t *) lwc_string_data(qname->name),
693 lwc_string_length(qname->name),
694 &name);
695 if (err != DOM_NO_ERR) {
696 return CSS_NOMEM;
697 }
698
699 err = dom_element_has_attribute((dom_node *)node, name, match);
700 if (err != DOM_NO_ERR) {
701 dom_string_unref(name);
702 return CSS_OK;
703 }
704
705 dom_string_unref(name);
706 return CSS_OK;
707 }
708
709
710 /**
711 * Test the given node for an attribute with a specific value
712 *
713 * This will return true (via the "match" pointer) if the libdom node
714 * has an attribute with the given name and value. The comparison is
715 * case-sensitive. It corresponds to node[attr=value] in CSS.
716 *
717 * \param pw Pointer to the current SVG parser state
718 * \param node Libdom SVG node to test
719 * \param qname Attribute name to check for
720 * \param value Attribute value to check for
721 * \param match Pointer to the test result
722 *
723 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
724 * intern the attribute name (which usually indicates memory
725 * exhaustion)
726 */
727 css_error node_has_attribute_equal(void *pw, void *node,
728 const css_qname *qname, lwc_string *value,
729 bool *match)
730 {
731 /* Implementation note: NetSurf always returns "no match" when
732 * the value is empty (length zero). We allow it, because why
733 * not? */
734
735 UNUSED(pw);
736 dom_string *name;
737 dom_string *attr_val;
738 dom_exception err;
739
740 /* Intern the attribute name as a dom_string so we can
741 * use dom_element_get_attribute() */
742 err = dom_string_create_interned(
743 (const uint8_t *) lwc_string_data(qname->name),
744 lwc_string_length(qname->name),
745 &name);
746 if (err != DOM_NO_ERR) {
747 return CSS_NOMEM;
748 }
749
750 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
751 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
752 /* There was an error getting the attribute's value or
753 * the attribute doesn't exist. So, no match? */
754 dom_string_unref(name);
755 *match = false;
756 return CSS_OK;
757 }
758
759 /* Otherwise, we have the attribute value from the given node
760 * and all we need to do is compare. */
761 dom_string_unref(name);
762 *match = dom_string_lwc_isequal(attr_val, value);
763 dom_string_unref(attr_val);
764
765 return CSS_OK;
766 }
767
768
769 /**
770 * Test the given node for an attribute with a specific value,
771 * possibly followed by a single hyphen
772 *
773 * This will return true (via the "match" pointer) if the libdom node
774 * has an attribute with the given name and value or with the given
775 * name and a value that is followed by exactly one hyphen. The
776 * comparison is case-sensitive. This corresponds to [attr|=value]
777 * in CSS.
778 *
779 * \param pw Pointer to the current SVG parser state
780 * \param node Libdom SVG node to test
781 * \param qname Attribute name to check for
782 * \param value Attribute value to check for
783 * \param match Pointer to the test result
784 *
785 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
786 * intern the attribute name (which usually indicates memory
787 * exhaustion)
788 */
789 css_error node_has_attribute_dashmatch(void *pw, void *node,
790 const css_qname *qname, lwc_string *value,
791 bool *match)
792 {
793 /* Implementation note: NetSurf always returns "no match" when
794 * the value is empty (length zero). We allow it, because why
795 * not? */
796
797 UNUSED(pw);
798 dom_string *name;
799 dom_string *attr_val;
800 dom_exception err;
801
802 const char *vdata; /* to hold the data underlying "value" */
803 size_t vdata_len;
804 const char *avdata; /* to hold the found attribute value data */
805 size_t avdata_len;
806
807 /* Intern the attribute name as a dom_string so we can
808 * use dom_element_get_attribute() */
809 err = dom_string_create_interned(
810 (const uint8_t *) lwc_string_data(qname->name),
811 lwc_string_length(qname->name),
812 &name);
813 if (err != DOM_NO_ERR) {
814 return CSS_NOMEM;
815 }
816
817 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
818 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
819 /* There was an error getting the attribute's value or
820 * the attribute doesn't exist. So, no match? */
821 dom_string_unref(name);
822 *match = false;
823 return CSS_OK;
824 }
825
826 /* Otherwise, we have the attribute value from the given node
827 * and all we need to do is compare. */
828 dom_string_unref(name);
829 *match = dom_string_lwc_isequal(attr_val, value);
830 if (*match) {
831 /* Exact match, we're done */
832 dom_string_unref(attr_val);
833 return CSS_OK;
834 }
835
836 /* No exact match, try it with a hyphen on the end */
837 vdata = lwc_string_data(value); /* needle */
838 vdata_len = lwc_string_length(value);
839 avdata = dom_string_data(attr_val); /* haystack */
840 avdata_len = dom_string_byte_length(attr_val);
841 dom_string_unref(attr_val);
842
843 if (avdata_len > vdata_len && avdata[vdata_len] == '-') {
844 if (strncasecmp(avdata, vdata, vdata_len) == 0) {
845 /* If there's a hyphen in the right position,
846 * it suffices to compare the strings only up
847 * to the hyphen */
848 *match = true;
849 }
850 }
851
852 return CSS_OK;
853 }
854
855
856 /**
857 * Test the given node for an attribute whose value is a
858 * space-separated list of words, one of which is the given word
859 *
860 * This will return true (via the "match" pointer) if the libdom node
861 * has an attribute with the given name and whose value when
862 * considered as a space-separated list of words contains the given
863 * word. The comparison is case-sensitive. This corresponds to
864 * [attr~=value] in CSS.
865 *
866 * \param pw Pointer to the current SVG parser state
867 * \param node Libdom SVG node to test
868 * \param qname Attribute name to check for
869 * \param word Value word to check for
870 * \param match Pointer to the test result
871 *
872 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
873 * intern the attribute name (which usually indicates memory
874 * exhaustion)
875 */
876 css_error node_has_attribute_includes(void *pw, void *node,
877 const css_qname *qname, lwc_string *word,
878 bool *match)
879 {
880 UNUSED(pw);
881
882 dom_string *name;
883 dom_string *attr_val;
884 dom_exception err;
885 size_t wordlen; /* length of "word" */
886
887 /* pointers used to parse a space-separated list of words */
888 const char *p;
889 const char *start;
890 const char *end;
891
892 *match = false; /* default to no match */
893
894 wordlen = lwc_string_length(word);
895 if (wordlen == 0) {
896 /* In this case, the spec says that "if 'val' is the
897 * empty string, it will never represent anything." */
898 return CSS_OK;
899 }
900
901 /* Intern the attribute name as a dom_string so we can
902 * use dom_element_get_attribute() */
903 err = dom_string_create_interned(
904 (const uint8_t *) lwc_string_data(qname->name),
905 lwc_string_length(qname->name),
906 &name);
907 if (err != DOM_NO_ERR) {
908 return CSS_NOMEM;
909 }
910
911 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
912 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
913 /* There was an error getting the attribute's value or
914 * the attribute doesn't exist. So, no match? */
915 dom_string_unref(name);
916 return CSS_OK;
917 }
918
919 /* Parse the list comparing each word against "word" */
920 start = dom_string_data(attr_val);
921 end = start + dom_string_byte_length(attr_val);
922 dom_string_unref(attr_val);
923
924 for (p = start; p <= end; p++) {
925 /* Move forward until we find the end of the first word */
926 if (*p == ' ' || *p == '\0') {
927 /* If the length of that word is the length of the
928 * word we're looking for, do the comparison. */
929 if ((size_t) (p - start) == wordlen &&
930 strncasecmp(start,
931 lwc_string_data(word),
932 wordlen) == 0) {
933 *match = true;
934 break;
935 }
936 /* No match? Set "start" to the beginning of
937 * the next word and loop. */
938 start = p + 1;
939 }
940 }
941
942 return CSS_OK;
943 }
944
945
946 /**
947 * Test the given node for an attribute whose value begins with the
948 * given prefix
949 *
950 * This will return true (via the "match" pointer) if the libdom node
951 * has an attribute with the given name and whose value begins with
952 * the given prefix string. The comparison is case-sensitive. This
953 * corresponds to [attr^=value] in CSS.
954 *
955 * \param pw Pointer to the current SVG parser state
956 * \param node Libdom SVG node to test
957 * \param qname Attribute name to check for
958 * \param prefix Value prefix to check for
959 * \param match Pointer to the test result
960 *
961 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
962 * intern the attribute name (which usually indicates memory
963 * exhaustion)
964 */
965 css_error node_has_attribute_prefix(void *pw, void *node,
966 const css_qname *qname, lwc_string *prefix,
967 bool *match)
968 {
969 UNUSED(pw);
970 dom_string *name;
971 dom_string *attr_val;
972 dom_exception err;
973 const char *avdata; /* attribute value data */
974 size_t avdata_len; /* length of that attribute value data */
975 size_t prefixlen; /* length of "prefix" */
976
977 prefixlen = lwc_string_length(prefix);
978 if (prefixlen == 0) {
979 /* In this case, the spec says that "if 'val' is the
980 * empty string, it will never represent anything." */
981 return CSS_OK;
982 }
983
984 /* Intern the attribute name as a dom_string so we can
985 * use dom_element_get_attribute() */
986 err = dom_string_create_interned(
987 (const uint8_t *) lwc_string_data(qname->name),
988 lwc_string_length(qname->name),
989 &name);
990 if (err != DOM_NO_ERR) {
991 return CSS_NOMEM;
992 }
993
994 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
995 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
996 /* There was an error getting the attribute's value or
997 * the attribute doesn't exist. So, no match? */
998 dom_string_unref(name);
999 *match = false;
1000 return CSS_OK;
1001 }
1002
1003 /* Otherwise, we have the attribute value from the given node,
1004 * and the first thing we want to do is check to see if the
1005 * whole thing matches the prefix. */
1006 dom_string_unref(name);
1007 *match = dom_string_lwc_isequal(attr_val, prefix);
1008
1009 /* If not, check to see if an, uh, prefix matches the
1010 * prefix */
1011 if (*match == false) {
1012 avdata = dom_string_data(attr_val);
1013 avdata_len = dom_string_byte_length(attr_val);
1014 if ((avdata_len >= prefixlen) &&
1015 (strncasecmp(avdata,
1016 lwc_string_data(prefix),
1017 prefixlen) == 0)) {
1018 /* Use strncasecmp to compare only the first
1019 * "n" characters, where "n" is the length of
1020 * the prefix. */
1021 *match = true;
1022 }
1023 }
1024
1025 dom_string_unref(attr_val);
1026
1027 return CSS_OK;
1028 }
1029
1030
1031 /**
1032 * Test the given node for an attribute whose value end with the
1033 * given suffix
1034 *
1035 * This will return true (via the "match" pointer) if the libdom node
1036 * has an attribute with the given name and whose value ends with
1037 * the given suffix string. The comparison is case-sensitive. This
1038 * corresponds to [attr$=value] in CSS.
1039 *
1040 * \param pw Pointer to the current SVG parser state
1041 * \param node Libdom SVG node to test
1042 * \param qname Attribute name to check for
1043 * \param suffix Value suffix to check for
1044 * \param match Pointer to the test result
1045 *
1046 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1047 * intern the attribute name (which usually indicates memory
1048 * exhaustion)
1049 */
1050 css_error node_has_attribute_suffix(void *pw, void *node,
1051 const css_qname *qname, lwc_string *suffix,
1052 bool *match)
1053 {
1054 UNUSED(pw);
1055 dom_string *name;
1056 dom_string *attr_val;
1057 dom_exception err;
1058 const char *avdata; /* attribute value data */
1059 size_t avdata_len; /* length of that attribute value data */
1060 size_t suffixlen; /* length of "suffix" */
1061
1062 /* convenience pointer we'll use when matching the suffix */
1063 const char *suffix_start;
1064
1065 suffixlen = lwc_string_length(suffix);
1066 if (suffixlen == 0) {
1067 /* In this case, the spec says that "if 'val' is the
1068 * empty string, it will never represent anything." */
1069 return CSS_OK;
1070 }
1071
1072 /* Intern the attribute name as a dom_string so we can
1073 * use dom_element_get_attribute() */
1074 err = dom_string_create_interned(
1075 (const uint8_t *) lwc_string_data(qname->name),
1076 lwc_string_length(qname->name),
1077 &name);
1078 if (err != DOM_NO_ERR) {
1079 return CSS_NOMEM;
1080 }
1081
1082 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
1083 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
1084 /* There was an error getting the attribute's value or
1085 * the attribute doesn't exist. So, no match? */
1086 dom_string_unref(name);
1087 *match = false;
1088 return CSS_OK;
1089 }
1090
1091 /* Otherwise, we have the attribute value from the given node,
1092 * and the first thing we want to do is check to see if the
1093 * whole thing matches the suffix. */
1094 dom_string_unref(name);
1095 *match = dom_string_lwc_isequal(attr_val, suffix);
1096
1097 /* If not, check to see if an, uh, suffix matches the
1098 * suffix */
1099 if (*match == false) {
1100 avdata = dom_string_data(attr_val);
1101 avdata_len = dom_string_byte_length(attr_val);
1102
1103 suffix_start = (char *)(avdata + avdata_len - suffixlen);
1104
1105 if ((avdata_len >= suffixlen) &&
1106 (strncasecmp(suffix_start,
1107 lwc_string_data(suffix),
1108 suffixlen) == 0)) {
1109 /* Use strncasecmp to compare only the last
1110 * "n" characters, where "n" is the length of
1111 * the suffix. */
1112 *match = true;
1113 }
1114 }
1115
1116 dom_string_unref(attr_val);
1117
1118 return CSS_OK;
1119 }
1120
1121
1122 /**
1123 * Implement node_has_attribute_substring() with optional case-
1124 * insensitivity. This corresponds to [attr*=value i] in CSS and is
1125 * not supported by libcss yet, but it allows us to factor out some
1126 * common code.
1127 */
1128 static css_error _node_has_attribute_substring(void *pw, void *node,
1129 const css_qname *qname, lwc_string *substring,
1130 bool *match, bool insensitive)
1131 {
1132 UNUSED(pw);
1133 dom_string *name;
1134 dom_string *attr_val;
1135 dom_exception err;
1136 size_t attr_len; /* length of attr_val */
1137 size_t substrlen; /* length of "substring" */
1138
1139 /* Convenience pointers we use when comparing substrings */
1140 const char *p;
1141 const char *p_max;
1142
1143 substrlen = lwc_string_length(substring);
1144 if (substrlen == 0) {
1145 /* In this case, the spec says that "if 'val' is the
1146 * empty string, it will never represent anything." */
1147 return CSS_OK;
1148 }
1149
1150 /* Intern the attribute name as a dom_string so we can
1151 * use dom_element_get_attribute() */
1152 err = dom_string_create_interned(
1153 (const uint8_t *) lwc_string_data(qname->name),
1154 lwc_string_length(qname->name),
1155 &name);
1156 if (err != DOM_NO_ERR) {
1157 return CSS_NOMEM;
1158 }
1159
1160 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
1161 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
1162 /* There was an error getting the attribute's value or
1163 * the attribute doesn't exist. So, no match? */
1164 dom_string_unref(name);
1165 *match = false;
1166 return CSS_OK;
1167 }
1168
1169 /* Otherwise, we have the attribute value from the given node,
1170 * and the first thing we want to do is check to see if the
1171 * whole thing matches the substring. */
1172 dom_string_unref(name);
1173
1174 if (insensitive) {
1175 *match = dom_string_caseless_lwc_isequal(attr_val, substring);
1176 }
1177 else {
1178 *match = dom_string_lwc_isequal(attr_val, substring);
1179 }
1180
1181 /* If not, check to see if an, uh, substring matches the
1182 * substring */
1183 if (*match == false) {
1184 p = dom_string_data(attr_val);
1185
1186 /* Check every long-enough suffix for a prefix match */
1187 attr_len = dom_string_byte_length(attr_val);
1188 if (attr_len >= substrlen) {
1189 p_max = p + attr_len - substrlen;
1190 while (p <= p_max) {
1191 if (strncasecmp(p,
1192 lwc_string_data(substring),
1193 substrlen) == 0) {
1194 *match = true;
1195 break;
1196 }
1197 p++;
1198 }
1199 }
1200 }
1201
1202 dom_string_unref(attr_val);
1203
1204 return CSS_OK;
1205 }
1206
1207 /**
1208 * Test the given node for an attribute whose value contains the
1209 * given substring
1210 *
1211 * This will return true (via the "match" pointer) if the libdom node
1212 * has an attribute with the given name and whose value contains the
1213 * given substring. The comparison is case-sensitive. This corresponds
1214 * to [attr*=value] in CSS.
1215 *
1216 * \param pw Pointer to the current SVG parser state
1217 * \param node Libdom SVG node to test
1218 * \param qname Attribute name to check for
1219 * \param substring Value substring to check for
1220 * \param match Pointer to the test result
1221 *
1222 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1223 * intern the attribute name (which usually indicates memory
1224 * exhaustion)
1225 */
1226 css_error node_has_attribute_substring(void *pw, void *node,
1227 const css_qname *qname, lwc_string *substring,
1228 bool *match)
1229 {
1230 return _node_has_attribute_substring(pw, node, qname, substring,
1231 match, false);
1232 }
1233
1234
1235 /**
1236 * Test whether or not the given node is the document's root element
1237 * This corresponds to the CSS :root pseudo-selector.
1238 *
1239 * \param pw Pointer to the current SVG parser state
1240 * \param node Libdom SVG node to test
1241 * \param match Pointer to the test result
1242 *
1243 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1244 */
1245 css_error node_is_root(void *pw, void *node, bool *match)
1246 {
1247 UNUSED(pw);
1248 dom_node *parent;
1249 dom_node_type type;
1250 dom_exception err;
1251
1252 err = dom_node_get_parent_node((dom_node *)node, &parent);
1253 if (err != DOM_NO_ERR) {
1254 return CSS_NOMEM;
1255 }
1256
1257 /* It's the root element if it doesn't have a parent element */
1258 if (parent != NULL) {
1259 err = dom_node_get_node_type(parent, &type);
1260 dom_node_unref(parent);
1261 if (err != DOM_NO_ERR) {
1262 return CSS_NOMEM;
1263 }
1264 if (type != DOM_DOCUMENT_NODE) {
1265 /* DOM_DOCUMENT_NODE is the only allowable
1266 * type of parent node for the root element */
1267 *match = false;
1268 return CSS_OK;
1269 }
1270 }
1271
1272 *match = true;
1273 return CSS_OK;
1274 }
1275
1276
1277 /**
1278 * Used internally in node_count_siblings() to "count" the given
1279 * sibling node. It factors out the node type and name checks.
1280 */
1281 static int node_count_siblings_check(dom_node *dnode,
1282 bool check_name,
1283 dom_string *name)
1284 {
1285 int ret;
1286 dom_node_type type;
1287 dom_exception exc;
1288 dom_string *dnode_name;
1289
1290 /* We flip this to 1 if/when we count this node */
1291 ret = 0;
1292
1293 if (dnode == NULL) {
1294 return ret;
1295 }
1296
1297 exc = dom_node_get_node_type(dnode, &type);
1298 if ((exc != DOM_NO_ERR) || (type != DOM_ELEMENT_NODE)) {
1299 /* We only count element siblings */
1300 return ret;
1301 }
1302
1303 /* ... with the right name */
1304 if (check_name) {
1305 dnode_name = NULL;
1306 exc = dom_node_get_node_name(dnode, &dnode_name);
1307
1308 if ((exc == DOM_NO_ERR) && (dnode_name != NULL)) {
1309 if (dom_string_isequal(name,
1310 dnode_name)) {
1311 ret = 1;
1312 }
1313 dom_string_unref(dnode_name);
1314 }
1315 }
1316 else {
1317 ret = 1;
1318 }
1319
1320 return ret;
1321 }
1322
1323 /**
1324 * Count the given node's sibling elements
1325 *
1326 * This counts the given node's sibling elements in one direction,
1327 * either forwards or backwards, in the DOM. Keep in mind that the
1328 * libdom tree is upside-down compared to the CSS one; so "next" and
1329 * "previous" are actually reversed; the default is to count preceding
1330 * libdom siblings which correspond to subsequent CSS siblings.
1331 *
1332 * This operation is central to the CSS :first-child, :nth-child, and
1333 * :last-child (et cetera) pseudo-selectors.
1334 *
1335 * If same_name is true, then only nodes having the same
1336 * (case-sensitive) name as the given node are counted.
1337 *
1338 * \param pw Pointer to the current SVG parser state
1339 * \param node Libdom SVG node whose siblings we're counting
1340 * \param same_name Whether or not to count only siblings having
1341 * the same name as the given node
1342 * \param after Count subsequent siblings rather than precedent
1343 * ones (the default)
1344 * \param count Pointer to the return value, the number of sibling
1345 * elements
1346 *
1347 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1348 */
1349 css_error node_count_siblings(void *pw, void *node,
1350 bool same_name, bool after, int32_t *count)
1351 {
1352 UNUSED(pw);
1353 dom_exception exc;
1354 dom_node *dnode; /* node, but with the right type */
1355 dom_string *dnode_name;
1356 dom_node *next; /* "next" sibling (depends on direction) */
1357
1358 /* Pointer to the "next sibling" function */
1359 dom_exception (*next_func)(dom_node *, dom_node **);
1360
1361 *count = 0;
1362
1363 dnode_name = NULL;
1364 dnode = (dom_node *)node;
1365 if (same_name) {
1366 exc = dom_node_get_node_name(dnode, &dnode_name);
1367 if ((exc != DOM_NO_ERR) || (dnode_name == NULL)) {
1368 return CSS_NOMEM;
1369 }
1370 }
1371
1372 /* Increment the reference counter for dnode for as long as
1373 * we retain a reference to it. */
1374 dnode = dom_node_ref(dnode);
1375
1376 next_func = dom_node_get_previous_sibling;
1377 if (after) {
1378 next_func = dom_node_get_next_sibling;
1379 }
1380
1381 do {
1382 exc = next_func(dnode, &next);
1383 if (exc != DOM_NO_ERR) {
1384 break;
1385 }
1386
1387 /* If next_func worked, we're about to swap "next"
1388 * with "dnode" meaning that we will no longer retain
1389 * a reference to the current dnode. */
1390 dom_node_unref(dnode);
1391 dnode = next;
1392
1393 *count += node_count_siblings_check(dnode,
1394 same_name,
1395 dnode_name);
1396 } while (dnode != NULL);
1397
1398 if (dnode_name != NULL) {
1399 dom_string_unref(dnode_name);
1400 }
1401
1402 return CSS_OK;
1403 }
1404
1405
1406 /**
1407 * Determine whether or not the given element is empty
1408 *
1409 * An element is "nonempty" if it has a child that is either an
1410 * element node or a text node.
1411 *
1412 * \param pw Pointer to the current SVG parser state
1413 * \param node Libdom SVG node to check for emptiness
1414 * \param is_empty Pointer to the return value
1415 *
1416 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1417 */
1418 css_error node_is_empty(void *pw, void *node, bool *is_empty)
1419 {
1420 UNUSED(pw);
1421 dom_node *child; /* current child node pointer */
1422 dom_node *next; /* next child node pointer */
1423 dom_node_type type; /* what type of node is "child" */
1424 dom_exception err;
1425
1426 /* Assume that it's empty by default */
1427 *is_empty = true;
1428
1429 /* Get the given node's first child. Implementation detail:
1430 * this increments the reference counter on the child node. */
1431 err = dom_node_get_first_child((dom_node *)node, &child);
1432 if (err != DOM_NO_ERR) {
1433 return CSS_NOMEM;
1434 }
1435
1436 /* And now loop through all children looking for a
1437 * text/element node. If we find one, the original
1438 * node is "nonempty" */
1439 while (child != NULL) {
1440 err = dom_node_get_node_type(child, &type);
1441 if (err != DOM_NO_ERR) {
1442 dom_node_unref(child);
1443 return CSS_NOMEM;
1444 }
1445
1446 if (type == DOM_ELEMENT_NODE || type == DOM_TEXT_NODE) {
1447 *is_empty = false;
1448 dom_node_unref(child);
1449 return CSS_OK;
1450 }
1451
1452 err = dom_node_get_next_sibling(child, &next);
1453 if (err != DOM_NO_ERR) {
1454 dom_node_unref(child);
1455 return CSS_NOMEM;
1456 }
1457
1458 /* If we're moving to the next node, we can release
1459 * the reference to the current one */
1460 dom_node_unref(child);
1461 child = next;
1462 }
1463
1464 return CSS_OK;
1465 }
1466
1467
1468 /**
1469 * Determine whether or not the given node is a link
1470 *
1471 * A node is a link if it is an element node whose name is "a" and if
1472 * it has an "href" attribute (case-sensitive). This selector
1473 * corresponds to node:link pseudo-class in CSS.
1474 *
1475 * This pseudo-class is a bit awkward because the two standards (HTML5
1476 * and CSS) disagree on what it means, and because libsvgtiny does not
1477 * have enough information to determine if a link has been "visited"
1478 * yet -- that's a UI property. CSS says that :link is for unvisited
1479 * links, which we can't determine. HTML5 says that each link must
1480 * be either a :link or :visited. Since we can't decide either way,
1481 * It seems less wrong to declare that all links are unvisited; i.e.
1482 * that they match :link.
1483 *
1484 * \param pw Pointer to the current SVG parser state
1485 * \param node Libdom SVG node to check
1486 * \param is_link Pointer to the boolean return value
1487 *
1488 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1489 */
1490 css_error node_is_link(void *pw, void *node, bool *is_link)
1491 {
1492 dom_exception exc;
1493 dom_node *dnode; /* node, but with the right type */
1494 dom_string *dnode_name;
1495 bool has_href;
1496 struct svgtiny_parse_state* state;
1497
1498 dnode = (dom_node *)node;
1499 dnode_name = NULL;
1500 has_href = false; /* assume no href attribute */
1501 *is_link = false; /* assume that it's not a link */
1502
1503 exc = dom_node_get_node_name(dnode, &dnode_name);
1504 if ((exc != DOM_NO_ERR) || (dnode_name == NULL)) {
1505 return CSS_NOMEM;
1506 }
1507
1508 state = (struct svgtiny_parse_state *)pw;
1509 if (dom_string_isequal(dnode_name, state->interned_a)) {
1510 exc = dom_element_has_attribute(node,
1511 state->interned_href,
1512 &has_href);
1513 if (exc == DOM_NO_ERR && has_href) {
1514 *is_link = true;
1515 }
1516 }
1517
1518 dom_string_unref(dnode_name);
1519 return CSS_OK;
1520 }
1521
1522 /**
1523 * Check if the given node is a link that has been visited already
1524 *
1525 * This check always fails because the SVG DOM does not have the
1526 * necessary information (it's a UI property).
1527 *
1528 * \param pw Pointer to the current SVG parser state; unused
1529 * \param node Libdom SVG node to check; unused
1530 * \param is_visited Pointer to the boolean return value
1531 *
1532 * \return Always returns CSS_OK
1533 */
1534 css_error node_is_visited(void *pw, void *node, bool *is_visited)
1535 {
1536 UNUSED(pw);
1537 UNUSED(node);
1538 *is_visited = false;
1539 return CSS_OK;
1540 }
1541
1542
1543 /**
1544 * Check if the given node is being "hovered" over
1545 *
1546 * This check always fails because the SVG DOM does not have the
1547 * necessary information (it's a UI property).
1548 *
1549 * \param pw Pointer to the current SVG parser state; unused
1550 * \param node Libdom SVG node to check; unused
1551 * \param is_hover Pointer to the boolean return value
1552 *
1553 * \return Always returns CSS_OK
1554 */
1555 css_error node_is_hover(void *pw, void *node, bool *is_hover)
1556 {
1557 UNUSED(pw);
1558 UNUSED(node);
1559 *is_hover = false;
1560 return CSS_OK;
1561 }
1562
1563
1564 /**
1565 * Check if the given node is "active"
1566 *
1567 * This check always fails because the SVG DOM does not have the
1568 * necessary information (it's a UI property).
1569 *
1570 * \param pw Pointer to the current SVG parser state; unused
1571 * \param node Libdom SVG node to check; unused
1572 * \param is_active Pointer to the boolean return value
1573 *
1574 * \return Always returns CSS_OK
1575 */
1576 css_error node_is_active(void *pw, void *node, bool *is_active)
1577 {
1578 UNUSED(pw);
1579 UNUSED(node);
1580 *is_active = false;
1581 return CSS_OK;
1582 }
1583
1584
1585 /**
1586 * Check if the given node has the focus
1587 *
1588 * This check always fails because the SVG DOM does not have the
1589 * necessary information (it's a UI property).
1590 *
1591 * \param pw Pointer to the current SVG parser state; unused
1592 * \param node Libdom SVG node to check; unused
1593 * \param is_focus Pointer to the boolean return value
1594 *
1595 * \return Always returns CSS_OK
1596 */
1597 css_error node_is_focus(void *pw, void *node, bool *is_focus)
1598 {
1599 UNUSED(pw);
1600 UNUSED(node);
1601 *is_focus = false;
1602 return CSS_OK;
1603 }
1604
1605
1606 /**
1607 * Check if the given node is enabled
1608 *
1609 * This check always fails because the SVG DOM does not have the
1610 * necessary information (it's a UI property).
1611 *
1612 * \param pw Pointer to the current SVG parser state; unused
1613 * \param node Libdom SVG node to check; unused
1614 * \param is_enabled Pointer to the boolean return value
1615 *
1616 * \return Always returns CSS_OK
1617 */
1618 css_error node_is_enabled(void *pw, void *node, bool *is_enabled)
1619 {
1620 UNUSED(pw);
1621 UNUSED(node);
1622 *is_enabled = false;
1623 return CSS_OK;
1624 }
1625
1626
1627 /**
1628 * Check if the given node is disabled
1629 *
1630 * This check always fails because the SVG DOM does not have the
1631 * necessary information (it's a UI property). Beware, until they are
1632 * implemented, this is NOT the logical negation of node_is_enabled!
1633 *
1634 * \param pw Pointer to the current SVG parser state; unused
1635 * \param node Libdom SVG node to check; unused
1636 * \param is_disabled Pointer to the boolean return value
1637 *
1638 * \return Always returns CSS_OK
1639 */
1640 css_error node_is_disabled(void *pw, void *node, bool *is_disabled)
1641 {
1642 UNUSED(pw);
1643 UNUSED(node);
1644 *is_disabled = false;
1645 return CSS_OK;
1646 }
1647
1648
1649 /**
1650 * Test whether or not the given node is "checked"
1651 *
1652 * This test always fails because the SVG DOM does not have the
1653 * necessary information (it's a UI property).
1654 *
1655 * \param pw Pointer to the current SVG parser state; unused
1656 * \param node Libdom SVG node to check; unused
1657 * \param is_checked Pointer to the boolean return value
1658 *
1659 * \return Always returns CSS_OK
1660 */
1661 css_error node_is_checked(void *pw, void *node, bool *is_checked)
1662 {
1663 UNUSED(pw);
1664 UNUSED(node);
1665 *is_checked = false;
1666 return CSS_OK;
1667 }
1668
1669
1670 /**
1671 * Check if the given node is the "target" of the document URL
1672 *
1673 * This test always fails because the SVG DOM does not have the
1674 * necessary information (it's a UI property).
1675 *
1676 * \param pw Pointer to the current SVG parser state; unused
1677 * \param node Libdom SVG node to check; unused
1678 * \param is_target Pointer to the boolean return value
1679 *
1680 * \return Always returns CSS_OK
1681 */
1682 css_error node_is_target(void *pw, void *node, bool *is_target)
1683 {
1684 UNUSED(pw);
1685 UNUSED(node);
1686 *is_target = false;
1687 return CSS_OK;
1688 }
1689
1690
1691 /**
1692 * Check if the given node is the given language
1693 *
1694 * This test is corresponds to the CSS :lang() selector and is not
1695 * fully implemented yet: it looks only for "lang" attributes on the
1696 * given element and its parents, and performs a simple substring
1697 * check. This results in a partial implementation of CSS Level 3 for
1698 * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
1699 * favor of the "lang" attribute that is defined only in SVG 2.0.
1700 *
1701 * \param pw Pointer to the current SVG parser state; unused
1702 * \param node Libdom SVG node to check
1703 * \param lang The language to match
1704 * \param is_lang Pointer to the boolean return value
1705 *
1706 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1707 */
1708 static css_error node_is_lang(void *pw, void *node,
1709 lwc_string *lang, bool *is_lang)
1710 {
1711 UNUSED(pw);
1712 /* SVG2 elements support both "lang" and "xml:lang"
1713 * attributes; earlier versions have only the XML
1714 * attribute. It would not be too hard to add support for
1715 * xml:lang" here. The main difficulty standing in the way of
1716 * a full Level 4 implementation is the complexity of the
1717 * :lang() selector:
1718 *
1719 * https://www.w3.org/TR/selectors-4/#the-lang-pseudo
1720 *
1721 */
1722
1723 css_error c_err;
1724 dom_exception d_err;
1725 dom_node *n; /* current node */
1726 dom_node *p; /* parent node */
1727 bool match; /* retval from node_has_attribute_substring() */
1728
1729 /* Define the attribute name "lang" that we're looking for.
1730 * We only use a css_qname here because that's what the
1731 * node_has_attribute_substring() takes; the namespace
1732 * portion of it is irrelevant. */
1733 css_qname attr;
1734 attr.ns = NULL;
1735
1736 if (lwc_intern_string("lang", 4, &attr.name) != lwc_error_ok) {
1737 return CSS_NOMEM;
1738 }
1739
1740 *is_lang = false; /* default to no match */
1741 n = (dom_node *)node;
1742
1743 /* Loop through all parents of the given node looking for a
1744 * substring match */
1745 while (n != NULL) {
1746 c_err = _node_has_attribute_substring(pw, (void *)n, &attr,
1747 lang, &match, true);
1748 if (c_err != CSS_OK) {
1749 lwc_string_destroy(attr.name);
1750 return c_err;
1751 }
1752 if (match) {
1753 /* matched this element; we're done */
1754 lwc_string_destroy(attr.name);
1755 *is_lang = true;
1756 return CSS_OK;
1757 }
1758
1759 /* no match on this element, try its parent */
1760 d_err = dom_node_get_parent_node(n, &p);
1761 if (d_err != DOM_NO_ERR) {
1762 lwc_string_destroy(attr.name);
1763 return CSS_NOMEM;
1764 }
1765 n = p;
1766 }
1767
1768 /* If we never find a match we may wind up here */
1769 lwc_string_destroy(attr.name);
1770 return CSS_OK;
1771 }
1772
1773
1774 /**
1775 * User-agent defaults for CSS properties
1776 *
1777 * For the moment, we provide no defaults, because libsvgtiny does not
1778 * yet support any CSS properties that might need them.
1779 *
1780 * \param pw Pointer to the current SVG parser state; unused
1781 * \param property LibCSS property identifier; unused
1782 * \param hint Pointer to hint object (a return value); unused
1783 *
1784 * \return Always returns CSS_INVALID
1785 */
1786 css_error ua_default_for_property(void *pw, uint32_t property,
1787 css_hint *hint)
1788 {
1789 UNUSED(pw);
1790 UNUSED(property);
1791 UNUSED(hint);
1792 return CSS_INVALID;
1793 }
1794
1795
1796 /**
1797 * Store libcss data on a node
1798 *
1799 * This is part of the libcss select handler API that we need to
1800 * implement. It is essentially a thin dom_node_set_user_data()
1801 * wrapper.
1802 *
1803 * \param pw Pointer to the current SVG parser state
1804 * \param node Libdom SVG node on which to store the data
1805 * \param libcss_node_data Pointer to the data to store
1806 *
1807 * \return Always returns CSS_OK
1808 */
1809 css_error set_libcss_node_data(void *pw, void *node,
1810 void *libcss_node_data)
1811 {
1812 struct svgtiny_parse_state *state;
1813 void *old_data;
1814
1815 /* A unique "userdata key" (a string) is used to identify this
1816 * data. The fourth parameter (NULL) is a "user handler
1817 * function." We will eventually have one, but for now we set
1818 * it to NULL to avoid a circular reference mess that would
1819 * break the build temporarily. */
1820 state = (struct svgtiny_parse_state *)pw;
1821 dom_node_set_user_data((dom_node *)node,
1822 state->interned_userdata_key,
1823 libcss_node_data,
1824 NULL,
1825 &old_data);
1826
1827 /* dom_node_set_user_data() always returns DOM_NO_ERR */
1828 return CSS_OK;
1829 }
1830
1831
1832 /**
1833 * Retrieve libcss data from a node
1834 *
1835 * This is part of the libcss select handler API that we need to
1836 * implement. It is essentially a thin dom_node_get_user_data()
1837 * wrapper.
1838 *
1839 * \param pw Pointer to the current SVG parser state
1840 * \param node Libdom SVG node from which to get the data
1841 * \param libcss_node_data Address at which to store a pointer to the data
1842 *
1843 * \return Always returns CSS_OK
1844 */
1845 css_error get_libcss_node_data(void *pw, void *node,
1846 void **libcss_node_data)
1847 {
1848 struct svgtiny_parse_state *state;
1849
1850 /* A unique "userdata key" (a string) is used to identify this
1851 * data. */
1852 state = (struct svgtiny_parse_state *)pw;
1853 dom_node_get_user_data((dom_node *)node,
1854 state->interned_userdata_key,
1855 libcss_node_data);
1856
1857 /* dom_node_get_user_data() always returns DOM_NO_ERR */
1858 return CSS_OK;
1859 }