mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -05:00
[js] hasChildNodes
This commit is contained in:
parent
22acacb47b
commit
0a395d7dbf
@ -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_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);
|
||||||
|
static bool element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
|
|
||||||
const spidermonkeyFunctionSpec element_funcs[] = {
|
const spidermonkeyFunctionSpec element_funcs[] = {
|
||||||
{ "contains", element_contains, 1 },
|
{ "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 },
|
||||||
|
{ "hasChildNodes", element_hasChildNodes, 0 },
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1538,6 +1540,34 @@ element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
|||||||
return true;
|
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 *
|
JSObject *
|
||||||
getElement(JSContext *ctx, void *node)
|
getElement(JSContext *ctx, void *node)
|
||||||
{
|
{
|
||||||
|
28
test/ecmascript/hasChildNodes.html
Normal file
28
test/ecmascript/hasChildNodes.html
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user