parser: escape js template literal syntax inside <script> tags (#9738)

pull/9745/head
涂紳騰(Shen-Teng Tu) 2021-04-15 17:39:39 +08:00 committed by GitHub
parent cb69c4c91a
commit d9fdae1de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -214,11 +214,15 @@ mut sb := strings.new_builder($lstartlength)\n
} else { } else {
source.writeln('</div>') source.writeln('</div>')
} }
} else if state == .js {
// replace `$` to `\$` at first to escape JavaScript template literal syntax
source.writeln(line.replace(r'$', r'\$').replace(r'$$', r'@').replace(r'.$',
r'.@').replace(r"'", r"\'"))
} else { } else {
// HTML, may include `@var` // HTML, may include `@var`
// escaped by cgen, unless it's a `vweb.RawHtml` string // escaped by cgen, unless it's a `vweb.RawHtml` string
source.writeln(line.replace('@', '$').replace('$$', '@').replace('.$', '.@').replace("'", source.writeln(line.replace(r'@', r'$').replace(r'$$', r'@').replace(r'.$',
"\\'")) r'.@').replace(r"'", r"\'"))
} }
} }
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)

View File

@ -17,6 +17,9 @@ color: red;
</script> </script>
<script> <script>
document.getElementById("demo").innerHTML = "Hello JavaScript!"; document.getElementById("demo").innerHTML = "Hello JavaScript!";
const classes = `header ${ isLargeScreen() ? '' :
`icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
</script> </script>
</head> </head>

View File

@ -17,6 +17,9 @@ color: red;
</script> </script>
<script> <script>
document.getElementById("demo").innerHTML = "Hello JavaScript!"; document.getElementById("demo").innerHTML = "Hello JavaScript!";
const classes = `header ${ isLargeScreen() ? '' :
`icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
</script> </script>
</head> </head>