SMJS: Use JS_GetInstancePrivate where applicable.
[elinks.git] / src / ecmascript / spidermonkey / document.c
blobb29c2e9f9951410cf050109cbc58ac7ba201c534
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, jsval id, jsval *vp);
51 static JSBool document_set_property(JSContext *ctx, JSObject *obj, jsval id, 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 enum document_prop { JSP_DOC_REF, JSP_DOC_TITLE, JSP_DOC_URL };
63 /* "cookie" is special; it isn't a regular property but we channel it to the
64 * cookie-module. XXX: Would it work if "cookie" was defined in this array? */
65 const JSPropertySpec document_props[] = {
66 { "location", JSP_DOC_URL, JSPROP_ENUMERATE },
67 { "referrer", JSP_DOC_REF, JSPROP_ENUMERATE | JSPROP_READONLY },
68 { "title", JSP_DOC_TITLE, JSPROP_ENUMERATE }, /* TODO: Charset? */
69 { "url", JSP_DOC_URL, JSPROP_ENUMERATE },
70 { NULL }
73 /* @document_class.getProperty */
74 static JSBool
75 document_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
77 JSObject *parent_win; /* instance of @window_class */
78 struct view_state *vs;
79 struct document_view *doc_view;
80 struct document *document;
81 struct session *ses;
83 /* This can be called if @obj if not itself an instance of the
84 * appropriate class but has one in its prototype chain. Fail
85 * such calls. */
86 if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
87 return JS_FALSE;
88 parent_win = JS_GetParent(ctx, obj);
89 assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
90 if_assert_failed return JS_FALSE;
92 vs = JS_GetInstancePrivate(ctx, parent_win,
93 (JSClass *) &window_class, NULL);
94 doc_view = vs->doc_view;
95 document = doc_view->document;
96 ses = doc_view->session;
98 if (JSVAL_IS_STRING(id)) {
99 struct form *form;
100 unsigned char *string = jsval_to_string(ctx, &id);
102 #ifdef CONFIG_COOKIES
103 if (!strcmp(string, "cookie")) {
104 struct string *cookies = send_cookies(vs->uri);
106 if (cookies) {
107 static unsigned char cookiestr[1024];
109 strncpy(cookiestr, cookies->source, 1024);
110 done_string(cookies);
112 string_to_jsval(ctx, vp, cookiestr);
113 } else {
114 string_to_jsval(ctx, vp, "");
116 return JS_TRUE;
118 #endif
119 foreach (form, document->forms) {
120 if (!form->name || strcasecmp(string, form->name))
121 continue;
123 object_to_jsval(ctx, vp, get_form_object(ctx, obj, find_form_view(doc_view, form)));
124 break;
126 return JS_TRUE;
129 if (!JSVAL_IS_INT(id))
130 return JS_TRUE;
132 undef_to_jsval(ctx, vp);
134 switch (JSVAL_TO_INT(id)) {
135 case JSP_DOC_REF:
136 switch (get_opt_int("protocol.http.referer.policy")) {
137 case REFERER_NONE:
138 /* oh well */
139 undef_to_jsval(ctx, vp);
140 break;
142 case REFERER_FAKE:
143 string_to_jsval(ctx, vp, get_opt_str("protocol.http.referer.fake"));
144 break;
146 case REFERER_TRUE:
147 /* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
148 if (ses->referrer) {
149 astring_to_jsval(ctx, vp, get_uri_string(ses->referrer, URI_HTTP_REFERRER));
151 break;
153 case REFERER_SAME_URL:
154 astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_HTTP_REFERRER));
155 break;
157 break;
158 case JSP_DOC_TITLE:
159 string_to_jsval(ctx, vp, document->title);
160 break;
161 case JSP_DOC_URL:
162 astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_ORIGINAL));
163 break;
164 default:
165 /* Unrecognized property ID; someone is using the
166 * object as an array. SMJS builtin classes (e.g.
167 * js_RegExpClass) just return JS_TRUE in this case
168 * and leave *@vp unchanged. Do the same here.
169 * (Actually not quite the same, as we already used
170 * @undef_to_jsval.) */
171 break;
174 return JS_TRUE;
177 /* @document_class.setProperty */
178 static JSBool
179 document_set_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
181 JSObject *parent_win; /* instance of @window_class */
182 struct view_state *vs;
183 struct document_view *doc_view;
184 struct document *document;
186 /* This can be called if @obj if not itself an instance of the
187 * appropriate class but has one in its prototype chain. Fail
188 * such calls. */
189 if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
190 return JS_FALSE;
191 parent_win = JS_GetParent(ctx, obj);
192 assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
193 if_assert_failed return JS_FALSE;
195 vs = JS_GetInstancePrivate(ctx, parent_win,
196 (JSClass *) &window_class, NULL);
197 doc_view = vs->doc_view;
198 document = doc_view->document;
200 if (JSVAL_IS_STRING(id)) {
201 #ifdef CONFIG_COOKIES
202 if (!strcmp(jsval_to_string(ctx, &id), "cookie")) {
203 set_cookie(vs->uri, jsval_to_string(ctx, vp));
204 /* Do NOT touch our .cookie property, evil
205 * SpiderMonkey!! */
206 return JS_FALSE;
208 #endif
209 return JS_TRUE;
212 if (!JSVAL_IS_INT(id))
213 return JS_TRUE;
215 switch (JSVAL_TO_INT(id)) {
216 case JSP_DOC_TITLE:
217 mem_free_set(&document->title, stracpy(jsval_to_string(ctx, vp)));
218 print_screen_status(doc_view->session);
219 break;
220 case JSP_DOC_URL:
221 /* According to the specs this should be readonly but some
222 * broken sites still assign to it (i.e.
223 * http://www.e-handelsfonden.dk/validering.asp?URL=www.polyteknisk.dk).
224 * So emulate window.location. */
225 location_goto(doc_view, jsval_to_string(ctx, vp));
226 break;
229 return JS_TRUE;
232 static JSBool document_write(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
234 const JSFunctionSpec document_funcs[] = {
235 { "write", document_write, 1 },
236 { NULL }
239 /* @document_funcs{"write"} */
240 static JSBool
241 document_write(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
243 #ifdef CONFIG_LEDS
244 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
245 #endif
247 /* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
248 * function' errors, I want to see just the useful ones. So just
249 * lighting a led and going away, no muss, no fuss. --pasky */
250 /* TODO: Perhaps we can introduce ecmascript.error_report_unsupported
251 * -> "Show information about the document using some valid,
252 * nevertheless unsupported methods/properties." --pasky too */
254 #ifdef CONFIG_LEDS
255 set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
256 #endif
258 boolean_to_jsval(ctx, rval, 0);
260 return JS_TRUE;