2005-09-15 09:58:31 -04:00
|
|
|
/* The SpiderMonkey document object implementation. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "ecmascript/spidermonkey/util.h"
|
|
|
|
|
|
|
|
#include "bfu/dialog.h"
|
|
|
|
#include "cache/cache.h"
|
|
|
|
#include "cookies/cookies.h"
|
|
|
|
#include "dialogs/menu.h"
|
|
|
|
#include "dialogs/status.h"
|
|
|
|
#include "document/html/frames.h"
|
|
|
|
#include "document/document.h"
|
|
|
|
#include "document/forms.h"
|
|
|
|
#include "document/view.h"
|
|
|
|
#include "ecmascript/ecmascript.h"
|
|
|
|
#include "ecmascript/spidermonkey/form.h"
|
|
|
|
#include "ecmascript/spidermonkey/location.h"
|
|
|
|
#include "ecmascript/spidermonkey/document.h"
|
2006-11-25 01:54:58 -05:00
|
|
|
#include "ecmascript/spidermonkey/window.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "intl/gettext/libintl.h"
|
|
|
|
#include "main/select.h"
|
|
|
|
#include "osdep/newwin.h"
|
|
|
|
#include "osdep/sysname.h"
|
|
|
|
#include "protocol/http/http.h"
|
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "session/history.h"
|
|
|
|
#include "session/location.h"
|
|
|
|
#include "session/session.h"
|
|
|
|
#include "session/task.h"
|
|
|
|
#include "terminal/tab.h"
|
|
|
|
#include "terminal/terminal.h"
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "viewer/text/draw.h"
|
|
|
|
#include "viewer/text/form.h"
|
|
|
|
#include "viewer/text/link.h"
|
|
|
|
#include "viewer/text/vs.h"
|
|
|
|
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
static JSBool document_get_property(JSContext *ctx, JSObject *obj, jsid id, jsval *vp);
|
|
|
|
static JSBool document_set_property(JSContext *ctx, JSObject *obj, jsid id, JSBool strict, jsval *vp);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* Each @document_class object must have a @window_class parent. */
|
2005-09-15 09:58:31 -04:00
|
|
|
const JSClass document_class = {
|
|
|
|
"document",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
document_get_property, document_set_property,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
|
|
|
|
};
|
|
|
|
|
2006-12-06 16:09:14 -05:00
|
|
|
/* Tinyids of properties. Use negative values to distinguish these
|
|
|
|
* from array indexes (even though this object has no array elements).
|
|
|
|
* ECMAScript code should not use these directly as in document[-1];
|
|
|
|
* future versions of ELinks may change the numbers. */
|
|
|
|
enum document_prop {
|
|
|
|
JSP_DOC_LOC = -1,
|
|
|
|
JSP_DOC_REF = -2,
|
|
|
|
JSP_DOC_TITLE = -3,
|
|
|
|
JSP_DOC_URL = -4,
|
|
|
|
};
|
2005-09-15 09:58:31 -04:00
|
|
|
/* "cookie" is special; it isn't a regular property but we channel it to the
|
|
|
|
* cookie-module. XXX: Would it work if "cookie" was defined in this array? */
|
|
|
|
const JSPropertySpec document_props[] = {
|
2006-02-01 03:31:26 -05:00
|
|
|
{ "location", JSP_DOC_LOC, JSPROP_ENUMERATE },
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "referrer", JSP_DOC_REF, JSPROP_ENUMERATE | JSPROP_READONLY },
|
|
|
|
{ "title", JSP_DOC_TITLE, JSPROP_ENUMERATE }, /* TODO: Charset? */
|
|
|
|
{ "url", JSP_DOC_URL, JSPROP_ENUMERATE },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @document_class.getProperty */
|
2005-09-15 09:58:31 -04:00
|
|
|
static JSBool
|
2011-04-19 16:41:05 -04:00
|
|
|
document_get_property(JSContext *ctx, JSObject *obj, jsid id, jsval *vp)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2006-11-24 01:50:12 -05:00
|
|
|
JSObject *parent_win; /* instance of @window_class */
|
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
struct session *ses;
|
|
|
|
|
2006-12-03 04:14:22 -05:00
|
|
|
/* This can be called if @obj if not itself an instance of the
|
|
|
|
* appropriate class but has one in its prototype chain. Fail
|
|
|
|
* such calls. */
|
|
|
|
if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
|
|
|
|
return JS_FALSE;
|
2006-11-24 01:50:12 -05:00
|
|
|
parent_win = JS_GetParent(ctx, obj);
|
2006-11-25 01:54:58 -05:00
|
|
|
assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
|
|
|
|
if_assert_failed return JS_FALSE;
|
|
|
|
|
2007-05-27 11:32:53 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, parent_win,
|
|
|
|
(JSClass *) &window_class, NULL);
|
2006-11-24 01:50:12 -05:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
|
|
|
ses = doc_view->session;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
struct form *form;
|
2011-04-19 16:41:05 -04:00
|
|
|
unsigned char *string = jsid_to_string(ctx, &id);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_COOKIES
|
|
|
|
if (!strcmp(string, "cookie")) {
|
|
|
|
struct string *cookies = send_cookies(vs->uri);
|
|
|
|
|
|
|
|
if (cookies) {
|
|
|
|
static unsigned char cookiestr[1024];
|
|
|
|
|
|
|
|
strncpy(cookiestr, cookies->source, 1024);
|
|
|
|
done_string(cookies);
|
|
|
|
|
|
|
|
string_to_jsval(ctx, vp, cookiestr);
|
|
|
|
} else {
|
|
|
|
string_to_jsval(ctx, vp, "");
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
foreach (form, document->forms) {
|
2008-10-18 21:25:00 -04:00
|
|
|
if (!form->name || c_strcasecmp(string, form->name))
|
2005-09-15 09:58:31 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
object_to_jsval(ctx, vp, get_form_object(ctx, obj, find_form_view(doc_view, form)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2005-09-15 09:58:31 -04:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
undef_to_jsval(ctx, vp);
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
switch (JSID_TO_INT(id)) {
|
2006-02-01 03:31:26 -05:00
|
|
|
case JSP_DOC_LOC:
|
2006-11-24 01:50:12 -05:00
|
|
|
JS_GetProperty(ctx, parent_win, "location", vp);
|
2006-02-01 03:31:26 -05:00
|
|
|
break;
|
2005-09-15 09:58:31 -04:00
|
|
|
case JSP_DOC_REF:
|
2007-08-28 12:41:18 -04:00
|
|
|
switch (get_opt_int("protocol.http.referer.policy", NULL)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
case REFERER_NONE:
|
|
|
|
/* oh well */
|
|
|
|
undef_to_jsval(ctx, vp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REFERER_FAKE:
|
2007-08-28 12:41:18 -04:00
|
|
|
string_to_jsval(ctx, vp, get_opt_str("protocol.http.referer.fake", NULL));
|
2005-09-15 09:58:31 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REFERER_TRUE:
|
|
|
|
/* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
|
|
|
|
if (ses->referrer) {
|
|
|
|
astring_to_jsval(ctx, vp, get_uri_string(ses->referrer, URI_HTTP_REFERRER));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REFERER_SAME_URL:
|
|
|
|
astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_HTTP_REFERRER));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSP_DOC_TITLE:
|
|
|
|
string_to_jsval(ctx, vp, document->title);
|
|
|
|
break;
|
|
|
|
case JSP_DOC_URL:
|
|
|
|
astring_to_jsval(ctx, vp, get_uri_string(document->uri, URI_ORIGINAL));
|
|
|
|
break;
|
|
|
|
default:
|
2007-05-27 11:36:31 -04:00
|
|
|
/* Unrecognized integer property ID; someone is using
|
|
|
|
* the object as an array. SMJS builtin classes (e.g.
|
2006-12-03 05:07:07 -05:00
|
|
|
* js_RegExpClass) just return JS_TRUE in this case
|
|
|
|
* and leave *@vp unchanged. Do the same here.
|
|
|
|
* (Actually not quite the same, as we already used
|
|
|
|
* @undef_to_jsval.) */
|
2005-09-15 09:58:31 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @document_class.setProperty */
|
2005-09-15 09:58:31 -04:00
|
|
|
static JSBool
|
2011-04-19 16:41:05 -04:00
|
|
|
document_set_property(JSContext *ctx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2006-11-24 01:50:12 -05:00
|
|
|
JSObject *parent_win; /* instance of @window_class */
|
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
|
2006-12-03 04:14:22 -05:00
|
|
|
/* This can be called if @obj if not itself an instance of the
|
|
|
|
* appropriate class but has one in its prototype chain. Fail
|
|
|
|
* such calls. */
|
|
|
|
if (!JS_InstanceOf(ctx, obj, (JSClass *) &document_class, NULL))
|
|
|
|
return JS_FALSE;
|
2006-11-24 01:50:12 -05:00
|
|
|
parent_win = JS_GetParent(ctx, obj);
|
2006-11-25 01:54:58 -05:00
|
|
|
assert(JS_InstanceOf(ctx, parent_win, (JSClass *) &window_class, NULL));
|
|
|
|
if_assert_failed return JS_FALSE;
|
|
|
|
|
2007-05-27 11:32:53 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, parent_win,
|
|
|
|
(JSClass *) &window_class, NULL);
|
2006-11-24 01:50:12 -05:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
#ifdef CONFIG_COOKIES
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!strcmp(jsid_to_string(ctx, &id), "cookie")) {
|
2005-09-15 09:58:31 -04:00
|
|
|
set_cookie(vs->uri, jsval_to_string(ctx, vp));
|
|
|
|
/* Do NOT touch our .cookie property, evil
|
|
|
|
* SpiderMonkey!! */
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2005-09-15 09:58:31 -04:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
switch (JSID_TO_INT(id)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
case JSP_DOC_TITLE:
|
|
|
|
mem_free_set(&document->title, stracpy(jsval_to_string(ctx, vp)));
|
2005-10-30 00:33:40 -04:00
|
|
|
print_screen_status(doc_view->session);
|
2005-09-15 09:58:31 -04:00
|
|
|
break;
|
2006-02-01 03:31:26 -05:00
|
|
|
case JSP_DOC_LOC:
|
2005-09-15 09:58:31 -04:00
|
|
|
case JSP_DOC_URL:
|
|
|
|
/* According to the specs this should be readonly but some
|
|
|
|
* broken sites still assign to it (i.e.
|
|
|
|
* http://www.e-handelsfonden.dk/validering.asp?URL=www.polyteknisk.dk).
|
|
|
|
* So emulate window.location. */
|
|
|
|
location_goto(doc_view, jsval_to_string(ctx, vp));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
static JSBool document_write(JSContext *ctx, uintN argc, jsval *rval);
|
|
|
|
static JSBool document_writeln(JSContext *ctx, uintN argc, jsval *rval);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-06-16 17:25:59 -04:00
|
|
|
const spidermonkeyFunctionSpec document_funcs[] = {
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "write", document_write, 1 },
|
2006-01-27 07:29:38 -05:00
|
|
|
{ "writeln", document_writeln, 1 },
|
2005-09-15 09:58:31 -04:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
2011-04-19 16:41:05 -04:00
|
|
|
document_write_do(JSContext *ctx, uintN argc, jsval *rval, int newline)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2011-04-19 16:41:05 -04:00
|
|
|
jsval val;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
|
2006-01-27 07:29:38 -05:00
|
|
|
struct string *ret = interpreter->ret;
|
2011-04-19 16:41:05 -04:00
|
|
|
jsval *argv = JS_ARGV(ctx, rval);
|
2006-01-27 07:29:38 -05:00
|
|
|
|
|
|
|
if (argc >= 1 && ret) {
|
2006-02-11 23:33:42 -05:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (; i < argc; ++i) {
|
|
|
|
unsigned char *code = jsval_to_string(ctx, &argv[i]);
|
|
|
|
|
|
|
|
add_to_string(ret, code);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-28 15:39:07 -05:00
|
|
|
if (newline)
|
|
|
|
add_char_to_string(ret, '\n');
|
2006-01-27 07:29:38 -05:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
/* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
|
|
|
|
* function' errors, I want to see just the useful ones. So just
|
|
|
|
* lighting a led and going away, no muss, no fuss. --pasky */
|
|
|
|
/* TODO: Perhaps we can introduce ecmascript.error_report_unsupported
|
|
|
|
* -> "Show information about the document using some valid,
|
|
|
|
* nevertheless unsupported methods/properties." --pasky too */
|
|
|
|
|
|
|
|
#ifdef CONFIG_LEDS
|
|
|
|
set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
|
|
|
|
#endif
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
boolean_to_jsval(ctx, &val, 0);
|
|
|
|
JS_SET_RVAL(ctx, rval, val);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2006-01-27 07:29:38 -05:00
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @document_funcs{"write"} */
|
2006-01-27 07:29:38 -05:00
|
|
|
static JSBool
|
2011-04-19 16:41:05 -04:00
|
|
|
document_write(JSContext *ctx, uintN argc, jsval *rval)
|
2006-01-27 07:29:38 -05:00
|
|
|
{
|
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
return document_write_do(ctx, argc, rval, 0);
|
2006-01-28 15:39:07 -05:00
|
|
|
}
|
2006-01-27 07:29:38 -05:00
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @document_funcs{"writeln"} */
|
2006-01-28 15:39:07 -05:00
|
|
|
static JSBool
|
2011-04-19 16:41:05 -04:00
|
|
|
document_writeln(JSContext *ctx, uintN argc, jsval *rval)
|
2006-01-28 15:39:07 -05:00
|
|
|
{
|
2011-04-19 16:41:05 -04:00
|
|
|
return document_write_do(ctx, argc, rval, 1);
|
2006-01-27 07:29:38 -05:00
|
|
|
}
|