]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny_css.c: implement node_has_attribute_substring() select handler
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 14 Oct 2023 00:22:49 +0000 (20:22 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 16:42:26 +0000 (11:42 -0500)
src/svgtiny_css.c

index 246309867a691f87b51e9143f17652c2350848f0..a1f9565c4a028e5ee95f180dca93f674bbd5949e 100644 (file)
@@ -39,6 +39,9 @@ static css_error node_has_attribute_prefix(void *pw, void *node,
 static css_error node_has_attribute_suffix(void *pw, void *node,
                const css_qname *qname, lwc_string *suffix,
                bool *match);
+static css_error node_has_attribute_substring(void *pw, void *node,
+               const css_qname *qname, lwc_string *substring,
+               bool *match);
 
 
 /**
@@ -1093,3 +1096,96 @@ css_error node_has_attribute_suffix(void *pw, void *node,
 
        return CSS_OK;
 }
+
+
+/**
+ * Test the given node for an attribute whose value contains the
+ * given substring
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name and whose value contains the
+ * given substring. 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 substring   Value substring 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_substring(void *pw, void *node,
+               const css_qname *qname, lwc_string *substring,
+               bool *match)
+{
+       UNUSED(pw);
+       dom_string *name;
+       dom_string *attr_val;
+       dom_exception err;
+       size_t attr_len;  /* length of attr_val */
+       size_t substrlen; /* length of "substring" */
+
+       /* Convenience pointers we use when comparing substrings */
+       const char *p;
+       const char *p_max;
+
+       substrlen = lwc_string_length(substring);
+       if (substrlen == 0) {
+               /* In this case, the spec says that "if 'val' is the
+                * empty string, it will never represent anything." */
+               return CSS_OK;
+       }
+
+       /* 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 the first thing we want to do is check to see if the
+        * whole thing matches the substring. */
+       dom_string_unref(name);
+       *match = dom_string_lwc_isequal(attr_val, substring);
+
+       /* If not, check to see if an, uh, substring matches the
+        * substring */
+       if (*match == false) {
+               p = dom_string_data(attr_val);
+
+               /* Check every long-enough suffix for a prefix match */
+               attr_len = dom_string_byte_length(attr_val);
+               if (attr_len >= substrlen) {
+                       p_max = p + attr_len - substrlen;
+                       while (p <= p_max) {
+                               if (strncasecmp(p,
+                                               lwc_string_data(substring),
+                                               substrlen) == 0) {
+                                       *match = true;
+                                       break;
+                               }
+                               p++;
+                       }
+               }
+       }
+
+       dom_string_unref(attr_val);
+
+       return CSS_OK;
+}