summaryrefslogtreecommitdiff
path: root/src/html/html_collection.c
blob: cba2b2d9f49cefc4ed5c2a91ac9b3ec0a8152a5f (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * 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 <assert.h>
#include <stdlib.h>

#include <libwapcaplet/libwapcaplet.h>

#include "html/html_collection.h"
#include "html/html_document.h"

#include "core/node.h"
#include "core/element.h"
#include "core/string.h"

/*-----------------------------------------------------------------------*/
/* Constructor and destructor */

/**
 * Create a dom_html_collection
 *
 * \param doc   The document
 * \param root  The root element of the collection
 * \param ic    The callback function used to determin whether certain node
 *              belongs to the collection
 * \param col   The result collection object
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_html_collection_create(struct dom_html_document *doc,
		struct dom_node_internal *root,
		dom_callback_is_in_collection ic,
		void *ctx,
		struct dom_html_collection **col)
{
	*col = malloc(sizeof(dom_html_collection));
	if (*col == NULL)
		return DOM_NO_MEM_ERR;
	
	return _dom_html_collection_initialise(doc, *col, root, ic, ctx);
}

/**
 * Intialiase a dom_html_collection
 *
 * \param doc   The document
 * \param col   The collection object to be initialised
 * \param root  The root element of the collection
 * \param ic    The callback function used to determin whether certain node
 *              beint32_ts to the collection
 * \return DOM_NO_ERR on success.
 */
dom_exception _dom_html_collection_initialise(struct dom_html_document *doc,
		struct dom_html_collection *col,
		struct dom_node_internal *root,
		dom_callback_is_in_collection ic, void *ctx)
{
	assert(doc != NULL);
	assert(ic != NULL);
	assert(root != NULL);

	col->doc = doc;
	dom_node_ref(doc);

	col->root = root;
	dom_node_ref(root);

	col->ic = ic;
	col->ctx = ctx;
	col->refcnt = 1;

	return DOM_NO_ERR;
}

/**
 * Finalise a dom_html_collection object
 *
 * \param col  The dom_html_collection object
 */
void _dom_html_collection_finalise(struct dom_html_collection *col)
{
	dom_node_unref(col->doc);
	col->doc = NULL;

	dom_node_unref(col->root);
	col->root = NULL;

	col->ic = NULL;
}

/**
 * Destroy a dom_html_collection object
 * \param col  The dom_html_collection object
 */
void _dom_html_collection_destroy(struct dom_html_collection *col)
{
	_dom_html_collection_finalise(col);

	free(col);
}


/*-----------------------------------------------------------------------*/
/* Public API */

/**
 * Get the length of this dom_html_collection
 *
 * \param col  The dom_html_collection object
 * \param len  The returned length of this collection
 * \return DOM_NO_ERR on success.
 */
dom_exception dom_html_collection_get_length(dom_html_collection *col,
		uint32_t *len)
{
	struct dom_node_internal *node = col->root;
	*len = 0;

	while (node != NULL) {
		if (node->type == DOM_ELEMENT_NODE && 
		    col->ic(node, col->ctx) == true)
			(*len)++;

		/* Depth first iterating */
		if (node->first_child != NULL) {
			node = node->first_child;
		} else if (node->next != NULL) {
			node = node->next;
		} else {
			/* No children and siblings */
			struct dom_node_internal *parent = node->parent;

			while (node != col->root &&
					node == parent->last_child) {
				node = parent;
				parent = parent->parent;
			}
			
			if (node == col->root)
				node = NULL;
			else
				node = node->next;
		}
	}

	return DOM_NO_ERR;
}

/**
 * Get the node with certain index
 *
 * \param col  The dom_html_collection object
 * \param index  The index number based on zero
 * \param node   The returned node object
 * \return DOM_NO_ERR on success.
 */
dom_exception dom_html_collection_item(dom_html_collection *col,
		uint32_t index, struct dom_node **node)
{
	struct dom_node_internal *n = col->root;
	uint32_t len = 0;

	while (n != NULL) {
		if (n->type == DOM_ELEMENT_NODE && 
		    col->ic(n, col->ctx) == true)
			len++;

		if (len == index + 1) {
			dom_node_ref(n);
			*node = (struct dom_node *) n;
			return DOM_NO_ERR;
		}

		/* Depth first iterating */
		if (n->first_child != NULL) {
			n = n->first_child;
		} else if (n->next != NULL) {
			n = n->next;
		} else {
			/* No children and siblings */
			struct dom_node_internal *parent = n->parent;

			while (n != col->root &&
					n == parent->last_child) {
				n = parent;
				parent = parent->parent;
			}
			
			if (n == col->root)
				n = NULL;
			else
				n = n->next;
		}
	}

	/* Not find the node */
	*node = NULL;
	return DOM_NO_ERR;
}

/**
 * Get the node in the collection according name
 *
 * \param col   The collection
 * \param name  The name of target node
 * \param node  The returned node object
 * \return DOM_NO_ERR on success.
 */
dom_exception dom_html_collection_named_item(dom_html_collection *col,
		dom_string *name, struct dom_node **node)
{
	struct dom_node_internal *n = col->root;
	dom_html_document *doc = (dom_html_document *)dom_node_get_owner(n);
	dom_exception err;
        while (n != NULL) {
		if (n->type == DOM_ELEMENT_NODE &&
		    col->ic(n, col->ctx) == true) {
			dom_string *id = NULL;
			dom_string *id_name = NULL;

			err = _dom_element_get_id((struct dom_element *) n,
					&id);
			if (err != DOM_NO_ERR) {
				return err;
			}

			if (id != NULL && dom_string_isequal(name, id)) {
				*node = (struct dom_node *) n;
				dom_node_ref(n);
				dom_string_unref(id);

				return DOM_NO_ERR;
			}

			if (id != NULL)
				dom_string_unref(id);

			/* Check for Name attr if id not matched/found */
			err = _dom_element_get_attribute((dom_element *)n,
					doc->memoised[hds_name], &id_name);
			if(err != DOM_NO_ERR) {
				return err;
			}
			if (id_name != NULL && dom_string_isequal(name, id_name)) {
				*node = (struct dom_node *) n;
				dom_node_ref(n);
				dom_string_unref(id_name);

				return DOM_NO_ERR;
			}

		}

		/* Depth first iterating */
		if (n->first_child != NULL) {
			n = n->first_child;
		} else if (n->next != NULL) {
			n = n->next;
		} else {
			/* No children and siblings */
			struct dom_node_internal *parent = n->parent;

			while (n != col->root &&
					n == parent->last_child) {
				n = parent;
				parent = parent->parent;
			}
			
			if (n == col->root)
				n = NULL;
			else
				n = n->next;
		}
	}

	/* Not found the target node */
	*node = NULL;

	return DOM_NO_ERR;
}

/**
 * Claim a reference on this collection
 *
 * \param col  The collection object
 */
void dom_html_collection_ref(dom_html_collection *col)
{
	if (col == NULL)
		return;
	
	col->refcnt ++;
}

/**
 * Relese a reference on this collection
 *
 * \param col  The collection object
 */
void dom_html_collection_unref(dom_html_collection *col)
{
	if (col == NULL)
		return;
	
	if (col->refcnt > 0)
		col->refcnt --;
	
	if (col->refcnt == 0)
		_dom_html_collection_destroy(col);
}