From 804d7c6f2f361256db2c98bf3ca73c16d0271f23 Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Sat, 25 Jul 2026 20:40:03 +0530 Subject: [PATCH] fix(format): recognize quote and backtick wrapping in is_wrapped When open == close (the quote and backtick entries in WRAP_MAP) the opening delimiter both opened and closed the depth counter on the same character, so is_wrapped returned False for any such string. wrap(text, open) with the default check_first=True therefore never detected already-wrapped input and double-wrapped it, e.g. wrap('`x`', '`') -> '``x``'. Identical delimiters cannot nest, so treat the text as wrapped when the delimiter does not reappear inside the content. test_is_wrapped only covered distinct delimiters; add the quote/backtick cases. --- packages/reflex-base/src/reflex_base/utils/format.py | 7 ++++++- tests/units/utils/test_format.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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):