]> gitweb.michael.orlitzky.com - libsvgtiny.git/blobdiff - src/svgtiny_css.c
src/svgtiny_css.c: implement node_has_attribute_suffix() select handler
[libsvgtiny.git] / src / svgtiny_css.c
index 6d345a753dacf4764cb3353ce983a1a8e651241c..246309867a691f87b51e9143f17652c2350848f0 100644 (file)
@@ -1,4 +1,5 @@
 #include <libcss/libcss.h>
+#include <strings.h> /* strncasecmp */
 
 #include "svgtiny.h"
 #include "svgtiny_internal.h"
@@ -19,6 +20,25 @@ static css_error node_has_name(void *pw, void *node,
                const css_qname *qname, bool *match);
 static css_error node_has_class(void *pw, void *node,
                lwc_string *name, bool *match);
+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);
+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);
+static css_error node_has_attribute_prefix(void *pw, void *node,
+               const css_qname *qname, lwc_string *prefix,
+               bool *match);
+static css_error node_has_attribute_suffix(void *pw, void *node,
+               const css_qname *qname, lwc_string *suffix,
+               bool *match);
 
 
 /**
@@ -580,3 +600,496 @@ css_error node_has_class(void *pw, void *node,
        dom_element_has_class((dom_node *)node, name, match);
        return CSS_OK;
 }
+
+
+/**
+ * Test the given node for the given id
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has the given id. The comparison is case-sensitive. It corresponds
+ * to node#id in CSS.
+ *
+ * \param pw       Pointer to the current SVG parser state
+ * \param node     Libdom SVG node to test
+ * \param name     Id to check for
+ * \param match    Pointer to the test result
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_has_id(void *pw, void *node,
+               lwc_string *name, bool *match)
+{
+       dom_string *attr;
+       dom_exception err;
+       struct svgtiny_parse_state *state;
+
+       attr = NULL;    /* a priori the "id" attribute may not exist */
+       *match = false; /* default to no match */
+
+       state = (struct svgtiny_parse_state *)pw;
+       err = dom_element_get_attribute((dom_node *)node,
+                               state->interned_id, &attr);
+       if (err != DOM_NO_ERR || attr == NULL) {
+               return CSS_OK;
+       }
+
+       *match = dom_string_lwc_isequal(attr, name);
+       dom_string_unref(attr);
+
+       return CSS_OK;
+}
+
+
+/**
+ * Test the given node for the given attribute
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name. The comparison is
+ * case-sensitive. It corresponds to node[attr] 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 match    Pointer to the test result
+ *
+ * \return Returns CSS_OK if successful and CSS_NOMEM if anything
+ *         goes wrong
+ */
+css_error node_has_attribute(void *pw, void *node,
+               const css_qname *qname, bool *match)
+{
+       UNUSED(pw);
+       dom_string *name;
+       dom_exception err;
+
+       /* intern the attribute name as a dom_string so we can
+        * delegate to dom_element_has_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_has_attribute((dom_node *)node, name, match);
+       if (err != DOM_NO_ERR) {
+               dom_string_unref(name);
+               return CSS_OK;
+       }
+
+       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;
+}
+
+
+/**
+ * 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;
+}
+
+
+/**
+ * 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;
+}
+
+
+/**
+ * Test the given node for an attribute whose value begins with the
+ * given prefix
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name and whose value begins with
+ * the given prefix string. 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 prefix   Value prefix 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_prefix(void *pw, void *node,
+               const css_qname *qname, lwc_string *prefix,
+               bool *match)
+{
+       UNUSED(pw);
+       dom_string *name;
+       dom_string *attr_val;
+       dom_exception err;
+       const char *avdata; /* attribute value data */
+       size_t avdata_len; /* length of that attribute value data */
+       size_t prefixlen; /* length of "prefix" */
+
+       prefixlen = lwc_string_length(prefix);
+       if (prefixlen == 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 prefix. */
+       dom_string_unref(name);
+       *match = dom_string_lwc_isequal(attr_val, prefix);
+
+       /* If not, check to see if an, uh, prefix matches the
+        * prefix */
+       if (*match == false) {
+               avdata = dom_string_data(attr_val);
+               avdata_len = dom_string_byte_length(attr_val);
+               if ((avdata_len >= prefixlen) &&
+                               (strncasecmp(avdata,
+                                       lwc_string_data(prefix),
+                                       prefixlen) == 0)) {
+                       /* Use strncasecmp to compare only the first
+                        * "n" characters, where "n" is the length of
+                        * the prefix. */
+                       *match = true;
+               }
+       }
+
+       dom_string_unref(attr_val);
+
+       return CSS_OK;
+}
+
+
+/**
+ * Test the given node for an attribute whose value end with the
+ * given suffix
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has an attribute with the given name and whose value ends with
+ * the given suffix string. 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 suffix   Value suffix 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_suffix(void *pw, void *node,
+               const css_qname *qname, lwc_string *suffix,
+               bool *match)
+{
+       UNUSED(pw);
+       dom_string *name;
+       dom_string *attr_val;
+       dom_exception err;
+       const char *avdata; /* attribute value data */
+       size_t avdata_len; /* length of that attribute value data */
+       size_t suffixlen; /* length of "suffix" */
+
+       /* convenience pointer we'll use when matching the suffix */
+       const char *suffix_start;
+
+       suffixlen = lwc_string_length(suffix);
+       if (suffixlen == 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 suffix. */
+       dom_string_unref(name);
+       *match = dom_string_lwc_isequal(attr_val, suffix);
+
+       /* If not, check to see if an, uh, suffix matches the
+        * suffix */
+       if (*match == false) {
+               avdata = dom_string_data(attr_val);
+               avdata_len = dom_string_byte_length(attr_val);
+
+               suffix_start = (char *)(avdata + avdata_len - suffixlen);
+
+               if ((avdata_len >= suffixlen) &&
+                               (strncasecmp(suffix_start,
+                                       lwc_string_data(suffix),
+                                       suffixlen) == 0)) {
+                       /* Use strncasecmp to compare only the last
+                        * "n" characters, where "n" is the length of
+                        * the suffix. */
+                       *match = true;
+               }
+       }
+
+       dom_string_unref(attr_val);
+
+       return CSS_OK;
+}