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

Ensure that queries against IDs are always fully case-insensitive #1076

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed how the app title in a `Header` is centred https://github.com/Textualize/textual/issues/1060
- Fixed the swapping of button variants https://github.com/Textualize/textual/issues/1048
- Fixed reserved characters in screenshots https://github.com/Textualize/textual/issues/993
- Fixed queries against IDs not being fully case-insensitive https://github.com/Textualize/textual/issues/1047

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/textual/css/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _check_class(self, node: DOMNode) -> bool:
return True

def _check_id(self, node: DOMNode) -> bool:
if not node.id == self._name_lower:
if (node._cmp_id is None) or (node._cmp_id.lower() != self._name_lower):
return False
if self.pseudo_classes and not node.has_pseudo_class(*self.pseudo_classes):
return False
Expand Down
2 changes: 2 additions & 0 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(
self._classes = set()
self._name = name
self._id = None
self._cmp_id = None
if id is not None:
self.id = id

Expand Down Expand Up @@ -308,6 +309,7 @@ def id(self, new_id: str) -> str:
f"Node 'id' attribute may not be changed once set (current id={self._id!r})"
)
self._id = new_id
self._cmp_id = new_id.lower()
return new_id

@property
Expand Down
4 changes: 3 additions & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class App(Widget):
app._add_child(help_view)

widget1 = Widget(id="widget1")
widget2 = Widget(id="widget2")
widget2 = Widget(id="Widget2") # Note case.
sidebar = Widget(id="sidebar")
sidebar.add_class("float")

Expand Down Expand Up @@ -54,7 +54,9 @@ class App(Widget):
assert list(app.query("#main")) == [main_view]
assert list(app.query("View#main")) == [main_view]
assert list(app.query("#widget1")) == [widget1]
assert list(app.query("#Widget1")) == [widget1] # Note case.
assert list(app.query("#widget2")) == [widget2]
assert list(app.query("#Widget2")) == [widget2] # Note case.

assert list(app.query("Widget.float")) == [sidebar, tooltip, helpbar]
assert list(app.query(Widget).filter(".float")) == [sidebar, tooltip, helpbar]
Expand Down