]> gitweb.michael.orlitzky.com - libsvgtiny.git/blobdiff - src/svgtiny_css.c
src/svgtiny_css.c: implement node_has_attribute_equal() select handler
[libsvgtiny.git] / src / svgtiny_css.c
index 76ee4ccc60b3adf722bf62d6a8773fcf5caba5cc..6e808475baa220cdf1d5a7f3a17200d3fe918972 100644 (file)
@@ -14,6 +14,18 @@ static css_error named_sibling_node(void *pw, void *node,
 static css_error named_generic_sibling_node(void *pw, void *node,
                const css_qname *qname, void **sibling);
 static css_error parent_node(void *pw, void *node, void **parent);
+static css_error sibling_node(void *pw, void *node, void **sibling);
+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);
 
 
 /**
@@ -433,3 +445,285 @@ css_error parent_node(void *pw, void *node, void **parent)
 
        return CSS_OK;
 }
+
+
+/**
+ * Find the "next-sibling" of the given element
+ *
+ * This search corresponds "+ *" in CSS and will find the first
+ * element node that immediately precedes the given node under the
+ * same parent in the DOM. In CSS the tree is viewed top-down and in
+ * libdom it is viewed from the bottom-up; as a result "next" and
+ * "previous" are sometimes backwards.
+ *
+ * \param pw       Pointer to the current SVG parser state
+ * \param node     Libdom SVG node
+ * \param sibling  Address at which to store the sibling node pointer
+ *
+ * \return Always returns CSS_OK
+ *
+ * \post If a suitable element is found, a pointer to it will be
+ *       stored at the address pointed to by \a sibling; otherwise,
+ *       NULL will be stored at the address pointed to by \a sibling
+ */
+css_error sibling_node(void *pw, void *node, void **sibling)
+{
+       UNUSED(pw);
+       dom_node *n = node; /* the current node */
+       dom_node *prev;     /* the previous node */
+       dom_exception err;
+       dom_node_type type;
+
+       *sibling = NULL; /* default to nothing found */
+
+       /* Begin the search; the first iteration we do outside of the
+        * loop. Implementation detil: dom_node_get_previous_sibling()
+        * increments the reference counter on the returned node. A
+        * comment within named_parent_node() explains why we
+        * decrement it ASAP. */
+       err = dom_node_get_previous_sibling(n, &n);
+       if (err != DOM_NO_ERR) {
+               return CSS_OK;
+       }
+
+       while (n != NULL) {
+               err = dom_node_get_node_type(n, &type);
+               if (err != DOM_NO_ERR) {
+                       dom_node_unref(n);
+                       return CSS_OK;
+               }
+
+               if (type == DOM_ELEMENT_NODE) {
+                       /* We found a sibling node that is also an
+                          element and that's all we wanted. */
+                       *sibling = n;
+                       dom_node_unref(n);
+                       return CSS_OK;
+               }
+
+               /* This sibling node was not an element; move on to
+                  the previous sibling */
+               err = dom_node_get_previous_sibling(n, &prev);
+               if (err != DOM_NO_ERR) {
+                       dom_node_unref(n);
+                       return CSS_OK;
+               }
+
+               dom_node_unref(n);
+               n = prev;
+       }
+
+       return CSS_OK;
+}
+
+
+/**
+ * Test the given node for the given name
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has the given name or if that name is the universal selector;
+ * otherwise it returns false. The comparison is case-sensitive. It
+ * corresponds to a rule like "body { ... }" in CSS.
+ *
+ * \param pw       Pointer to the current SVG parser state
+ * \param node     Libdom SVG node to test
+ * \param qname    Name to check for
+ * \param match    Pointer to the test result
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_has_name(void *pw, void *node,
+               const css_qname *qname, bool *match)
+{
+       struct svgtiny_parse_state *state;
+       dom_string *name;
+       dom_exception err;
+
+       /* Start by checking to see if qname is the universal selector */
+       state = (struct svgtiny_parse_state *)pw;
+       if (lwc_string_isequal(qname->name,
+                       state->interned_universal, match) == lwc_error_ok) {
+         if (*match) {
+               /* It's the universal selector. In NetSurf, all node
+                * names match the universal selector, and nothing in
+                * the libcss documentation suggests another approach,
+                * so we follow NetSurf here. */
+               return CSS_OK;
+         }
+       }
+
+       err = dom_node_get_node_name((dom_node *)node, &name);
+       if (err != DOM_NO_ERR) {
+         return CSS_OK;
+       }
+
+       /* Unlike with HTML, SVG element names are case-sensitive */
+       *match = dom_string_lwc_isequal(name, qname->name);
+       dom_string_unref(name);
+
+       return CSS_OK;
+}
+
+
+/**
+ * Test the given node for the given class
+ *
+ * This will return true (via the "match" pointer) if the libdom node
+ * has the given class. The comparison is case-sensitive. It
+ * corresponds to node.class in CSS.
+ *
+ * \param pw       Pointer to the current SVG parser state
+ * \param node     Libdom SVG node to test
+ * \param name     Class name to check for
+ * \param match    Pointer to the test result
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_has_class(void *pw, void *node,
+               lwc_string *name, bool *match)
+{
+       UNUSED(pw);
+       /* libdom implements this for us and apparently it cannot fail */
+       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;
+}