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

[js] hasAttribute, hasAttributes

This commit is contained in:
Witold Filipczyk 2021-05-09 20:26:08 +02:00
parent 1a8578aecb
commit 501a649068

View File

@ -842,8 +842,8 @@ static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *r
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = { const spidermonkeyFunctionSpec element_funcs[] = {
// { "hasAttribute", element_hasAttribute, 1 }, { "hasAttribute", element_hasAttribute, 1 },
// { "hasAttributes", element_hasAttributes, 0 }, { "hasAttributes", element_hasAttributes, 0 },
{ NULL } { NULL }
}; };
@ -864,12 +864,15 @@ element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false; return false;
tree<HTML::Node> *el = JS_GetPrivate(hobj); xmlpp::Element *el = JS_GetPrivate(hobj);
tree<HTML::Node>::iterator it = el->begin();
it->parseAttributes();
std::string attr = JS_EncodeString(ctx, args[0].toString()); if (!el) {
args.rval().setBoolean(it->attribute(attr).first); args.rval().setBoolean(false);
return true;
}
std::string v = JS_EncodeString(ctx, args[0].toString());
xmlpp::Attribute *attr = el->get_attribute(v);
args.rval().setBoolean((bool)attr);
return true; return true;
} }
@ -890,17 +893,18 @@ element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval)
args.rval().setBoolean(false); args.rval().setBoolean(false);
return true; return true;
} }
xmlpp::Element *el = JS_GetPrivate(hobj);
tree<HTML::Node> *el = JS_GetPrivate(hobj); if (!el) {
tree<HTML::Node>::iterator it = el->begin(); args.rval().setBoolean(false);
it->parseAttributes(); return true;
const std::map<std::string, std::string> attrs = it->attributes(); }
args.rval().setBoolean(!attrs.empty()); auto attrs = el->get_attributes();
args.rval().setBoolean((bool)attrs.size());
return true; return true;
} }
JSObject * JSObject *
getElement(JSContext *ctx, void *node) getElement(JSContext *ctx, void *node)
{ {