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

[js] element.contains

This commit is contained in:
Witold Filipczyk 2021-05-15 20:50:03 +02:00
parent 09988b7fcd
commit 760dff4419
2 changed files with 97 additions and 0 deletions

View File

@ -1066,17 +1066,82 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true; return true;
} }
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval); 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_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = { const spidermonkeyFunctionSpec element_funcs[] = {
{ "contains", element_contains, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 }, { "getAttributeNode", element_getAttributeNode, 1 },
{ "hasAttribute", element_hasAttribute, 1 }, { "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 }, { "hasAttributes", element_hasAttributes, 0 },
{ NULL } { NULL }
}; };
static void
check_contains(xmlpp::Node *node, xmlpp::Node *searched, bool *result_set, bool *result)
{
if (*result_set) {
return;
}
auto childs = node->get_children();
auto it = childs.begin();
auto end = childs.end();
for (; it != end; ++it) {
if (*it == searched) {
*result_set = true;
*result = true;
return;
}
check_contains(*it, searched, result_set, result);
}
}
static bool
element_contains(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);
if (!el2) {
args.rval().setBoolean(false);
return true;
}
bool result_set = false;
bool result = false;
check_contains(el, el2, &result_set, &result);
args.rval().setBoolean(result);
return true;
}
static bool static bool
element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval) element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {

View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">
<p>I am a p element inside div, and I have a <span id="mySPAN"><b>span</b></span> element inside of me.</p>
</div>
<span id="mySPAN2"><b>span</b></span>
<p>Click the button to find out if the div element contains a span element.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var span = document.getElementById("mySPAN");
var div = document.getElementById("myDIV").contains(span);
alert(div);
}
</script>
</body>
</html>