mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
[spidermonkey] element.closest
There is invalid free when selector is not matched.
This commit is contained in:
parent
10555292b4
commit
09447c7812
@ -4025,56 +4025,38 @@ element_closest(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
|||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
char *selector = jsval_to_string(ctx, args[0]);
|
||||||
|
if (!selector) {
|
||||||
|
args.rval().setNull();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot<dom_node>(hobj, 0);
|
||||||
#if 0
|
|
||||||
xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot<xmlpp::Element>(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) {
|
if (!el) {
|
||||||
args.rval().setNull();
|
args.rval().setNull();
|
||||||
return true;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
var element = document.getElementById("myElement");
|
var element = document.getElementById("myElement");
|
||||||
var closest = element.closest(".container");
|
var closest = element.closest("div.container");
|
||||||
|
|
||||||
console.error('element.closest.html');
|
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');
|
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');
|
||||||
|
Loading…
Reference in New Issue
Block a user