1 /*
2  * This file is part of libdom.
3  * Licensed under the MIT License,
4  *                http://www.opensource.org/licenses/mit-license.php
5  * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
6  */
7 
8 #include <stdlib.h>
9 
10 #include "events/mutation_event.h"
11 
12 static void _virtual_dom_mutation_event_destroy(struct dom_event *evt);
13 
14 static struct dom_event_private_vtable _event_vtable = {
15 	_virtual_dom_mutation_event_destroy
16 };
17 
18 /* Constructor */
_dom_mutation_event_create(struct dom_mutation_event ** evt)19 dom_exception _dom_mutation_event_create(struct dom_mutation_event **evt)
20 {
21 	*evt = malloc(sizeof(dom_mutation_event));
22 	if (*evt == NULL)
23 		return DOM_NO_MEM_ERR;
24 
25 	((struct dom_event *) *evt)->vtable = &_event_vtable;
26 
27 	return _dom_mutation_event_initialise(*evt);
28 }
29 
30 /* Destructor */
_dom_mutation_event_destroy(struct dom_mutation_event * evt)31 void _dom_mutation_event_destroy(struct dom_mutation_event *evt)
32 {
33 	_dom_mutation_event_finalise(evt);
34 
35 	free(evt);
36 }
37 
38 /* Initialise function */
_dom_mutation_event_initialise(struct dom_mutation_event * evt)39 dom_exception _dom_mutation_event_initialise(struct dom_mutation_event *evt)
40 {
41 	evt->related_node = NULL;
42 	evt->prev_value = NULL;
43 	evt->new_value = NULL;
44 	evt->attr_name = NULL;
45 
46 	return _dom_event_initialise(&evt->base);
47 }
48 
49 /* Finalise function */
_dom_mutation_event_finalise(struct dom_mutation_event * evt)50 void _dom_mutation_event_finalise(struct dom_mutation_event *evt)
51 {
52 	dom_node_unref(evt->related_node);
53 	dom_string_unref(evt->prev_value);
54 	dom_string_unref(evt->new_value);
55 	dom_string_unref(evt->attr_name);
56 
57 	evt->related_node = NULL;
58 	evt->prev_value = NULL;
59 	evt->new_value = NULL;
60 	evt->attr_name = NULL;
61 
62 	_dom_event_finalise(&evt->base);
63 }
64 
65 /* The virtual destroy function */
_virtual_dom_mutation_event_destroy(struct dom_event * evt)66 void _virtual_dom_mutation_event_destroy(struct dom_event *evt)
67 {
68 	_dom_mutation_event_destroy((dom_mutation_event *) evt);
69 }
70 
71 /*----------------------------------------------------------------------*/
72 /* The public API */
73 
74 /**
75  * Get the related node
76  *
77  * \param evt   The Event object
78  * \param node  The related node
79  * \return DOM_NO_ERR.
80  */
_dom_mutation_event_get_related_node(dom_mutation_event * evt,struct dom_node ** node)81 dom_exception _dom_mutation_event_get_related_node(dom_mutation_event *evt,
82 		struct dom_node **node)
83 {
84 	*node = evt->related_node;
85 	dom_node_ref(*node);
86 
87 	return DOM_NO_ERR;
88 }
89 
90 /**
91  * Get the old value
92  *
93  * \param evt  The Event object
94  * \param ret  The old value
95  * \return DOM_NO_ERR.
96  */
_dom_mutation_event_get_prev_value(dom_mutation_event * evt,dom_string ** ret)97 dom_exception _dom_mutation_event_get_prev_value(dom_mutation_event *evt,
98 		dom_string **ret)
99 {
100 	*ret = evt->prev_value;
101 	dom_string_ref(*ret);
102 
103 	return DOM_NO_ERR;
104 }
105 
106 /**
107  * Get the new value
108  *
109  * \param evt  The Event object
110  * \param ret  The new value
111  * \return DOM_NO_ERR.
112  */
_dom_mutation_event_get_new_value(dom_mutation_event * evt,dom_string ** ret)113 dom_exception _dom_mutation_event_get_new_value(dom_mutation_event *evt,
114 		dom_string **ret)
115 {
116 	*ret = evt->new_value;
117 	dom_string_ref(*ret);
118 
119 	return DOM_NO_ERR;
120 }
121 
122 /**
123  * Get the attr name
124  *
125  * \param evt  The Event object
126  * \param ret  The attribute name
127  * \return DOM_NO_ERR.
128  */
_dom_mutation_event_get_attr_name(dom_mutation_event * evt,dom_string ** ret)129 dom_exception _dom_mutation_event_get_attr_name(dom_mutation_event *evt,
130 		dom_string **ret)
131 {
132 	*ret = evt->attr_name;
133 	dom_string_ref(*ret);
134 
135 	return DOM_NO_ERR;
136 }
137 
138 /**
139  * Get the way the attribute change
140  *
141  * \param evt   The Event object
142  * \param type  The change type
143  * \return DOM_NO_ERR.
144  */
_dom_mutation_event_get_attr_change(dom_mutation_event * evt,dom_mutation_type * type)145 dom_exception _dom_mutation_event_get_attr_change(dom_mutation_event *evt,
146 		dom_mutation_type *type)
147 {
148 	*type = evt->change;
149 
150 	return DOM_NO_ERR;
151 }
152 
153 /**
154  * Initialise the MutationEvent
155  *
156  * \param evt         The Event object
157  * \param type        The type of this UIEvent
158  * \param bubble      Whether this event can bubble
159  * \param cancelable  Whether this event is cancelable
160  * \param node        The mutation node
161  * \param prev_value  The old value
162  * \param new_value   The new value
163  * \param attr_name   The attribute's name
164  * \param change      The change type
165  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
166  */
_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)167 dom_exception _dom_mutation_event_init(dom_mutation_event *evt,
168 		dom_string *type, bool bubble, bool cancelable,
169 		struct dom_node *node, dom_string *prev_value,
170 		dom_string *new_value, dom_string *attr_name,
171 		dom_mutation_type change)
172 {
173 	evt->related_node = node;
174 	dom_node_ref(node);
175 
176 	evt->prev_value = prev_value;
177 	dom_string_ref(prev_value);
178 
179 	evt->new_value = new_value;
180 	dom_string_ref(new_value);
181 
182 	evt->attr_name = attr_name;
183 	dom_string_ref(attr_name);
184 
185 	evt->change = change;
186 
187 	return _dom_event_init(&evt->base, type, bubble, cancelable);
188 }
189 
190 /**
191  * Initialise the MutationEvent with namespace
192  *
193  * \param evt         The Event object
194  * \param namespace   The namespace
195  * \param type        The type of this UIEvent
196  * \param bubble      Whether this event can bubble
197  * \param cancelable  Whether this event is cancelable
198  * \param node        The mutation node
199  * \param prev_value  The old value
200  * \param new_value   The new value
201  * \param attr_name   The attribute's name
202  * \param change      The change type
203  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
204  */
_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)205 dom_exception _dom_mutation_event_init_ns(dom_mutation_event *evt,
206 		dom_string *namespace, dom_string *type,
207 		bool bubble, bool cancelable, struct dom_node *node,
208 		dom_string *prev_value, dom_string *new_value,
209 		dom_string *attr_name, dom_mutation_type change)
210 {
211 	evt->related_node = node;
212 	dom_node_ref(node);
213 
214 	evt->prev_value = prev_value;
215 	dom_string_ref(prev_value);
216 
217 	evt->new_value = new_value;
218 	dom_string_ref(new_value);
219 
220 	evt->attr_name = attr_name;
221 	dom_string_ref(attr_name);
222 
223 	evt->change = change;
224 
225 	return _dom_event_init_ns(&evt->base, namespace, type, bubble,
226 			cancelable);
227 }
228 
229