1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[js] lastElementChild

This commit is contained in:
Witold Filipczyk 2021-05-10 19:14:39 +02:00
parent 39c0e7679d
commit a8e27fc730
2 changed files with 73 additions and 0 deletions

View File

@ -66,6 +66,7 @@ static bool element_set_property_innerHtml(JSContext *ctx, unsigned int argc, JS
static bool element_get_property_lang(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_lang(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_lastChild(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_lastElementChild(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_tagName(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -96,6 +97,7 @@ JSPropertySpec element_props[] = {
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),
JS_PSG("lastChild", element_get_property_lastChild, JSPROP_ENUMERATE),
JS_PSG("lastElementChild", element_get_property_lastElementChild, JSPROP_ENUMERATE),
JS_PSGS("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE),
JS_PSG("tagName", element_get_property_tagName, JSPROP_ENUMERATE),
JS_PSGS("textContent", element_get_property_textContent, element_set_property_textContent, JSPROP_ENUMERATE),
@ -448,6 +450,61 @@ element_get_property_lastChild(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_get_property_lastElementChild(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 nodes = el->get_children();
if (nodes.empty()) {
args.rval().setNull();
return true;
}
auto it = nodes.rbegin();
auto end = nodes.rend();
for (; it != end; ++it) {
const auto element = dynamic_cast<const xmlpp::Element*>(*it);
if (element) {
JSObject *elem = getElement(ctx, element);
args.rval().setObject(*elem);
return true;
}
}
args.rval().setNull();
return true;
}
static bool
element_get_property_tagName(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="/">BBB<b id="b1">AAA</b><u dir="auto" id="ble" title="test">UUU</u>AAAAAAA<!-- comment -->
</a>
<a id="bb" dir="blalalala" href="/">BB</a>
<script>
function aa()
{
alert(document.getElementById('blabla').lastElementChild.outerHTML);
}
</script>
<button onclick="return aa()">blabla lastElementChild outerHTML</button>
</body>
</html>