Skip to content

Bug: query_selector returns no matches for child/descendant combinators #77

Description

@zacharyvmm

Hi,

I ran into what looks like a bug in query_selector when using CSS combinators.

I tested selectors using both child combinators (>) and descendant combinators ( ), and they all return no match in the example below.

If combinators are intentionally unsupported, could that please be documented clearly, and could query_selector return an error instead of silently producing an empty iterator?

Current behavior

These selectors all fail to match:

  • div > a
  • .article > a
  • section a
  • #articles a
  • section > div > a
  • #articles > .article > a

Expected behavior

A <a> should be matched for each selector in the HTML below.

Minimal reproduction

use tl;

const HTML: &str = r#"
<html>
    <head>
        <title>Articles</title>
    </head>
    <body>
        <section id="articles">
            <div class="article">
                <a href="/post/1"><b>Post</b> &lt;1&gt;</a>
            </div>
        </section>
    </body>
</html>
"#;

fn main() {
    let dom = tl::parse(HTML, tl::ParserOptions::default()).unwrap();

    assert!(
        dom.query_selector("div > a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use child selector"
    );

    assert!(
        dom.query_selector(".article > a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use child selector with class"
    );

    assert!(
        dom.query_selector("section a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use descendant selector"
    );

    assert!(
        dom.query_selector("#articles a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use descendant selector with id"
    );

    assert!(
        dom.query_selector("section > div > a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use child selectors"
    );

    assert!(
        dom.query_selector("#articles > .article > a")
            .expect("Failed to create selection iterator")
            .next()
            .is_some(),
        "Cannot use child selectors"
    );
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions