From 937b77cfdf1deaacceca80177f409e3424b04c0b Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 12 May 2021 17:16:10 +0200 Subject: [PATCH] [js] document.body --- src/ecmascript/spidermonkey/document.c | 49 ++++++++++++++++++++++++++ test/ecmascript/body.html | 20 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/ecmascript/body.html diff --git a/src/ecmascript/spidermonkey/document.c b/src/ecmascript/spidermonkey/document.c index b6c928797..dc4610f17 100644 --- a/src/ecmascript/spidermonkey/document.c +++ b/src/ecmascript/spidermonkey/document.c @@ -114,6 +114,54 @@ document_get_property_anchors(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +document_get_property_body(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + 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::Element* root = (xmlpp::Element *)document->dom; + + 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) +{ + return true; +} + #ifdef CONFIG_COOKIES static bool document_get_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp) @@ -448,6 +496,7 @@ document_set_property_url(JSContext *ctx, int argc, JS::Value *vp) * cookie-module. XXX: Would it work if "cookie" was defined in this array? */ JSPropertySpec document_props[] = { JS_PSG("anchors", document_get_property_anchors, JSPROP_ENUMERATE), + JS_PSGS("body", document_get_property_body, document_set_property_body, JSPROP_ENUMERATE), #ifdef CONFIG_COOKIES JS_PSGS("cookie", document_get_property_cookie, document_set_property_cookie, JSPROP_ENUMERATE), #endif diff --git a/test/ecmascript/body.html b/test/ecmascript/body.html new file mode 100644 index 000000000..188a1991a --- /dev/null +++ b/test/ecmascript/body.html @@ -0,0 +1,20 @@ + + + + + + +/ +/home + +

+ + + + +