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

index 80d4de1ef327f5fadeb3fef6d3b24c4ac81323b5..ffc9c3e6ce1baf18d6f462c40752fd64b818a3a0 100644 (file)
@@ -43,6 +43,8 @@ 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);
+static css_error node_count_siblings(void *pw, void *node,
+               bool same_name, bool after, int32_t *count);
 
 
 /**
@@ -1232,3 +1234,132 @@ css_error node_is_root(void *pw, void *node, bool *match)
        *match = true;
        return CSS_OK;
 }
+
+
+/**
+ * Used internally in node_count_siblings() to "count" the given
+ * sibling node. It factors out the node type and name checks.
+ */
+static int node_count_siblings_check(dom_node *dnode,
+               bool check_name,
+               dom_string *name)
+{
+       int ret;
+       dom_node_type type;
+       dom_exception exc;
+       dom_string *dnode_name;
+
+       /* We flip this to 1 if/when we count this node */
+       ret = 0;
+
+       if (dnode == NULL) {
+               return ret;
+       }
+
+       exc = dom_node_get_node_type(dnode, &type);
+       if ((exc != DOM_NO_ERR) || (type != DOM_ELEMENT_NODE)) {
+               /* We only count element siblings */
+               return ret;
+       }
+
+       /* ... with the right name */
+       if (check_name) {
+               dnode_name = NULL;
+               exc = dom_node_get_node_name(dnode, &dnode_name);
+
+               if ((exc == DOM_NO_ERR) && (dnode_name != NULL)) {
+                       if (dom_string_isequal(name,
+                                       dnode_name)) {
+                                       ret = 1;
+                       }
+                       dom_string_unref(dnode_name);
+               }
+       }
+       else {
+               ret = 1;
+       }
+
+       return ret;
+}
+
+/**
+ * Count the given node's sibling elements
+ *
+ * This counts the given node's sibling elements in one direction,
+ * either forwards or backwards, in the DOM. Keep in mind that the
+ * libdom tree is upside-down compared to the CSS one; so "next" and
+ * "previous" are actually reversed; the default is to count preceding
+ * libdom siblings which correspond to subsequent CSS siblings.
+ *
+ * This operation is central to the CSS :first-child, :nth-child, and
+ * :last-child (et cetera) pseudo-selectors.
+ *
+ * If same_name is true, then only nodes having the same
+ * (case-sensitive) name as the given node are counted.
+ *
+ * \param pw          Pointer to the current SVG parser state
+ * \param node        Libdom SVG node whose siblings we're counting
+ * \param same_name   Whether or not to count only siblings having
+ *                    the same name as the given node
+ * \param after       Count subsequent siblings rather than precedent
+ *                    ones (the default)
+ * \param count       Pointer to the return value, the number of sibling
+ *                    elements
+ *
+ * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
+ */
+css_error node_count_siblings(void *pw, void *node,
+               bool same_name, bool after, int32_t *count)
+{
+       UNUSED(pw);
+       dom_exception exc;
+       dom_node *dnode; /* node, but with the right type */
+       dom_string *dnode_name;
+       dom_node *next; /* "next" sibling (depends on direction) */
+
+       /* Pointer to the "next sibling" function */
+       dom_exception (*next_func)(dom_node *, dom_node **);
+
+       *count = 0;
+
+       dnode_name = NULL;
+       dnode = (dom_node *)node;
+       if (same_name) {
+               exc = dom_node_get_node_name(dnode, &dnode_name);
+               if ((exc != DOM_NO_ERR) || (dnode_name == NULL)) {
+                       return CSS_NOMEM;
+               }
+       }
+
+       /* Increment the reference counter for dnode for as long as
+        * we retain a reference to it. */
+       dnode = dom_node_ref(dnode);
+
+       next_func = dom_node_get_previous_sibling;
+       if (after) {
+               next_func = dom_node_get_next_sibling;
+       }
+
+       do {
+               exc = next_func(dnode, &next);
+               if (exc != DOM_NO_ERR) {
+                       break;
+               }
+
+               /* If next_func worked, we're about to swap "next"
+                * with "dnode" meaning that we will no longer retain
+                * a reference to the current dnode. */
+               dom_node_unref(dnode);
+               dnode = next;
+
+               *count += node_count_siblings_check(dnode,
+                                       same_name,
+                                       dnode_name);
+       } while (dnode != NULL);
+
+       if (dnode_name != NULL) {
+               dom_string_unref(dnode_name);
+       }
+
+       return CSS_OK;
+}