From: Michael Orlitzky Date: Sat, 14 Oct 2023 00:42:35 +0000 (-0400) Subject: src/svgtiny_css.c: implement node_is_root() select handler X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=d89d98c07001fc6fc54b1bd9be7eaa3ca64b6294;p=libsvgtiny.git src/svgtiny_css.c: implement node_is_root() select handler --- diff --git a/src/svgtiny_css.c b/src/svgtiny_css.c index a1f9565..80d4de1 100644 --- a/src/svgtiny_css.c +++ b/src/svgtiny_css.c @@ -42,6 +42,7 @@ static css_error node_has_attribute_suffix(void *pw, void *node, 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); /** @@ -1189,3 +1190,45 @@ css_error node_has_attribute_substring(void *pw, void *node, 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; +}