Skip to content

Commit 54fc390

Browse files
authored
Raise error for whitespace in class shorthand (e.g. ".a b") (#153)
* Minor fixes in streaming docs * Test to assert result with '.a b' * Raise error for whitespace in class shorthand See discussion in #152.
1 parent 66bd301 commit 54fc390

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/htpy/_attributes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def id_class_names_from_css_str(x: t.Any) -> Mapping[str, Attribute]:
5757
ids = [part.removeprefix("#").strip() for part in parts if part.startswith("#")]
5858
classes = [part.strip() for part in parts if not part.startswith("#") if part]
5959

60+
for class_ in classes:
61+
if any(ch.isspace() for ch in class_):
62+
raise ValueError(
63+
"Whitespace is not allowed in class shorthand (use '.a.b' instead of '.a b')."
64+
)
65+
6066
assert len(ids) in (0, 1)
6167

6268
result: dict[str, Attribute] = {}

tests/test_attributes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ def test_id_class_wrong_order() -> None:
181181
div(".myclass#myid")
182182

183183

184+
def test_bad_classes_delimited_with_space() -> None:
185+
with pytest.raises(
186+
ValueError,
187+
match=r"Whitespace is not allowed in class shorthand \(use '.a.b' instead of '.a b'\).",
188+
):
189+
div(".a b")
190+
191+
184192
def test_id_class_bad_format() -> None:
185193
with pytest.raises(ValueError, match="id/class strings must start with # or ."):
186194
div("foo")

0 commit comments

Comments
 (0)