summaryrefslogtreecommitdiff
path: root/src/core/cdatasection.c
blob: b470df2ab0d0d04b89395f1fd399a640352ca053 (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
/*
 * 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>
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com> 
 */

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

/**
 * A DOM CDATA section
 */
struct dom_cdata_section {
	struct dom_text base;		/**< Base node */
};

static struct dom_node_protect_vtable cdata_section_protect_vtable = {
	DOM_CDATA_SECTION_PROTECT_VTABLE
};

/**
 * Create a CDATA section
 *
 * \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_cdata_section_create(struct dom_document *doc,
		struct lwc_string_s *name, struct dom_string *value,
		struct dom_cdata_section **result)
{
	struct dom_cdata_section *c;
	dom_exception err;

	/* Allocate the comment node */
	c = _dom_document_alloc(doc, NULL, sizeof(struct dom_cdata_section));
	if (c == NULL)
		return DOM_NO_MEM_ERR;
	
	/* Set up vtable */
	((dom_node_internal *) c)->base.vtable = &text_vtable;
	((dom_node_internal *) c)->vtable = &cdata_section_protect_vtable;

	/* And initialise the node */
	err = _dom_cdata_section_initialise(&c->base, doc,
			DOM_CDATA_SECTION_NODE, name, value);
	if (err != DOM_NO_ERR) {
		_dom_document_alloc(doc, c, 0);
		return err;
	}

	*result = c;

	return DOM_NO_ERR;
}

/**
 * Destroy a CDATA section
 *
 * \param doc    The owning document
 * \param cdata  The cdata section to destroy
 *
 * The contents of ::cdata will be destroyed and ::cdata will be freed.
 */
void _dom_cdata_section_destroy(struct dom_document *doc,
		struct dom_cdata_section *cdata)
{
	/* Clean up base node contents */
	_dom_cdata_section_finalise(doc, &cdata->base);

	/* Destroy the node */
	_dom_document_alloc(doc, cdata, 0);
}

/*--------------------------------------------------------------------------*/

/* The protected virtual functions */

/* The virtual destroy function of this class */
void __dom_cdata_section_destroy(struct dom_node_internal *node)
{
	struct dom_document *doc;
	doc = dom_node_get_owner(node);

	_dom_cdata_section_destroy(doc, (struct dom_cdata_section *) node);
}

/* The memory allocator of this class */
dom_exception _dom_cdata_section_alloc(struct dom_document *doc,
		struct dom_node_internal *n, struct dom_node_internal **ret)
{
	UNUSED(n);
	dom_cdata_section *a;
	
	a = _dom_document_alloc(doc, NULL, sizeof(struct dom_cdata_section));
	if (a == NULL)
		return DOM_NO_MEM_ERR;
	
	*ret = (dom_node_internal *) a;
	dom_node_set_owner(*ret, doc);

	return DOM_NO_ERR;
}

/* The copy constructor of this class */
dom_exception _dom_cdata_section_copy(struct dom_node_internal *new, 
		struct dom_node_internal *old)
{
	return _dom_characterdata_copy(new, old);
}