summaryrefslogtreecommitdiff
path: root/javascript/jsapi/location.bnd
blob: 03687ba1831ed3379060ddc529eae5f91d730ab1 (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
/* Binding to generate Location interface
 *
 * Copyright 2012 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
 */

webidlfile "html.idl";

hdrcomment "Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>";
hdrcomment "This file is part of NetSurf, http://www.netsurf-browser.org/";
hdrcomment "Released under the terms of the MIT License,";
hdrcomment "        http://www.opensource.org/licenses/mit-license";

preamble %{

#include "desktop/browser.h"
        
#include "utils/config.h"
#include "utils/log.h"
#include "javascript/jsapi.h"
#include "render/html_internal.h"

#include "location.h"

struct browser_window *jsapi_get_browser_window(JSContext *cx);

%}

binding location {
	type js_libdom; /* the binding type */

	interface Location; /* Web IDL interface to generate */

	private "nsurl *" url;

	property unshared href;

}

operation reload %{
	struct browser_window *bw;
	bw = jsapi_get_browser_window(cx);
	if (bw != NULL) {
		browser_window_reload(bw, false);
	} else {
		JSLOG("failed to get browser context");
	}
%}

/* void Location::assign(DOMString url); */
operation assign %{
	nsurl *joined;
	struct browser_window *bw;

	bw = jsapi_get_browser_window(cx);
	if (bw != NULL) {
		nsurl_join(private->url, url, &joined);
		browser_window_go(bw, nsurl_access(joined), NULL, true);
		nsurl_unref(joined);
	} else {
		JSLOG("failed to get browser context");
	}
%}

/* void Location::replace(DOMString url); */
operation replace %{
	nsurl *joined;
	struct browser_window *bw;

	bw = jsapi_get_browser_window(cx);
	if (bw != NULL) {
		nsurl_join(private->url, url, &joined);
		browser_window_go(bw, nsurl_access(joined), NULL, false);
		nsurl_unref(joined);
	} else {
		JSLOG("failed to get browser context");
	}
%}

getter href %{
	char *url_s = NULL;
	size_t url_l;

	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx,vp))) {
		/* already created - return it */
		return JS_TRUE;
	}

	nsurl_get(private->url, NSURL_COMPLETE, &url_s, &url_l);
	if (url_s != NULL) {
		jsret = JS_NewStringCopyN(cx, url_s, url_l);
		free(url_s);		
	}
%}

setter href %{
	JSString *url_jsstr = NULL;
	int url_len = 0;
	char *url = NULL;
	struct browser_window *bw;
	nsurl *joined;

	bw = jsapi_get_browser_window(cx);

	if (bw != NULL) {
		url_jsstr = JS_ValueToString(cx, *vp);
		if (url_jsstr != NULL) {
			JSString_to_char(url_jsstr, url, url_len);

			nsurl_join(private->url, url, &joined);

			browser_window_go(bw, nsurl_access(joined), NULL, false);
			nsurl_unref(joined);
		} else {
			JSLOG("failed to convert string value");
		}
	} else {
		JSLOG("failed to get browser context");
	}
%}

getter protocol %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_SCHEME);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}
%}

getter host %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_HOST);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}
%}

getter hostname %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_HOST);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}

%}

getter port %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_PORT);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}

%}

getter pathname %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_PATH);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}

%}

getter search %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_QUERY);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}

%}

getter hash %{
	lwc_string *component;
	component = nsurl_get_component(private->url, NSURL_FRAGMENT);
	if (component != NULL) {
		jsret = JS_NewStringCopyN(cx,
					  lwc_string_data(component),
					  lwc_string_length(component));
		lwc_string_unref(component);
	}
%}