Python: Give goto_url_hook only one argument, like follow_url_hook.
[elinks.git] / src / ecmascript / spidermonkey / location.c
blobe9a2aed71b44fd32945f25b05ae0c6cdd0e172bf
1 /* The SpiderMonkey location and history objects implementation. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "elinks.h"
13 #include "ecmascript/spidermonkey/util.h"
15 #include "bfu/dialog.h"
16 #include "cache/cache.h"
17 #include "cookies/cookies.h"
18 #include "dialogs/menu.h"
19 #include "dialogs/status.h"
20 #include "document/html/frames.h"
21 #include "document/document.h"
22 #include "document/forms.h"
23 #include "document/view.h"
24 #include "ecmascript/ecmascript.h"
25 #include "ecmascript/spidermonkey/location.h"
26 #include "ecmascript/spidermonkey/window.h"
27 #include "intl/gettext/libintl.h"
28 #include "main/select.h"
29 #include "osdep/newwin.h"
30 #include "osdep/sysname.h"
31 #include "protocol/http/http.h"
32 #include "protocol/uri.h"
33 #include "session/history.h"
34 #include "session/location.h"
35 #include "session/session.h"
36 #include "session/task.h"
37 #include "terminal/tab.h"
38 #include "terminal/terminal.h"
39 #include "util/conv.h"
40 #include "util/memory.h"
41 #include "util/string.h"
42 #include "viewer/text/draw.h"
43 #include "viewer/text/form.h"
44 #include "viewer/text/link.h"
45 #include "viewer/text/vs.h"
48 static JSBool history_back(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
49 static JSBool history_forward(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
50 static JSBool history_go(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
52 const JSClass history_class = {
53 "history",
54 JSCLASS_HAS_PRIVATE,
55 JS_PropertyStub, JS_PropertyStub,
56 JS_PropertyStub, JS_PropertyStub,
57 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
60 const JSFunctionSpec history_funcs[] = {
61 { "back", history_back, 0 },
62 { "forward", history_forward, 0 },
63 { "go", history_go, 1 },
64 { NULL }
67 /* @history_funcs{"back"} */
68 static JSBool
69 history_back(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
71 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
72 struct document_view *doc_view = interpreter->vs->doc_view;
73 struct session *ses = doc_view->session;
75 go_back(ses);
77 /* history_back() must return 0 for onClick to cause displaying previous page
78 * and return non zero for <a href="javascript:history.back()"> to prevent
79 * "calculating" new link. Returned value 2 is changed to 0 in function
80 * spidermonkey_eval_boolback */
81 return 2;
84 /* @history_funcs{"forward"} */
85 static JSBool
86 history_forward(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
88 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
89 struct document_view *doc_view = interpreter->vs->doc_view;
90 struct session *ses = doc_view->session;
92 go_unback(ses);
94 return 2;
97 /* @history_funcs{"go"} */
98 static JSBool
99 history_go(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
101 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
102 struct document_view *doc_view = interpreter->vs->doc_view;
103 struct session *ses = doc_view->session;
104 int index;
105 struct location *loc;
107 if (argc != 1)
108 return JS_TRUE;
110 index = atol(jsval_to_string(ctx, &argv[0]));
112 for (loc = cur_loc(ses);
113 loc != (struct location *) &ses->history.history;
114 loc = index > 0 ? loc->next : loc->prev) {
115 if (!index) {
116 go_history(ses, loc);
117 break;
120 index += index > 0 ? -1 : 1;
123 return 2;
127 static JSBool location_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp);
128 static JSBool location_set_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp);
130 /* Each @location_class object must have a @window_class parent. */
131 const JSClass location_class = {
132 "location",
133 JSCLASS_HAS_PRIVATE,
134 JS_PropertyStub, JS_PropertyStub,
135 location_get_property, location_set_property,
136 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
139 /* Tinyids of properties. Use negative values to distinguish these
140 * from array indexes (even though this object has no array elements).
141 * ECMAScript code should not use these directly as in location[-1];
142 * future versions of ELinks may change the numbers. */
143 enum location_prop {
144 JSP_LOC_HREF = -1,
146 const JSPropertySpec location_props[] = {
147 { "href", JSP_LOC_HREF, JSPROP_ENUMERATE },
148 { NULL }
151 /* @location_class.getProperty */
152 static JSBool
153 location_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
155 JSObject *parent_win; /* instance of @window_class */
156 struct view_state *vs;
158 /* This can be called if @obj if not itself an instance of the
159 * appropriate class but has one in its prototype chain. Fail
160 * such calls. */
161 if (!JS_InstanceOf(ctx, obj, (JSClass *) &location_class, NULL))
162 return JS_FALSE;
163 parent_win = JS_GetParent(ctx, obj);
164 assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
165 if_assert_failed return JS_FALSE;
167 vs = JS_GetPrivate(ctx, parent_win); /* from @window_class */
169 if (!JSVAL_IS_INT(id))
170 return JS_TRUE;
172 undef_to_jsval(ctx, vp);
174 switch (JSVAL_TO_INT(id)) {
175 case JSP_LOC_HREF:
176 astring_to_jsval(ctx, vp, get_uri_string(vs->uri, URI_ORIGINAL));
177 break;
178 default:
179 /* Unrecognized property ID; someone is using the
180 * object as an array. SMJS builtin classes (e.g.
181 * js_RegExpClass) just return JS_TRUE in this case
182 * and leave *@vp unchanged. Do the same here.
183 * (Actually not quite the same, as we already used
184 * @undef_to_jsval.) */
185 break;
188 return JS_TRUE;
191 /* @location_class.setProperty */
192 static JSBool
193 location_set_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
195 JSObject *parent_win; /* instance of @window_class */
196 struct view_state *vs;
197 struct document_view *doc_view;
199 /* This can be called if @obj if not itself an instance of the
200 * appropriate class but has one in its prototype chain. Fail
201 * such calls. */
202 if (!JS_InstanceOf(ctx, obj, (JSClass *) &location_class, NULL))
203 return JS_FALSE;
204 parent_win = JS_GetParent(ctx, obj);
205 assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
206 if_assert_failed return JS_FALSE;
208 vs = JS_GetPrivate(ctx, parent_win); /* from @window_class */
209 doc_view = vs->doc_view;
211 if (!JSVAL_IS_INT(id))
212 return JS_TRUE;
214 switch (JSVAL_TO_INT(id)) {
215 case JSP_LOC_HREF:
216 location_goto(doc_view, jsval_to_string(ctx, vp));
217 break;
220 return JS_TRUE;
223 static JSBool location_toString(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
225 const JSFunctionSpec location_funcs[] = {
226 { "toString", location_toString, 0 },
227 { "toLocaleString", location_toString, 0 },
228 { NULL }
231 /* @location_funcs{"toString"}, @location_funcs{"toLocaleString"} */
232 static JSBool
233 location_toString(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
235 return JS_GetProperty(ctx, obj, "href", rval);
238 struct delayed_goto {
239 /* It might look more convenient to pass doc_view around but it could
240 * disappear during wild dances inside of frames or so. */
241 struct view_state *vs;
242 struct uri *uri;
245 static void
246 delayed_goto(void *data)
248 struct delayed_goto *deg = data;
250 assert(deg);
251 if (deg->vs->doc_view
252 && deg->vs->doc_view == deg->vs->doc_view->session->doc_view) {
253 goto_uri_frame(deg->vs->doc_view->session, deg->uri,
254 deg->vs->doc_view->name,
255 CACHE_MODE_NORMAL);
257 done_uri(deg->uri);
258 mem_free(deg);
261 void
262 location_goto(struct document_view *doc_view, unsigned char *url)
264 unsigned char *new_abs_url;
265 struct uri *new_uri;
266 struct delayed_goto *deg;
268 /* Workaround for bug 611. Does not crash, but may lead to infinite loop.*/
269 if (!doc_view) return;
270 new_abs_url = join_urls(doc_view->document->uri,
271 trim_chars(url, ' ', 0));
272 if (!new_abs_url)
273 return;
274 new_uri = get_uri(new_abs_url, 0);
275 mem_free(new_abs_url);
276 if (!new_uri)
277 return;
278 deg = mem_calloc(1, sizeof(*deg));
279 if (!deg) {
280 done_uri(new_uri);
281 return;
283 assert(doc_view->vs);
284 deg->vs = doc_view->vs;
285 deg->uri = new_uri;
286 /* It does not seem to be very safe inside of frames to
287 * call goto_uri() right away. */
288 register_bottom_half(delayed_goto, deg);