1 /* The SpiderMonkey document object 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/form.h"
26 #include "ecmascript/spidermonkey/location.h"
27 #include "ecmascript/spidermonkey/document.h"
28 #include "ecmascript/spidermonkey/window.h"
29 #include "intl/gettext/libintl.h"
30 #include "main/select.h"
31 #include "osdep/newwin.h"
32 #include "osdep/sysname.h"
33 #include "protocol/http/http.h"
34 #include "protocol/uri.h"
35 #include "session/history.h"
36 #include "session/location.h"
37 #include "session/session.h"
38 #include "session/task.h"
39 #include "terminal/tab.h"
40 #include "terminal/terminal.h"
41 #include "util/conv.h"
42 #include "util/memory.h"
43 #include "util/string.h"
44 #include "viewer/text/draw.h"
45 #include "viewer/text/form.h"
46 #include "viewer/text/link.h"
47 #include "viewer/text/vs.h"
50 static JSBool
document_get_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, jsval
*vp
);
51 static JSBool
document_set_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, JSBool strict
, jsval
*vp
);
53 /* Each @document_class object must have a @window_class parent. */
54 const JSClass document_class
= {
57 JS_PropertyStub
, JS_PropertyStub
,
58 document_get_property
, document_set_property
,
59 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
62 /* Tinyids of properties. Use negative values to distinguish these
63 * from array indexes (even though this object has no array elements).
64 * ECMAScript code should not use these directly as in document[-1];
65 * future versions of ELinks may change the numbers. */
72 /* "cookie" is special; it isn't a regular property but we channel it to the
73 * cookie-module. XXX: Would it work if "cookie" was defined in this array? */
74 const JSPropertySpec document_props
[] = {
75 { "location", JSP_DOC_LOC
, JSPROP_ENUMERATE
},
76 { "referrer", JSP_DOC_REF
, JSPROP_ENUMERATE
| JSPROP_READONLY
},
77 { "title", JSP_DOC_TITLE
, JSPROP_ENUMERATE
}, /* TODO: Charset? */
78 { "url", JSP_DOC_URL
, JSPROP_ENUMERATE
},
82 /* @document_class.getProperty */
84 document_get_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, jsval
*vp
)
86 JSObject
*parent_win
; /* instance of @window_class */
87 struct view_state
*vs
;
88 struct document_view
*doc_view
;
89 struct document
*document
;
92 /* This can be called if @obj if not itself an instance of the
93 * appropriate class but has one in its prototype chain. Fail
95 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &document_class
, NULL
))
97 parent_win
= JS_GetParent(ctx
, obj
);
98 assert(JS_InstanceOf(ctx
, parent_win
, (JSClass
*) &window_class
, NULL
));
99 if_assert_failed
return JS_FALSE
;
101 vs
= JS_GetInstancePrivate(ctx
, parent_win
,
102 (JSClass
*) &window_class
, NULL
);
103 doc_view
= vs
->doc_view
;
104 document
= doc_view
->document
;
105 ses
= doc_view
->session
;
107 if (JSID_IS_STRING(id
)) {
109 unsigned char *string
= jsid_to_string(ctx
, &id
);
111 #ifdef CONFIG_COOKIES
112 if (!strcmp(string
, "cookie")) {
113 struct string
*cookies
= send_cookies(vs
->uri
);
116 static unsigned char cookiestr
[1024];
118 strncpy(cookiestr
, cookies
->source
, 1024);
119 done_string(cookies
);
121 string_to_jsval(ctx
, vp
, cookiestr
);
123 string_to_jsval(ctx
, vp
, "");
128 foreach (form
, document
->forms
) {
129 if (!form
->name
|| c_strcasecmp(string
, form
->name
))
132 object_to_jsval(ctx
, vp
, get_form_object(ctx
, obj
, find_form_view(doc_view
, form
)));
138 if (!JSID_IS_INT(id
))
141 undef_to_jsval(ctx
, vp
);
143 switch (JSID_TO_INT(id
)) {
145 JS_GetProperty(ctx
, parent_win
, "location", vp
);
148 switch (get_opt_int("protocol.http.referer.policy", NULL
)) {
151 undef_to_jsval(ctx
, vp
);
155 string_to_jsval(ctx
, vp
, get_opt_str("protocol.http.referer.fake", NULL
));
159 /* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
161 astring_to_jsval(ctx
, vp
, get_uri_string(ses
->referrer
, URI_HTTP_REFERRER
));
165 case REFERER_SAME_URL
:
166 astring_to_jsval(ctx
, vp
, get_uri_string(document
->uri
, URI_HTTP_REFERRER
));
171 string_to_jsval(ctx
, vp
, document
->title
);
174 astring_to_jsval(ctx
, vp
, get_uri_string(document
->uri
, URI_ORIGINAL
));
177 /* Unrecognized integer property ID; someone is using
178 * the object as an array. SMJS builtin classes (e.g.
179 * js_RegExpClass) just return JS_TRUE in this case
180 * and leave *@vp unchanged. Do the same here.
181 * (Actually not quite the same, as we already used
182 * @undef_to_jsval.) */
189 /* @document_class.setProperty */
191 document_set_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, JSBool strict
, jsval
*vp
)
193 JSObject
*parent_win
; /* instance of @window_class */
194 struct view_state
*vs
;
195 struct document_view
*doc_view
;
196 struct document
*document
;
198 /* This can be called if @obj if not itself an instance of the
199 * appropriate class but has one in its prototype chain. Fail
201 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &document_class
, NULL
))
203 parent_win
= JS_GetParent(ctx
, obj
);
204 assert(JS_InstanceOf(ctx
, parent_win
, (JSClass
*) &window_class
, NULL
));
205 if_assert_failed
return JS_FALSE
;
207 vs
= JS_GetInstancePrivate(ctx
, parent_win
,
208 (JSClass
*) &window_class
, NULL
);
209 doc_view
= vs
->doc_view
;
210 document
= doc_view
->document
;
212 if (JSID_IS_STRING(id
)) {
213 #ifdef CONFIG_COOKIES
214 if (!strcmp(jsid_to_string(ctx
, &id
), "cookie")) {
215 set_cookie(vs
->uri
, jsval_to_string(ctx
, vp
));
216 /* Do NOT touch our .cookie property, evil
224 if (!JSID_IS_INT(id
))
227 switch (JSID_TO_INT(id
)) {
229 mem_free_set(&document
->title
, stracpy(jsval_to_string(ctx
, vp
)));
230 print_screen_status(doc_view
->session
);
234 /* According to the specs this should be readonly but some
235 * broken sites still assign to it (i.e.
236 * http://www.e-handelsfonden.dk/validering.asp?URL=www.polyteknisk.dk).
237 * So emulate window.location. */
238 location_goto(doc_view
, jsval_to_string(ctx
, vp
));
245 static JSBool
document_write(JSContext
*ctx
, uintN argc
, jsval
*rval
);
246 static JSBool
document_writeln(JSContext
*ctx
, uintN argc
, jsval
*rval
);
248 const spidermonkeyFunctionSpec document_funcs
[] = {
249 { "write", document_write
, 1 },
250 { "writeln", document_writeln
, 1 },
255 document_write_do(JSContext
*ctx
, uintN argc
, jsval
*rval
, int newline
)
258 struct ecmascript_interpreter
*interpreter
= JS_GetContextPrivate(ctx
);
259 struct string
*ret
= interpreter
->ret
;
260 jsval
*argv
= JS_ARGV(ctx
, rval
);
262 if (argc
>= 1 && ret
) {
265 for (; i
< argc
; ++i
) {
266 unsigned char *code
= jsval_to_string(ctx
, &argv
[i
]);
268 add_to_string(ret
, code
);
272 add_char_to_string(ret
, '\n');
274 /* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
275 * function' errors, I want to see just the useful ones. So just
276 * lighting a led and going away, no muss, no fuss. --pasky */
277 /* TODO: Perhaps we can introduce ecmascript.error_report_unsupported
278 * -> "Show information about the document using some valid,
279 * nevertheless unsupported methods/properties." --pasky too */
282 set_led_value(interpreter
->vs
->doc_view
->session
->status
.ecmascript_led
, 'J');
285 boolean_to_jsval(ctx
, &val
, 0);
286 JS_SET_RVAL(ctx
, rval
, val
);
291 /* @document_funcs{"write"} */
293 document_write(JSContext
*ctx
, uintN argc
, jsval
*rval
)
296 return document_write_do(ctx
, argc
, rval
, 0);
299 /* @document_funcs{"writeln"} */
301 document_writeln(JSContext
*ctx
, uintN argc
, jsval
*rval
)
303 return document_write_do(ctx
, argc
, rval
, 1);