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

[js] document.anchors

This commit is contained in:
Witold Filipczyk 2021-05-12 17:08:05 +02:00
parent 799f256157
commit 8e97e8b048
2 changed files with 69 additions and 0 deletions

View File

@ -56,6 +56,7 @@
#include <iostream>
static bool document_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static void *document_parse(struct document *document);
JSClassOps document_ops = {
JS_PropertyStub, nullptr,
@ -71,6 +72,48 @@ JSClass document_class = {
&document_ops
};
static bool
document_get_property_anchors(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
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;
std::string xpath = "//a";
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;
}
#ifdef CONFIG_COOKIES
static bool
document_get_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp)
@ -404,6 +447,7 @@ document_set_property_url(JSContext *ctx, int argc, JS::Value *vp)
/* "cookie" is special; it isn't a regular property but we channel it to the
* cookie-module. XXX: Would it work if "cookie" was defined in this array? */
JSPropertySpec document_props[] = {
JS_PSG("anchors", document_get_property_anchors, JSPROP_ENUMERATE),
#ifdef CONFIG_COOKIES
JS_PSGS("cookie", document_get_property_cookie, document_set_property_cookie, JSPROP_ENUMERATE),
#endif

View File

@ -0,0 +1,25 @@
<!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>
<a href="/">/</a>
<a href="/home">/home</a>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.anchors.item(1).outerHTML;
alert(x);
}
</script>
</body>
</html>