Skip to content

Commit aea1643

Browse files
BUG: CSS strings truncated at ":" (#59720)
* second item in tuple is no longer truncated at first colon #59623 * added testcase for maybe_convert_css_to_tuples #59623 * maybe_convert_css_to_tuples() raises on strings without ":" * fixed implicit str concatination * Fixed raise on empty string * Update test_style.py * attr:; -> ("attr","") Same behavior as before patch * add test for "attr:;", ie empty value * str concatenation in the test broke mypy * revert explicit str concat * Invalidarg patch black (#1) * black test_style * Update style_render.py --------- Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
1 parent 7f54bec commit aea1643

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pandas/io/formats/style_render.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,9 @@ def concatenated_visible_rows(obj):
906906
row_body_headers = [
907907
{
908908
**col,
909-
"display_value": col["display_value"]
910-
if col["is_visible"]
911-
else "",
909+
"display_value": (
910+
col["display_value"] if col["is_visible"] else ""
911+
),
912912
"cellstyle": self.ctx_index[r, c],
913913
}
914914
for c, col in enumerate(row[:index_levels])
@@ -2069,18 +2069,18 @@ def maybe_convert_css_to_tuples(style: CSSProperties) -> CSSList:
20692069
('border','1px solid red')]
20702070
"""
20712071
if isinstance(style, str):
2072-
s = style.split(";")
2073-
try:
2074-
return [
2075-
(x.split(":")[0].strip(), x.split(":")[1].strip())
2076-
for x in s
2077-
if x.strip() != ""
2078-
]
2079-
except IndexError as err:
2072+
if style and ":" not in style:
20802073
raise ValueError(
20812074
"Styles supplied as string must follow CSS rule formats, "
20822075
f"for example 'attr: val;'. '{style}' was given."
2083-
) from err
2076+
)
2077+
s = style.split(";")
2078+
return [
2079+
(x.split(":")[0].strip(), ":".join(x.split(":")[1:]).strip())
2080+
for x in s
2081+
if x.strip() != ""
2082+
]
2083+
20842084
return style
20852085

20862086

pandas/tests/io/formats/style/test_style.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,19 @@ def test_maybe_convert_css_to_tuples(self):
886886
expected = []
887887
assert maybe_convert_css_to_tuples("") == expected
888888

889+
# issue #59623
890+
expected = [("a", "b"), ("c", "url('data:123')")]
891+
assert maybe_convert_css_to_tuples("a:b;c: url('data:123');") == expected
892+
893+
# if no value, return attr and empty string
894+
expected = [("a", ""), ("c", "")]
895+
assert maybe_convert_css_to_tuples("a:;c: ") == expected
896+
889897
def test_maybe_convert_css_to_tuples_err(self):
890-
msg = "Styles supplied as string must follow CSS rule formats"
898+
msg = (
899+
"Styles supplied as string must follow CSS rule formats, "
900+
"for example 'attr: val;'. 'err' was given."
901+
)
891902
with pytest.raises(ValueError, match=msg):
892903
maybe_convert_css_to_tuples("err")
893904

0 commit comments

Comments
 (0)