From 35d784780890cf66de25d4d0154c6920497c0fa6 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 12 May 2021 20:01:08 +0200 Subject: [PATCH] [js] document.images --- src/ecmascript/spidermonkey/document.c | 44 +++++++++++++++++++++++++- test/ecmascript/images.html | 22 +++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/ecmascript/images.html diff --git a/src/ecmascript/spidermonkey/document.c b/src/ecmascript/spidermonkey/document.c index c14a7ecaa..bca83febb 100644 --- a/src/ecmascript/spidermonkey/document.c +++ b/src/ecmascript/spidermonkey/document.c @@ -312,6 +312,48 @@ document_get_property_head(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +document_get_property_images(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 = "//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; +} + static bool document_get_property_links(JSContext *ctx, unsigned int argc, JS::Value *vp) { @@ -354,7 +396,6 @@ document_get_property_links(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } - static bool document_get_property_location(JSContext *ctx, unsigned int argc, JS::Value *vp) { @@ -629,6 +670,7 @@ JSPropertySpec document_props[] = { #endif JS_PSG("documentElement", document_get_property_documentElement, JSPROP_ENUMERATE), JS_PSG("head", document_get_property_head, JSPROP_ENUMERATE), + JS_PSG("images", document_get_property_images, JSPROP_ENUMERATE), JS_PSG("links", document_get_property_links, JSPROP_ENUMERATE), JS_PSGS("location", document_get_property_location, document_set_property_location, JSPROP_ENUMERATE), JS_PSG("referrer", document_get_property_referrer, JSPROP_ENUMERATE), diff --git a/test/ecmascript/images.html b/test/ecmascript/images.html new file mode 100644 index 000000000..6b335464e --- /dev/null +++ b/test/ecmascript/images.html @@ -0,0 +1,22 @@ + + + + + + +/ +/home + + + +

+ + + + +