summaryrefslogtreecommitdiff
path: root/javascript/duktape/NodeList.bnd
blob: 7ddf56d15b1497f6a88713ec8e0dd5d3422ca8e7 (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
/* NodeList binding for browser using duktape and libdom
 *
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * Released under the terms of the MIT License,
 *         http://www.opensource.org/licenses/mit-license
 */

class NodeList {
	private struct dom_nodelist *nodes;
};

init NodeList(struct dom_nodelist *nodes)
%{
	priv->nodes = nodes;
	dom_nodelist_ref(nodes);
%}

fini NodeList()
%{
	dom_nodelist_unref(priv->nodes);
%}

getter NodeList::length()
%{
	dom_exception err;
	uint32_t len;

	err = dom_nodelist_get_length(priv->nodes, &len);

	if (err != DOM_NO_ERR) return 0; /* coerced to undefined */

	duk_push_uint(ctx, (duk_uint_t)len);

	return 1;
%}

method NodeList::item()
%{
	unsigned long i = duk_to_uint(ctx, 0);
	dom_exception err;
	dom_node *node;

	err = dom_nodelist_item(priv->nodes, i, &node);

	if (err != DOM_NO_ERR) return 0; /* coerced to undefied */

	dukky_push_node(ctx, node);
	dom_node_unref(node);

	return 1;
%}