diff --git a/CHANGELOG.md b/CHANGELOG.md index 44a95d744f..bc2657ad15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/textual/dom.py b/src/textual/dom.py index 461e9acea7..2664881d91 100644 --- a/src/textual/dom.py +++ b/src/textual/dom.py @@ -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( diff --git a/tests/test_dom.py b/tests/test_dom.py index 6f06fb8091..ea14a25bfe 100644 --- a/tests/test_dom.py +++ b/tests/test_dom.py @@ -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! ", + "&ersand", + "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)