From 571706614798c4a9b09685112db511c1b45e1ea2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 7 Jan 2022 19:19:31 +0200 Subject: [PATCH] ci: fix `$tmpl` regression detected by gitly tests (after a73e146) --- vlib/v/parser/tmpl.v | 11 +++---- ...selective_interpolation_in_script_tag.html | 29 ++++++++++++------- .../tmpl_script_tag_interpolation_test.v | 16 +++++++++- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index ce5b1eb775..6d786f86fd 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -96,21 +96,22 @@ fn vweb_tmpl_${fn_name}() string { mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes for i := 0; i < lines.len; i++ { line := lines[i] + trimmed_line := line.trim_space() tline_number++ start_of_line_pos = end_of_line_pos end_of_line_pos += line.len + 1 - $if trace_tmpl ? { - eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, line: $line') - } if is_html_open_tag('style', line) { state = .css - } else if line == '' { + } else if trimmed_line == '' { state = .html } else if is_html_open_tag('script', line) { state = .js - } else if line == '' { + } else if trimmed_line == '' { state = .html } + $if trace_tmpl ? { + eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, state: ${state:10}, line: $line') + } if line.contains('@header') { position := line.index('@header') or { 0 } p.error_with_error(errors.Error{ diff --git a/vlib/v/tests/tmpl/selective_interpolation_in_script_tag.html b/vlib/v/tests/tmpl/selective_interpolation_in_script_tag.html index 1007509355..6d757a8686 100644 --- a/vlib/v/tests/tmpl/selective_interpolation_in_script_tag.html +++ b/vlib/v/tests/tmpl/selective_interpolation_in_script_tag.html @@ -1,11 +1,20 @@ -// V won't parse that - -// V will parse that - +This should be interpolated: +Username: @username + + // V will NOT do interpolation inside + + // V will interpolate the values here: + + +This should be interpolated too, because it is outside the script tag: +Year: @year +myint: @{benchmark_plot_data.myint(1)} +mydate: @{benchmark_plot_data.mydate()} diff --git a/vlib/v/tests/tmpl_script_tag_interpolation_test.v b/vlib/v/tests/tmpl_script_tag_interpolation_test.v index bb57f81e4d..a98f16cb41 100644 --- a/vlib/v/tests/tmpl_script_tag_interpolation_test.v +++ b/vlib/v/tests/tmpl_script_tag_interpolation_test.v @@ -5,12 +5,26 @@ struct PlotData { numerical_result []int } +fn (pd PlotData) mydate() string { + return pd.dates[0] +} + +fn (pd PlotData) myint(idx int) int { + return pd.numerical_result[idx] +} + fn test_template_interpolation_can_be_selectively_turned_on_in_script_tags() { benchmark_plot_data := PlotData{['2012-11-30', '2022-12-29'], [5, 6, 7, 1]} + username := 'abcd' + year := 2022 text := $tmpl('tmpl/selective_interpolation_in_script_tag.html') - // dump(text) + dump(text) + assert text.contains('Username: abcd') assert text.contains('var non_interpolated_labels = @benchmark_plot_data.dates;') assert text.contains('var non_interpolated_values = @benchmark_plot_data.numerical_result;') assert text.contains("var real_labels = ['2012-11-30', '2022-12-29']; //V_TEMPLATE") assert text.contains('var real_values = [5, 6, 7, 1]; //V_TEMPLATE') + assert text.contains('Year: 2022') + assert text.contains('myint: 6') + assert text.contains('mydate: 2012-11-30') }