From 1a4ad5565121da662b64693e42ac3bf084257968 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 25 Sep 2024 03:27:54 -0300 Subject: [PATCH] parser: add back the escape for `@` using `@@` in $tmpl() templates, add tests (the regression happened in 3115796 from 2024-09-22) (#22303) --- vlib/v/parser/tmpl.v | 2 +- vlib/v/tests/tmpl/escape.txt | 3 +++ vlib/v/tests/tmpl_escape_test.v | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/tmpl/escape.txt create mode 100644 vlib/v/tests/tmpl_escape_test.v diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index 4fa4ce00a..650e07e5e 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -77,7 +77,7 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri // HTML, may include `@var` // escaped by cgen, unless it's a `vweb.RawHtml` string trailing_bs := tmpl_str_end + 'sb_${fn_name}.write_u8(92)\n' + tmpl_str_start - replace_pairs := ['\\', '\\\\', r"'", "\\'", r'@', r'$', r'$$', r'\@'] + replace_pairs := ['\\', '\\\\', r"'", "\\'", r'@@', r'@', r'@', r'$', r'$$', r'\@'] mut rline := line.replace_each(replace_pairs) comptime_call_str := rline.find_between('\${', '}') if comptime_call_str.contains("\\'") { diff --git a/vlib/v/tests/tmpl/escape.txt b/vlib/v/tests/tmpl/escape.txt new file mode 100644 index 000000000..11fc11bef --- /dev/null +++ b/vlib/v/tests/tmpl/escape.txt @@ -0,0 +1,3 @@ +Hello @@world +Hello @world +Hello @ @world @ \ No newline at end of file diff --git a/vlib/v/tests/tmpl_escape_test.v b/vlib/v/tests/tmpl_escape_test.v new file mode 100644 index 000000000..7856d4220 --- /dev/null +++ b/vlib/v/tests/tmpl_escape_test.v @@ -0,0 +1,9 @@ +fn test_escape() { + world := 'world!' + res := $tmpl('tmpl/escape.txt') + + assert res == 'Hello @world +Hello world! +Hello $ world! $ +' +} -- 2.39.5