From 3e8186922dad5c6399dd4d747da9e4090ace1f3e Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 17 May 2021 17:25:50 +0200 Subject: [PATCH] [js] element.isSameNode --- src/ecmascript/spidermonkey/element.c | 35 +++++++++++++++++++++++++++ test/ecmascript/isSameNode.html | 27 +++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/ecmascript/isSameNode.html diff --git a/src/ecmascript/spidermonkey/element.c b/src/ecmascript/spidermonkey/element.c index cd55bb4ee..a890b3d54 100644 --- a/src/ecmascript/spidermonkey/element.c +++ b/src/ecmascript/spidermonkey/element.c @@ -1377,6 +1377,7 @@ static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Valu static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *rval); +static bool element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval); const spidermonkeyFunctionSpec element_funcs[] = { { "contains", element_contains, 1 }, @@ -1384,6 +1385,7 @@ const spidermonkeyFunctionSpec element_funcs[] = { { "hasAttribute", element_hasAttribute, 1 }, { "hasAttributes", element_hasAttributes, 0 }, { "hasChildNodes", element_hasChildNodes, 0 }, + { "isSameNode", element_isSameNode, 1 }, { NULL } }; @@ -1568,6 +1570,39 @@ element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *rval) return true; } +static bool +element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval) +{ + JSCompartment *comp = js::GetContextCompartment(ctx); + + if (!comp || argc != 1) { + return false; + } + + JS::CallArgs args = CallArgsFromVp(argc, rval); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + + struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp); + + if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) + return false; + + xmlpp::Element *el = JS_GetPrivate(hobj); + + if (!el) { + args.rval().setBoolean(false); + return true; + } + + JS::RootedObject node(ctx, &args[0].toObject()); + + xmlpp::Element *el2 = JS_GetPrivate(node); + args.rval().setBoolean(el == el2); + + return true; +} + + JSObject * getElement(JSContext *ctx, void *node) { diff --git a/test/ecmascript/isSameNode.html b/test/ecmascript/isSameNode.html new file mode 100644 index 000000000..2345b1eb7 --- /dev/null +++ b/test/ecmascript/isSameNode.html @@ -0,0 +1,27 @@ + + + + +

Click the button to check if the ul element with id="myList" is the same as the document's first ul element.

+ + + + + +

Note: Firefox stopped supported this method as of version 10, Instead, use === to compare if two nodes are the same.

+ +

Note: Internet Explorer 8 and earlier does not support the isSameNode() method.

+ +

+ + + + +