]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_css.c
src/svgtiny_css.c: implement named_sibling_node() 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 static css_error node_id(void *pw, void *node, lwc_string **id);
10 static css_error named_parent_node(void *pw, void *node,
11 const css_qname *qname, void **parent);
12 static css_error named_sibling_node(void *pw, void *node,
13 const css_qname *qname, void **sibling);
14
15
16 /**
17 * Resolve a relative URL to an absolute one by doing nothing. This is
18 * the simplest possible implementation of a URL resolver, needed for
19 * parsing CSS.
20 */
21 css_error svgtiny_resolve_url(void *pw,
22 const char *base, lwc_string *rel, lwc_string **abs)
23 {
24 UNUSED(pw);
25 UNUSED(base);
26
27 /* Copy the relative URL to the absolute one (the return
28 value) */
29 *abs = lwc_string_ref(rel);
30 return CSS_OK;
31 }
32
33 /**
34 * Create a stylesheet with the default set of params.
35 *
36 * \param sheet A stylesheet pointer, passed in by reference, that
37 * we use to store the newly-created stylesheet.
38 * \param inline_style True if this stylesheet represents an inline
39 * style, and false otherwise.
40 *
41 * \return The return value from css_stylesheet_create() is returned.
42 */
43 css_error svgtiny_create_stylesheet(css_stylesheet **sheet,
44 bool inline_style)
45 {
46 css_stylesheet_params params;
47
48 params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
49 params.level = CSS_LEVEL_DEFAULT;
50 params.charset = NULL;
51 params.url = "";
52 params.title = NULL;
53 params.allow_quirks = false;
54 params.inline_style = inline_style;
55 params.resolve = svgtiny_resolve_url;
56 params.resolve_pw = NULL;
57 params.import = NULL;
58 params.import_pw = NULL;
59 params.color = NULL;
60 params.color_pw = NULL;
61 params.font = NULL;
62 params.font_pw = NULL;
63
64 return css_stylesheet_create(&params, sheet);
65 }
66
67
68 /**************************/
69 /* libcss select handlers */
70 /**************************/
71 /*
72 * From here on we implement the "select handler "API defined in
73 * libcss's include/libcss/select.h and discussed briefly in its
74 * docs/API document.
75 */
76
77
78 /**
79 * Retrieve the given node's name
80 *
81 * \param pw Pointer to the current SVG parser state
82 * \param node Libdom SVG node
83 * \param qname Address at which to store the node name
84 *
85 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
86 */
87 css_error node_name(void *pw, void *node, css_qname *qname)
88 {
89 dom_string *name;
90 dom_exception err;
91 struct svgtiny_parse_state *state;
92
93 err = dom_node_get_node_name((dom_node *)node, &name);
94 if (err != DOM_NO_ERR) {
95 return CSS_NOMEM;
96 }
97
98 state = (struct svgtiny_parse_state *)pw;
99 qname->ns = lwc_string_ref(state->interned_svg_xmlns);
100
101 err = dom_string_intern(name, &qname->name);
102 if (err != DOM_NO_ERR) {
103 dom_string_unref(name);
104 return CSS_NOMEM;
105 }
106
107 dom_string_unref(name);
108
109 return CSS_OK;
110 }
111
112
113 /**
114 * Retrieve the given node's classes
115 *
116 * \param pw Pointer to the current SVG parser state
117 * \param node Libdom SVG node
118 * \param classes Address at which to store the class name array
119 * \param n_classes Address at which to store the length of the class
120 * name array
121 *
122 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
123 *
124 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
125 * because the underlying libdom function never fails
126 */
127 css_error node_classes(void *pw, void *node,
128 lwc_string ***classes, uint32_t *n_classes)
129 {
130 UNUSED(pw);
131 dom_exception err;
132
133 err = dom_element_get_classes((dom_node *)node, classes, n_classes);
134
135 /* The implementation does not do it, but the documentation
136 for dom_element_get_classes() says that a DOM_NO_MEM_ERR is
137 possible here, so we handle it to be on the safe side. */
138 if (err != DOM_NO_ERR) {
139 return CSS_NOMEM;
140 }
141
142 return CSS_OK;
143 }
144
145
146 /**
147 * Retrieve the given node's id
148 *
149 * \param pw Pointer to the current SVG parser state
150 * \param node Libdom SVG node
151 * \param id Address at which to store the id
152 *
153 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
154 */
155 css_error node_id(void *pw, void *node, lwc_string **id)
156 {
157 dom_string *attr;
158 dom_exception err;
159 struct svgtiny_parse_state *state;
160
161 /* Begin with the assumption that this node has no id */
162 *id = NULL;
163
164 state = (struct svgtiny_parse_state *)pw;
165 err = dom_element_get_attribute((dom_node *)node,
166 state->interned_id, &attr);
167 if (err != DOM_NO_ERR) {
168 return CSS_NOMEM;
169 }
170 else if (attr == NULL) {
171 /* The node has no id attribute and our return value
172 is already set to NULL so we're done */
173 return CSS_OK;
174 }
175
176 /* If we found an id attribute (a dom_string), intern it into
177 an lwc_string that we can return, and then cleanup the
178 dom_string. */
179 err = dom_string_intern(attr, id);
180 if (err != DOM_NO_ERR) {
181 dom_string_unref(attr);
182 return CSS_NOMEM;
183 }
184 dom_string_unref(attr);
185 return CSS_OK;
186 }
187
188
189
190 /**
191 * Find the first parent of the given element having the given name
192 *
193 * \param pw Pointer to the current SVG parser state
194 * \param node Libdom SVG node
195 * \param qname Name of the parent node to search for
196 * \param parent Address at which to store the parent node pointer
197 *
198 * \return Always returns CSS_OK
199 *
200 * \post If a suitable element is found, a pointer to it will be
201 * stored at the address pointed to by \a parent; otherwise,
202 * NULL will be stored at the address pointed to by \a parent
203 */
204 css_error named_parent_node(void *pw, void *node,
205 const css_qname *qname, void **parent)
206 {
207 UNUSED(pw);
208 /* dom_element_named_parent_node() was invented to implement
209 * this select handler so there isn't much for us to do except
210 * call it. It's OK if node isn't an element, libdom checks
211 * for it. */
212 dom_element_named_parent_node((dom_element *)node,
213 qname->name,
214 (struct dom_element **)parent);
215
216 /* Implementation detail: dom_element_named_parent_node()
217 * increments the reference count of the parent element before
218 * returning it to us. According to docs/RefCnt in the libdom
219 * repository, this will prevent the parent element from being
220 * destroyed if it is pruned from the DOM. That sounds good,
221 * since we don't want to be using a pointer to an object that
222 * has been destroyed... but we also have no way of later
223 * decrementing the reference count ourselves, and don't want
224 * to make the returned node eternal. Decrementing the
225 * reference counter now allows it to be destroyed when the
226 * DOM no longer needs it, and so long as no other parts of
227 * libsvgtiny are messing with the DOM during parsing, that
228 * shouldn't (ha ha) cause any problems. */
229 dom_node_unref(*parent);
230
231 return CSS_OK;
232 }
233
234
235 /**
236 * Find the "next-sibling" of the given element having the given name
237 *
238 * This search corresponds to the "+ foo" combinator in CSS and will
239 * find only "foo" element nodes that immediately precede the given
240 * node under the same parent in the DOM. In CSS the tree is viewed
241 * top-down and in libdom it is viewed from the bottom-up; as a result
242 * "next" and "previous" are sometimes backwards. This is case-sensitive.
243 *
244 * \param pw Pointer to the current SVG parser state
245 * \param node Libdom SVG node
246 * \param qname Name of the sibling node to search for
247 * \param sibling Address at which to store the sibling node pointer
248 *
249 * \return Always returns CSS_OK
250 *
251 * \post If a suitable element is found, a pointer to it will be
252 * stored at the address pointed to by \a sibling; otherwise,
253 * NULL will be stored at the address pointed to by \a sibling
254 */
255 css_error named_sibling_node(void *pw, void *node,
256 const css_qname *qname, void **sibling)
257 {
258 UNUSED(pw);
259 dom_node *n = node; /* the current node */
260 dom_node *prev; /* the previous node */
261 dom_exception err;
262 dom_node_type type;
263 dom_string *name;
264
265 *sibling = NULL; /* default to nothing found */
266
267 /* Begin the search; the first iteration we do outside of the
268 * loop. Implementation detil: dom_node_get_previous_sibling()
269 * increments the reference counter on the returned node. A
270 * comment within named_parent_node() explains why we
271 * decrement it ASAP. */
272 err = dom_node_get_previous_sibling(n, &n);
273 if (err != DOM_NO_ERR) {
274 return CSS_OK;
275 }
276
277 while (n != NULL) {
278 /* We're looking for the first ELEMENT sibling */
279 err = dom_node_get_node_type(n, &type);
280 if (err != DOM_NO_ERR) {
281 dom_node_unref(n);
282 return CSS_OK;
283 }
284
285 if (type == DOM_ELEMENT_NODE) {
286 /* We found an element node, does it have the
287 * right name? */
288 err = dom_node_get_node_name(n, &name);
289 if (err != DOM_NO_ERR) {
290 dom_node_unref(n);
291 return CSS_OK;
292 }
293
294 if (dom_string_lwc_isequal(name,
295 qname->name)) {
296 /* The name is right, return it */
297 *sibling = n;
298 }
299
300 /* There's only one next-sibling element node
301 * and we've already found it, so if its name
302 * wasn't right, we return the default value
303 * of NULL below */
304 dom_string_unref(name);
305 dom_node_unref(n);
306 return CSS_OK;
307 }
308
309 /* Not an element node, so we move on the the previous
310 * previous sibling */
311 err = dom_node_get_previous_sibling(n, &prev);
312 if (err != DOM_NO_ERR) {
313 dom_node_unref(n);
314 return CSS_OK;
315 }
316
317 dom_node_unref(n);
318 n = prev;
319 }
320
321 return CSS_OK;
322 }
323