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

[js] className (read)

This commit is contained in:
Witold Filipczyk 2021-05-04 18:34:10 +02:00
parent b0ee34bdd5
commit 1e8f86628d
2 changed files with 91 additions and 0 deletions

View File

@ -52,6 +52,8 @@
using namespace htmlcxx;
static bool element_get_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
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_id(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -79,6 +81,7 @@ JSClass element_class = {
};
JSPropertySpec element_props[] = {
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_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),
@ -89,6 +92,49 @@ JSPropertySpec element_props[] = {
JS_PS_END
};
static bool
element_get_property_className(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("class").second;
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
return true;
}
static bool
element_get_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -461,6 +507,34 @@ element_get_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_set_property_className(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;
}
static bool
element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1,17 @@
<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>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('aaaa').className);
}
</script>
<button onclick="return aa()">Click me!</button>
</body>
</html>