]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement node_is_root() 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
47
48 /**
49 * Resolve a relative URL to an absolute one by doing nothing. This is
50 * the simplest possible implementation of a URL resolver, needed for
51 * parsing CSS.
52 */
53 css_error svgtiny_resolve_url(void *pw,
54 const char *base, lwc_string *rel, lwc_string **abs)
55 {
56 UNUSED(pw);
57 UNUSED(base);
58
59 /* Copy the relative URL to the absolute one (the return
60 value) */
61 *abs = lwc_string_ref(rel);
62 return CSS_OK;
63 }
64
65 /**
66 * Create a stylesheet with the default set of params.
67 *
68 * \param sheet A stylesheet pointer, passed in by reference, that
69 * we use to store the newly-created stylesheet.
70 * \param inline_style True if this stylesheet represents an inline
71 * style, and false otherwise.
72 *
73 * \return The return value from css_stylesheet_create() is returned.
74 */
75 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
76 bool inline_style)
77 {
78 css_stylesheet_params params;
79
80 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
81 params.level = CSS_LEVEL_DEFAULT;
82 params.charset = NULL;
83 params.url = "";
84 params.title = NULL;
85 params.allow_quirks = false;
86 params.inline_style = inline_style;
87 params.resolve = svgtiny_resolve_url;
88 params.resolve_pw = NULL;
89 params.import = NULL;
90 params.import_pw = NULL;
91 params.color = NULL;
92 params.color_pw = NULL;
93 params.font = NULL;
94 params.font_pw = NULL;
95
96 return css_stylesheet_create(&params, sheet);
97 }
98
99
100 /**************************/
101 /* libcss select handlers */
102 /**************************/
103 /*
104 * From here on we implement the "select handler "API defined in
105 * libcss's include/libcss/select.h and discussed briefly in its
106 * docs/API document.
107 */
108
109
110 /**
111 * Retrieve the given node's name
112 *
113 * \param pw Pointer to the current SVG parser state
114 * \param node Libdom SVG node
115 * \param qname Address at which to store the node name
116 *
117 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
118 */
119 css_error node_name(void *pw, void *node, css_qname *qname)
120 {
121 dom_string *name;
122 dom_exception err;
123 struct svgtiny_parse_state *state;
124
125 err = dom_node_get_node_name((dom_node *)node, &name);
126 if (err != DOM_NO_ERR) {
127 return CSS_NOMEM;
128 }
129
130 state = (struct svgtiny_parse_state *)pw;
131 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
132
133 err = dom_string_intern(name, &qname->name);
134 if (err != DOM_NO_ERR) {
135 dom_string_unref(name);
136 return CSS_NOMEM;
137 }
138
139 dom_string_unref(name);
140
141 return CSS_OK;
142 }
143
144
145 /**
146 * Retrieve the given node's classes
147 *
148 * \param pw Pointer to the current SVG parser state
149 * \param node Libdom SVG node
150 * \param classes Address at which to store the class name array
151 * \param n_classes Address at which to store the length of the class
152 * name array
153 *
154 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
155 *
156 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
157 * because the underlying libdom function never fails
158 */
159 css_error node_classes(void *pw, void *node,
160 lwc_string ***classes, uint32_t *n_classes)
161 {
162 UNUSED(pw);
163 dom_exception err;
164
165 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
166
167 /* The implementation does not do it, but the documentation
168 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
169 possible here, so we handle it to be on the safe side. */
170 if (err != DOM_NO_ERR) {
171 return CSS_NOMEM;
172 }
173
174 return CSS_OK;
175 }
176
177
178 /**
179 * Retrieve the given node's id
180 *
181 * \param pw Pointer to the current SVG parser state
182 * \param node Libdom SVG node
183 * \param id Address at which to store the id
184 *
185 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
186 */
187 css_error node_id(void *pw, void *node, lwc_string **id)
188 {
189 dom_string *attr;
190 dom_exception err;
191 struct svgtiny_parse_state *state;
192
193 /* Begin with the assumption that this node has no id */
194 *id = NULL;
195
196 state = (struct svgtiny_parse_state *)pw;
197 err = dom_element_get_attribute((dom_node *)node,
198 state->interned_id, &attr);
199 if (err != DOM_NO_ERR) {
200 return CSS_NOMEM;
201 }
202 else if (attr == NULL) {
203 /* The node has no id attribute and our return value
204 is already set to NULL so we're done */
205 return CSS_OK;
206 }
207
208 /* If we found an id attribute (a dom_string), intern it into
209 an lwc_string that we can return, and then cleanup the
210 dom_string. */
211 err = dom_string_intern(attr, id);
212 if (err != DOM_NO_ERR) {
213 dom_string_unref(attr);
214 return CSS_NOMEM;
215 }
216 dom_string_unref(attr);
217 return CSS_OK;
218 }
219
220
221
222 /**
223 * Find the first parent of the given element having the given name
224 *
225 * \param pw Pointer to the current SVG parser state
226 * \param node Libdom SVG node
227 * \param qname Name of the parent node to search for
228 * \param parent Address at which to store the parent node pointer
229 *
230 * \return Always returns CSS_OK
231 *
232 * \post If a suitable element is found, a pointer to it will be
233 * stored at the address pointed to by \a parent; otherwise,
234 * NULL will be stored at the address pointed to by \a parent
235 */
236 css_error named_parent_node(void *pw, void *node,
237 const css_qname *qname, void **parent)
238 {
239 UNUSED(pw);
240 /* dom_element_named_parent_node() was invented to implement
241 * this select handler so there isn't much for us to do except
242 * call it. It's OK if node isn't an element, libdom checks
243 * for it. */
244 dom_element_named_parent_node((dom_element *)node,
245 qname->name,
246 (struct dom_element **)parent);
247
248 /* Implementation detail: dom_element_named_parent_node()
249 * increments the reference count of the parent element before
250 * returning it to us. According to docs/RefCnt in the libdom
251 * repository, this will prevent the parent element from being
252 * destroyed if it is pruned from the DOM. That sounds good,
253 * since we don't want to be using a pointer to an object that
254 * has been destroyed... but we also have no way of later
255 * decrementing the reference count ourselves, and don't want
256 * to make the returned node eternal. Decrementing the
257 * reference counter now allows it to be destroyed when the
258 * DOM no longer needs it, and so long as no other parts of
259 * libsvgtiny are messing with the DOM during parsing, that
260 * shouldn't (ha ha) cause any problems. */
261 dom_node_unref(*parent);
262
263 return CSS_OK;
264 }
265
266
267 /**
268 * Find the "next-sibling" of the given element having the given name
269 *
270 * This search corresponds to the "+ foo" combinator in CSS and will
271 * find only "foo" element nodes that immediately precede the given
272 * node under the same parent in the DOM. In CSS the tree is viewed
273 * top-down and in libdom it is viewed from the bottom-up; as a result
274 * "next" and "previous" are sometimes backwards. This is case-sensitive.
275 *
276 * \param pw Pointer to the current SVG parser state
277 * \param node Libdom SVG node
278 * \param qname Name of the sibling node to search for
279 * \param sibling Address at which to store the sibling node pointer
280 *
281 * \return Always returns CSS_OK
282 *
283 * \post If a suitable element is found, a pointer to it will be
284 * stored at the address pointed to by \a sibling; otherwise,
285 * NULL will be stored at the address pointed to by \a sibling
286 */
287 css_error named_sibling_node(void *pw, void *node,
288 const css_qname *qname, void **sibling)
289 {
290 UNUSED(pw);
291 dom_node *n = node; /* the current node */
292 dom_node *prev; /* the previous node */
293 dom_exception err;
294 dom_node_type type;
295 dom_string *name;
296
297 *sibling = NULL; /* default to nothing found */
298
299 /* Begin the search; the first iteration we do outside of the
300 * loop. Implementation detil: dom_node_get_previous_sibling()
301 * increments the reference counter on the returned node. A
302 * comment within named_parent_node() explains why we
303 * decrement it ASAP. */
304 err = dom_node_get_previous_sibling(n, &n);
305 if (err != DOM_NO_ERR) {
306 return CSS_OK;
307 }
308
309 while (n != NULL) {
310 /* We're looking for the first ELEMENT sibling */
311 err = dom_node_get_node_type(n, &type);
312 if (err != DOM_NO_ERR) {
313 dom_node_unref(n);
314 return CSS_OK;
315 }
316
317 if (type == DOM_ELEMENT_NODE) {
318 /* We found an element node, does it have the
319 * right name? */
320 err = dom_node_get_node_name(n, &name);
321 if (err != DOM_NO_ERR) {
322 dom_node_unref(n);
323 return CSS_OK;
324 }
325
326 if (dom_string_lwc_isequal(name,
327 qname->name)) {
328 /* The name is right, return it */
329 *sibling = n;
330 }
331
332 /* There's only one next-sibling element node
333 * and we've already found it, so if its name
334 * wasn't right, we return the default value
335 * of NULL below */
336 dom_string_unref(name);
337 dom_node_unref(n);
338 return CSS_OK;
339 }
340
341 /* Not an element node, so we move on the the previous
342 * previous sibling */
343 err = dom_node_get_previous_sibling(n, &prev);
344 if (err != DOM_NO_ERR) {
345 dom_node_unref(n);
346 return CSS_OK;
347 }
348
349 dom_node_unref(n);
350 n = prev;
351 }
352
353 return CSS_OK;
354 }
355
356
357 /**
358 * Find the first "subsequent-sibling" of the given element having the
359 * given name
360 *
361 * This search corresponds to the "~ foo" combinator in CSS and will
362 * find only "foo" element nodes that precede the given node (under
363 * the same parent) in the DOM. In CSS the tree is viewed top-down and
364 * in libdom it is viewed from the bottom-up; as a result "next" and
365 * "previous" are sometimes backwards. This is case-sensitive.
366 *
367 * \param pw Pointer to the current SVG parser state
368 * \param node Libdom SVG node
369 * \param qname Name of the sibling node to search for
370 * \param sibling Address at which to store the sibling node pointer
371 *
372 * \return Always returns CSS_OK
373 *
374 * \post If a suitable element is found, a pointer to it will be
375 * stored at the address pointed to by \a sibling; otherwise,
376 * NULL will be stored at the address pointed to by \a sibling
377 */
378 css_error named_generic_sibling_node(void *pw, void *node,
379 const css_qname *qname, void **sibling)
380 {
381 UNUSED(pw);
382 dom_node *n = node; /* the current node */
383 dom_node *prev; /* the previous node */
384 dom_exception err;
385 dom_node_type type;
386 dom_string *name;
387
388
389 *sibling = NULL; /* default to nothing found */
390
391 /* Begin the search; the first iteration we do outside of the
392 * loop. Implementation detil: dom_node_get_previous_sibling()
393 * increments the reference counter on the returned node. A
394 * comment within named_parent_node() explains why we
395 * decrement it ASAP. */
396 err = dom_node_get_previous_sibling(n, &n);
397 if (err != DOM_NO_ERR) {
398 return CSS_OK;
399 }
400
401 while (n != NULL) {
402 err = dom_node_get_node_type(n, &type);
403 if (err != DOM_NO_ERR) {
404 dom_node_unref(n);
405 return CSS_OK;
406 }
407
408 if (type == DOM_ELEMENT_NODE) {
409 /* We only want ELEMENT nodes */
410 err = dom_node_get_node_name(n, &name);
411 if (err != DOM_NO_ERR) {
412 dom_node_unref(n);
413 return CSS_OK;
414 }
415
416 if (dom_string_lwc_isequal(name,
417 qname->name)) {
418 /* Found one. Save it and stop the search */
419 dom_string_unref(name);
420 dom_node_unref(n);
421 *sibling = n;
422 return CSS_OK;
423 }
424
425 dom_string_unref(name);
426 }
427
428 /* This sibling wasn't an element with the desired
429 name, so move on to the previous sibling */
430 err = dom_node_get_previous_sibling(n, &prev);
431 if (err != DOM_NO_ERR) {
432 dom_node_unref(n);
433 return CSS_OK;
434 }
435
436 dom_node_unref(n);
437 n = prev;
438 }
439
440 return CSS_OK;
441 }
442
443
444 /**
445 * Return a pointer to the given node's parent
446 *
447 * \param pw Pointer to the current SVG parser state
448 * \param node Libdom SVG node
449 * \param parent Address at which to store the node's parent pointer
450 *
451 * \return Always returns CSS_OK
452 */
453 css_error parent_node(void *pw, void *node, void **parent)
454 {
455 UNUSED(pw);
456 /* Libdom basically implements this for us */
457 dom_element_parent_node(node, (struct dom_element **)parent);
458
459 /* See the comment in named_parent_node() for why we decrement
460 * this reference counter here. */
461 dom_node_unref(*parent);
462
463 return CSS_OK;
464 }
465
466
467 /**
468 * Find the "next-sibling" of the given element
469 *
470 * This search corresponds "+ *" in CSS and will find the first
471 * element node that immediately precedes the given node under the
472 * same parent in the DOM. In CSS the tree is viewed top-down and in
473 * libdom it is viewed from the bottom-up; as a result "next" and
474 * "previous" are sometimes backwards.
475 *
476 * \param pw Pointer to the current SVG parser state
477 * \param node Libdom SVG node
478 * \param sibling Address at which to store the sibling node pointer
479 *
480 * \return Always returns CSS_OK
481 *
482 * \post If a suitable element is found, a pointer to it will be
483 * stored at the address pointed to by \a sibling; otherwise,
484 * NULL will be stored at the address pointed to by \a sibling
485 */
486 css_error sibling_node(void *pw, void *node, void **sibling)
487 {
488 UNUSED(pw);
489 dom_node *n = node; /* the current node */
490 dom_node *prev; /* the previous node */
491 dom_exception err;
492 dom_node_type type;
493
494 *sibling = NULL; /* default to nothing found */
495
496 /* Begin the search; the first iteration we do outside of the
497 * loop. Implementation detil: dom_node_get_previous_sibling()
498 * increments the reference counter on the returned node. A
499 * comment within named_parent_node() explains why we
500 * decrement it ASAP. */
501 err = dom_node_get_previous_sibling(n, &n);
502 if (err != DOM_NO_ERR) {
503 return CSS_OK;
504 }
505
506 while (n != NULL) {
507 err = dom_node_get_node_type(n, &type);
508 if (err != DOM_NO_ERR) {
509 dom_node_unref(n);
510 return CSS_OK;
511 }
512
513 if (type == DOM_ELEMENT_NODE) {
514 /* We found a sibling node that is also an
515 element and that's all we wanted. */
516 *sibling = n;
517 dom_node_unref(n);
518 return CSS_OK;
519 }
520
521 /* This sibling node was not an element; move on to
522 the previous sibling */
523 err = dom_node_get_previous_sibling(n, &prev);
524 if (err != DOM_NO_ERR) {
525 dom_node_unref(n);
526 return CSS_OK;
527 }
528
529 dom_node_unref(n);
530 n = prev;
531 }
532
533 return CSS_OK;
534 }
535
536
537 /**
538 * Test the given node for the given name
539 *
540 * This will return true (via the "match" pointer) if the libdom node
541 * has the given name or if that name is the universal selector;
542 * otherwise it returns false. The comparison is case-sensitive. It
543 * corresponds to a rule like "body { ... }" in CSS.
544 *
545 * \param pw Pointer to the current SVG parser state
546 * \param node Libdom SVG node to test
547 * \param qname Name to check for
548 * \param match Pointer to the test result
549 *
550 * \return Always returns CSS_OK
551 */
552 css_error node_has_name(void *pw, void *node,
553 const css_qname *qname, bool *match)
554 {
555 struct svgtiny_parse_state *state;
556 dom_string *name;
557 dom_exception err;
558
559 /* Start by checking to see if qname is the universal selector */
560 state = (struct svgtiny_parse_state *)pw;
561 if (lwc_string_isequal(qname->name,
562 state->interned_universal, match) == lwc_error_ok) {
563 if (*match) {
564 /* It's the universal selector. In NetSurf, all node
565 * names match the universal selector, and nothing in
566 * the libcss documentation suggests another approach,
567 * so we follow NetSurf here. */
568 return CSS_OK;
569 }
570 }
571
572 err = dom_node_get_node_name((dom_node *)node, &name);
573 if (err != DOM_NO_ERR) {
574 return CSS_OK;
575 }
576
577 /* Unlike with HTML, SVG element names are case-sensitive */
578 *match = dom_string_lwc_isequal(name, qname->name);
579 dom_string_unref(name);
580
581 return CSS_OK;
582 }
583
584
585 /**
586 * Test the given node for the given class
587 *
588 * This will return true (via the "match" pointer) if the libdom node
589 * has the given class. The comparison is case-sensitive. It
590 * corresponds to node.class in CSS.
591 *
592 * \param pw Pointer to the current SVG parser state
593 * \param node Libdom SVG node to test
594 * \param name Class name to check for
595 * \param match Pointer to the test result
596 *
597 * \return Always returns CSS_OK
598 */
599 css_error node_has_class(void *pw, void *node,
600 lwc_string *name, bool *match)
601 {
602 UNUSED(pw);
603 /* libdom implements this for us and apparently it cannot fail */
604 dom_element_has_class((dom_node *)node, name, match);
605 return CSS_OK;
606 }
607
608
609 /**
610 * Test the given node for the given id
611 *
612 * This will return true (via the "match" pointer) if the libdom node
613 * has the given id. The comparison is case-sensitive. It corresponds
614 * to node#id in CSS.
615 *
616 * \param pw Pointer to the current SVG parser state
617 * \param node Libdom SVG node to test
618 * \param name Id to check for
619 * \param match Pointer to the test result
620 *
621 * \return Always returns CSS_OK
622 */
623 css_error node_has_id(void *pw, void *node,
624 lwc_string *name, bool *match)
625 {
626 dom_string *attr;
627 dom_exception err;
628 struct svgtiny_parse_state *state;
629
630 attr = NULL; /* a priori the "id" attribute may not exist */
631 *match = false; /* default to no match */
632
633 state = (struct svgtiny_parse_state *)pw;
634 err = dom_element_get_attribute((dom_node *)node,
635 state->interned_id, &attr);
636 if (err != DOM_NO_ERR || attr == NULL) {
637 return CSS_OK;
638 }
639
640 *match = dom_string_lwc_isequal(attr, name);
641 dom_string_unref(attr);
642
643 return CSS_OK;
644 }
645
646
647 /**
648 * Test the given node for the given attribute
649 *
650 * This will return true (via the "match" pointer) if the libdom node
651 * has an attribute with the given name. The comparison is
652 * case-sensitive. It corresponds to node[attr] in CSS.
653 *
654 * \param pw Pointer to the current SVG parser state
655 * \param node Libdom SVG node to test
656 * \param qname Attribute name to check for
657 * \param match Pointer to the test result
658 *
659 * \return Returns CSS_OK if successful and CSS_NOMEM if anything
660 * goes wrong
661 */
662 css_error node_has_attribute(void *pw, void *node,
663 const css_qname *qname, bool *match)
664 {
665 UNUSED(pw);
666 dom_string *name;
667 dom_exception err;
668
669 /* intern the attribute name as a dom_string so we can
670 * delegate to dom_element_has_attribute() */
671 err = dom_string_create_interned(
672 (const uint8_t *) lwc_string_data(qname->name),
673 lwc_string_length(qname->name),
674 &name);
675 if (err != DOM_NO_ERR) {
676 return CSS_NOMEM;
677 }
678
679 err = dom_element_has_attribute((dom_node *)node, name, match);
680 if (err != DOM_NO_ERR) {
681 dom_string_unref(name);
682 return CSS_OK;
683 }
684
685 dom_string_unref(name);
686 return CSS_OK;
687 }
688
689
690 /**
691 * Test the given node for an attribute with a specific value
692 *
693 * This will return true (via the "match" pointer) if the libdom node
694 * has an attribute with the given name and value. The comparison is
695 * case-sensitive. It corresponds to node[attr=value] in CSS.
696 *
697 * \param pw Pointer to the current SVG parser state
698 * \param node Libdom SVG node to test
699 * \param qname Attribute name to check for
700 * \param value Attribute value to check for
701 * \param match Pointer to the test result
702 *
703 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
704 * intern the attribute name (which usually indicates memory
705 * exhaustion)
706 */
707 css_error node_has_attribute_equal(void *pw, void *node,
708 const css_qname *qname, lwc_string *value,
709 bool *match)
710 {
711 /* Implementation note: NetSurf always returns "no match" when
712 * the value is empty (length zero). We allow it, because why
713 * not? */
714
715 UNUSED(pw);
716 dom_string *name;
717 dom_string *attr_val;
718 dom_exception err;
719
720 /* Intern the attribute name as a dom_string so we can
721 * use dom_element_get_attribute() */
722 err = dom_string_create_interned(
723 (const uint8_t *) lwc_string_data(qname->name),
724 lwc_string_length(qname->name),
725 &name);
726 if (err != DOM_NO_ERR) {
727 return CSS_NOMEM;
728 }
729
730 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
731 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
732 /* There was an error getting the attribute's value or
733 * the attribute doesn't exist. So, no match? */
734 dom_string_unref(name);
735 *match = false;
736 return CSS_OK;
737 }
738
739 /* Otherwise, we have the attribute value from the given node
740 * and all we need to do is compare. */
741 dom_string_unref(name);
742 *match = dom_string_lwc_isequal(attr_val, value);
743 dom_string_unref(attr_val);
744
745 return CSS_OK;
746 }
747
748
749 /**
750 * Test the given node for an attribute with a specific value,
751 * possibly followed by a single hyphen
752 *
753 * This will return true (via the "match" pointer) if the libdom node
754 * has an attribute with the given name and value or with the given
755 * name and a value that is followed by exactly one hyphen. The
756 * comparison is case-sensitive. This corresponds to [attr|=value]
757 * in CSS.
758 *
759 * \param pw Pointer to the current SVG parser state
760 * \param node Libdom SVG node to test
761 * \param qname Attribute name to check for
762 * \param value Attribute value to check for
763 * \param match Pointer to the test result
764 *
765 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
766 * intern the attribute name (which usually indicates memory
767 * exhaustion)
768 */
769 css_error node_has_attribute_dashmatch(void *pw, void *node,
770 const css_qname *qname, lwc_string *value,
771 bool *match)
772 {
773 /* Implementation note: NetSurf always returns "no match" when
774 * the value is empty (length zero). We allow it, because why
775 * not? */
776
777 UNUSED(pw);
778 dom_string *name;
779 dom_string *attr_val;
780 dom_exception err;
781
782 const char *vdata; /* to hold the data underlying "value" */
783 size_t vdata_len;
784 const char *avdata; /* to hold the found attribute value data */
785 size_t avdata_len;
786
787 /* Intern the attribute name as a dom_string so we can
788 * use dom_element_get_attribute() */
789 err = dom_string_create_interned(
790 (const uint8_t *) lwc_string_data(qname->name),
791 lwc_string_length(qname->name),
792 &name);
793 if (err != DOM_NO_ERR) {
794 return CSS_NOMEM;
795 }
796
797 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
798 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
799 /* There was an error getting the attribute's value or
800 * the attribute doesn't exist. So, no match? */
801 dom_string_unref(name);
802 *match = false;
803 return CSS_OK;
804 }
805
806 /* Otherwise, we have the attribute value from the given node
807 * and all we need to do is compare. */
808 dom_string_unref(name);
809 *match = dom_string_lwc_isequal(attr_val, value);
810 if (*match) {
811 /* Exact match, we're done */
812 dom_string_unref(attr_val);
813 return CSS_OK;
814 }
815
816 /* No exact match, try it with a hyphen on the end */
817 vdata = lwc_string_data(value); /* needle */
818 vdata_len = lwc_string_length(value);
819 avdata = dom_string_data(attr_val); /* haystack */
820 avdata_len = dom_string_byte_length(attr_val);
821 dom_string_unref(attr_val);
822
823 if (avdata_len > vdata_len && avdata[vdata_len] == '-') {
824 if (strncasecmp(avdata, vdata, vdata_len) == 0) {
825 /* If there's a hyphen in the right position,
826 * it suffices to compare the strings only up
827 * to the hyphen */
828 *match = true;
829 }
830 }
831
832 return CSS_OK;
833 }
834
835
836 /**
837 * Test the given node for an attribute whose value is a
838 * space-separated list of words, one of which is the given word
839 *
840 * This will return true (via the "match" pointer) if the libdom node
841 * has an attribute with the given name and whose value when
842 * considered as a space-separated list of words contains the given
843 * word. The comparison is case-sensitive. This corresponds to
844 * [attr~=value] in CSS.
845 *
846 * \param pw Pointer to the current SVG parser state
847 * \param node Libdom SVG node to test
848 * \param qname Attribute name to check for
849 * \param word Value word to check for
850 * \param match Pointer to the test result
851 *
852 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
853 * intern the attribute name (which usually indicates memory
854 * exhaustion)
855 */
856 css_error node_has_attribute_includes(void *pw, void *node,
857 const css_qname *qname, lwc_string *word,
858 bool *match)
859 {
860 UNUSED(pw);
861
862 dom_string *name;
863 dom_string *attr_val;
864 dom_exception err;
865 size_t wordlen; /* length of "word" */
866
867 /* pointers used to parse a space-separated list of words */
868 const char *p;
869 const char *start;
870 const char *end;
871
872 *match = false; /* default to no match */
873
874 wordlen = lwc_string_length(word);
875 if (wordlen == 0) {
876 /* In this case, the spec says that "if 'val' is the
877 * empty string, it will never represent anything." */
878 return CSS_OK;
879 }
880
881 /* Intern the attribute name as a dom_string so we can
882 * use dom_element_get_attribute() */
883 err = dom_string_create_interned(
884 (const uint8_t *) lwc_string_data(qname->name),
885 lwc_string_length(qname->name),
886 &name);
887 if (err != DOM_NO_ERR) {
888 return CSS_NOMEM;
889 }
890
891 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
892 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
893 /* There was an error getting the attribute's value or
894 * the attribute doesn't exist. So, no match? */
895 dom_string_unref(name);
896 return CSS_OK;
897 }
898
899 /* Parse the list comparing each word against "word" */
900 start = dom_string_data(attr_val);
901 end = start + dom_string_byte_length(attr_val);
902 dom_string_unref(attr_val);
903
904 for (p = start; p <= end; p++) {
905 /* Move forward until we find the end of the first word */
906 if (*p == ' ' || *p == '\0') {
907 /* If the length of that word is the length of the
908 * word we're looking for, do the comparison. */
909 if ((size_t) (p - start) == wordlen &&
910 strncasecmp(start,
911 lwc_string_data(word),
912 wordlen) == 0) {
913 *match = true;
914 break;
915 }
916 /* No match? Set "start" to the beginning of
917 * the next word and loop. */
918 start = p + 1;
919 }
920 }
921
922 return CSS_OK;
923 }
924
925
926 /**
927 * Test the given node for an attribute whose value begins with the
928 * given prefix
929 *
930 * This will return true (via the "match" pointer) if the libdom node
931 * has an attribute with the given name and whose value begins with
932 * the given prefix string. The comparison is case-sensitive. This
933 * corresponds to [attr^=value] in CSS.
934 *
935 * \param pw Pointer to the current SVG parser state
936 * \param node Libdom SVG node to test
937 * \param qname Attribute name to check for
938 * \param prefix Value prefix to check for
939 * \param match Pointer to the test result
940 *
941 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
942 * intern the attribute name (which usually indicates memory
943 * exhaustion)
944 */
945 css_error node_has_attribute_prefix(void *pw, void *node,
946 const css_qname *qname, lwc_string *prefix,
947 bool *match)
948 {
949 UNUSED(pw);
950 dom_string *name;
951 dom_string *attr_val;
952 dom_exception err;
953 const char *avdata; /* attribute value data */
954 size_t avdata_len; /* length of that attribute value data */
955 size_t prefixlen; /* length of "prefix" */
956
957 prefixlen = lwc_string_length(prefix);
958 if (prefixlen == 0) {
959 /* In this case, the spec says that "if 'val' is the
960 * empty string, it will never represent anything." */
961 return CSS_OK;
962 }
963
964 /* Intern the attribute name as a dom_string so we can
965 * use dom_element_get_attribute() */
966 err = dom_string_create_interned(
967 (const uint8_t *) lwc_string_data(qname->name),
968 lwc_string_length(qname->name),
969 &name);
970 if (err != DOM_NO_ERR) {
971 return CSS_NOMEM;
972 }
973
974 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
975 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
976 /* There was an error getting the attribute's value or
977 * the attribute doesn't exist. So, no match? */
978 dom_string_unref(name);
979 *match = false;
980 return CSS_OK;
981 }
982
983 /* Otherwise, we have the attribute value from the given node,
984 * and the first thing we want to do is check to see if the
985 * whole thing matches the prefix. */
986 dom_string_unref(name);
987 *match = dom_string_lwc_isequal(attr_val, prefix);
988
989 /* If not, check to see if an, uh, prefix matches the
990 * prefix */
991 if (*match == false) {
992 avdata = dom_string_data(attr_val);
993 avdata_len = dom_string_byte_length(attr_val);
994 if ((avdata_len >= prefixlen) &&
995 (strncasecmp(avdata,
996 lwc_string_data(prefix),
997 prefixlen) == 0)) {
998 /* Use strncasecmp to compare only the first
999 * "n" characters, where "n" is the length of
1000 * the prefix. */
1001 *match = true;
1002 }
1003 }
1004
1005 dom_string_unref(attr_val);
1006
1007 return CSS_OK;
1008 }
1009
1010
1011 /**
1012 * Test the given node for an attribute whose value end with the
1013 * given suffix
1014 *
1015 * This will return true (via the "match" pointer) if the libdom node
1016 * has an attribute with the given name and whose value ends with
1017 * the given suffix string. The comparison is case-sensitive. This
1018 * corresponds to [attr$=value] in CSS.
1019 *
1020 * \param pw Pointer to the current SVG parser state
1021 * \param node Libdom SVG node to test
1022 * \param qname Attribute name to check for
1023 * \param suffix Value suffix to check for
1024 * \param match Pointer to the test result
1025 *
1026 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1027 * intern the attribute name (which usually indicates memory
1028 * exhaustion)
1029 */
1030 css_error node_has_attribute_suffix(void *pw, void *node,
1031 const css_qname *qname, lwc_string *suffix,
1032 bool *match)
1033 {
1034 UNUSED(pw);
1035 dom_string *name;
1036 dom_string *attr_val;
1037 dom_exception err;
1038 const char *avdata; /* attribute value data */
1039 size_t avdata_len; /* length of that attribute value data */
1040 size_t suffixlen; /* length of "suffix" */
1041
1042 /* convenience pointer we'll use when matching the suffix */
1043 const char *suffix_start;
1044
1045 suffixlen = lwc_string_length(suffix);
1046 if (suffixlen == 0) {
1047 /* In this case, the spec says that "if 'val' is the
1048 * empty string, it will never represent anything." */
1049 return CSS_OK;
1050 }
1051
1052 /* Intern the attribute name as a dom_string so we can
1053 * use dom_element_get_attribute() */
1054 err = dom_string_create_interned(
1055 (const uint8_t *) lwc_string_data(qname->name),
1056 lwc_string_length(qname->name),
1057 &name);
1058 if (err != DOM_NO_ERR) {
1059 return CSS_NOMEM;
1060 }
1061
1062 err = dom_element_get_attribute((dom_node *)node, name, &attr_val);
1063 if ((err != DOM_NO_ERR) || (attr_val == NULL)) {
1064 /* There was an error getting the attribute's value or
1065 * the attribute doesn't exist. So, no match? */
1066 dom_string_unref(name);
1067 *match = false;
1068 return CSS_OK;
1069 }
1070
1071 /* Otherwise, we have the attribute value from the given node,
1072 * and the first thing we want to do is check to see if the
1073 * whole thing matches the suffix. */
1074 dom_string_unref(name);
1075 *match = dom_string_lwc_isequal(attr_val, suffix);
1076
1077 /* If not, check to see if an, uh, suffix matches the
1078 * suffix */
1079 if (*match == false) {
1080 avdata = dom_string_data(attr_val);
1081 avdata_len = dom_string_byte_length(attr_val);
1082
1083 suffix_start = (char *)(avdata + avdata_len - suffixlen);
1084
1085 if ((avdata_len >= suffixlen) &&
1086 (strncasecmp(suffix_start,
1087 lwc_string_data(suffix),
1088 suffixlen) == 0)) {
1089 /* Use strncasecmp to compare only the last
1090 * "n" characters, where "n" is the length of
1091 * the suffix. */
1092 *match = true;
1093 }
1094 }
1095
1096 dom_string_unref(attr_val);
1097
1098 return CSS_OK;
1099 }
1100
1101
1102 /**
1103 * Test the given node for an attribute whose value contains the
1104 * given substring
1105 *
1106 * This will return true (via the "match" pointer) if the libdom node
1107 * has an attribute with the given name and whose value contains the
1108 * given substring. The comparison is case-sensitive. This corresponds
1109 * to [attr*=value] in CSS.
1110 *
1111 * \param pw Pointer to the current SVG parser state
1112 * \param node Libdom SVG node to test
1113 * \param qname Attribute name to check for
1114 * \param substring Value substring to check for
1115 * \param match Pointer to the test result
1116 *
1117 * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot
1118 * intern the attribute name (which usually indicates memory
1119 * exhaustion)
1120 */
1121 css_error node_has_attribute_substring(void *pw, void *node,
1122 const css_qname *qname, lwc_string *substring,
1123 bool *match)
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 *match = dom_string_lwc_isequal(attr_val, substring);
1167
1168 /* If not, check to see if an, uh, substring matches the
1169 * substring */
1170 if (*match == false) {
1171 p = dom_string_data(attr_val);
1172
1173 /* Check every long-enough suffix for a prefix match */
1174 attr_len = dom_string_byte_length(attr_val);
1175 if (attr_len >= substrlen) {
1176 p_max = p + attr_len - substrlen;
1177 while (p <= p_max) {
1178 if (strncasecmp(p,
1179 lwc_string_data(substring),
1180 substrlen) == 0) {
1181 *match = true;
1182 break;
1183 }
1184 p++;
1185 }
1186 }
1187 }
1188
1189 dom_string_unref(attr_val);
1190
1191 return CSS_OK;
1192 }
1193
1194
1195 /**
1196 * Test whether or not the given node is the document's root element
1197 * This corresponds to the CSS :root pseudo-selector.
1198 *
1199 * \param pw Pointer to the current SVG parser state
1200 * \param node Libdom SVG node to test
1201 * \param match Pointer to the test result
1202 *
1203 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
1204 */
1205 css_error node_is_root(void *pw, void *node, bool *match)
1206 {
1207 UNUSED(pw);
1208 dom_node *parent;
1209 dom_node_type type;
1210 dom_exception err;
1211
1212 err = dom_node_get_parent_node((dom_node *)node, &parent);
1213 if (err != DOM_NO_ERR) {
1214 return CSS_NOMEM;
1215 }
1216
1217 /* It's the root element if it doesn't have a parent element */
1218 if (parent != NULL) {
1219 err = dom_node_get_node_type(parent, &type);
1220 dom_node_unref(parent);
1221 if (err != DOM_NO_ERR) {
1222 return CSS_NOMEM;
1223 }
1224 if (type != DOM_DOCUMENT_NODE) {
1225 /* DOM_DOCUMENT_NODE is the only allowable
1226 * type of parent node for the root element */
1227 *match = false;
1228 return CSS_OK;
1229 }
1230 }
1231
1232 *match = true;
1233 return CSS_OK;
1234 }