From 311579675629e738d750a7bb8c290f3498fa8315 Mon Sep 17 00:00:00 2001 From: Carlos Esquerdo Bernat Date: Sun, 22 Sep 2024 07:22:24 +0200 Subject: [PATCH] parser: fix `.${var}` used in a template, compiled by `$tmpl()` (fix #22231) (#22270) --- vlib/v/parser/tmpl.v | 5 ++--- vlib/v/tests/tmpl/dot_var.txt | 2 ++ vlib/v/tests/tmpl_dot_var_test.v | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/tmpl/dot_var.txt create mode 100644 vlib/v/tests/tmpl_dot_var_test.v diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index 34e304755..ee55801b5 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -77,9 +77,8 @@ 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 - round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$'] - round2 := [r'$$', r'\@', r'.$', r'.@'] - mut rline := line.replace_each(round1).replace_each(round2) + replace_pairs := ['\\', '\\\\', r"'", "\\'", r'@', r'$', r'$$', r'\@'] + mut rline := line.replace_each(replace_pairs) comptime_call_str := rline.find_between('\${', '}') if comptime_call_str.contains("\\'") { rline = rline.replace(comptime_call_str, comptime_call_str.replace("\\'", r"'")) diff --git a/vlib/v/tests/tmpl/dot_var.txt b/vlib/v/tests/tmpl/dot_var.txt new file mode 100644 index 000000000..bec446c26 --- /dev/null +++ b/vlib/v/tests/tmpl/dot_var.txt @@ -0,0 +1,2 @@ +.@one +.${two} diff --git a/vlib/v/tests/tmpl_dot_var_test.v b/vlib/v/tests/tmpl_dot_var_test.v new file mode 100644 index 000000000..8bc2c675c --- /dev/null +++ b/vlib/v/tests/tmpl_dot_var_test.v @@ -0,0 +1,11 @@ +fn template() string { + one := 1 + two := 2 + return $tmpl('tmpl/dot_var.txt') +} + +fn test_tmpl_with_dot_var() { + assert template() == '.1 +.2 +' +} -- 2.39.5