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

[js] element.setAttribute

This commit is contained in:
Witold Filipczyk 2021-06-02 18:26:05 +02:00
parent 4b5d115039
commit 07fccc9f87
2 changed files with 59 additions and 1 deletions

View File

@ -1593,6 +1593,7 @@ static bool element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *
static bool element_isEqualNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = {
{ "appendChild", element_appendChild, 1 },
@ -1603,7 +1604,7 @@ const spidermonkeyFunctionSpec element_funcs[] = {
{ "hasChildNodes", element_hasChildNodes, 0 },
{ "isEqualNode", element_isEqualNode, 1 },
{ "isSameNode", element_isSameNode, 1 },
{ "remove", element_remove, 0 },
{ "setAttribute", element_setAttribute, 2 },
{ NULL }
};
@ -1927,6 +1928,37 @@ element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp || argc != 2) {
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) {
return true;
}
std::string attr = JS_EncodeString(ctx, args[0].toString());
std::string value = JS_EncodeString(ctx, args[1].toString());
el->set_attribute(attr, value);
return true;
}
JSObject *
getElement(JSContext *ctx, void *node)
{

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p>Click the button to add a class attribute with the value of "democlass" to the h1 element.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementsByTagName("H1")[0].setAttribute("class", "democlass");
alert(document.getElementsByTagName("HTML")[0].outerHTML);
}
</script>
</body>
</html>