1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[js] document.body

This commit is contained in:
Witold Filipczyk 2021-05-12 17:16:10 +02:00
parent 8e97e8b048
commit 937b77cfdf
2 changed files with 69 additions and 0 deletions

View File

@ -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

20
test/ecmascript/body.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<body id="test">
<button onclick="myFunction()">Try it</button>
<a href="/">/</a>
<a href="/home">/home</a>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.body.id;
alert(x);
}
</script>
</body>
</html>