mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
[js] getElementsByTagName
TODO: sanitize input for this function
This commit is contained in:
parent
3e30d82fb4
commit
d22e77a796
@ -463,14 +463,16 @@ 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_getElementByName(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);
|
||||
|
||||
const spidermonkeyFunctionSpec document_funcs[] = {
|
||||
{ "write", document_write, 1 },
|
||||
{ "writeln", document_writeln, 1 },
|
||||
{ "replace", document_replace, 1 },
|
||||
{ "getElementById", document_getElementById, 1 },
|
||||
{ "getElementsByName", document_getElementByName, 1 },
|
||||
{ "getElementsByName", document_getElementsByName, 1 },
|
||||
{ "getElementsByTagName", document_getElementsByTagName, 1 },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
@ -769,7 +771,7 @@ document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
}
|
||||
|
||||
static bool
|
||||
document_getElementByName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
document_getElementsByName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
{
|
||||
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
||||
|
||||
@ -827,3 +829,61 @@ document_getElementByName(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
document_getElementsByTagName(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::transform(id.begin(), id.end(), id.begin(), ::tolower);
|
||||
|
||||
std::string xpath = "//";
|
||||
xpath += id;
|
||||
|
||||
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;
|
||||
}
|
||||
|
22
test/ecmascript/getElementsByTagName.html
Normal file
22
test/ecmascript/getElementsByTagName.html
Normal file
@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
First Name: <input name="fname" type="text" value="Michael"><br>
|
||||
First Name: <input name="fname" type="text" value="Doug">
|
||||
|
||||
<p>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()">Try it</button>
|
||||
|
||||
<p id="demo"></p>
|
||||
|
||||
<script>
|
||||
function myFunction() {
|
||||
var x = document.getElementsByTagName("P").item(1).outerHTML;
|
||||
alert(x);
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user