From 8e97e8b0482493e5fc8c2a525e59b888f8bc7547 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 12 May 2021 17:08:05 +0200 Subject: [PATCH] [js] document.anchors --- src/ecmascript/spidermonkey/document.c | 44 ++++++++++++++++++++++++++ test/ecmascript/anchors.html | 25 +++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/ecmascript/anchors.html diff --git a/src/ecmascript/spidermonkey/document.c b/src/ecmascript/spidermonkey/document.c index 77df80834..b6c928797 100644 --- a/src/ecmascript/spidermonkey/document.c +++ b/src/ecmascript/spidermonkey/document.c @@ -56,6 +56,7 @@ #include static bool document_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp); +static void *document_parse(struct document *document); JSClassOps document_ops = { JS_PropertyStub, nullptr, @@ -71,6 +72,48 @@ JSClass document_class = { &document_ops }; +static bool +document_get_property_anchors(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 = "//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; +} + #ifdef CONFIG_COOKIES static bool document_get_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp) @@ -404,6 +447,7 @@ document_set_property_url(JSContext *ctx, int argc, JS::Value *vp) /* "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[] = { + JS_PSG("anchors", document_get_property_anchors, JSPROP_ENUMERATE), #ifdef CONFIG_COOKIES JS_PSGS("cookie", document_get_property_cookie, document_set_property_cookie, JSPROP_ENUMERATE), #endif diff --git a/test/ecmascript/anchors.html b/test/ecmascript/anchors.html new file mode 100644 index 000000000..3064c3abd --- /dev/null +++ b/test/ecmascript/anchors.html @@ -0,0 +1,25 @@ + + + + +First Name:
+First Name: + +

Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".

+ + + +/ +/home + +

+ + + + +