1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[spidermonkey] element.closest

There is invalid free when selector is not matched.
This commit is contained in:
Witold Filipczyk 2024-05-05 21:02:36 +02:00
parent 10555292b4
commit 09447c7812
2 changed files with 26 additions and 44 deletions

View File

@ -4025,56 +4025,38 @@ element_closest(JSContext *ctx, unsigned int argc, JS::Value *vp)
#endif
return false;
}
char *selector = jsval_to_string(ctx, args[0]);
if (!selector) {
args.rval().setNull();
return true;
}
// TODO
#if 0
xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot<xmlpp::Element>(hobj, 0);
dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot<dom_node>(hobj, 0);
while (el) {
void *res = el_match_selector(selector, el);
if (res) {
el = (dom_node *)res;
break;
}
dom_node *node = NULL;
dom_exception exc = dom_node_get_parent_node(el, &node);
if (exc != DOM_NO_ERR || !node) {
el = NULL;
break;
}
el = node;
}
mem_free(selector);
if (!el) {
args.rval().setNull();
return true;
}
JSObject *ret = getElement(ctx, el);
args.rval().setObject(*ret);
struct string cssstr;
if (!init_string(&cssstr)) {
return false;
}
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;
try {
elements = el->find(xpath);
} catch (xmlpp::exception &e) {
args.rval().setNull();
return true;
}
if (elements.size() == 0) {
args.rval().setNull();
return true;
}
while (el)
{
for (auto node: elements)
{
if (isAncestor(el, static_cast<xmlpp::Element *>(node)))
{
JSObject *elem = getElement(ctx, node);
args.rval().setObject(*elem);
return true;
}
}
el = el->get_parent();
}
args.rval().setNull();
#endif
return true;
}

View File

@ -10,7 +10,7 @@
<script>
var element = document.getElementById("myElement");
var closest = element.closest(".container");
var closest = element.closest("div.container");
console.error('element.closest.html');
console.assert(closest.outerHTML === '<DIV class="demo container">Parent\n <DIV id="myElement" class="demo">The outer HTML of closest element will be shown.</DIV>\n </DIV>', 'TODO');