]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny_css.c: implement node_is_root() select handler
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 14 Oct 2023 00:42:35 +0000 (20:42 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 16:42:26 +0000 (11:42 -0500)
src/svgtiny_css.c

index a1f9565c4a028e5ee95f180dca93f674bbd5949e..80d4de1ef327f5fadeb3fef6d3b24c4ac81323b5 100644 (file)
@@ -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;
+}