mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -05:00
[quickjs] element.matches
Also change querySelector and querySelectorAll. They search from current node, not root.
This commit is contained in:
parent
73e84f3db4
commit
17a68b0cfd
@ -1396,6 +1396,46 @@ js_element_isSameNode(JSContext *ctx, JSValueConst this_val, int argc, JSValueCo
|
||||
return JS_NewBool(ctx, (el == el2));
|
||||
}
|
||||
|
||||
static JSValue
|
||||
js_element_matches(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
if (argc != 1) {
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
xmlpp::Element *el = JS_GetOpaque(this_val, js_element_class_id);
|
||||
|
||||
if (!el) {
|
||||
return JS_FALSE;
|
||||
}
|
||||
const char *str;
|
||||
size_t len;
|
||||
str = JS_ToCStringLen(ctx, &len, argv[0]);
|
||||
|
||||
if (!str) {
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
xmlpp::ustring css = str;
|
||||
xmlpp::ustring xpath = css2xpath(css);
|
||||
if (xpath[0] == '/' && xpath[1] == '/')
|
||||
{
|
||||
xpath = xmlpp::ustring("descendant-or-self::") + xpath.substr(2);
|
||||
}
|
||||
JS_FreeCString(ctx, str);
|
||||
|
||||
xmlpp::Node::NodeSet elements;
|
||||
|
||||
try {
|
||||
elements = el->find(xpath);
|
||||
} catch (xmlpp::exception) {
|
||||
return JS_FALSE;
|
||||
}
|
||||
|
||||
return JS_NewBool(ctx, elements.size());
|
||||
}
|
||||
|
||||
static JSValue
|
||||
js_element_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
||||
{
|
||||
@ -1419,6 +1459,11 @@ js_element_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSValu
|
||||
}
|
||||
xmlpp::ustring css = str;
|
||||
xmlpp::ustring xpath = css2xpath(css);
|
||||
|
||||
if (xpath[0] == '/' && xpath[1] == '/')
|
||||
{
|
||||
xpath = xmlpp::ustring("descendant-or-self::") + xpath.substr(2);
|
||||
}
|
||||
JS_FreeCString(ctx, str);
|
||||
xmlpp::Node::NodeSet elements;
|
||||
|
||||
@ -1459,6 +1504,11 @@ js_element_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JSV
|
||||
}
|
||||
xmlpp::ustring css = str;
|
||||
xmlpp::ustring xpath = css2xpath(css);
|
||||
|
||||
if (xpath[0] == '/' && xpath[1] == '/')
|
||||
{
|
||||
xpath = xmlpp::ustring("descendant-or-self::") + xpath.substr(2);
|
||||
}
|
||||
JS_FreeCString(ctx, str);
|
||||
xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet;
|
||||
|
||||
@ -1619,6 +1669,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = {
|
||||
JS_CFUNC_DEF("insertBefore", 2, js_element_insertBefore),
|
||||
JS_CFUNC_DEF("isEqualNode", 1, js_element_isEqualNode),
|
||||
JS_CFUNC_DEF("isSameNode", 1, js_element_isSameNode),
|
||||
JS_CFUNC_DEF("matches",1, js_element_matches),
|
||||
JS_CFUNC_DEF("querySelector",1, js_element_querySelector),
|
||||
JS_CFUNC_DEF("querySelectorAll",1, js_element_querySelectorAll),
|
||||
JS_CFUNC_DEF("remove", 0, js_element_remove),
|
||||
|
15
test/ecmascript/matches.html
Normal file
15
test/ecmascript/matches.html
Normal file
@ -0,0 +1,15 @@
|
||||
<ul id="birds">
|
||||
<li>Orange-winged parrot</li>
|
||||
<li class="endangered">Philippine eagle</li>
|
||||
<li>Great white pelican</li>
|
||||
</ul>
|
||||
|
||||
<script type="text/javascript">
|
||||
var birds = document.getElementsByTagName('li');
|
||||
|
||||
for (var i = 0; i < birds.length; i++) {
|
||||
if (birds[i].matches('.endangered')) {
|
||||
alert('The ' + birds[i].textContent + ' is endangered!');
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
x
Reference in New Issue
Block a user