Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-3185][REF-3180]Dont escape backticks in JS string interpolation #3566

Merged
merged 5 commits into from
Jun 26, 2024
Merged
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
29 changes: 25 additions & 4 deletions reflex/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,31 @@ def _escape_js_string(string: str) -> str:
Returns:
The escaped string.
"""
# Escape backticks.
string = string.replace(r"\`", "`")
string = string.replace("`", r"\`")
return string

# TODO: we may need to re-vist this logic after new Var API is implemented.
def escape_outside_segments(segment):
"""Escape backticks in segments outside of `${}`.

Args:
segment: The part of the string to escape.

Returns:
The escaped or unescaped segment.
"""
if segment.startswith("${") and segment.endswith("}"):
# Return the `${}` segment unchanged
return segment
else:
# Escape backticks in the segment
segment = segment.replace(r"\`", "`")
segment = segment.replace("`", r"\`")
return segment

# Split the string into parts, keeping the `${}` segments
parts = re.split(r"(\$\{.*?\})", string)
escaped_parts = [escape_outside_segments(part) for part in parts]
escaped_string = "".join(escaped_parts)
return escaped_string


def _wrap_js_string(string: str) -> str:
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ def test_wrap(text: str, open: str, expected: str, check_first: bool, num: int):
assert format.wrap(text, open, check_first=check_first, num=num) == expected


@pytest.mark.parametrize(
"string,expected_output",
[
("This is a random string", "This is a random string"),
(
"This is a random string with `backticks`",
"This is a random string with \\`backticks\\`",
),
(
"This is a random string with `backticks`",
"This is a random string with \\`backticks\\`",
),
(
"This is a string with ${someValue[`string interpolation`]} unescaped",
"This is a string with ${someValue[`string interpolation`]} unescaped",
),
(
"This is a string with `backticks` and ${someValue[`string interpolation`]} unescaped",
"This is a string with \\`backticks\\` and ${someValue[`string interpolation`]} unescaped",
),
(
"This is a string with `backticks`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
"This is a string with \\`backticks\\`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
),
],
)
def test_escape_js_string(string, expected_output):
assert format._escape_js_string(string) == expected_output


@pytest.mark.parametrize(
"text,indent_level,expected",
[
Expand Down
Loading