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