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

[js] getElementsByClassName

This commit is contained in:
Witold Filipczyk 2021-05-12 16:30:36 +02:00
parent d22e77a796
commit 799f256157
2 changed files with 82 additions and 0 deletions

View File

@ -463,6 +463,7 @@ static bool document_write(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_writeln(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_replace(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *rval);
@ -471,6 +472,7 @@ const spidermonkeyFunctionSpec document_funcs[] = {
{ "writeln", document_writeln, 1 },
{ "replace", document_replace, 1 },
{ "getElementById", document_getElementById, 1 },
{ "getElementsByClassName", document_getElementsByClassName, 1 },
{ "getElementsByName", document_getElementsByName, 1 },
{ "getElementsByTagName", document_getElementsByTagName, 1 },
{ NULL }
@ -770,6 +772,64 @@ document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
if (argc != 1) {
args.rval().setBoolean(false);
return true;
}
JSCompartment *comp = js::GetContextCompartment(ctx);
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct document_view *doc_view = interpreter->vs->doc_view;
struct document *document = doc_view->document;
if (!document->dom) {
document->dom = document_parse(document);
}
if (!document->dom) {
args.rval().setNull();
return true;
}
xmlpp::Element* root = (xmlpp::Element *)document->dom;
struct string idstr;
init_string(&idstr);
jshandle_value_to_char_string(&idstr, ctx, &args[0]);
std::string id = idstr.source;
std::string xpath = "//*[@class=\"";
xpath += id;
xpath += "\"]";
done_string(&idstr);
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
*elements = root->find(xpath);
if (elements->size() == 0) {
args.rval().setNull();
return true;
}
JSObject *elem = getCollection(ctx, elements);
if (elem) {
args.rval().setObject(*elem);
} else {
args.rval().setNull();
}
return true;
}
static bool
document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<body class="test">
First Name: <input name="fname" type="text" value="Michael"><br>
First Name: <input name="fname" type="text" value="Doug">
<p class="a">Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".</p>
<button onclick="myFunction()" class="test">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByClassName("test").item(1).outerHTML;
alert(x);
}
</script>
</body>
</html>