lexbor: CSS selector: expected to match, but it does not
It could be related with the :has addition, since the problem goes away when removing it. But this is a simplified example, where I would have expected a match, but it does not match:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div id="foo">
<div>
<h3 class="bar"></h3>
<div>
<a href="https://example.com/">
<p>
<img id="abc_123" alt="" />
</p>
</a>
</div>
</div>
</div>
</body>
</html>
This should match:
document.querySelector("#foo div:has(h3.bar) a > p > img[id^=abc_][alt]")
But with lexbor, I get no result. Here is an example:
#include <lexbor/html/html.h>
#include <lexbor/css/css.h>
#include <lexbor/selectors/selectors.h>
lxb_status_t
callback(const lxb_char_t *data, size_t len, void *ctx)
{
printf("%.*s", (int) len, (const char *) data);
return LXB_STATUS_OK;
}
lxb_status_t
find_callback(lxb_dom_node_t *node, lxb_css_selector_specificity_t spec, void *ctx)
{
unsigned *count = ctx;
(*count)++;
printf("%u) ", *count);
(void) lxb_html_serialize_cb(node, callback, NULL);
printf("\n");
return LXB_STATUS_OK;
}
int
main(int argc, const char *argv[])
{
unsigned count = 0;
lxb_status_t status;
lxb_dom_node_t *body;
lxb_selectors_t *selectors;
lxb_html_document_t *document;
lxb_css_parser_t *parser;
lxb_css_selector_list_t *list;
static const lxb_char_t html[] = "<div id=\"foo\">"
" <div>"
" <h3 class=\"bar\"></h3>"
" <div>"
" <a href=\"https://example.com/\">"
" <p>"
" <img id=\"abc_123\" alt=\"\" />"
" </p>"
" </a>"
" </div>"
" </div>"
"</div>";
static const lxb_char_t slctrs[] = "#foo div:has(h3.bar) a > p > img[id^=abc_][alt]";
// Note: it matches when the ":has" is removed like that:
// static const lxb_char_t slctrs[] = "#foo div a > p > img[id^=abc_][alt]";
document = lxb_html_document_create();
status = lxb_html_document_parse(document, html, sizeof(html) / sizeof(lxb_char_t) - 1);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
parser = lxb_css_parser_create();
status = lxb_css_parser_init(parser, NULL);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
selectors = lxb_selectors_create();
status = lxb_selectors_init(selectors);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
/* Parse and get the log. */
list = lxb_css_selectors_parse(parser, slctrs, sizeof(slctrs) / sizeof(lxb_char_t) - 1);
if (parser->status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
printf("Selectors: ");
(void) lxb_css_selector_serialize_list_chain(list, callback, NULL);
printf("\n");
body = lxb_dom_interface_node(document);
// Note: the following matches nothing, even though I had expected one match
printf("Found:\n--- begin ---\n");
status = lxb_selectors_find(selectors, body, list, find_callback, &count);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
printf("--- end ---\n");
(void) lxb_selectors_destroy(selectors, true);
(void) lxb_css_parser_destroy(parser, true);
lxb_css_selector_list_destroy_memory(list);
lxb_html_document_destroy(document);
return EXIT_SUCCESS;
}
Note: I’m normally using the Crystal wrapper, so I’m not too familiar with the C API.
Here is the issue on the Crystal library; even though my current assumption is that it not caused by the wrapper, but by the CSS engine: https://github.com/kostya/lexbor/issues/48
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 19 (15 by maintainers)
I put fuzzing testing overnight. If everything will be ok, I’ll commit the patch tomorrow. I’m sure it’ll be fine, it’s been running for three hours and no errors.
@philipp-classen @nielsdos
Guys, I haven’t forgotten about it, I’ve written it all up, it’s in the testing phase now. It’ll be a little while before we get a patch
@nielsdos thank you! I just got distracted and forgot about it a bit. But it’s okay, I’m on it.
Just a quick feedback: The new version is running in production now and looks all good. I cannot comment on performance, since CSS selectors are not the dominating factor in the workload.
From my side, we can close the issue. The bug is fixed. Thanks again!
Thanks a lot! I see it is already updated in the Crystal wrapper (passing there also the original example). Will be also able to test in production on Monday.
Thanks!
Thanks! I now have to wait for it to be included into php to be able to test 😃
@philipp-classen @momala454 @nielsdos
We’ve been coming to this for a long time, and now it’s happened 😃
Fixed!
I didn’t test for performance, only for correct operation. My feeling is that some selectors may be slower and others much faster. But these are feelings that are not supported by anything.
Hi @momala454
I’ve completely rewritten the approach to finding nodes on Selectors. Did some testing, everything works. I need a couple more days to review the code again and publish it. I apologize for the delay, it will be up soon.