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

[js] element.appendChild

This commit is contained in:
Witold Filipczyk 2021-06-01 21:00:21 +02:00
parent 3a328cc48c
commit 4b5d115039
3 changed files with 80 additions and 1 deletions

View File

@ -1303,6 +1303,12 @@ document_createComment(JSContext *ctx, unsigned int argc, JS::Value *vp)
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node(); xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) {
emptyDoc.create_root_node("root");
}
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) { if (!emptyRoot) {
args.rval().setNull(); args.rval().setNull();
return true; return true;
@ -1343,6 +1349,12 @@ document_createElement(JSContext *ctx, unsigned int argc, JS::Value *vp)
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node(); xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) {
emptyDoc.create_root_node("root");
}
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) { if (!emptyRoot) {
args.rval().setNull(); args.rval().setNull();
return true; return true;
@ -1383,6 +1395,12 @@ document_createTextNode(JSContext *ctx, unsigned int argc, JS::Value *vp)
xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node(); xmlpp::Element* emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) {
emptyDoc.create_root_node("root");
}
emptyRoot = (xmlpp::Element *)emptyDoc.get_root_node();
if (!emptyRoot) { if (!emptyRoot) {
args.rval().setNull(); args.rval().setNull();
return true; return true;

View File

@ -1584,6 +1584,7 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true; return true;
} }
static bool element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval); 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);
@ -1594,6 +1595,7 @@ static bool element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rva
static bool element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = { const spidermonkeyFunctionSpec element_funcs[] = {
{ "appendChild", element_appendChild, 1 },
{ "contains", element_contains, 1 }, { "contains", element_contains, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 }, { "getAttributeNode", element_getAttributeNode, 1 },
{ "hasAttribute", element_hasAttribute, 1 }, { "hasAttribute", element_hasAttribute, 1 },
@ -1627,6 +1629,39 @@ check_contains(xmlpp::Node *node, xmlpp::Node *searched, bool *result_set, bool
} }
} }
static bool
element_appendChild(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::Node *el2 = JS_GetPrivate(node);
el->import_node(el2);
return true;
}
static bool static bool
element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval) element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {
@ -1832,7 +1867,6 @@ element_isEqualNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true; return true;
} }
static bool static bool
element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval) element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<body>
<ul id="myList">
<li>Coffee</li>
<li>Tea</li>
</ul>
<p>Click the button to append an item to the end of the list.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var node = document.createElement("LI");
var textnode = document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
alert(document.getElementsByTagName("HTML")[0].outerHTML);
}
</script>
<p><strong>Note:</strong><br>First create an LI node,<br> then create a Text node,<br> then append the Text node to the LI node.<br>Finally append the LI node to the list.</p>
</body>
</html>