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"
|
2021-08-08 21:25:08 +02:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 15:58:31 +02:00
|
|
|
#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
|
|
|
|
2021-06-01 19:23:45 +02:00
|
|
|
static xmlpp::Document emptyDoc;
|
|
|
|
|
2021-05-25 18:12:04 +02:00
|
|
|
static JSObject *getDoctype(JSContext *ctx, void *node);
|
|
|
|
|
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 = {
|
2021-08-26 15:44:32 +02:00
|
|
|
nullptr, // addProperty
|
|
|
|
nullptr, // deleteProperty
|
|
|
|
nullptr, // enumerate
|
|
|
|
nullptr, // newEnumerate
|
|
|
|
nullptr, // resolve
|
|
|
|
nullptr, // mayResolve
|
|
|
|
nullptr, // finalize
|
|
|
|
nullptr, // call
|
|
|
|
nullptr, // hasInstance
|
|
|
|
nullptr, // construct
|
|
|
|
nullptr // trace JS_GlobalObjectTraceHook
|
2020-10-27 14:53:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-05-12 17:08:05 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_anchors(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
|
2021-05-12 17:08:05 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 17:08:05 +02:00
|
|
|
|
|
|
|
std::string xpath = "//a";
|
|
|
|
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-25 12:22:52 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_baseURI(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-25 12:22:52 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &document_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vs = interpreter->vs;
|
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str = get_uri_string(vs->uri, URI_BASE);
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:10 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_body(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 17:16:10 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 17:16:10 +02:00
|
|
|
|
|
|
|
std::string xpath = "//body";
|
|
|
|
xmlpp::Node::NodeSet elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements.size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto element = elements[0];
|
|
|
|
|
|
|
|
JSObject *body = getElement(ctx, element);
|
|
|
|
|
|
|
|
if (body) {
|
|
|
|
args.rval().setObject(*body);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
document_set_property_body(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 17:16:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
2021-08-27 19:46:05 +02:00
|
|
|
set_cookie(vs->uri, jsval_to_string(ctx, args[0]));
|
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
|
|
|
|
|
2021-05-23 20:49:21 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_charset(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-23 20:49:21 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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::Document* docu = (xmlpp::Document *)document->dom;
|
|
|
|
std::string encoding = docu->get_encoding();
|
|
|
|
|
|
|
|
if (encoding == "") {
|
|
|
|
encoding = "utf-8";
|
|
|
|
}
|
|
|
|
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, encoding.c_str()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:12:04 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_doctype(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-25 18:12:04 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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::Document* docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Dtd *dtd = docu->get_internal_subset();
|
|
|
|
|
|
|
|
if (!dtd) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
JSObject *elem = getDoctype(ctx, dtd);
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-23 20:49:21 +02:00
|
|
|
|
2021-05-12 17:22:34 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_documentElement(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 17:22:34 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 17:22:34 +02:00
|
|
|
|
|
|
|
std::string xpath = "//html";
|
|
|
|
xmlpp::Node::NodeSet elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements.size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto element = elements[0];
|
|
|
|
|
|
|
|
JSObject *html = getElement(ctx, element);
|
|
|
|
|
|
|
|
if (html) {
|
|
|
|
args.rval().setObject(*html);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:58:15 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_documentURI(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-25 15:58:15 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &document_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vs = interpreter->vs;
|
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str = get_uri_string(vs->uri, URI_BASE);
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-24 21:55:57 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_domain(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-24 21:55:57 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &document_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vs = interpreter->vs;
|
|
|
|
if (!vs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str = get_uri_string(vs->uri, URI_HOST);
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, str));
|
|
|
|
mem_free(str);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-23 18:34:33 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_forms(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-23 18:34:33 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-23 18:34:33 +02:00
|
|
|
|
|
|
|
std::string xpath = "//form";
|
|
|
|
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
|
|
|
|
|
|
|
|
*elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements->size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *elem = getForms(ctx, elements);
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
args.rval().setObject(*elem);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:28:03 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_head(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 17:28:03 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 17:28:03 +02:00
|
|
|
|
|
|
|
std::string xpath = "//head";
|
|
|
|
xmlpp::Node::NodeSet elements = root->find(xpath);
|
|
|
|
|
|
|
|
if (elements.size() == 0) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto element = elements[0];
|
|
|
|
|
|
|
|
JSObject *head = getElement(ctx, element);
|
|
|
|
|
|
|
|
if (head) {
|
|
|
|
args.rval().setObject(*head);
|
|
|
|
} else {
|
|
|
|
args.rval().setNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:01:08 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_images(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 20:01:08 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 20:01:08 +02:00
|
|
|
|
|
|
|
std::string xpath = "//img";
|
|
|
|
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 17:37:01 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_links(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 17:37:01 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 17:37:01 +02:00
|
|
|
|
|
|
|
std::string xpath = "//a[@href]|//area[@href]";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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 ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
2021-08-03 18:47:34 +02:00
|
|
|
args.rval().setObject(*(JSObject *)(interpreter->location_obj));
|
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
|
|
|
|
2021-05-19 19:05:03 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_nodeType(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-19 19:05:03 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
args.rval().setInt32(9);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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;
|
2021-08-27 19:46:05 +02:00
|
|
|
location_goto(doc_view, jsval_to_string(ctx, args[0]));
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 20:05:15 +02:00
|
|
|
static bool
|
|
|
|
document_get_property_scripts(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 20:05:15 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 20:05:15 +02:00
|
|
|
|
|
|
|
std::string xpath = "//script";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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;
|
2021-08-27 19:46:05 +02:00
|
|
|
mem_free_set(&document->title, stracpy(jsval_to_string(ctx, args[0])));
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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;
|
2021-08-27 19:46:05 +02:00
|
|
|
location_goto(doc_view, jsval_to_string(ctx, args[0]));
|
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[] = {
|
2021-05-12 17:08:05 +02:00
|
|
|
JS_PSG("anchors", document_get_property_anchors, JSPROP_ENUMERATE),
|
2021-05-25 12:22:52 +02:00
|
|
|
JS_PSG("baseURI", document_get_property_baseURI, JSPROP_ENUMERATE),
|
2021-05-12 17:16:10 +02:00
|
|
|
JS_PSGS("body", document_get_property_body, document_set_property_body, JSPROP_ENUMERATE),
|
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
|
2021-05-23 20:49:21 +02:00
|
|
|
|
|
|
|
JS_PSG("charset", document_get_property_charset, JSPROP_ENUMERATE),
|
|
|
|
JS_PSG("characterSet", document_get_property_charset, JSPROP_ENUMERATE),
|
2021-05-25 18:12:04 +02:00
|
|
|
JS_PSG("doctype", document_get_property_doctype, JSPROP_ENUMERATE),
|
2021-05-12 17:22:34 +02:00
|
|
|
JS_PSG("documentElement", document_get_property_documentElement, JSPROP_ENUMERATE),
|
2021-05-25 15:58:15 +02:00
|
|
|
JS_PSG("documentURI", document_get_property_documentURI, JSPROP_ENUMERATE),
|
2021-05-24 21:55:57 +02:00
|
|
|
JS_PSG("domain", document_get_property_domain, JSPROP_ENUMERATE),
|
2021-05-23 18:34:33 +02:00
|
|
|
JS_PSG("forms", document_get_property_forms, JSPROP_ENUMERATE),
|
2021-05-12 17:28:03 +02:00
|
|
|
JS_PSG("head", document_get_property_head, JSPROP_ENUMERATE),
|
2021-05-12 20:01:08 +02:00
|
|
|
JS_PSG("images", document_get_property_images, JSPROP_ENUMERATE),
|
2021-05-23 20:49:21 +02:00
|
|
|
JS_PSG("inputEncoding", document_get_property_charset, JSPROP_ENUMERATE),
|
2021-05-12 17:37:01 +02:00
|
|
|
JS_PSG("links", document_get_property_links, JSPROP_ENUMERATE),
|
2020-10-11 15:41:27 +02:00
|
|
|
JS_PSGS("location", document_get_property_location, document_set_property_location, JSPROP_ENUMERATE),
|
2021-05-19 19:05:03 +02:00
|
|
|
JS_PSG("nodeType", document_get_property_nodeType, JSPROP_ENUMERATE),
|
2020-10-11 15:41:27 +02:00
|
|
|
JS_PSG("referrer", document_get_property_referrer, JSPROP_ENUMERATE),
|
2021-05-12 20:05:15 +02:00
|
|
|
JS_PSG("scripts", document_get_property_scripts, JSPROP_ENUMERATE),
|
2020-10-11 15:41:27 +02:00
|
|
|
JS_PSGS("title", document_get_property_title, document_set_property_title, JSPROP_ENUMERATE), /* TODO: Charset? */
|
2021-05-25 16:55:04 +02:00
|
|
|
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
|
|
|
|
2021-08-20 16:08:51 +02:00
|
|
|
static bool document_createComment(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_createElement(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_createTextNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_write(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_writeln(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_replace(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
static bool document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
|
|
|
|
|
|
|
const spidermonkeyFunctionSpec document_funcs[] = {
|
|
|
|
{ "createComment", document_createComment, 1 },
|
|
|
|
{ "createElement", document_createElement, 1 },
|
|
|
|
{ "createTextNode", document_createTextNode, 1 },
|
|
|
|
{ "write", document_write, 1 },
|
|
|
|
{ "writeln", document_writeln, 1 },
|
|
|
|
{ "replace", document_replace, 1 },
|
|
|
|
{ "getElementById", document_getElementById, 1 },
|
|
|
|
{ "getElementsByClassName", document_getElementsByClassName, 1 },
|
|
|
|
{ "getElementsByName", document_getElementsByName, 1 },
|
|
|
|
{ "getElementsByTagName", document_getElementsByTagName, 1 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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);
|
|
|
|
|
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
|
|
|
|
2021-08-21 11:30:09 +02:00
|
|
|
if (spidermonkey_check_if_function_name(document_funcs, string)) {
|
|
|
|
return true;
|
2021-08-20 16:08:51 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:00:37 +01:00
|
|
|
foreach (form, document->forms) {
|
|
|
|
if (!form->name || c_strcasecmp(string, form->name))
|
|
|
|
continue;
|
|
|
|
|
2021-08-20 16:08:51 +02:00
|
|
|
JSObject *obj = get_form_object(ctx, hobj, find_form_view(doc_view, form));
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
hvp.setObject(*obj);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
hvp.setNull();
|
|
|
|
return true;
|
|
|
|
}
|
2005-09-15 15:58:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 16:08:51 +02:00
|
|
|
hvp.setUndefined();
|
|
|
|
return false;
|
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_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newline)
|
2005-09-15 15:58:31 +02:00
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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)
|
|
|
|
{
|
2021-08-30 14:14:51 +02:00
|
|
|
for (int i = 0; i < argc; ++i)
|
2021-02-24 11:23:34 +01:00
|
|
|
{
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&code, ctx, args[i]);
|
2021-02-24 11:23:34 +01:00
|
|
|
}
|
2021-08-30 14:14:51 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2021-07-03 12:55:36 +02:00
|
|
|
if (false && document->ecmascript_counter==0)
|
2021-02-24 11:23:34 +01:00
|
|
|
{
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
/* @document_funcs{"replace"} */
|
|
|
|
static bool
|
|
|
|
document_replace(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-02-24 11:23:34 +01:00
|
|
|
|
|
|
|
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-08-30 14:14:51 +02: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-06-13 16:41:54 +02:00
|
|
|
void *
|
2021-05-02 17:27:35 +02:00
|
|
|
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-08-14 21:57:57 +02:00
|
|
|
// Parse HTML and create a DOM tree
|
2021-07-18 16:54:31 +02:00
|
|
|
xmlDoc* doc = htmlReadDoc((xmlChar*)str.source, NULL, get_cp_mime_name(document->cp),
|
|
|
|
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
|
2021-05-09 19:56:00 +02:00
|
|
|
// Encapsulate raw libxml document in a libxml++ wrapper
|
2021-05-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = new xmlpp::Document(doc);
|
2021-05-02 17:27:35 +02:00
|
|
|
done_string(&str);
|
|
|
|
|
2021-05-23 20:49:21 +02:00
|
|
|
return (void *)docu;
|
2021-05-02 17:27:35 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 19:51:53 +02:00
|
|
|
static bool
|
|
|
|
document_createComment(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-06-01 19:51:53 +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;
|
|
|
|
|
|
|
|
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 21:00:21 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
emptyDoc.create_root_node("root");
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 19:51:53 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-06-01 19:51:53 +02:00
|
|
|
std::string text = idstr.source;
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::CommentNode *comment = emptyRoot->add_child_comment(text);
|
|
|
|
|
|
|
|
if (!comment) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *jelem = getElement(ctx, comment);
|
|
|
|
args.rval().setObject(*jelem);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-01 19:23:45 +02:00
|
|
|
static bool
|
|
|
|
document_createElement(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-06-01 19:23:45 +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;
|
|
|
|
|
|
|
|
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 21:00:21 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
emptyDoc.create_root_node("root");
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 19:23:45 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-06-01 19:23:45 +02:00
|
|
|
std::string text = idstr.source;
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::Element *elem = emptyRoot->add_child_element(text);
|
|
|
|
|
|
|
|
if (!elem) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *jelem = getElement(ctx, elem);
|
|
|
|
args.rval().setObject(*jelem);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-01 19:55:23 +02:00
|
|
|
static bool
|
|
|
|
document_createTextNode(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-06-01 19:55:23 +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;
|
|
|
|
|
|
|
|
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 21:00:21 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
emptyDoc.create_root_node("root");
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
|
|
|
|
|
2021-06-01 19:55:23 +02:00
|
|
|
if (!emptyRoot) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-06-01 19:55:23 +02:00
|
|
|
std::string text = idstr.source;
|
|
|
|
done_string(&idstr);
|
|
|
|
|
|
|
|
xmlpp::TextNode *textNode = emptyRoot->add_child_text(text);
|
|
|
|
|
|
|
|
if (!textNode) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *jelem = getElement(ctx, textNode);
|
|
|
|
args.rval().setObject(*jelem);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-01 19:23:45 +02:00
|
|
|
|
2021-05-01 20:39:13 +02:00
|
|
|
static bool
|
|
|
|
document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-01 20:39:13 +02:00
|
|
|
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-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-02 17:27:35 +02:00
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-05-02 17:27:35 +02:00
|
|
|
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];
|
2021-05-23 18:34:33 +02:00
|
|
|
|
2021-05-09 19:56:00 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 16:30:36 +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;
|
|
|
|
}
|
|
|
|
|
2021-05-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 16:30:36 +02:00
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-05-12 16:30:36 +02:00
|
|
|
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
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-11 19:02:54 +02:00
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-05-11 19:02:54 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-12 16:23:46 +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;
|
|
|
|
}
|
2021-05-23 20:49:21 +02:00
|
|
|
xmlpp::Document *docu = (xmlpp::Document *)document->dom;
|
|
|
|
xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node();
|
2021-05-12 16:23:46 +02:00
|
|
|
|
|
|
|
struct string idstr;
|
|
|
|
|
|
|
|
init_string(&idstr);
|
2021-08-30 14:14:51 +02:00
|
|
|
jshandle_value_to_char_string(&idstr, ctx, args[0]);
|
2021-05-12 16:23:46 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-05-25 18:12:04 +02:00
|
|
|
|
|
|
|
JSClassOps doctype_ops = {
|
2021-08-26 15:44:32 +02:00
|
|
|
nullptr, // addProperty
|
|
|
|
nullptr, // deleteProperty
|
|
|
|
nullptr, // enumerate
|
|
|
|
nullptr, // newEnumerate
|
|
|
|
nullptr, // resolve
|
|
|
|
nullptr, // mayResolve
|
|
|
|
nullptr, // finalize
|
|
|
|
nullptr, // call
|
|
|
|
nullptr, // hasInstance
|
|
|
|
nullptr, // construct
|
|
|
|
nullptr // trace JS_GlobalObjectTraceHook};
|
2021-05-25 18:12:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
JSClass doctype_class = {
|
|
|
|
"doctype",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
|
|
|
&doctype_ops
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
doctype_get_property_name(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-25 18:12:04 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &doctype_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
xmlpp::Dtd *dtd = JS_GetPrivate(hobj);
|
|
|
|
|
|
|
|
if (!dtd) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string v = dtd->get_name();
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-27 16:16:10 +02:00
|
|
|
static bool
|
|
|
|
doctype_get_property_publicId(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-27 16:16:10 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &doctype_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
xmlpp::Dtd *dtd = JS_GetPrivate(hobj);
|
|
|
|
|
|
|
|
if (!dtd) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string v = dtd->get_external_id();
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-26 08:22:33 +02:00
|
|
|
static bool
|
|
|
|
doctype_get_property_systemId(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-26 08:22:33 +02:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
JSCompartment *comp = js::GetContextCompartment(ctx);
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
|
|
|
|
|
|
|
|
/* 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, hobj, &doctype_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
xmlpp::Dtd *dtd = JS_GetPrivate(hobj);
|
|
|
|
|
|
|
|
if (!dtd) {
|
|
|
|
args.rval().setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string v = dtd->get_system_id();
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-25 18:12:04 +02:00
|
|
|
|
|
|
|
JSPropertySpec doctype_props[] = {
|
|
|
|
JS_PSG("name", doctype_get_property_name, JSPROP_ENUMERATE),
|
2021-05-27 16:16:10 +02:00
|
|
|
JS_PSG("publicId", doctype_get_property_publicId, JSPROP_ENUMERATE),
|
2021-05-26 08:22:33 +02:00
|
|
|
JS_PSG("systemId", doctype_get_property_systemId, JSPROP_ENUMERATE),
|
2021-05-25 18:12:04 +02:00
|
|
|
JS_PS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSObject *
|
|
|
|
getDoctype(JSContext *ctx, void *node)
|
|
|
|
{
|
2021-08-19 18:56:50 +02:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2021-05-25 18:12:04 +02:00
|
|
|
JSObject *el = JS_NewObject(ctx, &doctype_class);
|
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedObject r_el(ctx, el);
|
|
|
|
JS_DefineProperties(ctx, r_el, (JSPropertySpec *) doctype_props);
|
|
|
|
JS_SetPrivate(el, node);
|
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|