]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny_css.c: implement node_has_attribute_dashmatch() select handler
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 13 Oct 2023 01:00:53 +0000 (21:00 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 16:42:26 +0000 (11:42 -0500)
src/svgtiny_css.c

index 6e808475baa220cdf1d5a7f3a17200d3fe918972..5dbb83e1f7d182d2f296b6d3d8688ce468d42bb4 100644 (file)
@@ -1,4 +1,5 @@
 #include <libcss/libcss.h>
+#include <strings.h> /* strncasecmp */
 
 #include "svgtiny.h"
 #include "svgtiny_internal.h"
@@ -26,6 +27,9 @@ static css_error node_has_attribute(void *pw, void *node,
 static css_error node_has_attribute_equal(void *pw, void *node,
                const css_qname *qname, lwc_string *value,
                bool *match);
+static css_error node_has_attribute_dashmatch(void *pw, void *node,
+               const css_qname *qname, lwc_string *value,
+               bool *match);
 
 
 /**
@@ -727,3 +731,90 @@ css_error node_has_attribute_equal(void *pw, void *node,
 
        return CSS_OK;
 }
+
+
+/**
+ * Test the given node for an attribute with a specific value,
+ * possibly followed by a single hyphen
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name and value or with the given
+ * name and a value that is followed by exactly one hyphen. The
+ * comparison is case-sensitive. This corresponds to [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_dashmatch(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;
+
+       const char *vdata;   /* to hold the data underlying "value" */
+       size_t vdata_len;
+       const char *avdata;  /* to hold the found attribute value data */
+       size_t avdata_len;
+
+       /* 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);
+       if (*match) {
+               /* Exact match, we're done */
+               dom_string_unref(attr_val);
+               return CSS_OK;
+       }
+
+       /* No exact match, try it with a hyphen on the end */
+       vdata = lwc_string_data(value);     /* needle */
+       vdata_len = lwc_string_length(value);
+       avdata = dom_string_data(attr_val); /* haystack */
+       avdata_len = dom_string_byte_length(attr_val);
+       dom_string_unref(attr_val);
+
+       if (avdata_len > vdata_len && avdata[vdata_len] == '-') {
+               if (strncasecmp(avdata, vdata, vdata_len) == 0) {
+                       /* If there's a hyphen in the right position,
+                        * it suffices to compare the strings only up
+                        * to the hyphen */
+                       *match = true;
+               }
+       }
+
+       return CSS_OK;
+}