5 * This module defines the various node and node list data structures
6 * and functionality to modify and access them, such as adding a node as
7 * a child to a given node and getting the text string of a node as
8 * defined by the DOM specification.
12 * DOM documents are represented as a collection of nodes arranged in a
13 * hierarchic structure. At the root is either a #DOM_NODE_DOCUMENT or
14 * #DOM_NODE_DOCUMENT_FRAGMENT node, each of which may have multiple
15 * child nodes. There is a well-defined order that dictates which child
16 * nodes may be descendants of a given type of node. For example, text
17 * and attribute nodes can have no children, while elements node may
18 * have both attribute and element nodes as children but with each type
19 * in different node lists. The hierarchy is somewhat encoded in the
20 * type specific node data, however, certain node types also define
21 * "custom" node lists for conveniently storing additional "embedded"
22 * data, such as processing instruction nodes having an attribute node
23 * list for conveniently accessing variable-value pairs given for
24 * XML-specific processing instructions:
26 * @verbatim <?xml version="1.0"?> @endverbatim
30 * There are two types of list: unordered (the default) and
31 * alphabetically ordered (also called "maps"). Both types of list
32 * stores all contained nodes in the index-oriented #dom_node_list data
35 * When inserting a node into a list, first use either
36 * #get_dom_node_list_index or #get_dom_node_map_index (depending on
37 * whether the list is unordered or ordered respectively) to calculate
38 * the index at which to insert the new node. Then use
39 * #add_to_dom_node_list to insert the node in the list at the given
40 * position. Alternatively (and mostly preferred), simply use
41 * #add_dom_node to have all of the above done automatically plus some
44 * A variety of node list accessors are defined. The node structure does
45 * not define any "next" or "previous" members to get siblings due to
46 * reduce memory usage (this might have to change --jonas). Instead, use
47 * #get_dom_node_next and #get_dom_node_next to access siblings. To
48 * lookup the existence of a node in a sorted node list (map) use
49 * #get_dom_node_map_entry. If a specific and unique node subtype should
50 * be found use #get_dom_node_child that given a parent node will find a
51 * child node based on a specific child node type and subtype. Finally,
52 * list can be iterated in forward and reverse order using
53 * #foreach_dom_node and #foreachback_dom_node.
59 #include "dom/string.h"
66 DOM_NODE_UNKNOWN
= 0, /**< Node type used internally. */
68 DOM_NODE_ELEMENT
= 1, /**< Element node */
69 DOM_NODE_ATTRIBUTE
= 2, /**< Attribute node */
70 DOM_NODE_TEXT
= 3, /**< Text node */
71 DOM_NODE_CDATA_SECTION
= 4, /**< CData section node */
72 DOM_NODE_ENTITY_REFERENCE
= 5, /**< Entity reference node */
73 DOM_NODE_ENTITY
= 6, /**< Entity node */
74 DOM_NODE_PROCESSING_INSTRUCTION
= 7, /**< Processing instruction node */
75 DOM_NODE_COMMENT
= 8, /**< Comment node */
76 DOM_NODE_DOCUMENT
= 9, /**< Document root node */
77 DOM_NODE_DOCUMENT_TYPE
= 10, /**< Document type (DTD) node */
78 DOM_NODE_DOCUMENT_FRAGMENT
= 11, /**< Document fragment node */
79 DOM_NODE_NOTATION
= 12, /**< Notation node */
81 DOM_NODES
/**< The number of DOM nodes */
84 /* Following is the node specific data structures. They may contain no
85 * more than 4 pointers or something equivalent. */
87 /* The document URI is stored in the string / length members. */
88 struct dom_document_node
{
90 struct dom_document
*document
;
92 /* The child nodes. May be NULL. Ordered like they where inserted. */
93 /* FIXME: Should be just one element (root) node reference. */
94 struct dom_node_list
*children
;
98 struct dom_string public_id
;
99 struct dom_string system_id
;
102 struct dom_doctype_subset_info
{
103 struct dom_string internal
;
104 struct dom_id external
;
107 struct dom_document_type_node
{
108 /* These are really maps and should be sorted alphabetically. */
109 struct dom_node_list
*entities
;
110 struct dom_node_list
*notations
;
112 /* The string/length members of dom_node hold the name of the document
113 * type "<!DOCTYPE {name} ...>". This holds the ids for the external
114 * subset and the string of the internal subset. */
115 struct dom_doctype_subset_infot
*subset
;
118 /* Element nodes are indexed nodes stored in node lists of either
119 * other child nodes or the root node. */
120 struct dom_element_node
{
121 /* The child nodes. May be NULL. Ordered like they where inserted. */
122 struct dom_node_list
*children
;
124 /* Only element nodes can have attributes and element nodes can only be
125 * child nodes so the map is put here.
127 * The @map may be NULL if there are none. The @map nodes are sorted
128 * alphabetically according to the attributes name so it has fast
130 struct dom_node_list
*map
;
132 /* For <xsl:stylesheet ...> elements this holds the offset of
134 uint16_t namespace_offset
;
136 /* Special implementation dependent type specifier for example
137 * containing an enum value representing the element to reduce string
138 * comparing and only do one fast find mapping. */
142 /* Attribute nodes are named nodes stored in a node map of an element node. */
143 struct dom_attribute_node
{
144 /* The string that hold the attribute value. The @string / @length
145 * members of {struct dom_node} holds the name that identifies the node
147 struct dom_string value
;
149 /* For xml:lang="en" attributes this holds the offset of 'lang' */
150 uint16_t namespace_offset
;
152 /* Special implementation dependent type specifier. For HTML it (will)
153 * contain an enum value representing the attribute HTML_CLASS, HTML_ID etc.
154 * to reduce string comparing and only do one fast find mapping. */
157 /* The attribute value is delimited by quotes. Can be NUL, ' or ". */
158 unsigned char quoted
;
160 /* Was the attribute specified in the DTD as a default attribute or was
161 * it added from the document source. */
162 unsigned int specified
:1;
164 /* Has the node->string been converted to internal charset. */
165 unsigned int converted
:1;
167 /* Is the attribute a unique identifier meaning the owner (element)
168 * should be added to the document nodes @element_id hash. */
171 /* The attribute value references some other resource */
172 unsigned int reference
:1;
175 struct dom_text_node
{
176 /* The number of newlines the text string contains */
177 unsigned int newlines
;
179 /* We will need to add text nodes even if they contain only whitespace.
180 * In order to quickly identify such nodes this member is used. */
181 unsigned int only_space
:1;
183 /* Has the node->string been converted to internal charset. */
184 unsigned int converted
:1;
187 enum dom_proc_instruction_type
{
188 DOM_PROC_INSTRUCTION
,
190 /* Keep this group sorted */
191 DOM_PROC_INSTRUCTION_XML
, /* XML header */
192 DOM_PROC_INSTRUCTION_XML_STYLESHEET
, /* XML stylesheet link */
194 DOM_PROC_INSTRUCTION_TYPES
197 struct dom_proc_instruction_node
{
198 /* The target of the processing instruction (xml for '<?xml ... ?>')
199 * is in the @string / @length members. */
200 /* This holds the value to be processed */
201 struct dom_string instruction
;
203 /* For fast checking of the target type */
204 uint16_t type
; /* enum dom_proc_instruction_type */
206 /* For some processing instructions like xml the instructions contain
207 * attributes and those attribute can be collected in this @map. */
208 struct dom_node_list
*map
;
211 union dom_node_data
{
212 struct dom_document_node document
;
213 struct dom_document_type_node document_type
;
214 struct dom_element_node element
;
215 struct dom_attribute_node attribute
;
216 struct dom_text_node text
;
217 struct dom_id notation
;
218 /* For entities string/length hold the notation name */
219 struct dom_id entity
;
220 struct dom_proc_instruction_node proc_instruction
;
222 /* Node types without a union member yet (mostly because it hasn't
225 * DOM_NODE_CDATA_SECTION: Use dom_text_node?
226 * DOM_NODE_DOCUMENT_FRAGMENT: struct dom_node_list children;
227 * DOM_NODE_ENTITY_REFERENCE: unicode_val_T
234 * The node data structure is an abstract container that can be used to
235 * represent the hierarchic structure of a document, such as relation
236 * between elements, attributes, etc.
238 * @note This structure is size critical so keep ordering to make
239 * it easier to pack and avoid unneeded members.
242 /** The type of the node. Holds a #dom_node_type enum value. */
243 uint16_t type
; /* -> enum dom_node_type */
245 /** Was the node string allocated? */
246 unsigned int allocated
:1;
248 /** Type specific node string. Can contain either stuff like
249 * element name or for attributes the attribute name. */
250 struct dom_string string
;
252 /** The parent node. The parent node is NULL for the root node. */
253 struct dom_node
*parent
;
255 /** Type specific node data. */
256 union dom_node_data data
;
261 * A node list can be used for storing indexed nodes. If a node list
262 * should be sorted alphabetically use the #get_dom_node_map_index
263 * function to find the index of new nodes before inserting them. */
264 struct dom_node_list
{
266 struct dom_node
*entries
[1];
269 #define foreach_dom_node(list, node, i) \
270 for ((i) = 0; (i) < (list)->size; (i)++) \
271 if (((node) = (list)->entries[(i)]))
273 #define foreachback_dom_node(list, node, i) \
274 for ((i) = (list)->size - 1; (i) > 0; (i)--) \
275 if (((node) = (list)->entries[(i)]))
277 #define is_dom_node_list_member(list, member) \
278 ((list) && 0 <= (member) && (member) < (list)->size)
280 /* Adds @node to the list pointed to by @list_ptr at the given @position. If
281 * @position is -1 the node is added at the end. */
282 struct dom_node_list
*
283 add_to_dom_node_list(struct dom_node_list
**list_ptr
,
284 struct dom_node
*node
, int position
);
286 void done_dom_node_list(struct dom_node_list
*list
);
288 /* Returns the position or index where the @node has been inserted into the
289 * 'default' list of the @parent node. (Default means use get_dom_node_list()
290 * to acquire the list to search in. Returns -1, if the node is not found. */
291 int get_dom_node_list_index(struct dom_node
*parent
, struct dom_node
*node
);
293 /* Returns the position or index where the @node should be inserted into the
294 * node @list in order to the list to be alphabetically sorted. Assumes that
295 * @list is already sorted properly. */
296 int get_dom_node_map_index(struct dom_node_list
*list
, struct dom_node
*node
);
298 /* Returns the previous sibling to the node. */
299 struct dom_node
*get_dom_node_prev(struct dom_node
*node
);
301 /* Returns the next sibling to the node. */
302 struct dom_node
*get_dom_node_next(struct dom_node
*node
);
304 /* Returns first text node of the element or NULL. */
306 get_dom_node_child(struct dom_node
*node
, enum dom_node_type child_type
,
307 int16_t child_subtype
);
309 /* Looks up the @node_map for a node matching the requested type and name.
310 * The @subtype maybe be 0 indication unknown subtype and only name should be
311 * tested else it will indicate either the element or attribute private
314 get_dom_node_map_entry(struct dom_node_list
*node_map
,
315 enum dom_node_type type
, uint16_t subtype
,
316 struct dom_string
*name
);
318 /* Removes the node and all its children and free()s itself */
319 void done_dom_node(struct dom_node
*node
);
321 #ifndef DEBUG_MEMLEAK
323 /* The allocated argument is used as the value of node->allocated if >= 0.
324 * Use -1 to default node->allocated to the value of parent->allocated. */
327 init_dom_node_at(struct dom_node
*parent
, enum dom_node_type type
,
328 struct dom_string
*string
, int allocated
);
330 #define init_dom_node(type, string, allocated) \
331 init_dom_node_at(NULL, type, string, allocated)
333 #define add_dom_node(parent, type, string) \
334 init_dom_node_at(parent, type, string, -1)
338 init_dom_node_at(unsigned char *file
, int line
,
339 struct dom_node
*parent
, enum dom_node_type type
,
340 struct dom_string
*string
, int allocated
);
342 #define init_dom_node(type, string, allocated) \
343 init_dom_node_at(__FILE__, __LINE__, NULL, type, string, allocated)
345 #define add_dom_node(parent, type, string) \
346 init_dom_node_at(__FILE__, __LINE__, parent, type, string, -1)
348 #endif /* DEBUG_MEMLEAK */
350 #define add_dom_element(parent, string) \
351 add_dom_node(parent, DOM_NODE_ELEMENT, string)
353 static inline struct dom_node
*
354 add_dom_attribute(struct dom_node
*parent
, struct dom_string
*name
,
355 struct dom_string
*value
)
357 struct dom_node
*node
= add_dom_node(parent
, DOM_NODE_ATTRIBUTE
, name
);
360 struct dom_string
*str
= &node
->data
.attribute
.value
;
362 if (node
->allocated
) {
363 if (!init_dom_string(str
, value
->string
, value
->length
)) {
368 copy_dom_string(str
, value
);
375 static inline struct dom_node
*
376 add_dom_proc_instruction(struct dom_node
*parent
, struct dom_string
*string
,
377 struct dom_string
*instruction
)
379 struct dom_node
*node
= add_dom_node(parent
, DOM_NODE_PROCESSING_INSTRUCTION
, string
);
381 if (node
&& instruction
) {
382 struct dom_string
*str
= &node
->data
.proc_instruction
.instruction
;
384 if (node
->allocated
) {
385 if (!init_dom_string(str
, instruction
->string
, instruction
->length
)) {
390 copy_dom_string(str
, instruction
);
397 /* Compare two nodes returning non-zero if they differ. */
398 int dom_node_casecmp(struct dom_node
*node1
, struct dom_node
*node2
);
400 /* Returns the name of the node in an allocated string. */
401 struct dom_string
*get_dom_node_name(struct dom_node
*node
);
403 /* Returns the value of the node or NULL if no value is defined for the node
405 struct dom_string
*get_dom_node_value(struct dom_node
*node
);
407 /* Returns the name used for identifying the node type. */
408 struct dom_string
*get_dom_node_type_name(enum dom_node_type type
);
410 /* Based on the type of the parent and the node type return a proper list
411 * or NULL. This is useful when adding a node to a parent node. */
412 static inline struct dom_node_list
**
413 get_dom_node_list_by_type(struct dom_node
*parent
, enum dom_node_type type
)
415 switch (parent
->type
) {
416 case DOM_NODE_DOCUMENT
:
417 return &parent
->data
.document
.children
;
419 case DOM_NODE_ELEMENT
:
421 case DOM_NODE_ATTRIBUTE
:
422 return &parent
->data
.element
.map
;
425 return &parent
->data
.element
.children
;
428 case DOM_NODE_DOCUMENT_TYPE
:
430 case DOM_NODE_ENTITY
:
431 return &parent
->data
.document_type
.entities
;
433 case DOM_NODE_NOTATION
:
434 return &parent
->data
.document_type
.notations
;
440 case DOM_NODE_PROCESSING_INSTRUCTION
:
442 case DOM_NODE_ATTRIBUTE
:
443 return &parent
->data
.proc_instruction
.map
;
454 #define get_dom_node_list(parent, node) \
455 get_dom_node_list_by_type(parent, (node)->type)