From 66ef27a77694799e9bf2c31a04cb33c947cc1282 Mon Sep 17 00:00:00 2001 From: Dialga Date: Fri, 10 Sep 2021 05:27:24 +1200 Subject: [PATCH] tmpl: fix escaping @ (#11452) --- vlib/v/parser/tmpl.v | 11 +++++++++-- vlib/v/tests/inout/file.html | 5 +++++ vlib/v/tests/inout/tmpl_parse_html.out | 5 +++++ vlib/v/tests/inout/tmpl_parse_html.vv | 7 ++++++- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index ed373fa9a9..cecaeac3b0 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -224,8 +224,15 @@ mut sb := strings.new_builder($lstartlength)\n } else { // HTML, may include `@var` // escaped by cgen, unless it's a `vweb.RawHtml` string - source.writeln(line.replace_each([r'@', r'$', r'$$', r'@', r'.$', r'.@', r"'", r"\'", - '\\', parser.tmpl_str_end + 'sb.write_b(92)\n' + parser.tmpl_str_start])) + trailing_bs := parser.tmpl_str_end + 'sb.write_b(92)\n' + parser.tmpl_str_start + round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$'] + round2 := [r'$$', r'\@', r'.$', r'.@'] + rline := line.replace_each(round1).replace_each(round2) + if rline.ends_with('\\') { + source.writeln(rline[0..rline.len - 2] + trailing_bs) + } else { + source.writeln(rline) + } } } source.writeln(parser.tmpl_str_end) diff --git a/vlib/v/tests/inout/file.html b/vlib/v/tests/inout/file.html index 91f1dd4e94..811ef89b8b 100644 --- a/vlib/v/tests/inout/file.html +++ b/vlib/v/tests/inout/file.html @@ -26,11 +26,16 @@ document.getElementById("demo").innerHTML = "Hello JavaScript!"; const classes = `header ${ isLargeScreen() ? '' : `icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`; + +

@{title}

+

@title

+@website.link +Email
                                   _
diff --git a/vlib/v/tests/inout/tmpl_parse_html.out b/vlib/v/tests/inout/tmpl_parse_html.out
index f772cc184e..11dd79006d 100644
--- a/vlib/v/tests/inout/tmpl_parse_html.out
+++ b/vlib/v/tests/inout/tmpl_parse_html.out
@@ -26,11 +26,16 @@ document.getElementById("demo").innerHTML = "Hello JavaScript!";
 const classes = `header ${ isLargeScreen() ? '' :
 `icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
 
+
+
 
 
 
 

TEST

+

TEST

+example.com +Email
                                   _
diff --git a/vlib/v/tests/inout/tmpl_parse_html.vv b/vlib/v/tests/inout/tmpl_parse_html.vv
index 7bb09f97f5..3ee0001347 100644
--- a/vlib/v/tests/inout/tmpl_parse_html.vv
+++ b/vlib/v/tests/inout/tmpl_parse_html.vv
@@ -1,8 +1,13 @@
+struct Website {
+	link string
+}
+
 fn abc() string {
 	title := 'TEST'
+	website := Website{'example.com'}
 	return $tmpl('file.html')
 }
 
 fn main() {
 	print(abc())
-}
\ No newline at end of file
+}