1 /* The SpiderMonkey location and history objects implementation. */
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
= {
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 },
67 /* @history_funcs{"back"} */
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
;
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 */
84 /* @history_funcs{"forward"} */
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
;
97 /* @history_funcs{"go"} */
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
;
105 struct location
*loc
;
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
) {
116 go_history(ses
, loc
);
120 index
+= index
> 0 ? -1 : 1;
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
= {
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. */
146 const JSPropertySpec location_props
[] = {
147 { "href", JSP_LOC_HREF
, JSPROP_ENUMERATE
},
151 /* @location_class.getProperty */
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
161 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &location_class
, NULL
))
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
))
172 undef_to_jsval(ctx
, vp
);
174 switch (JSVAL_TO_INT(id
)) {
176 astring_to_jsval(ctx
, vp
, get_uri_string(vs
->uri
, URI_ORIGINAL
));
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.) */
191 /* @location_class.setProperty */
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
202 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &location_class
, NULL
))
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
))
214 switch (JSVAL_TO_INT(id
)) {
216 location_goto(doc_view
, jsval_to_string(ctx
, vp
));
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 },
231 /* @location_funcs{"toString"}, @location_funcs{"toLocaleString"} */
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
;
246 delayed_goto(void *data
)
248 struct delayed_goto
*deg
= data
;
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
,
262 location_goto(struct document_view
*doc_view
, unsigned char *url
)
264 unsigned char *new_abs_url
;
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));
274 new_uri
= get_uri(new_abs_url
, 0);
275 mem_free(new_abs_url
);
278 deg
= mem_calloc(1, sizeof(*deg
));
283 assert(doc_view
->vs
);
284 deg
->vs
= doc_view
->vs
;
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
);