1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-27 02:56:18 -04:00

[js] hasChildNodes

This commit is contained in:
Witold Filipczyk 2021-05-16 19:26:24 +02:00
parent 22acacb47b
commit 0a395d7dbf
2 changed files with 58 additions and 0 deletions

View File

@ -1376,12 +1376,14 @@ 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_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);
const spidermonkeyFunctionSpec element_funcs[] = {
{ "contains", element_contains, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 },
{ "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 },
{ "hasChildNodes", element_hasChildNodes, 0 },
{ NULL }
};
@ -1538,6 +1540,34 @@ element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp || argc != 0) {
return false;
}
JS::CallArgs args = CallArgsFromVp(argc, rval);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) {
args.rval().setBoolean(false);
return true;
}
xmlpp::Element *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setBoolean(false);
return true;
}
auto children = el->get_children();
args.rval().setBoolean((bool)children.size());
return true;
}
JSObject *
getElement(JSContext *ctx, void *node)
{

View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<body>
<ul id="myList">
<li>Coffee</li>
<li>Tea</li>
</ul>
<p>Click the button to see if the ul element has any child nodes.</p>
<button onclick="myFunction()">Try it</button>
<p>Try removing the child nodes of the list element, and the result will be false instead of true.</p>
<p><strong>Note:</strong> Whitespace inside a node is considered as text nodes, so if you leave any white space or line feeds inside an element, that element still has child nodes.</p>
<p id="demo"></p>
<script>
function myFunction() {
var list = document.getElementById("myList").hasChildNodes();
alert(list);
}
</script>
</body>
</html>