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

[js] element.nodeType

This commit is contained in:
Witold Filipczyk 2021-05-18 16:21:02 +02:00
parent 3e8186922d
commit e230acf2ab
2 changed files with 70 additions and 0 deletions

View File

@ -71,6 +71,7 @@ static bool element_get_property_lastChild(JSContext *ctx, unsigned int argc, JS
static bool element_get_property_lastElementChild(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_nextElementSibling(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_nextSibling(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_nodeType(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_parentElement(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -109,6 +110,7 @@ JSPropertySpec element_props[] = {
JS_PSG("lastElementChild", element_get_property_lastElementChild, JSPROP_ENUMERATE),
JS_PSG("nextElementSibling", element_get_property_nextElementSibling, JSPROP_ENUMERATE),
JS_PSG("nextSibling", element_get_property_nextSibling, JSPROP_ENUMERATE),
JS_PSG("nodeType", element_get_property_nodeType, JSPROP_ENUMERATE),
JS_PSGS("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE),
JS_PSG("parentElement", element_get_property_parentElement, JSPROP_ENUMERATE),
JS_PSG("parentNode", element_get_property_parentNode, JSPROP_ENUMERATE),
@ -622,6 +624,55 @@ element_get_property_nextElementSibling(JSContext *ctx, unsigned int argc, JS::V
return true;
}
static bool
element_get_property_nodeType(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct view_state *vs;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false;
vs = interpreter->vs;
if (!vs) {
return false;
}
xmlpp::Node *node = JS_GetPrivate(hobj);
if (!node) {
args.rval().setNull();
return true;
}
int ret = 8;
if (dynamic_cast<const xmlpp::Element*>(node)) {
ret = 1;
} else if (dynamic_cast<const xmlpp::Attribute*>(node)) {
ret = 2;
} else if (dynamic_cast<const xmlpp::TextNode*>(node)) {
ret = 3;
} else if (dynamic_cast<const xmlpp::CommentNode*>(node)) {
ret = 8;
}
args.rval().setInt32(ret);
return true;
}
static bool
element_get_property_nextSibling(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<body>
<p id="myP">Click the button to get the node type of this element.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myP").firstChild.nodeType;
alert(x);
}
</script>
</body>
</html>