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

Validate ids #4032

Merged
merged 7 commits into from
Jan 23, 2024
Merged
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 @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `SelectionList` option IDs are usable as soon as the widget is instantiated https://github.com/Textualize/textual/issues/3903
- Fix issue with `Strip.crop` when crop window start aligned with strip end https://github.com/Textualize/textual/pull/3998
- Fixed Strip.crop_extend https://github.com/Textualize/textual/pull/4011
- ID and class validation was too lenient https://github.com/Textualize/textual/issues/3954
- Fixed a crash if the `TextArea` language was set but tree-sitter lanuage binaries were not installed https://github.com/Textualize/textual/issues/4045


Expand Down
2 changes: 1 addition & 1 deletion src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def check_identifiers(description: str, *names: str) -> None:
description: Description of where identifier is used for error message.
*names: Identifiers to check.
"""
match = _re_identifier.match
match = _re_identifier.fullmatch
for name in names:
if match(name) is None:
raise BadIdentifier(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,24 @@ def test_walk_children_with_self_breadth(search):
]

assert children == ["f", "e", "d", "c", "b", "a"]


@pytest.mark.parametrize(
"identifier",
[
" bad",
" terrible ",
"worse! ",
"&ampersand",
"amper&sand",
"ampersand&",
"2_leading_digits",
"água", # water
"cão", # dog
"@'/.23",
],
)
def test_id_validation(identifier: str):
"""Regression tests for https://github.com/Textualize/textual/issues/3954."""
with pytest.raises(BadIdentifier):
DOMNode(id=identifier)
Loading