summaryrefslogtreecommitdiff
path: root/content/handlers/javascript/duktape/DOMTokenList.bnd
blob: 928d9ef35a84e3889ce66c183b554a0daeaa215f (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
/* DOMTokenList 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 DOMTokenList {
	private struct dom_tokenlist *tokens;
};

init DOMTokenList(struct dom_tokenlist *tokens)
%{
	priv->tokens = tokens;
	dom_tokenlist_ref(tokens);
%}

fini DOMTokenList()
%{
	dom_tokenlist_unref(priv->tokens);
%}

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

	err = dom_tokenlist_get_length(priv->tokens, &len);

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

	duk_push_uint(ctx, (duk_uint_t)len);

	return 1;
%}

method DOMTokenList::item()
%{
	unsigned long i = duk_to_uint(ctx, 0);
	dom_exception err;
	dom_string *value;

	err = dom_tokenlist_item(priv->tokens, i, &value);

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

    if (value == NULL) {
        duk_push_null(ctx);
        return 1;
    }

    duk_push_lstring(ctx, dom_string_data(value), dom_string_length(value));
	dom_string_unref(value);

	return 1;
%}

method DOMTokenList::add()
%{
	dom_exception exc;
	dom_string *value;
	duk_size_t slen;
	const char *s;
	duk_idx_t spos;

	for (spos = 0; spos < duk_get_top(ctx); ++spos) {
		s = duk_safe_to_lstring(ctx, spos, &slen);

		duk_safe_to_lstring(ctx, 0, &slen);

		exc = dom_string_create_interned((const uint8_t *)s, slen, &value);
		if (exc != DOM_NO_ERR) return 0;

		exc = dom_tokenlist_add(priv->tokens, value);
		dom_string_unref(value);
	}
	
	return 0;
%}

method DOMTokenList::remove()
%{
	dom_exception exc;
	dom_string *value;
	duk_size_t slen;
	const char *s;
	duk_idx_t spos;

	for (spos = 0; spos < duk_get_top(ctx); ++spos) {
		s = duk_safe_to_lstring(ctx, spos, &slen);

		duk_safe_to_lstring(ctx, 0, &slen);

		exc = dom_string_create_interned((const uint8_t *)s, slen, &value);
		if (exc != DOM_NO_ERR) return 0;

		exc = dom_tokenlist_remove(priv->tokens, value);
		dom_string_unref(value);
	}
	
	return 0;
%}

method DOMTokenList::contains()
%{
	dom_exception exc;
	dom_string *value;
	duk_size_t slen;
	const char *s = duk_safe_to_lstring(ctx, 0, &slen);
	bool present = false;

	exc = dom_string_create_interned((const uint8_t *)s, slen, &value);
	if (exc != DOM_NO_ERR) return 0;

	exc = dom_tokenlist_contains(priv->tokens, value, &present);
	dom_string_unref(value);
	if (exc != DOM_NO_ERR) return 0;

	duk_push_boolean(ctx, present);
	
	return 1;
%}

method DOMTokenList::toggle()
%{
	dom_exception exc;
	dom_string *value;
	duk_size_t slen;
	const char *s = duk_require_lstring(ctx, 0, &slen);
	bool toggle_set = duk_get_top(ctx) > 1;
	bool toggle = duk_opt_boolean(ctx, 1, 0);
	bool present;

	exc = dom_string_create_interned((const uint8_t *)s, slen, &value);
	if (exc != DOM_NO_ERR) return 0;

	exc = dom_tokenlist_contains(priv->tokens, value, &present);
	if (exc != DOM_NO_ERR) {
		dom_string_unref(value);
		return 0;
	}

	/* Decision matrix is based on present, toggle_set, and toggle */
	if (toggle_set) {
		if (toggle) {
			exc = dom_tokenlist_add(priv->tokens, value);
		} else {
			exc = dom_tokenlist_remove(priv->tokens, value);
		}
	} else {
		if (present) {
			exc = dom_tokenlist_add(priv->tokens, value);
		} else {
			exc = dom_tokenlist_remove(priv->tokens, value);
		}
	}
	dom_string_unref(value);

	return 0;
%}