diff --git a/packages/reflex-base/src/reflex_base/utils/format.py b/packages/reflex-base/src/reflex_base/utils/format.py index 35a2bc3e30a..b38f2fe0afa 100644 --- a/packages/reflex-base/src/reflex_base/utils/format.py +++ b/packages/reflex-base/src/reflex_base/utils/format.py @@ -95,9 +95,14 @@ def is_wrapped(text: str, open: str, close: str | None = None) -> bool: Whether the text is wrapped. """ close = get_close_char(open, close) - if not (text.startswith(open) and text.endswith(close)): + if len(text) < 2 or not (text.startswith(open) and text.endswith(close)): return False + # Identical delimiters (e.g. quotes or backticks) cannot nest, so the text + # is wrapped only when the delimiter does not reappear inside the content. + if open == close: + return open not in text[1:-1] + depth = 0 for ch in text[:-1]: if ch == open: diff --git a/tests/units/utils/test_format.py b/tests/units/utils/test_format.py index c7ae6397020..55d8b5bd9b0 100644 --- a/tests/units/utils/test_format.py +++ b/tests/units/utils/test_format.py @@ -171,6 +171,11 @@ def test_get_close_char(input: str, output: str): ("{wrap", "{", False), ("{wrap}", "(", False), ("(wrap)", "(", True), + # Identical delimiters (quotes/backticks) that cannot nest. + ("`wrap`", "`", True), + ('"wrap"', '"', True), + ("`a`b`", "`", False), + ("`", "`", False), ], ) def test_is_wrapped(text: str, open: str, expected: bool):