Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectors should be case insensitive #1047

Closed
willmcgugan opened this issue Oct 28, 2022 Discussed in #1041 · 2 comments
Closed

Selectors should be case insensitive #1047

willmcgugan opened this issue Oct 28, 2022 Discussed in #1041 · 2 comments

Comments

@willmcgugan
Copy link
Collaborator

It looks like selectors insist on being lower-case, which means that if you use upper case in an ID it won't match. Possibly true of classes as well. Need to investigate.

Discussed in #1041

Originally posted by mindset-team October 28, 2022
There's seems to be some case sensitivity issue with query/query_one. Here's an example app where widget id must be lower-cased in order to return a valid query result (vs. a NoMatches exception). The query selector seems to be case-insensitive.

from textual.app import App
from textual.widgets import Static

class GridLayoutExample(App):
    def compose(self):
        yield Static("some text here..", id="One")

    async def on_key(self, event):
        print(f'{self.query_one("#One")=}')

app = GridLayoutExample()

if __name__ == "__main__":
    app.run()

Above, on_key is simply used to trigger the query event and textual console opened in another terminal to monitor print output.
The code above fails with NoMatches: No nodes match <DOMQuery GridLayoutExample(title='GridLayoutExample') filter='#One'

However, changing id=One to id=one (in compose method yielding the static widget) works, no exception raised and console printout is valid:

[20:58:32] PRINT                                                                                                           latout.py:13
self.query_one("#One")=Static(id='one')

Is this behavior expected? It seems counter-intuitive that a selector would be case insensitive, but id would be coerced to be lower-cased for a query to work. I couldn't find any reference to case-[in]sensitivity in the textualize.io guide, or on github (after a quick review of the textual css parsing code and def query).

Not sure this elevates to a bug (and anyway maintaining all ID's in lower-case would avoid this issue) but since many of the examples have #One, #Two identifiers throughout the docs, the issue seems to merit a discussion here, a clear instruction in the docs, or designating as a bug to fix. Thoughts?

davep added a commit to davep/textual that referenced this issue Oct 31, 2022
Under the hood this now creates and holds a lower-cased version of the ID
and always compares against that. The query tests have also been updated to
throw in some mixed-case IDs to test with.

See Textualize#1041 and Textualize#1047.
@davep
Copy link
Contributor

davep commented Oct 31, 2022

After implementing this, and then having a chat about it in the office, we've decided that we'll follow the Web and do the opposite: case sensitivity will be fully-enforced for IDs (we should also check this for classes, etc, but for the moment this is about IDs).

@github-actions
Copy link

Did we solve your problem?

Glad we could help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants