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

[spidermonkey] element.closest

This commit is contained in:
Witold Filipczyk 2021-12-01 16:46:44 +01:00
parent 2e466eaf6a
commit 29fba4bb33

View File

@ -2309,6 +2309,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_cloneNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_closest(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);
@ -2328,6 +2329,7 @@ static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *r
const spidermonkeyFunctionSpec element_funcs[] = {
{ "appendChild", element_appendChild, 1 },
{ "cloneNode", element_cloneNode, 1 },
{ "closest", element_closest, 1 },
{ "contains", element_contains, 1 },
{ "getAttribute", element_getAttribute, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 },
@ -2496,6 +2498,69 @@ element_cloneNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
}
}
static bool
element_closest(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().setNull();
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);
if (xpath[0] == '/' && xpath[1] == '/')
{
xpath = xmlpp::ustring("descendant-or-self::") + xpath.substr(2);
}
done_string(&cssstr);
xmlpp::Node::NodeSet elements;
do {
try {
elements = el->find(xpath);
if (elements.size()) {
JSObject *elem = getElement(ctx, el);
args.rval().setObject(*elem);
return true;
}
el = dynamic_cast<xmlpp::Element*>(el->get_parent());
} catch (xmlpp::exception) {
args.rval().setNull();
return true;
}
} while (el);
args.rval().setNull();
return true;
}
static bool
element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval)
{