Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/reflex-base/src/reflex_base/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Escaped delimiters trigger double wrapping

When an already wrapped string contains an escaped instance of its delimiter, such as `a\`b`, this raw substring check treats the escaped backtick as structural and returns false, causing wrap() to add another delimiter pair to the valid expression.


depth = 0
for ch in text[:-1]:
if ch == open:
Expand Down
5 changes: 5 additions & 0 deletions tests/units/utils/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down