From 3a6b0b55524944328efe284f53f5915747455b05 Mon Sep 17 00:00:00 2001 From: NSPC911 <87571998+NSPC911@users.noreply.github.com> Date: Sun, 16 Nov 2025 15:31:31 +0800 Subject: [PATCH] feat(geometry): add support for css three value format --- src/textual/geometry.py | 11 ++++++++--- tests/test_geometry.py | 4 +--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/textual/geometry.py b/src/textual/geometry.py index e86c73aeaa..b60b68f05e 100644 --- a/src/textual/geometry.py +++ b/src/textual/geometry.py @@ -1202,13 +1202,15 @@ def totals(self) -> tuple[int, int]: def css(self) -> str: """A string containing the spacing in CSS format. - For example: "1" or "2 4" or "4 2 8 2". + For example: "1" or "2 4" or "3 5 2" or "4 2 8 2". """ top, right, bottom, left = self if top == right == bottom == left: return f"{top}" if (top, right) == (bottom, left): return f"{top} {right}" + if right == left: + return f"{top} {right} {bottom}" else: return f"{top} {right} {bottom} {left}" @@ -1217,7 +1219,7 @@ def unpack(cls, pad: SpacingDimensions) -> Spacing: """Unpack padding specified in CSS style. Args: - pad: An integer, or tuple of 1, 2, or 4 integers. + pad: An integer, or tuple of 1, 2, 3 or 4 integers. Raises: ValueError: If `pad` is an invalid value. @@ -1234,11 +1236,14 @@ def unpack(cls, pad: SpacingDimensions) -> Spacing: if pad_len == 2: pad_top, pad_right = cast(Tuple[int, int], pad) return cls(pad_top, pad_right, pad_top, pad_right) + if pad_len == 3: + pad_top, pad_side, pad_bottom = cast(Tuple[int, int, int], pad) + return cls(pad_top, pad_side, pad_bottom, pad_side) if pad_len == 4: top, right, bottom, left = cast(Tuple[int, int, int, int], pad) return cls(top, right, bottom, left) raise ValueError( - f"1, 2 or 4 integers required for spacing properties; {pad_len} given" + f"1, 2, 3 or 4 integers required for spacing properties; {pad_len} given" ) @classmethod diff --git a/tests/test_geometry.py b/tests/test_geometry.py index f290429815..65d62f22fd 100644 --- a/tests/test_geometry.py +++ b/tests/test_geometry.py @@ -383,14 +383,12 @@ def test_spacing_unpack(): assert Spacing.unpack(1) == Spacing(1, 1, 1, 1) assert Spacing.unpack((1,)) == Spacing(1, 1, 1, 1) assert Spacing.unpack((1, 2)) == Spacing(1, 2, 1, 2) + assert Spacing.unpack((1, 2, 3)) == Spacing(1, 2, 3, 2) assert Spacing.unpack((1, 2, 3, 4)) == Spacing(1, 2, 3, 4) with pytest.raises(ValueError): assert Spacing.unpack(()) == Spacing(1, 2, 1, 2) - with pytest.raises(ValueError): - assert Spacing.unpack((1, 2, 3)) == Spacing(1, 2, 1, 2) - with pytest.raises(ValueError): assert Spacing.unpack((1, 2, 3, 4, 5)) == Spacing(1, 2, 1, 2)