2005-09-15 15:58:31 +02: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"
|
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
#include "ecmascript/ecmascript.h"
|
2005-09-15 15:58:31 +02:00
|
|
|
#include "ecmascript/spidermonkey/util.h"
|
2020-10-23 22:34:58 +02:00
|
|
|
#include <jsfriendapi.h>
|
2005-09-15 15:58:31 +02:00
|
|
|
|
|
|
|
#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"
|
2021-05-02 17:27:35 +02:00
|
|
|
#include "ecmascript/spidermonkey/element.h"
|
2006-11-25 08:54:58 +02:00
|
|
|
#include "ecmascript/spidermonkey/window.h"
|
2005-09-15 15:58:31 +02: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"
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/HTMLparser.h>
|
|
|
|
#include <libxml++/libxml++.h>
|
2021-05-02 17:27:35 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool document_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-10-27 14:53:24 +01:00
|
|
|
JSClassOps document_ops = {
|
|
|
|
JS_PropertyStub, nullptr,
|
|
|
|
document_get_property, JS_StrictPropertyStub,
|
|
|
|
nullptr, nullptr, nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-23 23:33:43 +02:00
|
|
|
/* Each @document_class object must have a @window_class parent. */
|
2019-02-10 21:00:37 +01:00
|
|
|
JSClass document_class = {
|
2005-09-15 15:58:31 +02:00
|
|
|
"document",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
2020-10-27 14:53:24 +01:00
|
|
|
&document_ops
|
2005-09-15 15:58:31 +02:00
|
|
|
};
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
#ifdef CONFIG_COOKIES
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_get_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
2019-04-21 12:27:40 +02:00
|
|
|
struct string *cookies;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
cookies = send_cookies_js(vs->uri);
|
|
|
|
|
|
|
|
if (cookies) {
|
|
|
|
static unsigned char cookiestr[1024];
|
|
|
|
|
2019-11-15 16:40:29 +01:00
|
|
|
strncpy(cookiestr, cookies->source, 1023);
|
2019-02-10 21:00:37 +01:00
|
|
|
done_string(cookies);
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, cookiestr));
|
2019-02-10 21:00:37 +01:00
|
|
|
} else {
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, ""));
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_set_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
2020-10-11 15:41:27 +02:00
|
|
|
struct string *cookies;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_cookie(vs->uri, JS_EncodeString(ctx, args[0].toString()));
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_get_property_location(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-10-23 22:34:58 +02:00
|
|
|
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
|
2019-02-10 21:00:37 +01:00
|
|
|
|
|
|
|
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
|
2020-10-11 15:41:27 +02:00
|
|
|
if_assert_failed return false;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct session *ses;
|
|
|
|
int index;
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
vs = interpreter->vs;
|
|
|
|
struct document *document;
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
|
|
|
char *str = get_uri_string(document->uri, URI_ORIGINAL);
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
args.rval().setUndefined();
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_set_property_location(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2005-09-15 15:58:31 +02:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2006-11-24 08:50:12 +02:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2006-11-25 08:54:58 +02:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-11-24 08:50:12 +02:00
|
|
|
doc_view = vs->doc_view;
|
2020-10-11 15:41:27 +02:00
|
|
|
location_goto(doc_view, JS_EncodeString(ctx, args[0].toString()));
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
2005-09-15 15:58:31 +02:00
|
|
|
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_get_property_referrer(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
struct session *ses;
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2020-10-11 15:41:27 +02:00
|
|
|
|
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
|
|
|
ses = doc_view->session;
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
switch (get_opt_int("protocol.http.referer.policy", NULL)) {
|
|
|
|
case REFERER_NONE:
|
|
|
|
/* oh well */
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setUndefined();
|
2019-02-10 21:00:37 +01:00
|
|
|
break;
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
case REFERER_FAKE:
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, get_opt_str("protocol.http.referer.fake", NULL)));
|
2006-02-01 09:31:26 +01:00
|
|
|
break;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
|
|
|
case REFERER_TRUE:
|
|
|
|
/* XXX: Encode as in add_url_to_httset_prop_string(&prop, ) ? --pasky */
|
|
|
|
if (ses->referrer) {
|
2021-01-02 16:20:27 +01:00
|
|
|
char *str = get_uri_string(ses->referrer, URI_HTTP_REFERRER);
|
2020-10-11 15:41:27 +02:00
|
|
|
|
|
|
|
if (str) {
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
} else {
|
|
|
|
args.rval().setUndefined();
|
|
|
|
}
|
2005-09-15 15:58:31 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
|
|
|
case REFERER_SAME_URL:
|
2021-01-02 16:20:27 +01:00
|
|
|
char *str = get_uri_string(document->uri, URI_HTTP_REFERRER);
|
2020-10-11 15:41:27 +02:00
|
|
|
|
|
|
|
if (str) {
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
} else {
|
|
|
|
args.rval().setUndefined();
|
|
|
|
}
|
2005-09-15 15:58:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2005-09-15 15:58:31 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_get_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2005-09-15 15:58:31 +02:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, document->title));
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_set_property_title(JSContext *ctx, int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
|
|
|
|
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
|
2006-11-24 08:50:12 +02:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2020-10-27 14:53:24 +01:00
|
|
|
|
2020-10-28 22:32:20 +01:00
|
|
|
if (!vs || !vs->doc_view) {
|
2020-10-11 15:41:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2006-11-24 08:50:12 +02:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
2020-10-11 15:41:27 +02:00
|
|
|
mem_free_set(&document->title, stracpy(JS_EncodeString(ctx, args[0].toString())));
|
2019-02-10 21:00:37 +01:00
|
|
|
print_screen_status(doc_view->session);
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_get_property_url(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
2021-01-02 16:20:27 +01:00
|
|
|
char *str = get_uri_string(document->uri, URI_ORIGINAL);
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
if (str) {
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
} else {
|
|
|
|
args.rval().setUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-02-10 21:00:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_set_property_url(JSContext *ctx, int argc, JS::Value *vp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2020-11-16 22:00:48 +01:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
2020-10-11 15:41:27 +02:00
|
|
|
struct document *document;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2020-10-11 15:41:27 +02:00
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 21:00:37 +01:00
|
|
|
doc_view = vs->doc_view;
|
2020-10-11 15:41:27 +02:00
|
|
|
location_goto(doc_view, JS_EncodeString(ctx, args[0].toString()));
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2019-02-10 21:00:37 +01: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? */
|
|
|
|
JSPropertySpec document_props[] = {
|
2005-09-15 15:58:31 +02:00
|
|
|
#ifdef CONFIG_COOKIES
|
2020-10-11 15:41:27 +02:00
|
|
|
JS_PSGS("cookie", document_get_property_cookie, document_set_property_cookie, JSPROP_ENUMERATE),
|
2005-09-15 15:58:31 +02:00
|
|
|
#endif
|
2020-10-11 15:41:27 +02:00
|
|
|
JS_PSGS("location", document_get_property_location, document_set_property_location, JSPROP_ENUMERATE),
|
|
|
|
JS_PSG("referrer", document_get_property_referrer, JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("title", document_get_property_title, document_set_property_title, JSPROP_ENUMERATE), /* TODO: Charset? */
|
|
|
|
JS_PSGS("url", document_get_property_url, document_set_property_url, JSPROP_ENUMERATE),
|
2020-11-21 17:54:47 +01:00
|
|
|
JS_PS_END
|
2019-02-10 21:00:37 +01:00
|
|
|
};
|
2005-09-15 15:58:31 +02:00
|
|
|
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
/* @document_class.getProperty */
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-05 20:14:55 +02:00
|
|
|
document_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
|
2019-02-10 21:00:37 +01:00
|
|
|
{
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::RootedObject parent_win(ctx); /* instance of @window_class */
|
2019-02-10 21:00:37 +01:00
|
|
|
struct view_state *vs;
|
|
|
|
struct document_view *doc_view;
|
|
|
|
struct document *document;
|
|
|
|
struct form *form;
|
2021-01-02 16:20:27 +01:00
|
|
|
char *string;
|
2020-11-16 22:00:48 +01:00
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-11-15 17:55:58 +01:00
|
|
|
JSClass* classPtr = JS_GetClass(hobj);
|
2019-02-10 21:00:37 +01:00
|
|
|
|
|
|
|
if (classPtr != &document_class)
|
2020-10-11 15:41:27 +02:00
|
|
|
return false;
|
2019-02-10 21:00:37 +01:00
|
|
|
|
2020-11-16 22:00:48 +01:00
|
|
|
vs = interpreter->vs;
|
2019-02-10 21:00:37 +01:00
|
|
|
doc_view = vs->doc_view;
|
|
|
|
document = doc_view->document;
|
2020-11-15 17:55:58 +01:00
|
|
|
|
|
|
|
if (!JSID_IS_STRING(hid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
string = jsid_to_string(ctx, hid);
|
2019-02-10 21:00:37 +01:00
|
|
|
|
|
|
|
foreach (form, document->forms) {
|
|
|
|
if (!form->name || c_strcasecmp(string, form->name))
|
|
|
|
continue;
|
|
|
|
|
2020-11-15 17:55:58 +01:00
|
|
|
hvp.setObject(*get_form_object(ctx, hobj, find_form_view(doc_view, form)));
|
2005-09-15 15:58:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2005-09-15 15:58:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:34:58 +02:00
|
|
|
static bool document_write(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_writeln(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
2021-02-24 11:23:34 +01:00
|
|
|
static bool document_replace(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
2021-05-01 20:39:13 +02:00
|
|
|
static bool document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
2021-05-12 16:30:36 +02:00
|
|
|
static bool document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
2021-05-12 16:23:46 +02:00
|
|
|
static bool document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2008-06-17 00:25:59 +03:00
|
|
|
const spidermonkeyFunctionSpec document_funcs[] = {
|
2005-09-15 15:58:31 +02:00
|
|
|
{ "write", document_write, 1 },
|
2006-01-27 13:29:38 +01:00
|
|
|
{ "writeln", document_writeln, 1 },
|
2021-02-24 11:23:34 +01:00
|
|
|
{ "replace", document_replace, 1 },
|
2021-05-01 20:39:13 +02:00
|
|
|
{ "getElementById", document_getElementById, 1 },
|
2021-05-12 16:30:36 +02:00
|
|
|
{ "getElementsByClassName", document_getElementsByClassName, 1 },
|
2021-05-12 16:23:46 +02:00
|
|
|
{ "getElementsByName", document_getElementsByName, 1 },
|
|
|
|
{ "getElementsByTagName", document_getElementsByTagName, 1 },
|
2005-09-15 15:58:31 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newline)
|
2005-09-15 15:58:31 +02:00
|
|
|
{
|
2020-10-28 22:32:20 +01:00
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
2020-10-23 22:34:58 +02:00
|
|
|
JS::Value val;
|
2019-04-21 12:27:40 +02:00
|
|
|
struct string *ret = interpreter->ret;
|
2020-10-11 15:41:27 +02:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, rval);
|
2006-01-27 13:29:38 +01:00
|
|
|
|
2021-02-25 13:40:41 +01:00
|
|
|
struct string code;
|
2021-02-24 11:23:34 +01:00
|
|
|
|
2021-02-25 21:58:20 +01:00
|
|
|
init_string(&code);
|
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
if (argc >= 1)
|
|
|
|
{
|
|
|
|
for (int i=0;i<argc;++i)
|
|
|
|
{
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2021-02-25 21:38:42 +01:00
|
|
|
jshandle_value_to_char_string(&code,ctx,&args[i]);
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newline)
|
|
|
|
{
|
2021-02-25 13:40:41 +01:00
|
|
|
add_to_string(&code, "\n");
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
2006-01-27 13:29:38 +01:00
|
|
|
}
|
2021-02-25 13:40:41 +01:00
|
|
|
//DBG("%s",code.source);
|
2021-02-24 11:23:34 +01:00
|
|
|
|
2005-09-15 15:58:31 +02: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 */
|
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct document *document;
|
|
|
|
document = doc_view->document;
|
|
|
|
struct cache_entry *cached = doc_view->document->cached;
|
|
|
|
cached = doc_view->document->cached;
|
2021-02-25 13:40:41 +01:00
|
|
|
struct fragment *f = get_cache_fragment(cached);
|
2021-02-26 16:14:43 +01:00
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
if (f && f->length)
|
|
|
|
{
|
|
|
|
if (document->ecmascript_counter==0)
|
|
|
|
{
|
2021-02-25 13:40:41 +01:00
|
|
|
add_fragment(cached,0,code.source,code.length);
|
2021-02-24 11:23:34 +01:00
|
|
|
} else {
|
2021-02-25 13:40:41 +01:00
|
|
|
add_fragment(cached,f->length,code.source,code.length);
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
|
|
|
document->ecmascript_counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-09-15 15:58:31 +02:00
|
|
|
#ifdef CONFIG_LEDS
|
|
|
|
set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
|
|
|
|
#endif
|
|
|
|
|
2021-02-25 21:58:20 +01:00
|
|
|
done_string(&code);
|
2020-10-11 15:41:27 +02:00
|
|
|
args.rval().setBoolean(false);
|
2005-09-15 15:58:31 +02:00
|
|
|
|
2020-10-11 15:41:27 +02:00
|
|
|
return true;
|
2005-09-15 15:58:31 +02:00
|
|
|
}
|
2006-01-27 13:29:38 +01:00
|
|
|
|
2006-11-23 23:33:43 +02:00
|
|
|
/* @document_funcs{"write"} */
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_write(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
2006-01-27 13:29:38 +01:00
|
|
|
{
|
|
|
|
|
2011-04-19 22:41:05 +02:00
|
|
|
return document_write_do(ctx, argc, rval, 0);
|
2006-01-28 20:39:07 +00:00
|
|
|
}
|
2006-01-27 13:29:38 +01:00
|
|
|
|
2006-11-23 23:33:43 +02:00
|
|
|
/* @document_funcs{"writeln"} */
|
2020-10-11 15:41:27 +02:00
|
|
|
static bool
|
2020-10-23 22:34:58 +02:00
|
|
|
document_writeln(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
2006-01-28 20:39:07 +00:00
|
|
|
{
|
2011-04-19 22:41:05 +02:00
|
|
|
return document_write_do(ctx, argc, rval, 1);
|
2006-01-27 13:29:38 +01:00
|
|
|
}
|
2021-02-24 11:23:34 +01:00
|
|
|
|
2021-02-25 13:40:41 +01:00
|
|
|
void
|
|
|
|
string_replace(struct string *res, struct string *inp, struct string *what, struct string *repl)
|
2021-02-24 11:23:34 +01:00
|
|
|
{
|
2021-02-25 13:40:41 +01:00
|
|
|
struct string tmp;
|
|
|
|
struct string tmp2;
|
|
|
|
char *head;
|
|
|
|
char *found;
|
|
|
|
char *ins;
|
|
|
|
char *tmp_cnt;
|
|
|
|
|
|
|
|
init_string(&tmp);
|
|
|
|
init_string(&tmp2);
|
2021-02-25 21:58:20 +01:00
|
|
|
|
2021-02-26 18:43:19 +01:00
|
|
|
add_string_to_string(&tmp, inp);
|
2021-02-25 13:40:41 +01:00
|
|
|
|
|
|
|
head = tmp.source;
|
|
|
|
int count = 0;
|
|
|
|
ins = head;
|
|
|
|
if (what->length==0)
|
2021-02-24 11:23:34 +01:00
|
|
|
{
|
2021-02-26 18:43:19 +01:00
|
|
|
add_string_to_string(res, inp);
|
2021-02-25 13:40:41 +01:00
|
|
|
return;
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 13:40:41 +01:00
|
|
|
// count occurence of string in input
|
|
|
|
for (count = 0; tmp_cnt = strstr(ins, what->source); ++count)
|
2021-02-24 11:23:34 +01:00
|
|
|
{
|
2021-02-25 13:40:41 +01:00
|
|
|
ins = tmp_cnt + what->length;
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
2021-02-25 13:40:41 +01:00
|
|
|
|
|
|
|
for (int i=0;i<count;i++) {
|
|
|
|
// find occurence of string
|
|
|
|
found=strstr(head,what->source);
|
|
|
|
// count chars before and after occurence
|
|
|
|
int bf_len=found-tmp.source;
|
|
|
|
int af_len=tmp.length-bf_len-what->length;
|
|
|
|
// move head by what
|
|
|
|
found+=what->length;
|
|
|
|
// join the before, needle and after to res
|
|
|
|
add_bytes_to_string(&tmp2,tmp.source,bf_len);
|
|
|
|
add_bytes_to_string(&tmp2,repl->source,repl->length);
|
|
|
|
add_bytes_to_string(&tmp2,found,af_len);
|
|
|
|
// clear tmp string and tmp2 string
|
2021-02-26 16:22:18 +01:00
|
|
|
done_string(&tmp);
|
2021-02-25 13:40:41 +01:00
|
|
|
init_string(&tmp);
|
2021-02-26 18:43:19 +01:00
|
|
|
add_string_to_string(&tmp, &tmp2);
|
2021-02-26 16:22:18 +01:00
|
|
|
done_string(&tmp2);
|
2021-02-25 13:40:41 +01:00
|
|
|
init_string(&tmp2);
|
|
|
|
//printf("TMP: %s |\n",tmp.source);
|
|
|
|
head = tmp.source;
|
|
|
|
}
|
2021-02-26 18:43:19 +01:00
|
|
|
add_string_to_string(res, &tmp);
|
2021-02-25 21:58:20 +01:00
|
|
|
|
|
|
|
done_string(&tmp);
|
|
|
|
done_string(&tmp2);
|
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* @document_funcs{"replace"} */
|
|
|
|
static bool
|
|
|
|
document_replace(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct session *ses = doc_view->session;
|
|
|
|
struct terminal *term = ses->tab->term;
|
|
|
|
struct string *ret = interpreter->ret;
|
|
|
|
struct document *document;
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
document = doc_view->document;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2021-02-25 13:40:41 +01:00
|
|
|
struct string needle;
|
|
|
|
struct string heystack;
|
2021-02-24 11:23:34 +01:00
|
|
|
|
2021-02-25 21:58:20 +01:00
|
|
|
init_string(&needle);
|
|
|
|
init_string(&heystack);
|
|
|
|
|
2021-02-25 21:38:42 +01:00
|
|
|
jshandle_value_to_char_string(&needle, ctx, &args[0]);
|
|
|
|
jshandle_value_to_char_string(&heystack, ctx, &args[1]);
|
2021-02-24 11:23:34 +01:00
|
|
|
|
2021-02-25 13:40:41 +01:00
|
|
|
//DBG("doc replace %s %s\n", needle.source, heystack.source);
|
2021-02-24 11:23:34 +01:00
|
|
|
|
|
|
|
int nu_len=0;
|
|
|
|
int fd_len=0;
|
|
|
|
unsigned char *nu;
|
|
|
|
struct cache_entry *cached = doc_view->document->cached;
|
2021-02-25 13:40:41 +01:00
|
|
|
struct fragment *f = get_cache_fragment(cached);
|
2021-02-26 18:43:19 +01:00
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
if (f && f->length)
|
|
|
|
{
|
2021-02-25 13:40:41 +01:00
|
|
|
fd_len=f->length;
|
|
|
|
|
|
|
|
struct string f_data;
|
|
|
|
init_string(&f_data);
|
|
|
|
add_to_string(&f_data,f->data);
|
|
|
|
|
|
|
|
struct string nu_str;
|
2021-02-25 21:58:20 +01:00
|
|
|
init_string(&nu_str);
|
2021-02-25 13:40:41 +01:00
|
|
|
string_replace(&nu_str,&f_data,&needle,&heystack);
|
|
|
|
nu_len=nu_str.length;
|
2021-02-24 11:23:34 +01:00
|
|
|
delete_entry_content(cached);
|
|
|
|
/* This is very ugly, indeed. And Yes fd_len isn't
|
|
|
|
* logically correct. But using nu_len will cause
|
|
|
|
* the document to render improperly.
|
2021-02-25 13:40:41 +01:00
|
|
|
* TBD: somehow better rerender the document
|
|
|
|
* now it's places on the session level in doc_loading_callback */
|
|
|
|
int ret = add_fragment(cached,0,nu_str.source,fd_len);
|
2021-02-24 11:23:34 +01:00
|
|
|
normalize_cache_entry(cached,nu_len);
|
|
|
|
document->ecmascript_counter++;
|
2021-02-25 13:40:41 +01:00
|
|
|
//DBG("doc replace %s %s\n", needle.source, heystack.source);
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 21:58:20 +01:00
|
|
|
done_string(&needle);
|
|
|
|
done_string(&heystack);
|
|
|
|
|
2021-02-24 11:23:34 +01:00
|
|
|
args.rval().setBoolean(true);
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2021-05-02 17:27:35 +02:00
|
|
|
static void *
|
|
|
|
document_parse(struct document *document)
|
|
|
|
{
|
|
|
|
struct cache_entry *cached = document->cached;
|
|
|
|
struct fragment *f = get_cache_fragment(cached);
|
|
|
|
|
|
|
|
if (!f || !f->length) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct string str;
|
|
|
|
init_string(&str);
|
|
|
|
|
|
|
|
add_bytes_to_string(&str, f->data, f->length);
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
// Parse HTML and create a DOM tree
|
|
|
|
xmlDoc* doc = htmlReadDoc((xmlChar*)str.source, NULL, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
|
|
|
|
// Encapsulate raw libxml document in a libxml++ wrapper
|
|
|
|
xmlNode* r = xmlDocGetRootElement(doc);
|
|
|
|
xmlpp::Element* root = new xmlpp::Element(r);
|
2021-05-02 17:27:35 +02:00
|
|
|
done_string(&str);
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
return (void *)root;
|
2021-05-02 17:27:35 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 20:39:13 +02:00
|
|
|
static bool
|
|
|
|
document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-02 17:27:35 +02:00
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct document *document = doc_view->document;
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
document->dom = document_parse(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
xmlpp::Element* root = (xmlpp::Element *)document->dom;
|
2021-05-02 17:27:35 +02:00
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
|
|
|
jshandle_value_to_char_string(&idstr, ctx, &args[0]);
|
|
|
|
std::string id = idstr.source;
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
std::string xpath = "//*[@id=\"";
|
|
|
|
xpath += id;
|
|
|
|
xpath += "\"]";
|
2021-05-02 17:27:35 +02:00
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
auto elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements.size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
2021-05-02 17:27:35 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
auto node = elements[0];
|
|
|
|
JSObject *elem = getElement(ctx, node);
|
|
|
|
|
2021-05-02 17:27:35 +02:00
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
2021-05-01 20:39:13 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-11 19:02:54 +02:00
|
|
|
|
2021-05-12 16:30:36 +02:00
|
|
|
static bool
|
|
|
|
document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct document *document = doc_view->document;
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
document->dom = document_parse(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)document->dom;
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
|
|
|
jshandle_value_to_char_string(&idstr, ctx, &args[0]);
|
|
|
|
std::string id = idstr.source;
|
|
|
|
|
|
|
|
std::string xpath = "//*[@class=\"";
|
|
|
|
xpath += id;
|
|
|
|
xpath += "\"]";
|
|
|
|
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
|
|
|
|
|
|
|
|
*elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements->size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *elem = getCollection(ctx, elements);
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-11 19:02:54 +02:00
|
|
|
static bool
|
2021-05-12 16:23:46 +02:00
|
|
|
document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2021-05-11 19:02:54 +02:00
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct document *document = doc_view->document;
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
document->dom = document_parse(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)document->dom;
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
|
|
|
jshandle_value_to_char_string(&idstr, ctx, &args[0]);
|
|
|
|
std::string id = idstr.source;
|
|
|
|
|
|
|
|
std::string xpath = "//*[@id=\"";
|
|
|
|
xpath += id;
|
|
|
|
xpath += "\"]|//*[@name=\"";
|
|
|
|
xpath += id;
|
|
|
|
xpath += "\"]";
|
|
|
|
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
|
|
|
|
|
|
|
|
*elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements->size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *elem = getCollection(ctx, elements);
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-12 16:23:46 +02:00
|
|
|
|
|
|
|
static bool
|
|
|
|
document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
struct document_view *doc_view = interpreter->vs->doc_view;
|
|
|
|
struct document *document = doc_view->document;
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
document->dom = document_parse(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!document->dom) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)document->dom;
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
|
|
|
jshandle_value_to_char_string(&idstr, ctx, &args[0]);
|
|
|
|
std::string id = idstr.source;
|
|
|
|
std::transform(id.begin(), id.end(), id.begin(), ::tolower);
|
|
|
|
|
|
|
|
std::string xpath = "//";
|
|
|
|
xpath += id;
|
|
|
|
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
|
|
|
|
|
|
|
|
*elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements->size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *elem = getCollection(ctx, elements);
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|