]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement node_classes() select handler
[libsvgtiny.git] / src / svgtiny_css.c
1 #include <libcss/libcss.h>
2
3 #include "svgtiny.h"
4 #include "svgtiny_internal.h"
5
6 static css_error node_name(void *pw, void *node, css_qname *qname);
7 static css_error node_classes(void *pw, void *node,
8 lwc_string ***classes, uint32_t *n_classes);
9
10
11 /**
12 * Resolve a relative URL to an absolute one by doing nothing. This is
13 * the simplest possible implementation of a URL resolver, needed for
14 * parsing CSS.
15 */
16 css_error svgtiny_resolve_url(void *pw,
17 const char *base, lwc_string *rel, lwc_string **abs)
18 {
19 UNUSED(pw);
20 UNUSED(base);
21
22 /* Copy the relative URL to the absolute one (the return
23 value) */
24 *abs = lwc_string_ref(rel);
25 return CSS_OK;
26 }
27
28 /**
29 * Create a stylesheet with the default set of params.
30 *
31 * \param sheet A stylesheet pointer, passed in by reference, that
32 * we use to store the newly-created stylesheet.
33 * \param inline_style True if this stylesheet represents an inline
34 * style, and false otherwise.
35 *
36 * \return The return value from css_stylesheet_create() is returned.
37 */
38 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
39 bool inline_style)
40 {
41 css_stylesheet_params params;
42
43 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
44 params.level = CSS_LEVEL_DEFAULT;
45 params.charset = NULL;
46 params.url = "";
47 params.title = NULL;
48 params.allow_quirks = false;
49 params.inline_style = inline_style;
50 params.resolve = svgtiny_resolve_url;
51 params.resolve_pw = NULL;
52 params.import = NULL;
53 params.import_pw = NULL;
54 params.color = NULL;
55 params.color_pw = NULL;
56 params.font = NULL;
57 params.font_pw = NULL;
58
59 return css_stylesheet_create(&params, sheet);
60 }
61
62
63 /**************************/
64 /* libcss select handlers */
65 /**************************/
66 /*
67 * From here on we implement the "select handler "API defined in
68 * libcss's include/libcss/select.h and discussed briefly in its
69 * docs/API document.
70 */
71
72
73 /**
74 * Retrieve the given node's name
75 *
76 * \param pw Pointer to the current SVG parser state
77 * \param node Libdom SVG node
78 * \param qname Address at which to store the node name
79 *
80 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
81 */
82 css_error node_name(void *pw, void *node, css_qname *qname)
83 {
84 dom_string *name;
85 dom_exception err;
86 struct svgtiny_parse_state *state;
87
88 err = dom_node_get_node_name((dom_node *)node, &name);
89 if (err != DOM_NO_ERR) {
90 return CSS_NOMEM;
91 }
92
93 state = (struct svgtiny_parse_state *)pw;
94 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
95
96 err = dom_string_intern(name, &qname->name);
97 if (err != DOM_NO_ERR) {
98 dom_string_unref(name);
99 return CSS_NOMEM;
100 }
101
102 dom_string_unref(name);
103
104 return CSS_OK;
105 }
106
107
108 /**
109 * Retrieve the given node's classes
110 *
111 * \param pw Pointer to the current SVG parser state
112 * \param node Libdom SVG node
113 * \param classes Address at which to store the class name array
114 * \param n_classes Address at which to store the length of the class
115 * name array
116 *
117 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
118 *
119 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
120 * because the underlying libdom function never fails
121 */
122 css_error node_classes(void *pw, void *node,
123 lwc_string ***classes, uint32_t *n_classes)
124 {
125 UNUSED(pw);
126 dom_exception err;
127
128 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
129
130 /* The implementation does not do it, but the documentation
131 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
132 possible here, so we handle it to be on the safe side. */
133 if (err != DOM_NO_ERR) {
134 return CSS_NOMEM;
135 }
136
137 return CSS_OK;
138 }