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

[js] getAttribute

This commit is contained in:
Witold Filipczyk 2021-09-08 10:41:36 +02:00
parent 5737a4d345
commit 686e83afaa
2 changed files with 71 additions and 1 deletions

View File

@ -1963,6 +1963,7 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
static bool element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
@ -1976,6 +1977,7 @@ static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *r
const spidermonkeyFunctionSpec element_funcs[] = {
{ "appendChild", element_appendChild, 1 },
{ "contains", element_contains, 1 },
{ "getAttribute", element_getAttribute, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 },
{ "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 },
@ -2103,6 +2105,52 @@ element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_getAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Realm *comp = js::GetContextRealm(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::GetRealmPrivate(comp);
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false;
xmlpp::Element *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setBoolean(false);
return true;
}
char *vv = jsval_to_string(ctx, args[0]);
if (vv) {
std::string v = vv;
xmlpp::Attribute *attr = el->get_attribute(v);
if (!attr) {
args.rval().setNull();
} else {
xmlpp::ustring val = attr->get_value();
args.rval().setString(JS_NewStringCopyZ(ctx, val.c_str()));
}
mem_free(vv);
} else {
args.rval().setNull();
}
return true;
}
static bool
element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
@ -2140,7 +2188,6 @@ element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{

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').getAttribute('rel'));
}
function bb()
{
alert(document.getElementById('ble').getAttribute('href'));
}
</script>
<button onclick="return aa()">blabla rel nofollow</button>
<button onclick="return bb()">ble href null</button>
</body>
</html>