mirror of
https://github.com/rkd77/elinks.git
synced 2025-01-03 14:57:44 -05:00
[querySelector] Added to element
This commit is contained in:
parent
160deb55d9
commit
408f644b4c
@ -23,6 +23,7 @@
|
||||
#include "document/forms.h"
|
||||
#include "document/view.h"
|
||||
#include "ecmascript/ecmascript.h"
|
||||
#include "ecmascript/spidermonkey/css2xpath.h"
|
||||
#include "ecmascript/spidermonkey/element.h"
|
||||
#include "ecmascript/spidermonkey/window.h"
|
||||
#include "intl/libintl.h"
|
||||
@ -2308,6 +2309,8 @@ static bool element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *
|
||||
static bool element_insertBefore(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
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_querySelector(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
static bool element_querySelectorAll(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
static bool element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
static bool element_removeChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||
@ -2324,6 +2327,8 @@ const spidermonkeyFunctionSpec element_funcs[] = {
|
||||
{ "insertBefore", element_insertBefore, 2 },
|
||||
{ "isEqualNode", element_isEqualNode, 1 },
|
||||
{ "isSameNode", element_isSameNode, 1 },
|
||||
{ "querySelector", element_querySelector, 1 },
|
||||
{ "querySelectorAll", element_querySelectorAll, 1 },
|
||||
{ "remove", element_remove, 0 },
|
||||
{ "removeChild", element_removeChild, 1 },
|
||||
{ "setAttribute", element_setAttribute, 2 },
|
||||
@ -2887,6 +2892,121 @@ element_isSameNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
element_querySelector(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
||||
|
||||
if (argc != 1) {
|
||||
args.rval().setBoolean(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
||||
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) {
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
xmlpp::Element *el = JS_GetPrivate(hobj);
|
||||
|
||||
if (!el) {
|
||||
args.rval().setBoolean(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
struct string cssstr;
|
||||
init_string(&cssstr);
|
||||
jshandle_value_to_char_string(&cssstr, ctx, args[0]);
|
||||
xmlpp::ustring css = cssstr.source;
|
||||
|
||||
xmlpp::ustring xpath = css2xpath(css);
|
||||
|
||||
done_string(&cssstr);
|
||||
|
||||
auto elements = el->find(xpath);
|
||||
|
||||
if (elements.size() == 0) {
|
||||
args.rval().setNull();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto node = elements[0];
|
||||
|
||||
JSObject *elem = getElement(ctx, node);
|
||||
|
||||
if (elem) {
|
||||
args.rval().setObject(*elem);
|
||||
} else {
|
||||
args.rval().setNull();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
element_querySelectorAll(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
||||
|
||||
if (argc != 1) {
|
||||
args.rval().setBoolean(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
||||
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) {
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
xmlpp::Element *el = JS_GetPrivate(hobj);
|
||||
|
||||
if (!el) {
|
||||
args.rval().setBoolean(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
struct string cssstr;
|
||||
|
||||
init_string(&cssstr);
|
||||
jshandle_value_to_char_string(&cssstr, ctx, args[0]);
|
||||
xmlpp::ustring css = cssstr.source;
|
||||
|
||||
xmlpp::ustring xpath = css2xpath(css);
|
||||
|
||||
done_string(&cssstr);
|
||||
|
||||
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
|
||||
|
||||
*elements = el->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
|
||||
element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||
{
|
||||
|
29
test/ecmascript/el_querySelector.html
Normal file
29
test/ecmascript/el_querySelector.html
Normal file
@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
#myDIV {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="myDIV">
|
||||
<h2 class="example">A heading with class="example" in div</h2>
|
||||
<p class="example">A paragraph with class="example" in div.</p>
|
||||
</div>
|
||||
|
||||
<p>Click the button to change the text of the first element with class="example" in div.</p>
|
||||
|
||||
<button onclick="myFunction()">Try it</button>
|
||||
|
||||
<script>
|
||||
function myFunction() {
|
||||
var x = document.getElementById("myDIV");
|
||||
x.querySelector(".example").innerHTML = "Hello World!";
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
32
test/ecmascript/el_querySelectorAll.html
Normal file
32
test/ecmascript/el_querySelectorAll.html
Normal file
@ -0,0 +1,32 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
#myDIV {
|
||||
border: 1px solid black;
|
||||
margin: 5px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="myDIV">
|
||||
<h2 class="example">A heading with class="example" in div</h2>
|
||||
<p class="example">A paragraph with class="example" in div.</p>
|
||||
</div>
|
||||
|
||||
<p>Click the button to add a background color to the first element in DIV with class="example" (index 0).</p>
|
||||
|
||||
<button onclick="myFunction()">Try it</button>
|
||||
|
||||
<p><strong>Note:</strong> The querySelectorAll() method is not supported in Internet Explorer 8 and earlier versions.</p>
|
||||
|
||||
<script>
|
||||
function myFunction() {
|
||||
var x = document.getElementById("myDIV").querySelectorAll(".example");
|
||||
x[1].innerText = "red";
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user