1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-27 01:25:34 +00:00

[js] firstChild

This commit is contained in:
Witold Filipczyk 2021-05-09 21:38:23 +02:00
parent e020f83d34
commit 8c7517189d
2 changed files with 64 additions and 0 deletions

View File

@ -57,6 +57,7 @@ static bool element_get_property_className(JSContext *ctx, unsigned int argc, JS
static bool element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_firstChild(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_innerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -87,6 +88,7 @@ JSPropertySpec element_props[] = {
JS_PSG("childElementCount", element_get_property_childElementCount, JSPROP_ENUMERATE),
JS_PSGS("className", element_get_property_className, element_set_property_className, JSPROP_ENUMERATE),
JS_PSGS("dir", element_get_property_dir, element_set_property_dir, JSPROP_ENUMERATE),
JS_PSG("firstChild", element_get_property_firstChild, JSPROP_ENUMERATE),
JS_PSGS("id", element_get_property_id, element_set_property_id, JSPROP_ENUMERATE),
JS_PSGS("innerHTML", element_get_property_innerHtml, element_set_property_innerHtml, JSPROP_ENUMERATE),
JS_PSGS("lang", element_get_property_lang, element_set_property_lang, JSPROP_ENUMERATE),
@ -218,6 +220,52 @@ element_get_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_get_property_firstChild(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::Element *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setNull();
return true;
}
auto node = el->get_first_child();
if (!node) {
args.rval().setNull();
return true;
}
JSObject *elem = getElement(ctx, node);
args.rval().setObject(*elem);
return true;
}
static bool
element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1,16 @@
<html>
<body>
<a href="/home">BBB</a>
<b dir="ltr" id="aaaa" class="a b c">bbb</b>
<a dir="rtl" id="blabla" href="/"><b id="b1">AAA</b><u dir="auto" id="ble" title="test">UUU</u>AAAAAAA
</a>
<a id="bb" dir="blalalala" href="/">BB</a>
<script>
function aa()
{
alert(document.getElementById('blabla').firstChild.id);
}
</script>
<button onclick="return aa()">Click me!</button>
</body>
</html>