X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fsvgtiny_css.c;h=e2af9a41d6a6402f04d56e73286aee096ff6c775;hb=c88ec5e238bb43f3832ec3c54a6d2f5c49e6076a;hp=01b566f93c876ef5f86111b62aa089f614734930;hpb=1c82bd4791dc03bca184c5517f6ff4b5bb592788;p=libsvgtiny.git diff --git a/src/svgtiny_css.c b/src/svgtiny_css.c index 01b566f..e2af9a4 100644 --- a/src/svgtiny_css.c +++ b/src/svgtiny_css.c @@ -9,6 +9,14 @@ static css_error node_classes(void *pw, void *node, 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); /** @@ -228,3 +236,321 @@ css_error named_parent_node(void *pw, void *node, 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; +}