1
0
Fork 0

[quickjs] Fixes in querySelector

This commit is contained in:
Witold Filipczyk 2021-12-17 15:24:55 +01:00
parent 65a091ee1a
commit 8ec96e4f90
2 changed files with 24 additions and 4 deletions

View File

@ -1483,6 +1483,19 @@ js_element_matches(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst
return JS_NewBool(ctx, elements.size());
}
static bool
isAncestor(xmlpp::Element *el, xmlpp::Element *node)
{
while (node) {
if (el == node) {
return true;
}
node = node->get_parent();
}
return false;
}
static JSValue
js_element_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
@ -1516,12 +1529,15 @@ js_element_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSValu
return JS_NULL;
}
if (elements.size() == 0) {
return JS_NULL;
for (auto node: elements)
{
if (isAncestor(el, node))
{
return getElement(ctx, node);
}
}
auto node = elements[0];
return getElement(ctx, node);
return JS_NULL;
}
static JSValue
@ -1558,6 +1574,7 @@ js_element_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JSV
*elements = el->find(xpath);
} catch (xmlpp::exception) {
}
elements->erase(std::remove_if(elements->begin(), elements->end(), [el](xmlpp::Node *node){return !isAncestor(el, node);}));
return getCollection(ctx, elements);
}

View File

@ -1,3 +1,6 @@
<div>
<span>first span</span>
</div>
<div>
<h5>Original content</h5>
<p>