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

index eb45ddf41aa53a27ec975a7d8986b1f260811896..8abaa03de863e308de1c09e3ea0d60ba3682326e 100644 (file)
@@ -6,6 +6,7 @@
 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);
 
 
 /**
@@ -136,3 +137,46 @@ 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;
+}