Skip to content

Commit

Permalink
tmpl: fix extra newline added on @if-@else-@EnD block (#22289)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Sep 24, 2024
1 parent e974460 commit d5f0db9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
18 changes: 5 additions & 13 deletions vlib/v/parser/tmpl.v
Original file line number Diff line number Diff line change
Expand Up @@ -327,44 +327,36 @@ 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 {
// by default, just copy 1:1
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 {
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/tmpl/if_cond.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
aaa
@if cond
bbb
@else
zzz
@end
ccc

aaa
@if cond
bbb
@end
ccc
24 changes: 24 additions & 0 deletions vlib/v/tests/tmpl_if_cond_test.v
Original file line number Diff line number Diff line change
@@ -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
'
}
4 changes: 0 additions & 4 deletions vlib/v/tests/tmpl_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}

Expand Down

0 comments on commit d5f0db9

Please sign in to comment.