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
, 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
= {
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
},
73 /* @document_class.getProperty */
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
;
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
86 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &document_class
, NULL
))
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
)) {
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
);
107 static unsigned char cookiestr
[1024];
109 strncpy(cookiestr
, cookies
->source
, 1024);
110 done_string(cookies
);
112 string_to_jsval(ctx
, vp
, cookiestr
);
114 string_to_jsval(ctx
, vp
, "");
119 foreach (form
, document
->forms
) {
120 if (!form
->name
|| strcasecmp(string
, form
->name
))
123 object_to_jsval(ctx
, vp
, get_form_object(ctx
, obj
, find_form_view(doc_view
, form
)));
129 if (!JSVAL_IS_INT(id
))
132 undef_to_jsval(ctx
, vp
);
134 switch (JSVAL_TO_INT(id
)) {
136 switch (get_opt_int("protocol.http.referer.policy")) {
139 undef_to_jsval(ctx
, vp
);
143 string_to_jsval(ctx
, vp
, get_opt_str("protocol.http.referer.fake"));
147 /* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
149 astring_to_jsval(ctx
, vp
, get_uri_string(ses
->referrer
, URI_HTTP_REFERRER
));
153 case REFERER_SAME_URL
:
154 astring_to_jsval(ctx
, vp
, get_uri_string(document
->uri
, URI_HTTP_REFERRER
));
159 string_to_jsval(ctx
, vp
, document
->title
);
162 astring_to_jsval(ctx
, vp
, get_uri_string(document
->uri
, URI_ORIGINAL
));
165 /* Unrecognized integer property ID; someone is using
166 * the 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.) */
177 /* @document_class.setProperty */
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
189 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &document_class
, NULL
))
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
212 if (!JSVAL_IS_INT(id
))
215 switch (JSVAL_TO_INT(id
)) {
217 mem_free_set(&document
->title
, stracpy(jsval_to_string(ctx
, vp
)));
218 print_screen_status(doc_view
->session
);
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
));
232 static JSBool
document_write(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
);
234 const spidermonkeyFunctionSpec document_funcs
[] = {
235 { "write", document_write
, 1 },
239 /* @document_funcs{"write"} */
241 document_write(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
)
244 struct ecmascript_interpreter
*interpreter
= JS_GetContextPrivate(ctx
);
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 */
255 set_led_value(interpreter
->vs
->doc_view
->session
->status
.ecmascript_led
, 'J');
258 boolean_to_jsval(ctx
, rval
, 0);