summaryrefslogtreecommitdiff
path: root/src/events/mutation_event.c
blob: aeeefe7e75781a32c4b4182a4efc47eb892f96ec (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
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
 */

#include <stdlib.h>

#include "events/mutation_event.h"
#include "core/document.h"

static void _virtual_dom_mutation_event_destroy(struct dom_event *evt);

static struct dom_event_private_vtable _event_vtable = {
	_virtual_dom_mutation_event_destroy
};

/* Constructor */
dom_exception _dom_mutation_event_create(struct dom_document *doc, 
		struct dom_mutation_event **evt)
{
	*evt = malloc(sizeof(dom_mutation_event));
	if (*evt == NULL) 
		return DOM_NO_MEM_ERR;
	
	((struct dom_event *) *evt)->vtable = &_event_vtable;

	return _dom_mutation_event_initialise(doc, *evt);
}

/* Destructor */
void _dom_mutation_event_destroy(struct dom_mutation_event *evt)
{
	_dom_mutation_event_finalise(evt);

	free(evt);
}

/* Initialise function */
dom_exception _dom_mutation_event_initialise(struct dom_document *doc, 
		struct dom_mutation_event *evt)
{
	evt->related_node = NULL;
	evt->prev_value = NULL;
	evt->new_value = NULL;
	evt->attr_name = NULL;

	return _dom_event_initialise(doc, &evt->base);
}

/* Finalise function */
void _dom_mutation_event_finalise(struct dom_mutation_event *evt)
{
	dom_node_unref(evt->related_node);
	dom_string_unref(evt->prev_value);
	dom_string_unref(evt->new_value);
	dom_string_unref(evt->attr_name);
	
	evt->related_node = NULL;
	evt->prev_value = NULL;
	evt->new_value = NULL;
	evt->attr_name = NULL;

	_dom_event_finalise(&evt->base);
}

/* The virtual destroy function */
void _virtual_dom_mutation_event_destroy(struct dom_event *evt)
{
	_dom_mutation_event_destroy((dom_mutation_event *) evt);
}

/*----------------------------------------------------------------------*/
/* The public API */

/**
 * Get the related node
 *
 * \param evt   The Event object
 * \param node  The related node
 * \return DOM_NO_ERR.
 */
dom_exception _dom_mutation_event_get_related_node(dom_mutation_event *evt,
		struct dom_node **node)
{
	*node = evt->related_node;
	dom_node_ref(*node);

	return DOM_NO_ERR;
}

/** 
 * Get the old value
 *
 * \param evt  The Event object
 * \param ret  The old value
 * \return DOM_NO_ERR.
 */
dom_exception _dom_mutation_event_get_prev_value(dom_mutation_event *evt,
		dom_string **ret)
{
	*ret = evt->prev_value;
	dom_string_ref(*ret);

	return DOM_NO_ERR;
}

/**
 * Get the new value
 *
 * \param evt  The Event object
 * \param ret  The new value
 * \return DOM_NO_ERR.
 */
dom_exception _dom_mutation_event_get_new_value(dom_mutation_event *evt,
		dom_string **ret)
{
	*ret = evt->new_value;
	dom_string_ref(*ret);

	return DOM_NO_ERR;
}

/**
 * Get the attr name
 *
 * \param evt  The Event object
 * \param ret  The attribute name
 * \return DOM_NO_ERR.
 */
dom_exception _dom_mutation_event_get_attr_name(dom_mutation_event *evt,
		dom_string **ret)
{
	*ret = evt->attr_name;
	dom_string_ref(*ret);

	return DOM_NO_ERR;
}

/**
 * Get the way the attribute change
 *
 * \param evt   The Event object
 * \param type  The change type
 * \return DOM_NO_ERR.
 */
dom_exception _dom_mutation_event_get_attr_change(dom_mutation_event *evt,
		dom_mutation_type *type)
{
	*type = evt->change;

	return DOM_NO_ERR;
}

/**
 * Initialise the MutationEvent
 *
 * \param evt         The Event object
 * \param type        The type of this UIEvent
 * \param bubble      Whether this event can bubble
 * \param cancelable  Whether this event is cancelable
 * \param node        The mutation node
 * \param prev_value  The old value
 * \param new_value   The new value
 * \param attr_name   The attribute's name
 * \param change      The change type
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_mutation_event_init(dom_mutation_event *evt, 
		dom_string *type, bool bubble, bool cancelable, 
		struct dom_node *node, dom_string *prev_value, 
		dom_string *new_value, dom_string *attr_name,
		dom_mutation_type change)
{
	evt->related_node = node;
	dom_node_ref(node);

	evt->prev_value = prev_value;
	dom_string_ref(prev_value);

	evt->new_value = new_value;
	dom_string_ref(new_value);

	evt->attr_name = attr_name;
	dom_string_ref(attr_name);

	evt->change = change;

	return _dom_event_init(&evt->base, type, bubble, cancelable);
}

/**
 * Initialise the MutationEvent with namespace
 *
 * \param evt         The Event object
 * \param namespace   The namespace
 * \param type        The type of this UIEvent
 * \param bubble      Whether this event can bubble
 * \param cancelable  Whether this event is cancelable
 * \param node        The mutation node
 * \param prev_value  The old value
 * \param new_value   The new value
 * \param attr_name   The attribute's name
 * \param change      The change type
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_mutation_event_init_ns(dom_mutation_event *evt,
		dom_string *namespace, dom_string *type,
		bool bubble, bool cancelable, struct dom_node *node,
		dom_string *prev_value, dom_string *new_value,
		dom_string *attr_name, dom_mutation_type change)
{
	evt->related_node = node;
	dom_node_ref(node);

	evt->prev_value = prev_value;
	dom_string_ref(prev_value);

	evt->new_value = new_value;
	dom_string_ref(new_value);

	evt->attr_name = attr_name;
	dom_string_ref(attr_name);

	evt->change = change;

	return _dom_event_init_ns(&evt->base, namespace, type, bubble,
			cancelable);
}