From 5f03df7bcecdde39d1199cca6bc1bea85ff9650f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 12 Oct 2023 19:42:45 -0400 Subject: [PATCH] src/svgtiny_css.c: implement node_has_attribute_equal() select handler --- src/svgtiny_css.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/svgtiny_css.c b/src/svgtiny_css.c index 0267325..6e80847 100644 --- a/src/svgtiny_css.c +++ b/src/svgtiny_css.c @@ -23,6 +23,9 @@ static css_error node_has_id(void *pw, void *node, lwc_string *name, bool *match); static css_error node_has_attribute(void *pw, void *node, const css_qname *qname, bool *match); +static css_error node_has_attribute_equal(void *pw, void *node, + const css_qname *qname, lwc_string *value, + bool *match); /** @@ -665,3 +668,62 @@ css_error node_has_attribute(void *pw, void *node, dom_string_unref(name); return CSS_OK; } + + +/** + * Test the given node for an attribute with a specific value + * + * This will return true (via the "match" pointer) if the libdom node + * has an attribute with the given name and value. The comparison is + * case-sensitive. It corresponds to node[attr=value] in CSS. + * + * \param pw Pointer to the current SVG parser state + * \param node Libdom SVG node to test + * \param qname Attribute name to check for + * \param value Attribute value to check for + * \param match Pointer to the test result + * + * \return Returns CSS_OK if successful and CSS_NOMEM if we cannot + * intern the attribute name (which usually indicates memory + * exhaustion) + */ +css_error node_has_attribute_equal(void *pw, void *node, + const css_qname *qname, lwc_string *value, + bool *match) +{ + /* Implementation note: NetSurf always returns "no match" when + * the value is empty (length zero). We allow it, because why + * not? */ + + UNUSED(pw); + dom_string *name; + dom_string *attr_val; + dom_exception err; + + /* Intern the attribute name as a dom_string so we can + * use dom_element_get_attribute() */ + err = dom_string_create_interned( + (const uint8_t *) lwc_string_data(qname->name), + lwc_string_length(qname->name), + &name); + if (err != DOM_NO_ERR) { + return CSS_NOMEM; + } + + err = dom_element_get_attribute((dom_node *)node, name, &attr_val); + if ((err != DOM_NO_ERR) || (attr_val == NULL)) { + /* There was an error getting the attribute's value or + * the attribute doesn't exist. So, no match? */ + dom_string_unref(name); + *match = false; + return CSS_OK; + } + + /* Otherwise, we have the attribute value from the given node + * and all we need to do is compare. */ + dom_string_unref(name); + *match = dom_string_lwc_isequal(attr_val, value); + dom_string_unref(attr_val); + + return CSS_OK; +} -- 2.44.2