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