#include #include /* strncasecmp */ #include "svgtiny.h" #include "svgtiny_internal.h" static css_error node_name(void *pw, void *node, css_qname *qname); static css_error node_classes(void *pw, void *node, lwc_string ***classes, uint32_t *n_classes); static css_error node_id(void *pw, void *node, lwc_string **id); static css_error named_parent_node(void *pw, void *node, const css_qname *qname, void **parent); static css_error named_sibling_node(void *pw, void *node, const css_qname *qname, void **sibling); static css_error named_generic_sibling_node(void *pw, void *node, const css_qname *qname, void **sibling); static css_error parent_node(void *pw, void *node, void **parent); static css_error sibling_node(void *pw, void *node, void **sibling); static css_error node_has_name(void *pw, void *node, const css_qname *qname, bool *match); static css_error node_has_class(void *pw, void *node, lwc_string *name, bool *match); static css_error node_has_id(void *pw, void *node, lwc_string *name, bool *match); static css_error node_has_attribute(void *pw, void *node, const css_qname *qname, bool *match); static css_error node_has_attribute_equal(void *pw, void *node, const css_qname *qname, lwc_string *value, bool *match); static css_error node_has_attribute_dashmatch(void *pw, void *node, const css_qname *qname, lwc_string *value, bool *match); static css_error node_has_attribute_includes(void *pw, void *node, const css_qname *qname, lwc_string *word, bool *match); static css_error node_has_attribute_prefix(void *pw, void *node, const css_qname *qname, lwc_string *prefix, bool *match); static css_error node_has_attribute_suffix(void *pw, void *node, const css_qname *qname, lwc_string *suffix, bool *match); static css_error node_has_attribute_substring(void *pw, void *node, const css_qname *qname, lwc_string *substring, bool *match); static css_error node_is_root(void *pw, void *node, bool *match); static css_error node_count_siblings(void *pw, void *node, bool same_name, bool after, int32_t *count); /** * Resolve a relative URL to an absolute one by doing nothing. This is * the simplest possible implementation of a URL resolver, needed for * parsing CSS. */ css_error svgtiny_resolve_url(void *pw, const char *base, lwc_string *rel, lwc_string **abs) { UNUSED(pw); UNUSED(base); /* Copy the relative URL to the absolute one (the return value) */ *abs = lwc_string_ref(rel); return CSS_OK; } /** * Create a stylesheet with the default set of params. * * \param sheet A stylesheet pointer, passed in by reference, that * we use to store the newly-created stylesheet. * \param inline_style True if this stylesheet represents an inline * style, and false otherwise. * * \return The return value from css_stylesheet_create() is returned. */ css_error svgtiny_create_stylesheet(css_stylesheet **sheet, bool inline_style) { css_stylesheet_params params; params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1; params.level = CSS_LEVEL_DEFAULT; params.charset = NULL; params.url = ""; params.title = NULL; params.allow_quirks = false; params.inline_style = inline_style; params.resolve = svgtiny_resolve_url; params.resolve_pw = NULL; params.import = NULL; params.import_pw = NULL; params.color = NULL; params.color_pw = NULL; params.font = NULL; params.font_pw = NULL; return css_stylesheet_create(¶ms, sheet); } /**************************/ /* libcss select handlers */ /**************************/ /* * From here on we implement the "select handler "API defined in * libcss's include/libcss/select.h and discussed briefly in its * docs/API document. */ /** * Retrieve the given node's name * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param qname Address at which to store the node name * * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong */ css_error node_name(void *pw, void *node, css_qname *qname) { dom_string *name; dom_exception err; struct svgtiny_parse_state *state; err = dom_node_get_node_name((dom_node *)node, &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } state = (struct svgtiny_parse_state *)pw; qname->ns = lwc_string_ref(state->interned_svg_xmlns); err = dom_string_intern(name, &qname->name); if (err != DOM_NO_ERR) { dom_string_unref(name); return CSS_NOMEM; } dom_string_unref(name); return CSS_OK; } /** * Retrieve the given node's classes * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param classes Address at which to store the class name array * \param n_classes Address at which to store the length of the class * name array * * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong * * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1, * because the underlying libdom function never fails */ css_error node_classes(void *pw, void *node, lwc_string ***classes, uint32_t *n_classes) { UNUSED(pw); dom_exception err; err = dom_element_get_classes((dom_node *)node, classes, n_classes); /* The implementation does not do it, but the documentation for dom_element_get_classes() says that a DOM_NO_MEM_ERR is possible here, so we handle it to be on the safe side. */ if (err != DOM_NO_ERR) { return CSS_NOMEM; } return CSS_OK; } /** * Retrieve the given node's id * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param id Address at which to store the id * * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong */ css_error node_id(void *pw, void *node, lwc_string **id) { dom_string *attr; dom_exception err; struct svgtiny_parse_state *state; /* Begin with the assumption that this node has no id */ *id = NULL; state = (struct svgtiny_parse_state *)pw; err = dom_element_get_attribute((dom_node *)node, state->interned_id, &attr); if (err != DOM_NO_ERR) { return CSS_NOMEM; } else if (attr == NULL) { /* The node has no id attribute and our return value is already set to NULL so we're done */ return CSS_OK; } /* If we found an id attribute (a dom_string), intern it into an lwc_string that we can return, and then cleanup the dom_string. */ err = dom_string_intern(attr, id); if (err != DOM_NO_ERR) { dom_string_unref(attr); return CSS_NOMEM; } dom_string_unref(attr); return CSS_OK; } /** * Find the first parent of the given element having the given name * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param qname Name of the parent node to search for * \param parent Address at which to store the parent node pointer * * \return Always returns CSS_OK * * \post If a suitable element is found, a pointer to it will be * stored at the address pointed to by \a parent; otherwise, * NULL will be stored at the address pointed to by \a parent */ css_error named_parent_node(void *pw, void *node, const css_qname *qname, void **parent) { UNUSED(pw); /* dom_element_named_parent_node() was invented to implement * this select handler so there isn't much for us to do except * call it. It's OK if node isn't an element, libdom checks * for it. */ dom_element_named_parent_node((dom_element *)node, qname->name, (struct dom_element **)parent); /* Implementation detail: dom_element_named_parent_node() * increments the reference count of the parent element before * returning it to us. According to docs/RefCnt in the libdom * repository, this will prevent the parent element from being * destroyed if it is pruned from the DOM. That sounds good, * since we don't want to be using a pointer to an object that * has been destroyed... but we also have no way of later * decrementing the reference count ourselves, and don't want * to make the returned node eternal. Decrementing the * reference counter now allows it to be destroyed when the * DOM no longer needs it, and so long as no other parts of * libsvgtiny are messing with the DOM during parsing, that * shouldn't (ha ha) cause any problems. */ dom_node_unref(*parent); return CSS_OK; } /** * Find the "next-sibling" of the given element having the given name * * This search corresponds to the "+ foo" combinator in CSS and will * find only "foo" element nodes that immediately precede the given * node under the same parent in the DOM. In CSS the tree is viewed * top-down and in libdom it is viewed from the bottom-up; as a result * "next" and "previous" are sometimes backwards. This is case-sensitive. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param qname Name of the sibling node to search for * \param sibling Address at which to store the sibling node pointer * * \return Always returns CSS_OK * * \post If a suitable element is found, a pointer to it will be * stored at the address pointed to by \a sibling; otherwise, * NULL will be stored at the address pointed to by \a sibling */ css_error named_sibling_node(void *pw, void *node, const css_qname *qname, void **sibling) { UNUSED(pw); dom_node *n = node; /* the current node */ dom_node *prev; /* the previous node */ dom_exception err; dom_node_type type; dom_string *name; *sibling = NULL; /* default to nothing found */ /* Begin the search; the first iteration we do outside of the * loop. Implementation detil: dom_node_get_previous_sibling() * increments the reference counter on the returned node. A * comment within named_parent_node() explains why we * decrement it ASAP. */ err = dom_node_get_previous_sibling(n, &n); if (err != DOM_NO_ERR) { return CSS_OK; } while (n != NULL) { /* We're looking for the first ELEMENT sibling */ err = dom_node_get_node_type(n, &type); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } if (type == DOM_ELEMENT_NODE) { /* We found an element node, does it have the * right name? */ err = dom_node_get_node_name(n, &name); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } if (dom_string_lwc_isequal(name, qname->name)) { /* The name is right, return it */ *sibling = n; } /* There's only one next-sibling element node * and we've already found it, so if its name * wasn't right, we return the default value * of NULL below */ dom_string_unref(name); dom_node_unref(n); return CSS_OK; } /* Not an element node, so we move on the the previous * previous sibling */ err = dom_node_get_previous_sibling(n, &prev); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } dom_node_unref(n); n = prev; } return CSS_OK; } /** * Find the first "subsequent-sibling" of the given element having the * given name * * This search corresponds to the "~ foo" combinator in CSS and will * find only "foo" element nodes that precede the given node (under * the same parent) in the DOM. In CSS the tree is viewed top-down and * in libdom it is viewed from the bottom-up; as a result "next" and * "previous" are sometimes backwards. This is case-sensitive. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param qname Name of the sibling node to search for * \param sibling Address at which to store the sibling node pointer * * \return Always returns CSS_OK * * \post If a suitable element is found, a pointer to it will be * stored at the address pointed to by \a sibling; otherwise, * NULL will be stored at the address pointed to by \a sibling */ css_error named_generic_sibling_node(void *pw, void *node, const css_qname *qname, void **sibling) { UNUSED(pw); dom_node *n = node; /* the current node */ dom_node *prev; /* the previous node */ dom_exception err; dom_node_type type; dom_string *name; *sibling = NULL; /* default to nothing found */ /* Begin the search; the first iteration we do outside of the * loop. Implementation detil: dom_node_get_previous_sibling() * increments the reference counter on the returned node. A * comment within named_parent_node() explains why we * decrement it ASAP. */ err = dom_node_get_previous_sibling(n, &n); if (err != DOM_NO_ERR) { return CSS_OK; } while (n != NULL) { err = dom_node_get_node_type(n, &type); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } if (type == DOM_ELEMENT_NODE) { /* We only want ELEMENT nodes */ err = dom_node_get_node_name(n, &name); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } if (dom_string_lwc_isequal(name, qname->name)) { /* Found one. Save it and stop the search */ dom_string_unref(name); dom_node_unref(n); *sibling = n; return CSS_OK; } dom_string_unref(name); } /* This sibling wasn't an element with the desired name, so move on to the previous sibling */ err = dom_node_get_previous_sibling(n, &prev); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } dom_node_unref(n); n = prev; } return CSS_OK; } /** * Return a pointer to the given node's parent * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param parent Address at which to store the node's parent pointer * * \return Always returns CSS_OK */ css_error parent_node(void *pw, void *node, void **parent) { UNUSED(pw); /* Libdom basically implements this for us */ dom_element_parent_node(node, (struct dom_element **)parent); /* See the comment in named_parent_node() for why we decrement * this reference counter here. */ dom_node_unref(*parent); return CSS_OK; } /** * Find the "next-sibling" of the given element * * This search corresponds "+ *" in CSS and will find the first * element node that immediately precedes the given node under the * same parent in the DOM. In CSS the tree is viewed top-down and in * libdom it is viewed from the bottom-up; as a result "next" and * "previous" are sometimes backwards. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node * \param sibling Address at which to store the sibling node pointer * * \return Always returns CSS_OK * * \post If a suitable element is found, a pointer to it will be * stored at the address pointed to by \a sibling; otherwise, * NULL will be stored at the address pointed to by \a sibling */ css_error sibling_node(void *pw, void *node, void **sibling) { UNUSED(pw); dom_node *n = node; /* the current node */ dom_node *prev; /* the previous node */ dom_exception err; dom_node_type type; *sibling = NULL; /* default to nothing found */ /* Begin the search; the first iteration we do outside of the * loop. Implementation detil: dom_node_get_previous_sibling() * increments the reference counter on the returned node. A * comment within named_parent_node() explains why we * decrement it ASAP. */ err = dom_node_get_previous_sibling(n, &n); if (err != DOM_NO_ERR) { return CSS_OK; } while (n != NULL) { err = dom_node_get_node_type(n, &type); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } if (type == DOM_ELEMENT_NODE) { /* We found a sibling node that is also an element and that's all we wanted. */ *sibling = n; dom_node_unref(n); return CSS_OK; } /* This sibling node was not an element; move on to the previous sibling */ err = dom_node_get_previous_sibling(n, &prev); if (err != DOM_NO_ERR) { dom_node_unref(n); return CSS_OK; } dom_node_unref(n); n = prev; } return CSS_OK; } /** * Test the given node for the given name * * This will return true (via the "match" pointer) if the libdom node * has the given name or if that name is the universal selector; * otherwise it returns false. The comparison is case-sensitive. It * corresponds to a rule like "body { ... }" in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Name to check for * \param match Pointer to the test result * * \return Always returns CSS_OK */ css_error node_has_name(void *pw, void *node, const css_qname *qname, bool *match) { struct svgtiny_parse_state *state; dom_string *name; dom_exception err; /* Start by checking to see if qname is the universal selector */ state = (struct svgtiny_parse_state *)pw; if (lwc_string_isequal(qname->name, state->interned_universal, match) == lwc_error_ok) { if (*match) { /* It's the universal selector. In NetSurf, all node * names match the universal selector, and nothing in * the libcss documentation suggests another approach, * so we follow NetSurf here. */ return CSS_OK; } } err = dom_node_get_node_name((dom_node *)node, &name); if (err != DOM_NO_ERR) { return CSS_OK; } /* Unlike with HTML, SVG element names are case-sensitive */ *match = dom_string_lwc_isequal(name, qname->name); dom_string_unref(name); return CSS_OK; } /** * Test the given node for the given class * * This will return true (via the "match" pointer) if the libdom node * has the given class. The comparison is case-sensitive. It * corresponds to node.class in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param name Class name to check for * \param match Pointer to the test result * * \return Always returns CSS_OK */ css_error node_has_class(void *pw, void *node, lwc_string *name, bool *match) { UNUSED(pw); /* libdom implements this for us and apparently it cannot fail */ dom_element_has_class((dom_node *)node, name, match); return CSS_OK; } /** * Test the given node for the given id * * This will return true (via the "match" pointer) if the libdom node * has the given id. The comparison is case-sensitive. It corresponds * to node#id in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param name Id to check for * \param match Pointer to the test result * * \return Always returns CSS_OK */ css_error node_has_id(void *pw, void *node, lwc_string *name, bool *match) { dom_string *attr; dom_exception err; struct svgtiny_parse_state *state; attr = NULL; /* a priori the "id" attribute may not exist */ *match = false; /* default to no match */ state = (struct svgtiny_parse_state *)pw; err = dom_element_get_attribute((dom_node *)node, state->interned_id, &attr); if (err != DOM_NO_ERR || attr == NULL) { return CSS_OK; } *match = dom_string_lwc_isequal(attr, name); dom_string_unref(attr); return CSS_OK; } /** * Test the given node for the given attribute * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name. The comparison is * case-sensitive. It corresponds to node[attr] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if anything * goes wrong */ css_error node_has_attribute(void *pw, void *node, const css_qname *qname, bool *match) { UNUSED(pw); dom_string *name; dom_exception err; /* intern the attribute name as a dom_string so we can * delegate to dom_element_has_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_has_attribute((dom_node *)node, name, match); if (err != DOM_NO_ERR) { dom_string_unref(name); return CSS_OK; } dom_string_unref(name); return CSS_OK; } /** * Test the given node for an attribute with a specific value * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and value. The comparison is * case-sensitive. It corresponds to node[attr=value] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param value Attribute value to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_equal(void *pw, void *node, const css_qname *qname, lwc_string *value, bool *match) { /* Implementation note: NetSurf always returns "no match" when * the value is empty (length zero). We allow it, because why * not? */ UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); *match = false; return CSS_OK; } /* Otherwise, we have the attribute value from the given node * and all we need to do is compare. */ dom_string_unref(name); *match = dom_string_lwc_isequal(attr_val, value); dom_string_unref(attr_val); return CSS_OK; } /** * Test the given node for an attribute with a specific value, * possibly followed by a single hyphen * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and value or with the given * name and a value that is followed by exactly one hyphen. The * comparison is case-sensitive. This corresponds to [attr|=value] * in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param value Attribute value to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_dashmatch(void *pw, void *node, const css_qname *qname, lwc_string *value, bool *match) { /* Implementation note: NetSurf always returns "no match" when * the value is empty (length zero). We allow it, because why * not? */ UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; const char *vdata; /* to hold the data underlying "value" */ size_t vdata_len; const char *avdata; /* to hold the found attribute value data */ size_t avdata_len; /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); *match = false; return CSS_OK; } /* Otherwise, we have the attribute value from the given node * and all we need to do is compare. */ dom_string_unref(name); *match = dom_string_lwc_isequal(attr_val, value); if (*match) { /* Exact match, we're done */ dom_string_unref(attr_val); return CSS_OK; } /* No exact match, try it with a hyphen on the end */ vdata = lwc_string_data(value); /* needle */ vdata_len = lwc_string_length(value); avdata = dom_string_data(attr_val); /* haystack */ avdata_len = dom_string_byte_length(attr_val); dom_string_unref(attr_val); if (avdata_len > vdata_len && avdata[vdata_len] == '-') { if (strncasecmp(avdata, vdata, vdata_len) == 0) { /* If there's a hyphen in the right position, * it suffices to compare the strings only up * to the hyphen */ *match = true; } } return CSS_OK; } /** * Test the given node for an attribute whose value is a * space-separated list of words, one of which is the given word * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and whose value when * considered as a space-separated list of words contains the given * word. The comparison is case-sensitive. This corresponds to * [attr~=value] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param word Value word to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_includes(void *pw, void *node, const css_qname *qname, lwc_string *word, bool *match) { UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; size_t wordlen; /* length of "word" */ /* pointers used to parse a space-separated list of words */ const char *p; const char *start; const char *end; *match = false; /* default to no match */ wordlen = lwc_string_length(word); if (wordlen == 0) { /* In this case, the spec says that "if 'val' is the * empty string, it will never represent anything." */ return CSS_OK; } /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); return CSS_OK; } /* Parse the list comparing each word against "word" */ start = dom_string_data(attr_val); end = start + dom_string_byte_length(attr_val); dom_string_unref(attr_val); for (p = start; p <= end; p++) { /* Move forward until we find the end of the first word */ if (*p == ' ' || *p == '\0') { /* If the length of that word is the length of the * word we're looking for, do the comparison. */ if ((size_t) (p - start) == wordlen && strncasecmp(start, lwc_string_data(word), wordlen) == 0) { *match = true; break; } /* No match? Set "start" to the beginning of * the next word and loop. */ start = p + 1; } } return CSS_OK; } /** * Test the given node for an attribute whose value begins with the * given prefix * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and whose value begins with * the given prefix string. The comparison is case-sensitive. This * corresponds to [attr^=value] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param prefix Value prefix to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_prefix(void *pw, void *node, const css_qname *qname, lwc_string *prefix, bool *match) { UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; const char *avdata; /* attribute value data */ size_t avdata_len; /* length of that attribute value data */ size_t prefixlen; /* length of "prefix" */ prefixlen = lwc_string_length(prefix); if (prefixlen == 0) { /* In this case, the spec says that "if 'val' is the * empty string, it will never represent anything." */ return CSS_OK; } /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); *match = false; return CSS_OK; } /* Otherwise, we have the attribute value from the given node, * and the first thing we want to do is check to see if the * whole thing matches the prefix. */ dom_string_unref(name); *match = dom_string_lwc_isequal(attr_val, prefix); /* If not, check to see if an, uh, prefix matches the * prefix */ if (*match == false) { avdata = dom_string_data(attr_val); avdata_len = dom_string_byte_length(attr_val); if ((avdata_len >= prefixlen) && (strncasecmp(avdata, lwc_string_data(prefix), prefixlen) == 0)) { /* Use strncasecmp to compare only the first * "n" characters, where "n" is the length of * the prefix. */ *match = true; } } dom_string_unref(attr_val); return CSS_OK; } /** * Test the given node for an attribute whose value end with the * given suffix * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and whose value ends with * the given suffix string. The comparison is case-sensitive. This * corresponds to [attr$=value] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param suffix Value suffix to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_suffix(void *pw, void *node, const css_qname *qname, lwc_string *suffix, bool *match) { UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; const char *avdata; /* attribute value data */ size_t avdata_len; /* length of that attribute value data */ size_t suffixlen; /* length of "suffix" */ /* convenience pointer we'll use when matching the suffix */ const char *suffix_start; suffixlen = lwc_string_length(suffix); if (suffixlen == 0) { /* In this case, the spec says that "if 'val' is the * empty string, it will never represent anything." */ return CSS_OK; } /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); *match = false; return CSS_OK; } /* Otherwise, we have the attribute value from the given node, * and the first thing we want to do is check to see if the * whole thing matches the suffix. */ dom_string_unref(name); *match = dom_string_lwc_isequal(attr_val, suffix); /* If not, check to see if an, uh, suffix matches the * suffix */ if (*match == false) { avdata = dom_string_data(attr_val); avdata_len = dom_string_byte_length(attr_val); suffix_start = (char *)(avdata + avdata_len - suffixlen); if ((avdata_len >= suffixlen) && (strncasecmp(suffix_start, lwc_string_data(suffix), suffixlen) == 0)) { /* Use strncasecmp to compare only the last * "n" characters, where "n" is the length of * the suffix. */ *match = true; } } dom_string_unref(attr_val); return CSS_OK; } /** * Test the given node for an attribute whose value contains the * given substring * * This will return true (via the "match" pointer) if the libdom node * has an attribute with the given name and whose value contains the * given substring. The comparison is case-sensitive. This corresponds * to [attr*=value] in CSS. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param qname Attribute name to check for * \param substring Value substring to check for * \param match Pointer to the test result * * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot * intern the attribute name (which usually indicates memory * exhaustion) */ css_error node_has_attribute_substring(void *pw, void *node, const css_qname *qname, lwc_string *substring, bool *match) { UNUSED(pw); dom_string *name; dom_string *attr_val; dom_exception err; size_t attr_len; /* length of attr_val */ size_t substrlen; /* length of "substring" */ /* Convenience pointers we use when comparing substrings */ const char *p; const char *p_max; substrlen = lwc_string_length(substring); if (substrlen == 0) { /* In this case, the spec says that "if 'val' is the * empty string, it will never represent anything." */ return CSS_OK; } /* Intern the attribute name as a dom_string so we can * use dom_element_get_attribute() */ err = dom_string_create_interned( (const uint8_t *) lwc_string_data(qname->name), lwc_string_length(qname->name), &name); if (err != DOM_NO_ERR) { return CSS_NOMEM; } err = dom_element_get_attribute((dom_node *)node, name, &attr_val); if ((err != DOM_NO_ERR) || (attr_val == NULL)) { /* There was an error getting the attribute's value or * the attribute doesn't exist. So, no match? */ dom_string_unref(name); *match = false; return CSS_OK; } /* Otherwise, we have the attribute value from the given node, * and the first thing we want to do is check to see if the * whole thing matches the substring. */ dom_string_unref(name); *match = dom_string_lwc_isequal(attr_val, substring); /* If not, check to see if an, uh, substring matches the * substring */ if (*match == false) { p = dom_string_data(attr_val); /* Check every long-enough suffix for a prefix match */ attr_len = dom_string_byte_length(attr_val); if (attr_len >= substrlen) { p_max = p + attr_len - substrlen; while (p <= p_max) { if (strncasecmp(p, lwc_string_data(substring), substrlen) == 0) { *match = true; break; } p++; } } } dom_string_unref(attr_val); return CSS_OK; } /** * Test whether or not the given node is the document's root element * This corresponds to the CSS :root pseudo-selector. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node to test * \param match Pointer to the test result * * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong */ css_error node_is_root(void *pw, void *node, bool *match) { UNUSED(pw); dom_node *parent; dom_node_type type; dom_exception err; err = dom_node_get_parent_node((dom_node *)node, &parent); if (err != DOM_NO_ERR) { return CSS_NOMEM; } /* It's the root element if it doesn't have a parent element */ if (parent != NULL) { err = dom_node_get_node_type(parent, &type); dom_node_unref(parent); if (err != DOM_NO_ERR) { return CSS_NOMEM; } if (type != DOM_DOCUMENT_NODE) { /* DOM_DOCUMENT_NODE is the only allowable * type of parent node for the root element */ *match = false; return CSS_OK; } } *match = true; return CSS_OK; } /** * Used internally in node_count_siblings() to "count" the given * sibling node. It factors out the node type and name checks. */ static int node_count_siblings_check(dom_node *dnode, bool check_name, dom_string *name) { int ret; dom_node_type type; dom_exception exc; dom_string *dnode_name; /* We flip this to 1 if/when we count this node */ ret = 0; if (dnode == NULL) { return ret; } exc = dom_node_get_node_type(dnode, &type); if ((exc != DOM_NO_ERR) || (type != DOM_ELEMENT_NODE)) { /* We only count element siblings */ return ret; } /* ... with the right name */ if (check_name) { dnode_name = NULL; exc = dom_node_get_node_name(dnode, &dnode_name); if ((exc == DOM_NO_ERR) && (dnode_name != NULL)) { if (dom_string_isequal(name, dnode_name)) { ret = 1; } dom_string_unref(dnode_name); } } else { ret = 1; } return ret; } /** * Count the given node's sibling elements * * This counts the given node's sibling elements in one direction, * either forwards or backwards, in the DOM. Keep in mind that the * libdom tree is upside-down compared to the CSS one; so "next" and * "previous" are actually reversed; the default is to count preceding * libdom siblings which correspond to subsequent CSS siblings. * * This operation is central to the CSS :first-child, :nth-child, and * :last-child (et cetera) pseudo-selectors. * * If same_name is true, then only nodes having the same * (case-sensitive) name as the given node are counted. * * \param pw Pointer to the current SVG parser state * \param node Libdom SVG node whose siblings we're counting * \param same_name Whether or not to count only siblings having * the same name as the given node * \param after Count subsequent siblings rather than precedent * ones (the default) * \param count Pointer to the return value, the number of sibling * elements * * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong */ css_error node_count_siblings(void *pw, void *node, bool same_name, bool after, int32_t *count) { UNUSED(pw); dom_exception exc; dom_node *dnode; /* node, but with the right type */ dom_string *dnode_name; dom_node *next; /* "next" sibling (depends on direction) */ /* Pointer to the "next sibling" function */ dom_exception (*next_func)(dom_node *, dom_node **); *count = 0; dnode_name = NULL; dnode = (dom_node *)node; if (same_name) { exc = dom_node_get_node_name(dnode, &dnode_name); if ((exc != DOM_NO_ERR) || (dnode_name == NULL)) { return CSS_NOMEM; } } /* Increment the reference counter for dnode for as long as * we retain a reference to it. */ dnode = dom_node_ref(dnode); next_func = dom_node_get_previous_sibling; if (after) { next_func = dom_node_get_next_sibling; } do { exc = next_func(dnode, &next); if (exc != DOM_NO_ERR) { break; } /* If next_func worked, we're about to swap "next" * with "dnode" meaning that we will no longer retain * a reference to the current dnode. */ dom_node_unref(dnode); dnode = next; *count += node_count_siblings_check(dnode, same_name, dnode_name); } while (dnode != NULL); if (dnode_name != NULL) { dom_string_unref(dnode_name); } return CSS_OK; }