1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

[js] document.links

This commit is contained in:
Witold Filipczyk 2021-05-12 17:37:01 +02:00
parent ed3a11b288
commit 22cd7a0052
2 changed files with 78 additions and 0 deletions

View File

@ -312,6 +312,49 @@ document_get_property_head(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
document_get_property_links(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[@href]|//area[@href]";
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_get_property_location(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -586,6 +629,7 @@ JSPropertySpec document_props[] = {
#endif
JS_PSG("documentElement", document_get_property_documentElement, JSPROP_ENUMERATE),
JS_PSG("head", document_get_property_head, JSPROP_ENUMERATE),
JS_PSG("links", document_get_property_links, JSPROP_ENUMERATE),
JS_PSGS("location", document_get_property_location, document_set_property_location, JSPROP_ENUMERATE),
JS_PSG("referrer", document_get_property_referrer, JSPROP_ENUMERATE),
JS_PSGS("title", document_get_property_title, document_set_property_title, JSPROP_ENUMERATE), /* TODO: Charset? */

View File

@ -0,0 +1,34 @@
<!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>
<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<a href="/">/</a>
<a href="/home">/home</a>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.links.length;
alert(x);
}
</script>
</body>
</html>