1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[js] element.getAttributeNode

This commit is contained in:
Witold Filipczyk 2021-05-14 21:49:56 +02:00
parent 5c5621aadd
commit 09988b7fcd
2 changed files with 57 additions and 0 deletions

View File

@ -1066,15 +1066,49 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true; return true;
} }
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_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = { const spidermonkeyFunctionSpec element_funcs[] = {
{ "getAttributeNode", element_getAttributeNode, 1 },
{ "hasAttribute", element_hasAttribute, 1 }, { "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 }, { "hasAttributes", element_hasAttributes, 0 },
{ NULL } { NULL }
}; };
static bool
element_getAttributeNode(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;
xmlpp::Element *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setUndefined();
return true;
}
std::string v = JS_EncodeString(ctx, args[0].toString());
xmlpp::Attribute *attr = el->get_attribute(v);
JSObject *obj = getAttr(ctx, attr);
args.rval().setObject(*obj);
return true;
}
static bool static bool
element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval) element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<h1 class="democlass">Hello World</h1>
<p>Click the button to display the value of the class attribute node of the h1 element.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var elmnt = document.getElementsByTagName("H1")[0];
var attr = elmnt.getAttributeNode("class").value;
alert(attr);
}
</script>
</body>
</html>