1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-01-03 14:57:44 -05:00

[js] element.isSameNode

This commit is contained in:
Witold Filipczyk 2021-05-17 17:25:50 +02:00
parent 0a395d7dbf
commit 3e8186922d
2 changed files with 62 additions and 0 deletions

View File

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

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<body>
<p>Click the button to check if the ul element with id="myList" is the same as the document's first ul element.</p>
<button onclick="myFunction()">Try it</button>
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<p><strong>Note:</strong> Firefox stopped supported this method as of version 10, Instead, use === to compare if two nodes are the same.</p>
<p><strong>Note:</strong> Internet Explorer 8 and earlier does not support the isSameNode() method.</p>
<p id="demo"></p>
<script>
function myFunction() {
var item1 = document.getElementById("myList");
var item2 = document.getElementsByTagName("UL")[0];
var x = item1.isSameNode(item2);
alert(x);
}
</script>
</body>
</html>