summaryrefslogtreecommitdiff
path: root/src/core/implementation.c
blob: 7380771c909da7f8ca919eee64ccb8d4376f32d4 (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
/*
 * 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 <string.h>

#include <dom/core/implementation.h>

#include "core/document.h"
#include "core/document_type.h"

#include "html/html_document.h"

#include "utils/namespace.h"
#include "utils/utils.h"
#include "utils/validate.h"

/**
 * Test whether a DOM implementation implements a specific feature
 * and version
 *
 * \param feature  The feature to test for
 * \param version  The version number of the feature to test for
 * \param result   Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_implementation_has_feature(
		const char *feature, const char *version,
		bool *result)
{
	UNUSED(feature);
	UNUSED(version);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Create a document type node
 *
 * \param qname      The qualified name of the document type
 * \param public_id  The external subset public identifier
 * \param system_id  The external subset system identifier
 * \param doctype    Pointer to location to receive result
 * \return DOM_NO_ERR on success,
 *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
 *         DOM_NAMESPACE_ERR         if ::qname is malformed,
 *         DOM_NOT_SUPPORTED_ERR     if ::impl does not support the feature
 *                                   "XML" and the language exposed through
 *                                   Document does not support XML
 *                                   namespaces.
 *
 * The doctype will be referenced, so the client need not do this
 * explicitly. The client must unref the doctype once it has
 * finished with it.
 */
dom_exception dom_implementation_create_document_type(
		const char *qname, const char *public_id, 
		const char *system_id,
		struct dom_document_type **doctype)
{
	struct dom_document_type *d;
	dom_string *qname_s = NULL, *prefix = NULL, *lname = NULL;
	dom_string *public_id_s = NULL, *system_id_s = NULL;
	dom_exception err;

	if (qname != NULL) {
		err = dom_string_create((const uint8_t *) qname,
				strlen(qname), &qname_s);
		if (err != DOM_NO_ERR)
			return err;
	}

	err = _dom_namespace_split_qname(qname_s, &prefix, &lname);
	if (err != DOM_NO_ERR) {
		dom_string_unref(qname_s);
		return err;
	}

	if (public_id != NULL) {
		err = dom_string_create((const uint8_t *) public_id,
				strlen(public_id), &public_id_s);
		if (err != DOM_NO_ERR) {
			dom_string_unref(lname);
			dom_string_unref(prefix);
			dom_string_unref(qname_s);
			return err;
		}
	}

	if (system_id != NULL) {
		err = dom_string_create((const uint8_t *) system_id,
				strlen(system_id), &system_id_s);
		if (err != DOM_NO_ERR) {
			dom_string_unref(public_id_s);
			dom_string_unref(lname);
			dom_string_unref(prefix);
			dom_string_unref(qname_s);
			return err;
		}
	}

	/* Create the doctype */
	err = _dom_document_type_create(qname_s, public_id_s, system_id_s, &d);

	if (err == DOM_NO_ERR)
		*doctype = d;

	dom_string_unref(system_id_s);
	dom_string_unref(public_id_s);
	dom_string_unref(prefix);
	dom_string_unref(lname);
	dom_string_unref(qname_s);

	return err;
}

/**
 * Create a document node
 *
 * \param impl_type  The type of document object to create
 * \param namespace  The namespace URI of the document element
 * \param qname      The qualified name of the document element
 * \param doctype    The type of document to create
 * \param doc        Pointer to location to receive result
 * \return DOM_NO_ERR on success,
 *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
 *         DOM_NAMESPACE_ERR         if ::qname is malformed, or if ::qname
 *                                   has a prefix and ::namespace is NULL,
 *                                   or if ::qname is NULL and ::namespace
 *                                   is non-NULL, or if ::qname has a prefix
 *                                   "xml" and ::namespace is not
 *                                   "http://www.w3.org/XML/1998/namespace",
 *                                   or if ::impl does not support the "XML"
 *                                   feature and ::namespace is non-NULL,
 *         DOM_WRONG_DOCUMENT_ERR    if ::doctype is already being used by a
 *                                   document, or if it was not created by
 *                                   ::impl,
 *         DOM_NOT_SUPPORTED_ERR     if ::impl does not support the feature
 *                                   "XML" and the language exposed through
 *                                   Document does not support XML
 *                                   namespaces.
 *
 * The document will be referenced, so the client need not do this
 * explicitly. The client must unref the document once it has
 * finished with it.
 */
dom_exception dom_implementation_create_document(
		uint32_t impl_type,
		const char *namespace, const char *qname,
		struct dom_document_type *doctype,
		dom_events_default_action_fetcher daf,
		void *daf_ctx,
		struct dom_document **doc)
{
	struct dom_document *d;
	dom_string *namespace_s = NULL, *qname_s = NULL;
	dom_exception err;

	if (namespace != NULL) {
		err = dom_string_create((const uint8_t *) namespace,
				strlen(namespace), &namespace_s);
		if (err != DOM_NO_ERR)
			return err;
	}

	if (qname != NULL) {
		err = dom_string_create((const uint8_t *) qname, 
				strlen(qname), &qname_s);
		if (err != DOM_NO_ERR) {
			dom_string_unref(namespace_s);
			return err;
		}
	}

	if (qname_s != NULL && _dom_validate_name(qname_s) == false) {
		dom_string_unref(qname_s);
		dom_string_unref(namespace_s);
		return DOM_INVALID_CHARACTER_ERR;
	}
  
	err = _dom_namespace_validate_qname(qname_s, namespace_s);
	if (err != DOM_NO_ERR) {
		dom_string_unref(qname_s);
		dom_string_unref(namespace_s);
		return DOM_NAMESPACE_ERR;
	}

	if (doctype != NULL && dom_node_get_parent(doctype) != NULL) {
		dom_string_unref(qname_s);
		dom_string_unref(namespace_s);
		return DOM_WRONG_DOCUMENT_ERR;
	}

	/* Create document object that reflects the required APIs */
 	if (impl_type == DOM_IMPLEMENTATION_HTML) {
		dom_html_document *html_doc;

		err = _dom_html_document_create(daf, daf_ctx, &html_doc);

		d = (dom_document *) html_doc;
	} else {
		err = _dom_document_create(daf, daf_ctx, &d);
	}

	if (err != DOM_NO_ERR) {
		dom_string_unref(qname_s);
		dom_string_unref(namespace_s);
		return err;
	}

	/* Set its doctype, if necessary */
	if (doctype != NULL) {
		struct dom_node *ins_doctype = NULL;

		err = dom_node_append_child((struct dom_node *) d, 
				(struct dom_node *) doctype, &ins_doctype);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) d);
			dom_string_unref(qname_s);
			dom_string_unref(namespace_s);
			return err;
		}

		/* Not interested in inserted doctype */
		if (ins_doctype != NULL)
			dom_node_unref(ins_doctype);
	}

	/* Create root element and attach it to document */
	if (qname_s != NULL) {
		struct dom_element *e;
		struct dom_node *inserted;

		err = dom_document_create_element_ns(d, namespace_s, qname_s, &e);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) d);
			dom_string_unref(qname_s);
			dom_string_unref(namespace_s);
			return err;
		}

		err = dom_node_append_child((struct dom_node *) d,
				(struct dom_node *) e, &inserted);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) e);
			dom_node_unref((struct dom_node *) d);
			dom_string_unref(qname_s);
			dom_string_unref(namespace_s);
			return err;
		}

		/* No longer interested in inserted node */
		dom_node_unref(inserted);

		/* Done with element */
		dom_node_unref((struct dom_node *) e);
	}

	/* Clean up strings we created */
	dom_string_unref(qname_s);
	dom_string_unref(namespace_s);

	*doc = d;

	return DOM_NO_ERR;
}

/**
 * Retrieve a specialized object which implements the specified
 * feature and version
 *
 * \param feature  The requested feature
 * \param version  The version number of the feature
 * \param object   Pointer to location to receive object
 * \return DOM_NO_ERR.
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function.
 */
dom_exception dom_implementation_get_feature(
		const char *feature, const char *version,
		void **object)
{
	UNUSED(feature);
	UNUSED(version);
	UNUSED(object);

	return DOM_NOT_SUPPORTED_ERR;
}