Skip to content

Commit

Permalink
BUG: CSS strings truncated at ":" (#59720)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
invalidarg and mroeschke authored Oct 4, 2024
1 parent 7f54bec commit aea1643
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 12 additions & 12 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ def concatenated_visible_rows(obj):
row_body_headers = [
{
**col,
"display_value": col["display_value"]
if col["is_visible"]
else "",
"display_value": (
col["display_value"] if col["is_visible"] else ""
),
"cellstyle": self.ctx_index[r, c],
}
for c, col in enumerate(row[:index_levels])
Expand Down Expand Up @@ -2069,18 +2069,18 @@ def maybe_convert_css_to_tuples(style: CSSProperties) -> CSSList:
('border','1px solid red')]
"""
if isinstance(style, str):
s = style.split(";")
try:
return [
(x.split(":")[0].strip(), x.split(":")[1].strip())
for x in s
if x.strip() != ""
]
except IndexError as err:
if style and ":" not in style:
raise ValueError(
"Styles supplied as string must follow CSS rule formats, "
f"for example 'attr: val;'. '{style}' was given."
) from err
)
s = style.split(";")
return [
(x.split(":")[0].strip(), ":".join(x.split(":")[1:]).strip())
for x in s
if x.strip() != ""
]

return style


Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,19 @@ def test_maybe_convert_css_to_tuples(self):
expected = []
assert maybe_convert_css_to_tuples("") == expected

# issue #59623
expected = [("a", "b"), ("c", "url('data:123')")]
assert maybe_convert_css_to_tuples("a:b;c: url('data:123');") == expected

# if no value, return attr and empty string
expected = [("a", ""), ("c", "")]
assert maybe_convert_css_to_tuples("a:;c: ") == expected

def test_maybe_convert_css_to_tuples_err(self):
msg = "Styles supplied as string must follow CSS rule formats"
msg = (
"Styles supplied as string must follow CSS rule formats, "
"for example 'attr: val;'. 'err' was given."
)
with pytest.raises(ValueError, match=msg):
maybe_convert_css_to_tuples("err")

Expand Down

0 comments on commit aea1643

Please sign in to comment.