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