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

index 5dbb83e1f7d182d2f296b6d3d8688ce468d42bb4..3b03d9148273bac2a5bbe97597b514fb60f30a65 100644 (file)
@@ -30,6 +30,9 @@ static css_error node_has_attribute_equal(void *pw, void *node,
 static css_error node_has_attribute_dashmatch(void *pw, void *node,
                const css_qname *qname, lwc_string *value,
                bool *match);
+static css_error node_has_attribute_includes(void *pw, void *node,
+               const css_qname *qname, lwc_string *word,
+               bool *match);
 
 
 /**
@@ -818,3 +821,93 @@ css_error node_has_attribute_dashmatch(void *pw, void *node,
 
        return CSS_OK;
 }
+
+
+/**
+ * Test the given node for an attribute whose value is a
+ * space-separated list of words, one of which is the given word
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name and whose value when
+ * considered as a space-separated list of words contains the given
+ * word. 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 word     Value word 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_includes(void *pw, void *node,
+               const css_qname *qname, lwc_string *word,
+               bool *match)
+{
+       UNUSED(pw);
+
+       dom_string *name;
+       dom_string *attr_val;
+       dom_exception err;
+       size_t wordlen; /* length of "word" */
+
+       /* pointers used to parse a space-separated list of words */
+       const char *p;
+       const char *start;
+       const char *end;
+
+       *match = false; /* default to no match */
+
+       wordlen = lwc_string_length(word);
+       if (wordlen == 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);
+               return CSS_OK;
+       }
+
+       /* Parse the list comparing each word against "word" */
+       start = dom_string_data(attr_val);
+       end = start + dom_string_byte_length(attr_val);
+       dom_string_unref(attr_val);
+
+       for (p = start; p <= end; p++) {
+               /* Move forward until we find the end of the first word */
+               if (*p == ' ' || *p == '\0') {
+                       /* If the length of that word is the length of the
+                        * word we're looking for, do the comparison. */
+                       if ((size_t) (p - start) == wordlen &&
+                               strncasecmp(start,
+                                       lwc_string_data(word),
+                                       wordlen) == 0) {
+                               *match = true;
+                               break;
+                       }
+                       /* No match? Set "start" to the beginning of
+                        * the next word and loop. */
+                       start = p + 1;
+               }
+       }
+
+       return CSS_OK;
+}