]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny_css.c: implement node_has_name() select handler
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 12 Oct 2023 22:10:05 +0000 (18:10 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 20 Nov 2023 16:42:26 +0000 (11:42 -0500)
src/svgtiny_css.c

index c7faffddef88e447fc1618805cbb450050ff955a..e2af9a41d6a6402f04d56e73286aee096ff6c775 100644 (file)
@@ -15,6 +15,8 @@ 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);
 
 
 /**
@@ -504,3 +506,51 @@ css_error sibling_node(void *pw, void *node, void **sibling)
 
        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;
+}