]> gitweb.michael.orlitzky.com - libsvgtiny.git/blobdiff - src/svgtiny_css.c
src/svgtiny_css.c: implement named_generic_sibling_node() select handler
[libsvgtiny.git] / src / svgtiny_css.c
index eb45ddf41aa53a27ec975a7d8986b1f260811896..750fee48d8b96849d59559394fd044b95b2f0ad1 100644 (file)
@@ -6,6 +6,13 @@
 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);
+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);
 
 
 /**
@@ -136,3 +143,269 @@ css_error node_classes(void *pw, void *node,
 
        return CSS_OK;
 }
+
+
+/**
+ * Retrieve the given node's id
+ *
+ * \param pw    Pointer to the current SVG parser state
+ * \param node  Libdom SVG node
+ * \param id    Address at which to store the id
+ *
+ * \return      CSS_OK on success, or CSS_NOMEM if anything goes wrong
+ */
+css_error node_id(void *pw, void *node, lwc_string **id)
+{
+       dom_string *attr;
+       dom_exception err;
+       struct svgtiny_parse_state *state;
+
+       /* Begin with the assumption that this node has no id */
+       *id = NULL;
+
+       state = (struct svgtiny_parse_state *)pw;
+       err = dom_element_get_attribute((dom_node *)node,
+                                       state->interned_id, &attr);
+       if (err != DOM_NO_ERR) {
+               return CSS_NOMEM;
+       }
+       else if (attr == NULL) {
+               /* The node has no id attribute and our return value
+                  is already set to NULL so we're done */
+               return CSS_OK;
+       }
+
+       /* If we found an id attribute (a dom_string), intern it into
+          an lwc_string that we can return, and then cleanup the
+          dom_string. */
+       err = dom_string_intern(attr, id);
+       if (err != DOM_NO_ERR) {
+               dom_string_unref(attr);
+               return CSS_NOMEM;
+       }
+       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;
+}
+
+
+/**
+ * Find the "next-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 immediately 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_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) {
+               /* We're looking for the first ELEMENT sibling */
+               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 found an element node, does it have the
+                        * right name? */
+                       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)) {
+                               /* The name is right, return it */
+                               *sibling = n;
+                       }
+
+                       /* There's only one next-sibling element node
+                        * and we've already found it, so if its name
+                        * wasn't right, we return the default value
+                        * of NULL below */
+                       dom_string_unref(name);
+                       dom_node_unref(n);
+                       return CSS_OK;
+               }
+
+               /* Not an element node, so we move on the the previous
+                * 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;
+}
+
+
+/**
+ * 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;
+}