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

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

/**
 * Create a text node
 *
 * \param doc     The owning document
 * \param name    The name of the node to create
 * \param value   The text content of the node
 * \param result  Pointer to location to receive created node
 * \return DOM_NO_ERR                on success,
 *         DOM_NO_MEM_ERR            on memory exhaustion.
 *
 * ::doc, ::name and ::value will have their reference counts increased.
 *
 * The returned node will already be referenced.
 */
dom_exception dom_text_create(struct dom_document *doc,
		struct dom_string *name, struct dom_string *value,
		struct dom_text **result)
{
	struct dom_text *t;
	dom_exception err;

	/* Allocate the text node */
	t = dom_document_alloc(doc, NULL, sizeof(struct dom_text));
	if (t == NULL)
		return DOM_NO_MEM_ERR;

	/* And initialise the node */
	err = dom_text_initialise(t, doc, DOM_TEXT_NODE, name, value);
	if (err != DOM_NO_ERR) {
		dom_document_alloc(doc, t, 0);
		return err;
	}

	*result = t;

	return DOM_NO_ERR;
}

/**
 * Destroy a text node
 *
 * \param doc   The owning document
 * \param text  The text node to destroy
 *
 * The contents of ::text will be destroyed and ::text will be freed.
 */
void dom_text_destroy(struct dom_document *doc, struct dom_text *text)
{
	/* Finalise node */
	dom_text_finalise(doc, text);

	/* Free node */
	dom_document_alloc(doc, text, 0);
}

/**
 * Initialise a text node
 *
 * \param text   The node to initialise
 * \param doc    The owning document
 * \param type   The type of the node
 * \param name   The name of the node to create
 * \param value  The text content of the node
 * \return DOM_NO_ERR on success.
 *
 * ::doc, ::name and ::value will have their reference counts increased.
 */
dom_exception dom_text_initialise(struct dom_text *text,
		struct dom_document *doc, dom_node_type type,
		struct dom_string *name, struct dom_string *value)
{
	dom_exception err;

	/* Initialise the base class */
	err = dom_characterdata_initialise(&text->base, doc, type,
			name, value);
	if (err != DOM_NO_ERR)
		return err;

	/* Perform our type-specific initialisation */
	text->element_content_whitespace = false;

	return DOM_NO_ERR;
}

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

/**
 * Split a text node at a given character offset
 *
 * \param text  The node to split
 * \param offset  Character offset to split at
 * \param result  Pointer to location to receive new node
 * \return DOM_NO_ERR                      on success,
 *         DOM_INDEX_SIZE_ERR              if ::offset is greater than the
 *                                         number of characters in ::text,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::text is readonly.
 *
 * The returned node will be referenced. The client should unref the node
 * once it has finished with it.
 */
dom_exception dom_text_split_text(struct dom_text *text,
		unsigned long offset, struct dom_text **result)
{
	UNUSED(text);
	UNUSED(offset);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Determine if a text node contains element content whitespace
 *
 * \param text    The node to consider
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_text_get_is_element_content_whitespace(
		struct dom_text *text, bool *result)
{
	*result = text->element_content_whitespace;

	return DOM_NO_ERR;
}

/**
 * Retrieve all text in Text nodes logically adjacent to a Text node
 *
 * \param text    Text node to consider
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_text_get_whole_text(struct dom_text *text,
		struct dom_string **result)
{
	UNUSED(text);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Replace the text of a Text node and all logically adjacent Text nodes
 *
 * \param text     Text node to consider
 * \param content  Replacement content
 * \param result   Pointer to location to receive Text node
 * \return DOM_NO_ERR                      on success,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if one of the Text nodes being
 *                                         replaced is readonly.
 *
 * The returned node will be referenced. The client should unref the node
 * once it has finished with it.
 */
dom_exception dom_text_replace_whole_text(struct dom_text *text,
		struct dom_string *content, struct dom_text **result)
{
	UNUSED(text);
	UNUSED(content);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}