1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tmpl: inline styles with attributes (#9605)

This commit is contained in:
涂紳騰(Shen-Teng Tu) 2021-04-06 21:17:40 +08:00 committed by GitHub
parent a0648a3ec2
commit 7d5c1c2ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 2 deletions

View File

@ -19,6 +19,42 @@ enum State {
// span // span.{
}
// check HTML open tag `<name attr="x" >`
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('<>') { // `<name <bad> >`
return false
}
if sub == name { // `<name>`
return true
} else {
len = name.len
if sub.len <= len { // `<nam>` or `<meme>`
return false
}
if sub[..len + 1] != '$name ' { // not `<name ...>`
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 == '<style>' {
if is_html_open_tag('style', line) {
state = .css
} else if line == '</style>' {
state = .html
} else if line == '<script>' {
} else if is_html_open_tag('script', line) {
state = .js
} else if line == '</script>' {
state = .html

View File

@ -218,3 +218,49 @@ for s in text_expr {
println('===================')
}
*/
fn test_fn_is_html_open_tag() {
mut s := '<style>'
mut b := is_html_open_tag('style', s)
assert b == true
s = '<style media="print" custom-attr >'
b = is_html_open_tag('style', s)
assert b == true
s = '<style/>'
b = is_html_open_tag('style', s)
assert b == false
s = 'styl'
b = is_html_open_tag('style', s)
assert b == false
s = 'style'
b = is_html_open_tag('style', s)
assert b == false
s = '<style'
b = is_html_open_tag('style', s)
assert b == false
s = '<<style>'
b = is_html_open_tag('style', s)
assert b == false
s = '<style>>'
b = is_html_open_tag('style', s)
assert b == false
s = '<stylex>'
b = is_html_open_tag('style', s)
assert b == false
s = '<html>'
b = is_html_open_tag('style', s)
assert b == false
s = '<sript>'
b = is_html_open_tag('style', s)
assert b == false
}

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
}
</style>
<style media="print">
h1 {
color: red;
}
</style>
<script type="application/ld+json">
<!-- JSON-LD -->
</script>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</head>
<body>
<header>
<h1>@{title}</h1>
</header>
</body>
</html>

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
}
</style>
<style media="print">
h1 {
color: red;
}
</style>
<script type="application/ld+json">
<!-- JSON-LD -->
</script>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</head>
<body>
<header>
<h1>TEST</h1>
</header>
</body>
</html>

View File

@ -0,0 +1,8 @@
fn abc() string {
title := 'TEST'
return $tmpl('file.html')
}
fn main() {
print(abc())
}