1 #include <libcss/libcss.h>
4 #include "svgtiny_internal.h"
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
);
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
21 css_error
svgtiny_resolve_url(void *pw
,
22 const char *base
, lwc_string
*rel
, lwc_string
**abs
)
27 /* Copy the relative URL to the absolute one (the return
29 *abs
= lwc_string_ref(rel
);
34 * Create a stylesheet with the default set of params.
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.
41 * \return The return value from css_stylesheet_create() is returned.
43 css_error
svgtiny_create_stylesheet(css_stylesheet
**sheet
,
46 css_stylesheet_params params
;
48 params
.params_version
= CSS_STYLESHEET_PARAMS_VERSION_1
;
49 params
.level
= CSS_LEVEL_DEFAULT
;
50 params
.charset
= NULL
;
53 params
.allow_quirks
= false;
54 params
.inline_style
= inline_style
;
55 params
.resolve
= svgtiny_resolve_url
;
56 params
.resolve_pw
= NULL
;
58 params
.import_pw
= NULL
;
60 params
.color_pw
= NULL
;
62 params
.font_pw
= NULL
;
64 return css_stylesheet_create(¶ms
, sheet
);
68 /**************************/
69 /* libcss select handlers */
70 /**************************/
72 * From here on we implement the "select handler "API defined in
73 * libcss's include/libcss/select.h and discussed briefly in its
79 * Retrieve the given node's name
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
85 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
87 css_error
node_name(void *pw
, void *node
, css_qname
*qname
)
91 struct svgtiny_parse_state
*state
;
93 err
= dom_node_get_node_name((dom_node
*)node
, &name
);
94 if (err
!= DOM_NO_ERR
) {
98 state
= (struct svgtiny_parse_state
*)pw
;
99 qname
->ns
= lwc_string_ref(state
->interned_svg_xmlns
);
101 err
= dom_string_intern(name
, &qname
->name
);
102 if (err
!= DOM_NO_ERR
) {
103 dom_string_unref(name
);
107 dom_string_unref(name
);
114 * Retrieve the given node's classes
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
122 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
124 * \note CSS_NOMEM is not possible in practice as of libdom-0.4.1,
125 * because the underlying libdom function never fails
127 css_error
node_classes(void *pw
, void *node
,
128 lwc_string
***classes
, uint32_t *n_classes
)
133 err
= dom_element_get_classes((dom_node
*)node
, classes
, n_classes
);
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
) {
147 * Retrieve the given node's id
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
153 * \return CSS_OK on success, or CSS_NOMEM if anything goes wrong
155 css_error
node_id(void *pw
, void *node
, lwc_string
**id
)
159 struct svgtiny_parse_state
*state
;
161 /* Begin with the assumption that this node has no id */
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
) {
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 */
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
179 err
= dom_string_intern(attr
, id
);
180 if (err
!= DOM_NO_ERR
) {
181 dom_string_unref(attr
);
184 dom_string_unref(attr
);
191 * Find the first parent of the given element having the given name
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
198 * \return Always returns CSS_OK
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
204 css_error
named_parent_node(void *pw
, void *node
,
205 const css_qname
*qname
, void **parent
)
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
212 dom_element_named_parent_node((dom_element
*)node
,
214 (struct dom_element
**)parent
);
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
);
236 * Find the "next-sibling" of the given element having the given name
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.
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
249 * \return Always returns CSS_OK
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
255 css_error
named_sibling_node(void *pw
, void *node
,
256 const css_qname
*qname
, void **sibling
)
259 dom_node
*n
= node
; /* the current node */
260 dom_node
*prev
; /* the previous node */
265 *sibling
= NULL
; /* default to nothing found */
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
) {
278 /* We're looking for the first ELEMENT sibling */
279 err
= dom_node_get_node_type(n
, &type
);
280 if (err
!= DOM_NO_ERR
) {
285 if (type
== DOM_ELEMENT_NODE
) {
286 /* We found an element node, does it have the
288 err
= dom_node_get_node_name(n
, &name
);
289 if (err
!= DOM_NO_ERR
) {
294 if (dom_string_lwc_isequal(name
,
296 /* The name is right, return it */
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
304 dom_string_unref(name
);
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
) {