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