summaryrefslogtreecommitdiff
path: root/src/core/attr.c
blob: 6200ab1c8757a4110b127a7d7013f8e954e84fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <stddef.h>
#include <string.h>

#include <dom/core/attr.h>
#include <dom/core/document.h>
#include <dom/core/string.h>

#include "core/attr.h"
#include "core/document.h"
#include "core/entity_ref.h"
#include "core/node.h"
#include "utils/utils.h"

struct dom_element;
struct dom_type_info;

/**
 * DOM node attribute
 */
struct dom_attr {
	struct dom_node base;		/**< Base node */

	bool specified;			/**< Whether attribute was specified
					 * or defaulted */

	struct dom_type_info *schema_type_info;	/**< Type information */

	bool is_id;			/**< Attribute is of type ID */
};

/**
 * Create an attribute node
 *
 * \param doc     The owning document
 * \param name    The name of the node to create
 * \param result  Pointer to location to receive created attribute
 * \return DOM_NO_ERR                on success,
 *         DOM_INVALID_CHARACTER_ERR if ::name is invalid,
 *         DOM_NO_MEM_ERR            on memory exhaustion.
 *
 * ::doc and ::name will have their reference counts increased.
 *
 * The returned attribute will already be referenced.
 */
dom_exception dom_attr_create(struct dom_document *doc,
		struct dom_string *name, struct dom_attr **result)
{
	struct dom_attr *a;
	dom_exception err;

	/** \todo Sanity check the attribute name */

	/* Allocate the element */
	a = dom_document_alloc(doc, NULL, sizeof(struct dom_attr));
	if (a == NULL)
		return DOM_NO_MEM_ERR;

	/* Initialise the base class */
	err = dom_node_initialise(&a->base, doc, DOM_ATTRIBUTE_NODE,
			name, NULL);
	if (err != DOM_NO_ERR) {
		dom_document_alloc(doc, a, 0);
		return err;
	}

	/* Perform our type-specific initialisation */
	a->specified = false;
	a->schema_type_info = NULL;
	a->is_id = false;

	*result = a;

	return DOM_NO_ERR;
}

/**
 * Destroy an attribute node
 *
 * \param doc   The owning document
 * \param attr  The attribute to destroy
 *
 * The contents of ::attr will be destroyed and ::attr will be freed
 */
void dom_attr_destroy(struct dom_document *doc, struct dom_attr *attr)
{
	struct dom_node *c, *d;

	/* Destroy children of this node */
	for (c = attr->base.first_child; c != NULL; c = d) {
		d = c->next;

		/* Detach child */
		c->parent = NULL;

		if (c->refcnt > 0) {
			/* Something is using this child */

			/** \todo add to list of nodes pending deletion */

			continue;
		}

		/* Detach from sibling list */
		c->previous = NULL;
		c->next = NULL;

		dom_node_destroy(c);
	}

	/* Now, clean up this node and destroy it */

	if (attr->schema_type_info != NULL) {
		/** \todo destroy schema type info */
	}

	dom_node_finalise(doc, &attr->base);

	dom_document_alloc(doc, attr, 0);
}

/**
 * Retrieve an attribute's name
 *
 * \param attr    Attribute to retrieve name from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * The returned string will have its reference count increased. It is
 * the responsibility of the caller to unref the string once it has
 * finished with it.
 */
dom_exception dom_attr_get_name(struct dom_attr *attr,
		struct dom_string **result)
{
	struct dom_node *a = (struct dom_node *) attr;

	/** \todo Handle case where a->localname != NULL */

	if (a->name != NULL)
		dom_string_ref(a->name);
	*result = a->name;

	return DOM_NO_ERR;
}

/**
 * Determine if attribute was specified or defaulted
 *
 * \param attr    Attribute to inspect
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_attr_get_specified(struct dom_attr *attr, bool *result)
{
	*result = attr->specified;

	return DOM_NO_ERR;
}

/**
 * Retrieve an attribute's value
 *
 * \param attr    Attribute to retrieve value from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * The returned string will have its reference count increased. It is
 * the responsibility of the caller to unref the string once it has
 * finished with it.
 */
dom_exception dom_attr_get_value(struct dom_attr *attr,
		struct dom_string **result)
{
	struct dom_node *a = (struct dom_node *) attr;
	struct dom_node *c;
	uint8_t *rep;
	size_t rep_len;
	size_t rep_alloc;
	dom_exception err;

#define CHUNK 128

	rep = dom_document_alloc(a->owner, NULL, CHUNK);
	if (rep == NULL)
		return DOM_NO_MEM_ERR;

	rep_len = 0;
	rep_alloc = CHUNK;

	/* Traverse children, building a string representation as we go */
	for (c = a->first_child; c != NULL; c = c->next) {
		if (c->type == DOM_TEXT_NODE && c->value != NULL) {
			const uint8_t *data;
			size_t len;

			err = dom_string_get_data(c->value, &data, &len);
			if (err != DOM_NO_ERR) {
				dom_document_alloc(a->owner, rep, 0);
				return err;
			}

			/* Extend buffer, if necessary */
			if (rep_len + len >= rep_alloc) {
				uint8_t *temp;
				size_t required = (rep_len + len) - rep_alloc;

				/* Round required up to a chunk boundary */
				required = 
					(required + CHUNK - 1) & ~(CHUNK - 1);

				temp = dom_document_alloc(a->owner, rep, 
						rep_alloc + required);
				if (temp == NULL) {
					dom_document_alloc(a->owner, rep, 0);
					return DOM_NO_MEM_ERR;
				}

				rep = temp;
				rep_alloc += required;
			}

			/* Copy text into buffer */
			memcpy(rep + rep_len, data, len);

			/* And fix up length information */
			rep_len += len;
		} else if (c->type == DOM_ENTITY_REFERENCE_NODE) {
			struct dom_string *tr;
			const uint8_t *data;
			size_t len;

			/* Get textual representation of entity */
			err = dom_entity_reference_get_textual_representation(
					(struct dom_entity_reference *) c,
					&tr);
			if (err != DOM_NO_ERR) {
				dom_document_alloc(a->owner, rep, 0);
				return err;
			}

			err = dom_string_get_data(tr, &data, &len);
			if (err != DOM_NO_ERR) {
				dom_string_unref(tr);
				dom_document_alloc(a->owner, rep, 0);
				return err;
			}

			/* Extend buffer, if necessary */
			if (rep_len + len >= rep_alloc) {
				uint8_t *temp;
				size_t required = (rep_len + len) - rep_alloc;

				/* Round required up to a chunk boundary */
				required = 
					(required + CHUNK - 1) & ~(CHUNK - 1);

				temp = dom_document_alloc(a->owner, rep, 
						rep_alloc + required);
				if (temp == NULL) {
					dom_document_alloc(a->owner, rep, 0);
					return DOM_NO_MEM_ERR;
				}

				rep = temp;
				rep_alloc += required;
			}

			/* Copy text into buffer */
			memcpy(rep + rep_len, data, len);

			/* And fix up length information */
			rep_len += len;

			/* No longer need textual representation */
			dom_string_unref(tr);
		}
	}

#undef CHUNK

	/* Create DOMString */
	err = dom_string_create_from_ptr(a->owner, rep, rep_len, result);
	if (err != DOM_NO_ERR) {
		dom_document_alloc(a->owner, rep, 0);
		return err;
	}

	/* Cleanup */
	dom_document_alloc(a->owner, rep, 0);

	return DOM_NO_ERR;
}

/**
 * Set an attribute's value
 *
 * \param attr   Attribute to retrieve value from
 * \param value  New value for attribute
 * \return DOM_NO_ERR                      on success,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if attribute is readonly.
 */
dom_exception dom_attr_set_value(struct dom_attr *attr,
		struct dom_string *value)
{
	struct dom_node *a = (struct dom_node *) attr;
	struct dom_node *c, *d;
	struct dom_text *text;
	dom_exception err;

	/* Ensure attribute is writable */
	if (_dom_node_readonly(a))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* Create text node containing new value */
	err = dom_document_create_text_node(a->owner, value, &text);
	if (err != DOM_NO_ERR)
		return err;

	/* Destroy children of this node */
	for (c = a->first_child; c != NULL; c = d) {
		d = c->next;

		/* Detach child */
		c->parent = NULL;

		if (c->refcnt > 0) {
			/* Something is using this child */

			/** \todo add to list of nodes pending deletion */

			continue;
		}

		/* Detach from sibling list */
		c->previous = NULL;
		c->next = NULL;

		dom_node_destroy(c);
	}

	/* And insert the text node as the value */
	((struct dom_node *) text)->parent = a;
	a->first_child = a->last_child = (struct dom_node *) text;

	return DOM_NO_ERR;
}

/**
 * Retrieve the owning element of an attribute
 *
 * \param attr    The attribute to extract owning element from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * The returned node will have its reference count increased. The caller
 * should unref it once it has finished with it.
 */
dom_exception dom_attr_get_owner(struct dom_attr *attr,
		struct dom_element **result)
{
	struct dom_node *a = (struct dom_node *) attr;

	/* If there is an owning element, increase its reference count */
	if (a->parent != NULL)
		dom_node_ref(a->parent);

	*result = (struct dom_element *) a->parent;

	return DOM_NO_ERR;
}

/**
 * Retrieve an attribute's type information
 *
 * \param attr    The attribute to extract type information from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * The returned type info will have its reference count increased. The caller
 * should unref it once it has finished with it.
 */
dom_exception dom_attr_get_schema_type_info(struct dom_attr *attr,
		struct dom_type_info **result)
{
	UNUSED(attr);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Determine if an attribute if of type ID
 *
 * \param attr    The attribute to inspect
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_attr_is_id(struct dom_attr *attr, bool *result)
{
	*result = attr->is_id;

	return DOM_NO_ERR;
}