Skip to content

Commit 31bd75f

Browse files
[REF-3185][REF-3180]Dont escape backticks in JS string interpolation (#3566)
* Dont escape backticks in JS string interpolation * add unit tests * Fix darglint * add a note to re-visit after new Var API is implemented * tests should have a good meaning
1 parent d425ede commit 31bd75f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

reflex/utils/format.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,31 @@ def _escape_js_string(string: str) -> str:
210210
Returns:
211211
The escaped string.
212212
"""
213-
# Escape backticks.
214-
string = string.replace(r"\`", "`")
215-
string = string.replace("`", r"\`")
216-
return string
213+
214+
# TODO: we may need to re-vist this logic after new Var API is implemented.
215+
def escape_outside_segments(segment):
216+
"""Escape backticks in segments outside of `${}`.
217+
218+
Args:
219+
segment: The part of the string to escape.
220+
221+
Returns:
222+
The escaped or unescaped segment.
223+
"""
224+
if segment.startswith("${") and segment.endswith("}"):
225+
# Return the `${}` segment unchanged
226+
return segment
227+
else:
228+
# Escape backticks in the segment
229+
segment = segment.replace(r"\`", "`")
230+
segment = segment.replace("`", r"\`")
231+
return segment
232+
233+
# Split the string into parts, keeping the `${}` segments
234+
parts = re.split(r"(\$\{.*?\})", string)
235+
escaped_parts = [escape_outside_segments(part) for part in parts]
236+
escaped_string = "".join(escaped_parts)
237+
return escaped_string
217238

218239

219240
def _wrap_js_string(string: str) -> str:

tests/utils/test_format.py

+30
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ def test_wrap(text: str, open: str, expected: str, check_first: bool, num: int):
9696
assert format.wrap(text, open, check_first=check_first, num=num) == expected
9797

9898

99+
@pytest.mark.parametrize(
100+
"string,expected_output",
101+
[
102+
("This is a random string", "This is a random string"),
103+
(
104+
"This is a random string with `backticks`",
105+
"This is a random string with \\`backticks\\`",
106+
),
107+
(
108+
"This is a random string with `backticks`",
109+
"This is a random string with \\`backticks\\`",
110+
),
111+
(
112+
"This is a string with ${someValue[`string interpolation`]} unescaped",
113+
"This is a string with ${someValue[`string interpolation`]} unescaped",
114+
),
115+
(
116+
"This is a string with `backticks` and ${someValue[`string interpolation`]} unescaped",
117+
"This is a string with \\`backticks\\` and ${someValue[`string interpolation`]} unescaped",
118+
),
119+
(
120+
"This is a string with `backticks`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
121+
"This is a string with \\`backticks\\`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
122+
),
123+
],
124+
)
125+
def test_escape_js_string(string, expected_output):
126+
assert format._escape_js_string(string) == expected_output
127+
128+
99129
@pytest.mark.parametrize(
100130
"text,indent_level,expected",
101131
[

0 commit comments

Comments
 (0)