mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -05:00
[js] getAttribute
This commit is contained in:
parent
5737a4d345
commit
686e83afaa
@ -1963,6 +1963,7 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|||||||
|
|
||||||
static bool element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
static bool element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
|
static bool element_getAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
|
||||||
@ -1976,6 +1977,7 @@ static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *r
|
|||||||
const spidermonkeyFunctionSpec element_funcs[] = {
|
const spidermonkeyFunctionSpec element_funcs[] = {
|
||||||
{ "appendChild", element_appendChild, 1 },
|
{ "appendChild", element_appendChild, 1 },
|
||||||
{ "contains", element_contains, 1 },
|
{ "contains", element_contains, 1 },
|
||||||
|
{ "getAttribute", element_getAttribute, 1 },
|
||||||
{ "getAttributeNode", element_getAttributeNode, 1 },
|
{ "getAttributeNode", element_getAttributeNode, 1 },
|
||||||
{ "hasAttribute", element_hasAttribute, 1 },
|
{ "hasAttribute", element_hasAttribute, 1 },
|
||||||
{ "hasAttributes", element_hasAttributes, 0 },
|
{ "hasAttributes", element_hasAttributes, 0 },
|
||||||
@ -2103,6 +2105,52 @@ element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
element_getAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||||
|
{
|
||||||
|
#ifdef ECMASCRIPT_DEBUG
|
||||||
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||||
|
#endif
|
||||||
|
JS::Realm *comp = js::GetContextRealm(ctx);
|
||||||
|
|
||||||
|
if (!comp || argc != 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::CallArgs args = CallArgsFromVp(argc, rval);
|
||||||
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
||||||
|
|
||||||
|
struct ecmascript_interpreter *interpreter = JS::GetRealmPrivate(comp);
|
||||||
|
|
||||||
|
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
xmlpp::Element *el = JS_GetPrivate(hobj);
|
||||||
|
|
||||||
|
if (!el) {
|
||||||
|
args.rval().setBoolean(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
char *vv = jsval_to_string(ctx, args[0]);
|
||||||
|
|
||||||
|
if (vv) {
|
||||||
|
std::string v = vv;
|
||||||
|
xmlpp::Attribute *attr = el->get_attribute(v);
|
||||||
|
|
||||||
|
if (!attr) {
|
||||||
|
args.rval().setNull();
|
||||||
|
} else {
|
||||||
|
xmlpp::ustring val = attr->get_value();
|
||||||
|
args.rval().setString(JS_NewStringCopyZ(ctx, val.c_str()));
|
||||||
|
}
|
||||||
|
mem_free(vv);
|
||||||
|
} else {
|
||||||
|
args.rval().setNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||||
{
|
{
|
||||||
@ -2140,7 +2188,6 @@ element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
|
||||||
{
|
{
|
||||||
|
23
test/ecmascript/getAttribute.html
Normal file
23
test/ecmascript/getAttribute.html
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<a href="/home">BBB</a>
|
||||||
|
<b id="aaaa">bbb</b>
|
||||||
|
<a id="blabla" href="/" rel="nofollow">
|
||||||
|
<b>AAA</b><u id="ble">UUU</u>AAAAAAA
|
||||||
|
</a>
|
||||||
|
<a id="bb" href="/">BB</a>
|
||||||
|
<script>
|
||||||
|
function aa()
|
||||||
|
{
|
||||||
|
alert(document.getElementById('blabla').getAttribute('rel'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function bb()
|
||||||
|
{
|
||||||
|
alert(document.getElementById('ble').getAttribute('href'));
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<button onclick="return aa()">blabla rel nofollow</button>
|
||||||
|
<button onclick="return bb()">ble href null</button>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user