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

[mujs] element.closest

This commit is contained in:
Witold Filipczyk 2024-05-07 16:41:30 +02:00
parent 786cbf9500
commit 4da6e1f85a

View File

@ -2256,46 +2256,54 @@ mjs_element_closest(js_State *J)
#ifdef ECMASCRIPT_DEBUG #ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
// TODO dom_node *el = (dom_node *)(mjs_getprivate(J, 0));
#if 0 void *res = NULL;
xmlpp::Element *el = static_cast<xmlpp::Element *>(mjs_getprivate(J, 0)); struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J);
struct document_view *doc_view = interpreter->vs->doc_view;
struct document *document = doc_view->document;
if (!el) { if (!document->dom) {
js_pushnull(J); js_pushnull(J);
return; return;
} }
const char *str = js_tostring(J, 1); const char *selector = js_tostring(J, 1);
xmlpp::ustring css = str;
xmlpp::ustring xpath = css2xpath(css);
xmlpp::Node::NodeSet elements; if (!selector) {
js_pushnull(J);
return;
}
dom_node *root = NULL; /* root element of document */
/* Get root element */
dom_exception exc = dom_document_get_document_element(document->dom, &root);
try { if (exc != DOM_NO_ERR || !root) {
elements = el->find(xpath);
} catch (xmlpp::exception &e) {
js_pushnull(J); js_pushnull(J);
return; return;
} }
if (elements.size() == 0) { while (el) {
js_pushnull(J); res = el_match_selector(selector, el);
return;
}
while (el) if (res) {
{ break;
for (auto node: elements)
{
if (isAncestor(el, node))
{
mjs_push_element(J, node);
return;
}
} }
el = el->get_parent(); if (el == root) {
break;
}
dom_node *node = NULL;
exc = dom_node_get_parent_node(el, &node);
if (exc != DOM_NO_ERR || !node) {
break;
}
el = node;
} }
#endif dom_node_unref(root);
js_pushnull(J);
if (!res) {
js_pushnull(J);
return;
}
mjs_push_element(J, res);
} }
static void static void