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

index 2a59f6cd5adb39c1c5a8dbc41655a345ded561ac..750fee48d8b96849d59559394fd044b95b2f0ad1 100644 (file)
@@ -11,6 +11,8 @@ 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);
 
 
 /**
@@ -321,3 +323,89 @@ css_error named_sibling_node(void *pw, void *node,
        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;
+}