]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny_css.c: implement named_parent_node() select handler
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 12 Oct 2023 11:42:02 +0000 (07:42 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 01:37:04 +0000 (20:37 -0500)
src/svgtiny_css.c

index 8abaa03de863e308de1c09e3ea0d60ba3682326e..01b566f93c876ef5f86111b62aa089f614734930 100644 (file)
@@ -7,6 +7,8 @@ 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);
 
 
 /**
@@ -180,3 +182,49 @@ css_error node_id(void *pw, void *node, lwc_string **id)
        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;
+}