Skip to content

Commit 898fd80

Browse files
authored
Temp solution to fix invalid material icon error rendering (#9113)
## Describe your changes Temp fix for invalid material icon StreamlitAPIException rendering ## GitHub Issue Link (if applicable) ## Testing Plan - Explanation of why no additional tests are needed - Unit Tests (JS and/or Python) - E2E Tests - Any manual testing needed? --- **Contribution License Agreement** By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
1 parent b2c88c6 commit 898fd80

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/streamlit/string_util.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,15 @@ def validate_material_icon(maybe_material_icon: str | None) -> str:
9494

9595
icon_regex = r"^\s*:(.+)\/(.+):\s*$"
9696
icon_match = re.match(icon_regex, maybe_material_icon)
97+
# Since our markdown processing needs to change the `/` to `_` in order to
98+
# correctly render the icon, we need to add a zero-width space before the
99+
# `/` to avoid this transformation here.
100+
invisible_white_space = "\u200b"
97101

98102
if not icon_match:
99103
raise StreamlitAPIException(
100-
f'The value `"{maybe_material_icon}"` is not a valid Material icon. '
101-
f"Please use a Material icon shortcode like **`:material/thumb_up:`**"
104+
f'The value `"{maybe_material_icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon. '
105+
f"Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**"
102106
)
103107

104108
pack_name, icon_name = icon_match.groups()
@@ -109,8 +113,8 @@ def validate_material_icon(maybe_material_icon: str | None) -> str:
109113
or not is_material_icon(icon_name)
110114
):
111115
raise StreamlitAPIException(
112-
f'The value `"{maybe_material_icon}"` is not a valid Material icon.'
113-
f" Please use a Material icon shortcode like **`:material/thumb_up:`**. "
116+
f'The value `"{maybe_material_icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon.'
117+
f" Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**. "
114118
)
115119

116120
return f":{pack_name}/{icon_name}:"

lib/tests/streamlit/elements/layouts_test.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ def test_invalid_emoji_icon(self):
240240
def test_invalid_material_icon(self):
241241
"""Test that it throws an error on invalid material icon"""
242242
icon = ":material/invalid:"
243+
invisible_white_space = "\u200b"
243244
with self.assertRaises(StreamlitAPIException) as e:
244245
st.expander("label", icon=icon)
245246
self.assertEqual(
246247
str(e.exception),
247-
f'The value `"{icon}"` is not a valid Material icon.'
248-
f" Please use a Material icon shortcode like **`:material/thumb_up:`**. ",
248+
f'The value `"{icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon.'
249+
f" Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**. ",
249250
)
250251

251252

0 commit comments

Comments
 (0)