Skip to content

Query selector parsing with parents wrong (precedence not handled) #54

Description

@tslnc04

If we have the query selector div > .hello.world > span, the current query selector parser produces the following result:

Parent(
    Tag([div]),
    And(
        Class([hello]),
        Parent(
            Class([world]),
            Tag([span])
        )
    )
)

This is equivalent to div > (.hello AND .world > span), which would be written as div.world > span.hello. This is a bit unexpected, as the spec would suggest the following as the result:

Parent (
    Tag([div]),
    Parent(
        And(
            Class([hello]),
            Class([world])
        ),
        Tag([span])
    )
)

I ran into this issue while making an attempt at fixing #22 but this seems to be a prerequisite to implementing child selectors. Since the parent combinator is associative, it's also possible to parse the query as left associative instead of right associative:

Parent (
    Parent (
        Tag([div]),
        And(
            Class([hello]),
            Class([world])
        ),
    ),
    Tag([span])
)

which seems to be easier for eventually implementing child selectors, since you can match a node to the right and its parent to the left. That assumes you have access to the parent though, which is part of how I had been planning on implementing the parent selector. Otherwise, you'd likely have to approach it top-down.

To fix this issue though, it seems like one would need to add a bit more complexity to the query selector parser to handle precedence. I believe this is what @kelko was running into with and and or operations in #22.

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