summaryrefslogtreecommitdiff
path: root/src/core/characterdata.c
blob: 317dfd293eb16f08b1c085914703c5e05302c63d (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
/*
 * 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 <dom/core/characterdata.h>
#include <dom/core/string.h>

#include "core/characterdata.h"
#include "core/document.h"
#include "core/node.h"
#include "utils/utils.h"

/**
 * Initialise a character data node
 *
 * \param node   The node to initialise
 * \param doc    The document which owns the node
 * \param type   The node type required
 * \param name   The node name, or NULL
 * \param value  The node value, or NULL
 * \return DOM_NO_ERR on success.
 *
 * ::doc, ::name and ::value will have their reference counts increased.
 */
dom_exception dom_characterdata_initialise(struct dom_characterdata *cdata,
		struct dom_document *doc, dom_node_type type,
		struct dom_string *name, struct dom_string *value)
{
	return dom_node_initialise(&cdata->base, doc, type, 
			name, value, NULL, NULL);
}

/**
 * Finalise a character data node
 *
 * \param doc    The owning document
 * \param cdata  The node to finalise
 *
 * The contents of ::cdata will be cleaned up. ::cdata will not be freed.
 */
void dom_characterdata_finalise(struct dom_document *doc,
		struct dom_characterdata *cdata)
{
	dom_node_finalise(doc, &cdata->base);
}

/**
 * Retrieve data from a character data node
 *
 * \param cdata  Character data node to retrieve data from
 * \param data   Pointer to location to receive data
 * \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.
 *
 * DOM3Core states that this can raise DOMSTRING_SIZE_ERR. It will not in
 * this implementation; dom_strings are unbounded.
 */
dom_exception dom_characterdata_get_data(struct dom_characterdata *cdata,
		struct dom_string **data)
{
	struct dom_node *c = (struct dom_node *) cdata;

	if (c->value != NULL) {
		dom_string_ref(c->value);
	}
	*data = c->value;

	return DOM_NO_ERR;
}

/**
 * Set the content of a character data node
 *
 * \param cdata  Node to set the content of
 * \param data   New value for node
 * \return DOM_NO_ERR                      on success,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::cdata is readonly.
 *
 * The new content will have its reference count increased, so the caller
 * should unref it after the call (as the caller should have already claimed
 * a reference on the string). The node's existing content will be unrefed.
 */
dom_exception dom_characterdata_set_data(struct dom_characterdata *cdata,
		struct dom_string *data)
{
	struct dom_node *c = (struct dom_node *) cdata;

	if (_dom_node_readonly(c)) {
		return DOM_NO_MODIFICATION_ALLOWED_ERR;
	}

	if (c->value != NULL) {
		dom_string_unref(c->value);
	}

	dom_string_ref(data);
	c->value = data;

	return DOM_NO_ERR;
}

/**
 * Get the length (in characters) of a character data node's content
 *
 * \param cdata   Node to read content length of
 * \param length  Pointer to location to receive character length of content
 * \return DOM_NO_ERR.
 */
dom_exception dom_characterdata_get_length(struct dom_characterdata *cdata,
		unsigned long *length)
{
	struct dom_node *c = (struct dom_node *) cdata;

	if (c->value != NULL) {
		*length = dom_string_length(c->value);
	} else {
		*length = 0;
	}

	return DOM_NO_ERR;
}

/**
 * Extract a range of data from a character data node
 *
 * \param cdata   The node to extract data from
 * \param offset  The character offset of substring to extract
 * \param count   The number of characters to extract
 * \param data    Pointer to location to receive substring
 * \return DOM_NO_ERR         on success,
 *         DOM_INDEX_SIZE_ERR if ::offset is greater than the number of
 *                            characters in ::cdata.
 *
 * 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.
 *
 * DOM3Core states that this can raise DOMSTRING_SIZE_ERR. It will not in
 * this implementation; dom_strings are unbounded.
 */
dom_exception dom_characterdata_substring_data(
		struct dom_characterdata *cdata, unsigned long offset,
		unsigned long count, struct dom_string **data)
{
	struct dom_node *c = (struct dom_node *) cdata;
	uint32_t len, end;

	if (c->value != NULL) {
		len = dom_string_length(c->value);
	} else {
		len = 0;
	}

	if (offset >= len) {
		return DOM_INDEX_SIZE_ERR;
	}

	end = (offset + count) >= len ? len : offset + count;

	return dom_string_substr(c->value, offset, end, data);
}

/**
 * Append data to the end of a character data node's content
 *
 * \param cdata  The node to append data to
 * \param data   The data to append
 * \return DOM_NO_ERR                      on success,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::cdata is readonly.
 */
dom_exception dom_characterdata_append_data(struct dom_characterdata *cdata,
		struct dom_string *data)
{
	struct dom_node *c = (struct dom_node *) cdata;
	struct dom_string *temp;
	dom_exception err;

	if (_dom_node_readonly(c)) {
		return DOM_NO_MODIFICATION_ALLOWED_ERR;
	}

	err = dom_string_insert(c->value, data, 
			c->value != NULL ? dom_string_length(c->value) : 0,
			&temp);
	if (err != DOM_NO_ERR) {
		return err;
	}

	if (c->value != NULL) {
		dom_string_unref(c->value);
	}

	c->value = temp;

	return DOM_NO_ERR;
}

/**
 * Insert data into a character data node's content
 *
 * \param cdata   The node to insert into
 * \param offset  The character offset to insert at
 * \param data    The data to insert
 * \return DOM_NO_ERR                      on success,
 *         DOM_INDEX_SIZE_ERR              if ::offset is greater than the
 *                                         number of characters in ::cdata,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::cdata is readonly.
 */
dom_exception dom_characterdata_insert_data(struct dom_characterdata *cdata,
		unsigned long offset, struct dom_string *data)
{
	struct dom_node *c = (struct dom_node *) cdata;
	struct dom_string *temp;
	uint32_t len;
	dom_exception err;

	if (_dom_node_readonly(c)) {
		return DOM_NO_MODIFICATION_ALLOWED_ERR;
	}

	if (c->value != NULL) {
		len = dom_string_length(c->value);
	} else {
		len = 0;
	}

	if (offset >= len) {
		return DOM_INDEX_SIZE_ERR;
	}

	err = dom_string_insert(c->value, data, offset, &temp);
	if (err != DOM_NO_ERR) {
		return err;
	}

	if (c->value != NULL) {
		dom_string_unref(c->value);
	}

	c->value = temp;

	return DOM_NO_ERR;
}

/**
 * Delete data from a character data node's content
 *
 * \param cdata   The node to delete from
 * \param offset  The character offset to start deletion from
 * \param count   The number of characters to delete
 * \return DOM_NO_ERR                      on success,
 *         DOM_INDEX_SIZE_ERR              if ::offset is greater than the
 *                                         number of characters in ::cdata,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::cdata is readonly.
 */
dom_exception dom_characterdata_delete_data(struct dom_characterdata *cdata,
		unsigned long offset, unsigned long count)
{
	struct dom_node *c = (struct dom_node *) cdata;
	struct dom_string *temp;
	uint32_t len, end;
	dom_exception err;

	if (_dom_node_readonly(c)) {
		return DOM_NO_MODIFICATION_ALLOWED_ERR;
	}

	if (c->value != NULL) {
		len = dom_string_length(c->value);
	} else {
		len = 0;
	}

	if (offset >= len) {
		return DOM_INDEX_SIZE_ERR;
	}

	end = (offset + count) >= len ? len : offset + count;

	err = dom_string_replace(c->value, NULL, offset, end, &temp);
	if (err != DOM_NO_ERR) {
		return err;
	}

	if (c->value != NULL) {
		dom_string_unref(c->value);
	}

	c->value = temp;

	return DOM_NO_ERR;
}

/**
 * Replace a section of a character data node's content
 *
 * \param cdata   The node to modify
 * \param offset  The character offset of the sequence to replace
 * \param count   The number of characters to replace
 * \param data    The replacement data
 * \return DOM_NO_ERR                      on success,
 *         DOM_INDEX_SIZE_ERR              if ::offset is greater than the
 *                                         number of characters in ::cdata,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::cdata is readonly.
 */
dom_exception dom_characterdata_replace_data(struct dom_characterdata *cdata,
		unsigned long offset, unsigned long count,
		struct dom_string *data)
{
	struct dom_node *c = (struct dom_node *) cdata;
	struct dom_string *temp;
	uint32_t len, end;
	dom_exception err;

	if (_dom_node_readonly(c)) {
		return DOM_NO_MODIFICATION_ALLOWED_ERR;
	}

	if (c->value != NULL) {
		len = dom_string_length(c->value);
	} else {
		len = 0;
	}

	if (offset >= len) {
		return DOM_INDEX_SIZE_ERR;
	}

	end = (offset + count) >= len ? len : offset + count;

	err = dom_string_replace(c->value, data, offset, end, &temp);
	if (err != DOM_NO_ERR) {
		return err;
	}

	if (c->value != NULL) {
		dom_string_unref(c->value);
	}

	c->value = temp;

	return DOM_NO_ERR;
}