From: Michael Orlitzky Date: Thu, 12 Oct 2023 02:25:00 +0000 (-0400) Subject: src/svgtiny_css.c: implement node_id() select handler X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=8a193eb5807addc9e89b5804360d1cdc72cb1889;p=libsvgtiny.git src/svgtiny_css.c: implement node_id() select handler --- diff --git a/src/svgtiny_css.c b/src/svgtiny_css.c index eb45ddf..8abaa03 100644 --- a/src/svgtiny_css.c +++ b/src/svgtiny_css.c @@ -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; +}