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

[js] title (read)

This commit is contained in:
Witold Filipczyk 2021-05-04 15:42:52 +02:00
parent cf5841b8de
commit adadc03821
2 changed files with 92 additions and 0 deletions

View File

@ -54,6 +54,8 @@ static bool element_get_property_innerHtml(JSContext *ctx, unsigned int argc, JS
static bool element_set_property_innerHtml(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_title(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp);
JSClassOps element_ops = {
JS_PropertyStub, nullptr,
@ -71,6 +73,7 @@ JSPropertySpec element_props[] = {
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("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE),
JS_PSGS("title", element_get_property_title, element_set_property_title, JSPROP_ENUMERATE),
JS_PS_END
};
@ -117,6 +120,49 @@ element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_get_property_title(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;
}
tree<HTML::Node> *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setNull();
return true;
}
tree<HTML::Node>::iterator it = el->begin();
it->parseAttributes();
std::string v = it->attribute("title").second;
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
return true;
}
static int was_el = 0;
static void
@ -358,6 +404,34 @@ element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
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;
struct view_state *vs = interpreter->vs;
if (!vs) {
return true;
}
return true;
}
JSObject *
getElement(JSContext *ctx, void *node)

View File

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