summaryrefslogtreecommitdiff
path: root/content/handlers/javascript/duktape/NamedNodeMap.bnd
blob: c81993c922a51b356b31338f82742a75f99e6e61 (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
/* NamedNodeMap binding for browser using duktape and libdom
 *
 * Copyright 2020 Daniel Silverstone <dsilvers@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 NamedNodeMap {
	private struct dom_namednodemap *map;
};

init NamedNodeMap(struct dom_namednodemap *map)
%{
	priv->map = map;
	dom_namednodemap_ref(map);
%}

fini NamedNodeMap()
%{
	dom_namednodemap_unref(priv->map);
%}

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

	err = dom_namednodemap_get_length(priv->map, &len);

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

	duk_push_uint(ctx, (duk_uint_t)len);

	return 1;
%}

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

	err = dom_namednodemap_item(priv->map, i, &node);

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

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

	return 1;
%}

method NamedNodeMap::getNamedItem()
%{
	duk_size_t size;
	const char *s = duk_safe_to_lstring(ctx, 0, &size);
	dom_exception exc;
	dom_node *attr = NULL;
	dom_string *name = NULL;

	exc = dom_string_create((const uint8_t*)s, size, &name);
	if (exc != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	exc = dom_namednodemap_get_named_item(priv->map, name, &attr);
	dom_string_unref(name);
	if (exc != DOM_NO_ERR) return 0; /* coerced to undefined */
	
	dukky_push_node(ctx, attr);
	dom_node_unref(attr);
	return 1;
%}