From 7d5c1c2ddb145b84ede39c04ec4d7846437844a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=82=E7=B4=B3=E9=A8=B0=28Shen-Teng=20Tu=29?= Date: Tue, 6 Apr 2021 21:17:40 +0800 Subject: [PATCH] tmpl: inline styles with attributes (#9605) --- vlib/v/parser/tmpl.v | 40 ++++++++++++++++++++-- vlib/v/parser/v_parser_test.v | 46 ++++++++++++++++++++++++++ vlib/v/tests/inout/file.html | 29 ++++++++++++++++ vlib/v/tests/inout/tmpl_parse_html.out | 29 ++++++++++++++++ vlib/v/tests/inout/tmpl_parse_html.vv | 8 +++++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/inout/file.html create mode 100644 vlib/v/tests/inout/tmpl_parse_html.out create mode 100644 vlib/v/tests/inout/tmpl_parse_html.vv diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index 22956cfe93..a0cb5e3ca5 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -19,6 +19,42 @@ enum State { // span // span.{ } +// check HTML open tag `` +fn is_html_open_tag(name string, s string) bool { + mut len := s.len + if len < name.len { + return false + } + mut sub := s[0..1] + if sub != '<' { // not start with '<' + return false + } + sub = s[len - 1..len] + if sub != '>' { // not end with '<' + return false + } + sub = s[len - 2..len - 1] + if sub == '/' { // self-closing + return false + } + sub = s[1..len - 1] + if sub.contains_any('<>') { // ` >` + return false + } + if sub == name { // `` + return true + } else { + len = name.len + if sub.len <= len { // `` or `` + return false + } + if sub[..len + 1] != '$name ' { // not `` + return false + } + return true + } +} + // compile_file compiles the content of a file by the given path as a template pub fn (mut p Parser) compile_template_file(template_file string, fn_name string) string { mut lines := os.read_lines(template_file) or { @@ -50,11 +86,11 @@ mut sb := strings.new_builder($lstartlength)\n eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, line: $oline') } line := oline.trim_space() - if line == '' { state = .html - } else if line == '' { state = .html diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index c3aa19f7f5..1cd1e86527 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -218,3 +218,49 @@ for s in text_expr { println('===================') } */ + +fn test_fn_is_html_open_tag() { + mut s := ' + + + + + + +
+

@{title}

+
+ + + \ No newline at end of file diff --git a/vlib/v/tests/inout/tmpl_parse_html.out b/vlib/v/tests/inout/tmpl_parse_html.out new file mode 100644 index 0000000000..c855f16616 --- /dev/null +++ b/vlib/v/tests/inout/tmpl_parse_html.out @@ -0,0 +1,29 @@ + + + + + + + + + + + +
+

TEST

+
+ + + \ No newline at end of file diff --git a/vlib/v/tests/inout/tmpl_parse_html.vv b/vlib/v/tests/inout/tmpl_parse_html.vv new file mode 100644 index 0000000000..7bb09f97f5 --- /dev/null +++ b/vlib/v/tests/inout/tmpl_parse_html.vv @@ -0,0 +1,8 @@ +fn abc() string { + title := 'TEST' + return $tmpl('file.html') +} + +fn main() { + print(abc()) +} \ No newline at end of file