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

[js] element.hasAttribute(...)

This commit is contained in:
Witold Filipczyk 2021-05-07 19:03:53 +02:00
parent 745387e386
commit 0bdbb6aca6
2 changed files with 58 additions and 1 deletions

View File

@ -816,6 +816,40 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = {
{ "hasAttribute", element_hasAttribute, 1 },
{ NULL }
};
static bool
element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp || argc != 1) {
return false;
}
JS::CallArgs args = CallArgsFromVp(argc, rval);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false;
tree<HTML::Node> *el = JS_GetPrivate(hobj);
tree<HTML::Node>::iterator it = el->begin();
it->parseAttributes();
std::string attr = JS_EncodeString(ctx, args[0].toString());
args.rval().setBoolean(it->attribute(attr).first);
return true;
}
JSObject *
getElement(JSContext *ctx, void *node)
@ -829,7 +863,7 @@ getElement(JSContext *ctx, void *node)
JS::RootedObject r_el(ctx, el);
JS_DefineProperties(ctx, r_el, (JSPropertySpec *) element_props);
// spidermonkey_DefineFunctions(ctx, el, element_funcs);
spidermonkey_DefineFunctions(ctx, el, element_funcs);
JS_SetPrivate(el, node);

View File

@ -0,0 +1,23 @@
<html>
<body>
<a href="/home">BBB</a>
<b id="aaaa">bbb</b>
<a id="blabla" href="/" rel="nofollow">
<b>AAA</b><u id="ble">UUU</u>AAAAAAA
</a>
<a id="bb" href="/">BB</a>
<script>
function aa()
{
alert(document.getElementById('blabla').hasAttribute('rel'));
}
function bb()
{
alert(document.getElementById('ble').hasAttribute('href'));
}
</script>
<button onclick="return aa()">blabla rel true</button>
<button onclick="return bb()">ble href false</button>
</body>
</html>