tmpl: fix escaping @ (#11452)

pull/11456/head
Dialga 2021-09-10 05:27:24 +12:00 committed by GitHub
parent ab3adf3346
commit 66ef27a776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View File

@ -224,8 +224,15 @@ mut sb := strings.new_builder($lstartlength)\n
} else {
// HTML, may include `@var`
// escaped by cgen, unless it's a `vweb.RawHtml` string
source.writeln(line.replace_each([r'@', r'$', r'$$', r'@', r'.$', r'.@', r"'", r"\'",
'\\', parser.tmpl_str_end + 'sb.write_b(92)\n' + parser.tmpl_str_start]))
trailing_bs := parser.tmpl_str_end + 'sb.write_b(92)\n' + parser.tmpl_str_start
round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$']
round2 := [r'$$', r'\@', r'.$', r'.@']
rline := line.replace_each(round1).replace_each(round2)
if rline.ends_with('\\') {
source.writeln(rline[0..rline.len - 2] + trailing_bs)
} else {
source.writeln(rline)
}
}
}
source.writeln(parser.tmpl_str_end)

View File

@ -26,11 +26,16 @@ document.getElementById("demo").innerHTML = "Hello JavaScript!";
const classes = `header ${ isLargeScreen() ? '' :
`icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@@5.1.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@@5.1.0/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<header>
<h1>@{title}</h1>
<p>@title</p>
<a href="@website.link">@website.link</a>
<a href="mailto:example@@@website.link">Email</a>
</header>
<pre>
_

View File

@ -26,11 +26,16 @@ document.getElementById("demo").innerHTML = "Hello JavaScript!";
const classes = `header ${ isLargeScreen() ? '' :
`icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<header>
<h1>TEST</h1>
<p>TEST</p>
<a href="example.com">example.com</a>
<a href="mailto:example@example.com">Email</a>
</header>
<pre>
_

View File

@ -1,5 +1,10 @@
struct Website {
link string
}
fn abc() string {
title := 'TEST'
website := Website{'example.com'}
return $tmpl('file.html')
}