iconv: Bail out of the loop when an illegal sequence of bytes occurs.
[elinks/elinks-j605.git] / src / ecmascript / spidermonkey / document.c
blobe82d264b1b4fc23db90cb3399fc66da44a922b48
1 /* The SpiderMonkey document object 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/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 = {
55 "document",
56 JSCLASS_HAS_PRIVATE,
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. */
66 enum document_prop {
67 JSP_DOC_LOC = -1,
68 JSP_DOC_REF = -2,
69 JSP_DOC_TITLE = -3,
70 JSP_DOC_URL = -4,
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 },
79 { NULL }
82 /* @document_class.getProperty */
83 static JSBool
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;
90 struct session *ses;
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
94 * such calls. */
95 if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
96 return JS_FALSE;
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)) {
108 struct form *form;
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);
115 if (cookies) {
116 static unsigned char cookiestr[1024];
118 strncpy(cookiestr, cookies->source, 1024);
119 done_string(cookies);
121 string_to_jsval(ctx, vp, cookiestr);
122 } else {
123 string_to_jsval(ctx, vp, "");
125 return JS_TRUE;
127 #endif
128 foreach (form, document->forms) {
129 if (!form->name || c_strcasecmp(string, form->name))
130 continue;
132 object_to_jsval(ctx, vp, get_form_object(ctx, obj, find_form_view(doc_view, form)));
133 break;
135 return JS_TRUE;
138 if (!JSID_IS_INT(id))
139 return JS_TRUE;
141 undef_to_jsval(ctx, vp);
143 switch (JSID_TO_INT(id)) {
144 case JSP_DOC_LOC:
145 JS_GetProperty(ctx, parent_win, "location", vp);
146 break;
147 case JSP_DOC_REF:
148 switch (get_opt_int("protocol.http.referer.policy", NULL)) {
149 case REFERER_NONE:
150 /* oh well */
151 undef_to_jsval(ctx, vp);
152 break;
154 case REFERER_FAKE:
155 string_to_jsval(ctx, vp, get_opt_str("protocol.http.referer.fake", NULL));
156 break;
158 case REFERER_TRUE:
159 /* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
160 if (ses->referrer) {
161 astring_to_jsval(ctx, vp, get_uri_string(ses->referrer, URI_HTTP_REFERRER));
163 break;
165 case REFERER_SAME_URL:
166 astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_HTTP_REFERRER));
167 break;
169 break;
170 case JSP_DOC_TITLE:
171 string_to_jsval(ctx, vp, document->title);
172 break;
173 case JSP_DOC_URL:
174 astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_ORIGINAL));
175 break;
176 default:
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.) */
183 break;
186 return JS_TRUE;
189 /* @document_class.setProperty */
190 static JSBool
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
200 * such calls. */
201 if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
202 return JS_FALSE;
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
217 * SpiderMonkey!! */
218 return JS_FALSE;
220 #endif
221 return JS_TRUE;
224 if (!JSID_IS_INT(id))
225 return JS_TRUE;
227 switch (JSID_TO_INT(id)) {
228 case JSP_DOC_TITLE:
229 mem_free_set(&document->title, stracpy(jsval_to_string(ctx, vp)));
230 print_screen_status(doc_view->session);
231 break;
232 case JSP_DOC_LOC:
233 case JSP_DOC_URL:
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));
239 break;
242 return JS_TRUE;
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 },
251 { NULL }
254 static JSBool
255 document_write_do(JSContext *ctx, uintN argc, jsval *rval, int newline)
257 jsval val;
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) {
263 int i = 0;
265 for (; i < argc; ++i) {
266 unsigned char *code = jsval_to_string(ctx, &argv[i]);
268 add_to_string(ret, code);
271 if (newline)
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 */
281 #ifdef CONFIG_LEDS
282 set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
283 #endif
285 boolean_to_jsval(ctx, &val, 0);
286 JS_SET_RVAL(ctx, rval, val);
288 return JS_TRUE;
291 /* @document_funcs{"write"} */
292 static JSBool
293 document_write(JSContext *ctx, uintN argc, jsval *rval)
296 return document_write_do(ctx, argc, rval, 0);
299 /* @document_funcs{"writeln"} */
300 static JSBool
301 document_writeln(JSContext *ctx, uintN argc, jsval *rval)
303 return document_write_do(ctx, argc, rval, 1);