From 26e45c1f1871a59ab13e8da7e3623a70f7c4e6fc Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 17 Aug 2023 09:45:25 -0700 Subject: [PATCH] format_dict: only unescape quotes when removing outer quotes (#1609) --- reflex/utils/format.py | 18 +++++++++++++++--- tests/components/test_tag.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/reflex/utils/format.py b/reflex/utils/format.py index 0c92dc738..3eb6ae8d3 100644 --- a/reflex/utils/format.py +++ b/reflex/utils/format.py @@ -473,10 +473,22 @@ def format_dict(prop: ComponentStyle) -> str: # Dump the dict to a string. fprop = json_dumps(prop) + def unescape_double_quotes_in_var(m: re.Match) -> str: + # Since the outer quotes are removed, the inner escaped quotes must be unescaped. + return re.sub('\\\\"', '"', m.group(1)) + # This substitution is necessary to unwrap var values. - fprop = re.sub('"{', "", fprop) - fprop = re.sub('}"', "", fprop) - fprop = re.sub('\\\\"', '"', fprop) + fprop = re.sub( + pattern=r""" + (? Event([E("mock_event", {})], _e)}', @@ -57,6 +66,13 @@ def mock_event(arg): {"a": BaseVar(name='state.colors["val"]', type_="str")}, '{{"a": state.colors["val"]}}', ), + # tricky real-world case from markdown component + ( + { + "h1": f"{{({{node, ...props}}) => }}" + }, + '{{"h1": ({node, ...props}) => }}', + ), ], ) def test_format_prop(prop: Var, formatted: str):