]> gitweb.michael.orlitzky.com - libsvgtiny.git/blobdiff - src/svgtiny_css.c
src/svgtiny_css.c: use our userdata handler in set_libcss_node_data()
[libsvgtiny.git] / src / svgtiny_css.c
index ebf432d3fa6bbfeb5e0ec5aa3b67f56ab3f5dccf..075d87d139089d19469050808d085a9888265c11 100644 (file)
@@ -4,10 +4,13 @@
 #include "svgtiny.h"
 #include "svgtiny_internal.h"
 
+/* select handler callbacks */
 static css_error node_name(void *pw, void *node, css_qname *qname);
 static css_error node_classes(void *pw, void *node,
                lwc_string ***classes, uint32_t *n_classes);
 static css_error node_id(void *pw, void *node, lwc_string **id);
+static css_error named_ancestor_node(void *pw, void *node,
+               const css_qname *qname, void **ancestor);
 static css_error named_parent_node(void *pw, void *node,
                const css_qname *qname, void **parent);
 static css_error named_sibling_node(void *pw, void *node,
@@ -46,6 +49,28 @@ static css_error node_is_root(void *pw, void *node, bool *match);
 static css_error node_count_siblings(void *pw, void *node,
                bool same_name, bool after, int32_t *count);
 static css_error node_is_empty(void *pw, void *node, bool *is_empty);
+static css_error node_is_link(void *pw, void *node, bool *is_link);
+static css_error node_is_visited(void *pw, void *node, bool *is_visited);
+static css_error node_is_hover(void *pw, void *node, bool *is_hover);
+static css_error node_is_active(void *pw, void *node, bool *is_active);
+static css_error node_is_focus(void *pw, void *node, bool *is_focus);
+static css_error node_is_enabled(void *pw, void *node, bool *is_enabled);
+static css_error node_is_disabled(void *pw, void *node, bool *is_disabled);
+static css_error node_is_checked(void *pw, void *node, bool *is_checked);
+static css_error node_is_target(void *pw, void *node, bool *is_target);
+static css_error node_is_lang(void *pw, void *node,
+               lwc_string *lang, bool *is_lang);
+static css_error node_presentational_hint(void *pw, void *node,
+               uint32_t *nhints, css_hint **hints);
+static css_error ua_default_for_property(void *pw, uint32_t property,
+       css_hint *hint);
+static css_error set_libcss_node_data(void *pw, void *node,
+               void *libcss_node_data);
+static css_error get_libcss_node_data(void *pw, void *node,
+               void **libcss_node_data);
+
+/* select handler vtable */
+static struct css_select_handler svgtiny_select_handler;
 
 
 /**
@@ -221,6 +246,40 @@ css_error node_id(void *pw, void *node, lwc_string **id)
 }
 
 
+/**
+ * Find the first ancestor of the given element having the given name
+ *
+ * This function thinly wraps dom_element_named_ancestor_node(), which
+ * performs exactly the search we want.
+ *
+ * \param pw        Pointer to the current SVG parser state
+ * \param node      Libdom SVG node whose ancestor we want
+ * \param qname     Name of the ancestor node to search for
+ * \param ancestor  Address at which to store the ancestor 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 ancestor; otherwise,
+ *       NULL will be stored at the address pointed to by \a ancestor
+ */
+css_error named_ancestor_node(void *pw, void *node,
+               const css_qname *qname, void **ancestor)
+{
+       UNUSED(pw);
+
+       /* It's OK if node isn't an element, libdom checks for it. */
+       dom_element_named_ancestor_node((dom_element *)node,
+                       qname->name,
+                       (struct dom_element **)ancestor);
+
+       /* A comment in named_parent_node() explains why we unref
+        * this here. */
+       dom_node_unref(*ancestor);
+
+       return CSS_OK;
+}
+
 
 /**
  * Find the first parent of the given element having the given name
@@ -1103,29 +1162,16 @@ css_error node_has_attribute_suffix(void *pw, void *node,
 
 
 /**
- * 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)
+ * Implement node_has_attribute_substring() with optional case-
+ * insensitivity. This corresponds to [attr*=value i] in CSS and is
+ * not supported by libcss yet, but it allows us to factor out some
+ * common code.
  */
-css_error node_has_attribute_substring(void *pw, void *node,
+static css_error _node_has_attribute_substring(void *pw, void *node,
                const css_qname *qname, lwc_string *substring,
-               bool *match)
+               bool *match, bool insensitive)
 {
-       UNUSED(pw);
+       UNUSED(pw);
        dom_string *name;
        dom_string *attr_val;
        dom_exception err;
@@ -1166,7 +1212,13 @@ css_error node_has_attribute_substring(void *pw, void *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 (insensitive) {
+               *match = dom_string_caseless_lwc_isequal(attr_val, substring);
+       }
+       else {
+               *match = dom_string_lwc_isequal(attr_val, substring);
+       }
 
        /* If not, check to see if an, uh, substring matches the
         * substring */
@@ -1194,6 +1246,33 @@ css_error node_has_attribute_substring(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)
+{
+       return _node_has_attribute_substring(pw, node, qname, substring,
+                       match, false);
+}
+
 
 /**
  * Test whether or not the given node is the document's root element
@@ -1426,3 +1505,515 @@ css_error node_is_empty(void *pw, void *node, bool *is_empty)
 
        return CSS_OK;
 }
+
+
+/**
+ * Determine whether or not the given node is a link
+ *
+ * A node is a link if it is an element node whose name is "a" and if
+ * it has an "href" attribute (case-sensitive). This selector
+ * corresponds to node:link pseudo-class in CSS.
+ *
+ * This pseudo-class is a bit awkward because the two standards (HTML5
+ * and CSS) disagree on what it means, and because libsvgtiny does not
+ * have enough information to determine if a link has been "visited"
+ * yet -- that's a UI property. CSS says that :link is for unvisited
+ * links, which we can't determine. HTML5 says that each link must
+ * be either a :link or :visited. Since we can't decide either way,
+ * It seems less wrong to declare that all links are unvisited; i.e.
+ * that they match :link.
+ *
+ * \param pw          Pointer to the current SVG parser state
+ * \param node        Libdom SVG node to check
+ * \param is_link     Pointer to the boolean return value
+ *
+ * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
+ */
+css_error node_is_link(void *pw, void *node, bool *is_link)
+{
+       dom_exception exc;
+       dom_node *dnode; /* node, but with the right type */
+       dom_string *dnode_name;
+       bool has_href;
+       struct svgtiny_parse_state* state;
+
+       dnode = (dom_node *)node;
+       dnode_name = NULL;
+       has_href = false; /* assume no href attribute */
+       *is_link = false; /* assume that it's not a link */
+
+       exc = dom_node_get_node_name(dnode, &dnode_name);
+       if ((exc != DOM_NO_ERR) || (dnode_name == NULL)) {
+               return CSS_NOMEM;
+       }
+
+       state = (struct svgtiny_parse_state *)pw;
+       if (dom_string_isequal(dnode_name, state->interned_a)) {
+               exc = dom_element_has_attribute(node,
+                                       state->interned_href,
+                                       &has_href);
+               if (exc == DOM_NO_ERR && has_href) {
+                       *is_link = true;
+               }
+       }
+
+       dom_string_unref(dnode_name);
+       return CSS_OK;
+}
+
+/**
+ * Check if the given node is a link that has been visited already
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw           Pointer to the current SVG parser state; unused
+ * \param node         Libdom SVG node to check; unused
+ * \param is_visited   Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_visited(void *pw, void *node, bool *is_visited)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_visited = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is being "hovered" over
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw          Pointer to the current SVG parser state; unused
+ * \param node        Libdom SVG node to check; unused
+ * \param is_hover    Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_hover(void *pw, void *node, bool *is_hover)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_hover = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is "active"
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw          Pointer to the current SVG parser state; unused
+ * \param node        Libdom SVG node to check; unused
+ * \param is_active   Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_active(void *pw, void *node, bool *is_active)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_active = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node has the focus
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw          Pointer to the current SVG parser state; unused
+ * \param node        Libdom SVG node to check; unused
+ * \param is_focus    Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_focus(void *pw, void *node, bool *is_focus)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_focus = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is enabled
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param node          Libdom SVG node to check; unused
+ * \param is_enabled    Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_enabled(void *pw, void *node, bool *is_enabled)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_enabled = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is disabled
+ *
+ * This check always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property). Beware, until they are
+ * implemented, this is NOT the logical negation of node_is_enabled!
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param node          Libdom SVG node to check; unused
+ * \param is_disabled   Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_disabled(void *pw, void *node, bool *is_disabled)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_disabled = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Test whether or not the given node is "checked"
+ *
+ * This test always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param node          Libdom SVG node to check; unused
+ * \param is_checked    Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_checked(void *pw, void *node, bool *is_checked)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_checked = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is the "target" of the document URL
+ *
+ * This test always fails because the SVG DOM does not have the
+ * necessary information (it's a UI property).
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param node          Libdom SVG node to check; unused
+ * \param is_target     Pointer to the boolean return value
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_is_target(void *pw, void *node, bool *is_target)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *is_target = false;
+       return CSS_OK;
+}
+
+
+/**
+ * Check if the given node is the given language
+ *
+ * This test is corresponds to the CSS :lang() selector and is not
+ * fully implemented yet: it looks only for "lang" attributes on the
+ * given element and its parents, and performs a simple substring
+ * check. This results in a partial implementation of CSS Level 3 for
+ * SVG 2.0. In particular, it ignores all "xml:lang" attributes in
+ * favor of the "lang" attribute that is defined only in SVG 2.0.
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param node          Libdom SVG node to check
+ * \param lang          The language to match
+ * \param is_lang       Pointer to the boolean return value
+ *
+ * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
+ */
+static css_error node_is_lang(void *pw, void *node,
+               lwc_string *lang, bool *is_lang)
+{
+       UNUSED(pw);
+       /* SVG2 elements support both "lang" and "xml:lang"
+        * attributes; earlier versions have only the XML
+        * attribute. It would not be too hard to add support for
+        * xml:lang" here. The main difficulty standing in the way of
+        * a full Level 4 implementation is the complexity of the
+        * :lang() selector:
+        *
+        *   https://www.w3.org/TR/selectors-4/#the-lang-pseudo
+        *
+        */
+
+       css_error c_err;
+       dom_exception d_err;
+       dom_node *n; /* current node */
+       dom_node *p; /* parent node */
+       bool match;  /* retval from node_has_attribute_substring() */
+
+       /* Define the attribute name "lang" that we're looking for.
+        * We only use a css_qname here because that's what the
+        * node_has_attribute_substring() takes; the namespace
+        * portion of it is irrelevant. */
+       css_qname attr;
+       attr.ns = NULL;
+
+       if (lwc_intern_string("lang", 4, &attr.name) != lwc_error_ok) {
+               return CSS_NOMEM;
+       }
+
+       *is_lang = false; /* default to no match */
+       n = (dom_node *)node;
+
+       /* Loop through all parents of the given node looking for a
+        * substring match */
+       while (n != NULL) {
+               c_err = _node_has_attribute_substring(pw, (void *)n, &attr,
+                                               lang, &match, true);
+               if (c_err != CSS_OK) {
+                       lwc_string_destroy(attr.name);
+                       return c_err;
+               }
+               if (match) {
+                       /* matched this element; we're done */
+                       lwc_string_destroy(attr.name);
+                       *is_lang = true;
+                       return CSS_OK;
+               }
+
+               /* no match on this element, try its parent */
+               d_err = dom_node_get_parent_node(n, &p);
+               if (d_err != DOM_NO_ERR) {
+                       lwc_string_destroy(attr.name);
+                       return CSS_NOMEM;
+               }
+               n = p;
+       }
+
+       /* If we never find a match we may wind up here */
+       lwc_string_destroy(attr.name);
+       return CSS_OK;
+}
+
+
+/**
+ * Return presentational hints for the given node
+ *
+ * Unless an SVG is being rendered from within an HTML document,
+ * there are no presentational hints. We always return an empty
+ * array (no hints).
+ *
+ * \param pw        Pointer to the current SVG parser state; unused
+ * \param node      Libdom SVG node whose hints we want; unused
+ * \param nhints    How many hints are returned (return by reference)
+ * \param hints     Array of css_hint structures (return by reference)
+ *
+ * \return Always returns CSS_OK
+ */
+css_error node_presentational_hint(void *pw, void *node,
+               uint32_t *nhints, css_hint **hints)
+{
+       UNUSED(pw);
+       UNUSED(node);
+       *nhints = 0;
+       *hints = NULL;
+       return CSS_OK;
+}
+
+
+/**
+ * User-agent defaults for CSS properties
+ *
+ * For the moment, we provide no defaults, because libsvgtiny does not
+ * yet support any CSS properties that might need them.
+ *
+ * \param pw            Pointer to the current SVG parser state; unused
+ * \param property      LibCSS property identifier; unused
+ * \param hint          Pointer to hint object (a return value); unused
+ *
+ * \return Always returns CSS_INVALID
+ */
+css_error ua_default_for_property(void *pw, uint32_t property,
+       css_hint *hint)
+{
+       UNUSED(pw);
+       UNUSED(property);
+       UNUSED(hint);
+       return CSS_INVALID;
+}
+
+
+/**
+ * A "user handler" function that we pass to dom_node_set_user_data()
+ * in set_libcss_node_data(). The set_libcss_node_data() documentation
+ * says that if the node (on which data is set) is is deleted or
+ * cloned, or if its ancestors are modified, then we must call
+ * css_libcss_node_data_handler() on the user data. This function
+ * basically just checks to see which DOM event has happened and
+ * calls css_libcss_node_data_handler() when any of those criteria
+ * are met.
+ */
+static void svgtiny_dom_user_data_handler(dom_node_operation operation,
+       dom_string *key, void *data, struct dom_node *src,
+       struct dom_node *dst)
+{
+       /* We only care about the userdata that is identified by our
+        * userdata key. Unfortunately I see no obvious way to obtain
+        * the copy of the userdata key that is already interned in
+        * svgtiny_strings.h; so we duplicate it here (ugh). */
+       dom_string *str;
+       dom_string_create((const uint8_t *)"_libcss_user_data", 17, &str);
+       if (dom_string_isequal(str,key) == false || data == NULL) {
+               /* Wrong key, or no data */
+               return;
+       }
+
+       /* Check the DOM operation, and make the corresponding call to
+        * css_libcss_node_data_handler(). No error handling is done.
+        */
+       switch (operation) {
+               case DOM_NODE_CLONED:
+                       css_libcss_node_data_handler(&svgtiny_select_handler,
+                                               CSS_NODE_CLONED,
+                                               NULL, src, dst, data);
+                       break;
+               case DOM_NODE_RENAMED:
+                       css_libcss_node_data_handler(&svgtiny_select_handler,
+                                               CSS_NODE_MODIFIED,
+                                               NULL, src, NULL, data);
+                       break;
+               case DOM_NODE_IMPORTED:
+               case DOM_NODE_ADOPTED:
+               case DOM_NODE_DELETED:
+                       css_libcss_node_data_handler(&svgtiny_select_handler,
+                                               CSS_NODE_DELETED,
+                                               NULL, src, NULL, data);
+               default:
+                       /* Our list of cases should have been exhaustive */
+                       assert(false);
+       }
+}
+
+/**
+ * Store libcss data on a node
+ *
+ * This is part of the libcss select handler API that we need to
+ * implement. It is essentially a thin dom_node_set_user_data()
+ * wrapper.
+ *
+ * \param pw                 Pointer to the current SVG parser state
+ * \param node               Libdom SVG node on which to store the data
+ * \param libcss_node_data   Pointer to the data to store
+ *
+ * \return Always returns CSS_OK
+ */
+css_error set_libcss_node_data(void *pw, void *node,
+               void *libcss_node_data)
+{
+       struct svgtiny_parse_state *state;
+       void *old_data;
+
+       /* A unique "userdata key" (a string) is used to identify this
+        * data. */
+       state = (struct svgtiny_parse_state *)pw;
+       dom_node_set_user_data((dom_node *)node,
+                               state->interned_userdata_key,
+                               libcss_node_data,
+                               svgtiny_dom_user_data_handler,
+                               &old_data);
+
+       /* dom_node_set_user_data() always returns DOM_NO_ERR */
+       return CSS_OK;
+}
+
+
+/**
+ * Retrieve libcss data from a node
+ *
+ * This is part of the libcss select handler API that we need to
+ * implement. It is essentially a thin dom_node_get_user_data()
+ * wrapper.
+ *
+ * \param pw                 Pointer to the current SVG parser state
+ * \param node               Libdom SVG node from which to get the data
+ * \param libcss_node_data   Address at which to store a pointer to the data
+ *
+ * \return Always returns CSS_OK
+ */
+css_error get_libcss_node_data(void *pw, void *node,
+               void **libcss_node_data)
+{
+       struct svgtiny_parse_state *state;
+
+       /* A unique "userdata key" (a string) is used to identify this
+        * data. */
+       state = (struct svgtiny_parse_state *)pw;
+       dom_node_get_user_data((dom_node *)node,
+                               state->interned_userdata_key,
+                               libcss_node_data);
+
+       /* dom_node_get_user_data() always returns DOM_NO_ERR */
+       return CSS_OK;
+}
+
+
+/**
+ * The vtable of select handler callbacks passed by libsvgtiny to
+ * css_select_style().
+ */
+static css_select_handler svgtiny_select_handler = {
+       CSS_SELECT_HANDLER_VERSION_1,
+       node_name,
+       node_classes,
+       node_id,
+       named_ancestor_node,
+       named_parent_node,
+       named_sibling_node,
+       named_generic_sibling_node,
+       parent_node,
+       sibling_node,
+       node_has_name,
+       node_has_class,
+       node_has_id,
+       node_has_attribute,
+       node_has_attribute_equal,
+       node_has_attribute_dashmatch,
+       node_has_attribute_includes,
+       node_has_attribute_prefix,
+       node_has_attribute_suffix,
+       node_has_attribute_substring,
+       node_is_root,
+       node_count_siblings,
+       node_is_empty,
+       node_is_link,
+       node_is_visited,
+       node_is_hover,
+       node_is_active,
+       node_is_focus,
+       node_is_enabled,
+       node_is_disabled,
+       node_is_checked,
+       node_is_target,
+       node_is_lang,
+       node_presentational_hint,
+       ua_default_for_property,
+       set_libcss_node_data,
+       get_libcss_node_data,
+};