From d5f0db98470c3307f51cd0168da5f1181aaa3fbb Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 24 Sep 2024 17:59:08 -0300 Subject: [PATCH] tmpl: fix extra newline added on @if-@else-@end block (#22289) --- vlib/v/parser/tmpl.v | 18 +++++------------- vlib/v/tests/tmpl/if_cond.txt | 13 +++++++++++++ vlib/v/tests/tmpl_if_cond_test.v | 24 ++++++++++++++++++++++++ vlib/v/tests/tmpl_test.v | 4 ---- 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 vlib/v/tests/tmpl/if_cond.txt create mode 100644 vlib/v/tests/tmpl_if_cond_test.v diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index ee55801b5c9ef7..4fa4ce00ac359b 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -327,37 +327,28 @@ fn vweb_tmpl_${fn_name}() string { source.writeln(tmpl_str_end) pos := line.index('@if') or { continue } source.writeln('if ' + line[pos + 4..] + '{') - source.writeln(tmpl_str_start) + source.write_string(tmpl_str_start) continue } if line.contains('@end') { - // Remove new line byte - source.go_back(1) source.writeln(tmpl_str_end) source.writeln('}') - source.writeln(tmpl_str_start) + source.write_string(tmpl_str_start) continue } if line.contains('@else') { - // Remove new line byte - source.go_back(1) source.writeln(tmpl_str_end) pos := line.index('@else') or { continue } source.writeln('}' + line[pos + 1..] + '{') // source.writeln(' } else { ') - source.writeln(tmpl_str_start) + source.write_string(tmpl_str_start) continue } if line.contains('@for') { - // Remove an extra unnecessary newline added before in state == .simple - // Can break stuff like Markdown - if source.len > 1 { - source.go_back(1) - } source.writeln(tmpl_str_end) pos := line.index('@for') or { continue } source.writeln('for ' + line[pos + 4..] + '{') - source.writeln(tmpl_str_start) + source.write_string(tmpl_str_start) continue } if state == .simple { @@ -365,6 +356,7 @@ fn vweb_tmpl_${fn_name}() string { source.writeln(insert_template_code(fn_name, tmpl_str_start, line)) continue } + // in_write = false // The .simple mode ends here. The rest handles .html/.css/.js state transitions. if state != .simple { diff --git a/vlib/v/tests/tmpl/if_cond.txt b/vlib/v/tests/tmpl/if_cond.txt new file mode 100644 index 00000000000000..9aa1ef666fc666 --- /dev/null +++ b/vlib/v/tests/tmpl/if_cond.txt @@ -0,0 +1,13 @@ +aaa +@if cond +bbb +@else +zzz +@end +ccc + +aaa +@if cond +bbb +@end +ccc \ No newline at end of file diff --git a/vlib/v/tests/tmpl_if_cond_test.v b/vlib/v/tests/tmpl_if_cond_test.v new file mode 100644 index 00000000000000..ea92e6ea3ca8fa --- /dev/null +++ b/vlib/v/tests/tmpl_if_cond_test.v @@ -0,0 +1,24 @@ +fn test_tmpl_if_cond() { + cond := true + processed := $tmpl('tmpl/if_cond.txt') + assert processed == 'aaa +bbb +ccc + +aaa +bbb +ccc +' +} + +fn test_tmpl_if_else_cond() { + cond := false + processed := $tmpl('tmpl/if_cond.txt') + assert processed == 'aaa +zzz +ccc + +aaa +ccc +' +} diff --git a/vlib/v/tests/tmpl_test.v b/vlib/v/tests/tmpl_test.v index a80f6cdecd4b9b..09c6f204abb64b 100644 --- a/vlib/v/tests/tmpl_test.v +++ b/vlib/v/tests/tmpl_test.v @@ -46,10 +46,8 @@ numbers: [1, 2, 3] vlang/ui, downloaded 3201 times. vlang/vtl, downloaded 123 times. - this is not ignored - so, it's basically true" assert one().trim_space() == expected @@ -86,10 +84,8 @@ numbers: [1, 2, 3] vlang/ui, downloaded 3201 times. vlang/vtl, downloaded 123 times. - this is not ignored - so, it's basically true" }