Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.parser: fix comptime panic for $tmpl("x.html"), with a template file containing % at a line end #21402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions vlib/v/parser/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The next block test the template parser will not comptime panic on lines
ending with different special characters:
<p>
<br>Line ending with percent %
<br>Line ending with at @
<br>Line ending with ampersand &
<br>Line ending with hash #
<br>Line ending with slash /
<br>Line ending with dollar $
<br>Line ending with caret ^
</p>
Last line.
35 changes: 4 additions & 31 deletions vlib/v/parser/tmpl.v
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ fn vweb_tmpl_${fn_name}() string {
i--
continue
}
mut isp := false

if line.contains('%') {
isp = true
}
if line.contains('@if ') {
source.writeln(parser.tmpl_str_end)
pos := line.index('@if') or { continue }
Expand Down Expand Up @@ -261,28 +256,6 @@ fn vweb_tmpl_${fn_name}() string {
source.writeln(tmpl_str_start)
continue
}
// @foo => ${foo}
/*
if line.contains('@') {
mut pos := line.index('@') or { 0 }
if line.len > pos + 1 && line[pos + 1].is_letter() {
if line.contains('img') {
start := pos + 1
for pos < line.len && line[pos] != `)` {
pos++
}
source.write_string('\${')
source.write_string(line[start..pos + 1])
source.write_string('}')
println(source.str())
continue

// println('@@@ ${line}')
// println('${line}')
}
}
}
*/
if state == .simple {
// by default, just copy 1:1
source.writeln(insert_template_code(fn_name, tmpl_str_start, line))
Expand Down Expand Up @@ -357,11 +330,11 @@ fn vweb_tmpl_${fn_name}() string {
}
else {}
}
// %translation_key => ${tr('translation_key')}
if isp {
pos := line.index('%') or { 0 }

if pos := line.index('%') {
// %translation_key => ${tr('translation_key')}
mut line_ := line
if line[pos + 1].is_letter() { //|| line[pos + 1] == '_' {
if pos + 1 < line.len && line[pos + 1].is_letter() { //|| line[pos + 1] == '_' {
mut end := pos + 1
for end < line.len && (line[end].is_letter() || line[end] == `_`) {
end++
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/parser/tmpl_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Add more examples of potentially buggy patterns in vlib/v/parser/templates/index.html
fn test_tmpl_comptime() {
index := $tmpl('templates/index.html').trim_space()
// dump(index)
assert index.contains('<br>Line ending with percent %\n')
assert index.contains('<br>Line ending with at $\n')
assert index.contains('<br>Line ending with ampersand &\n')
assert index.contains('<br>Line ending with hash #\n')
assert index.contains('<br>Line ending with slash /\n')
assert index.contains('<br>Line ending with dollar $\n')
assert index.contains('<br>Line ending with caret ^\n')
}
Loading