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