written = j + 1
}
}
+ } else if isComment(c.state) && c.delim == delimNone {
+ switch c.state {
+ case stateJSBlockCmt:
+ // http://es5.github.com/#x7.4:
+ // "Comments behave like white space and are
+ // discarded except that, if a MultiLineComment
+ // contains a line terminator character, then
+ // the entire comment is considered to be a
+ // LineTerminator for purposes of parsing by
+ // the syntactic grammar."
+ if bytes.IndexAny(s[written:i1], "\n\r\u2028\u2029") != -1 {
+ b.WriteByte('\n')
+ } else {
+ b.WriteByte(' ')
+ }
+ case stateCSSBlockCmt:
+ b.WriteByte(' ')
+ }
+ written = i1
+ }
+ if c.state != c1.state && isComment(c1.state) && c1.delim == delimNone {
+ // Preserve the portion between written and the comment start.
+ cs := i1 - 2
+ if c1.state == stateHTMLCmt {
+ // "<!--" instead of "/*" or "//"
+ cs -= 2
+ }
+ b.Write(s[written:cs])
+ written = i1
}
c, i = c1, i1
}
if written != 0 && c.state != stateError {
- b.Write(n.Text[written:])
+ if !isComment(c.state) || c.delim != delimNone {
+ b.Write(n.Text[written:])
+ }
e.editTextNode(n, b.Bytes())
}
return c
{
"HTML comment",
"<b>Hello, <!-- name of world -->{{.C}}</b>",
- // TODO: Elide comment.
- "<b>Hello, <!-- name of world --><Cincinatti></b>",
+ "<b>Hello, <Cincinatti></b>",
},
{
"HTML comment not first < in text node.",
"<<!-- -->!--",
- "<<!-- -->!--",
+ "<!--",
},
{
"HTML normalization 1",
{
"HTML normalization 3",
"a<<!-- --><!-- -->b",
- "a<<!-- --><!-- -->b",
+ "a<b",
},
{
"Split HTML comment",
"<b>Hello, <!-- name of {{if .T}}city -->{{.C}}{{else}}world -->{{.W}}{{end}}</b>",
- "<b>Hello, <!-- name of city --><Cincinatti></b>",
+ "<b>Hello, <Cincinatti></b>",
},
{
"JS line comment",
"<script>for (;;) { if (c()) break// foo not a label\n" +
"foo({{.T}});}</script>",
- "<script>for (;;) { if (c()) break// foo not a label\n" +
+ "<script>for (;;) { if (c()) break\n" +
"foo( true );}</script>",
},
{
// Newline separates break from call. If newline
// removed, then break will consume label leaving
// code invalid.
- "<script>for (;;) { if (c()) break/* foo not a label\n" +
- " */foo( true );}</script>",
+ "<script>for (;;) { if (c()) break\n" +
+ "foo( true );}</script>",
},
{
"JS single-line block comment",
// removed, then break will consume label leaving
// code invalid.
"<script>for (;;) {\n" +
- "if (c()) break/* foo a label */foo;" +
+ "if (c()) break foo;" +
"x( true );}</script>",
},
{
"JS block comment flush with mathematical division",
"<script>var a/*b*//c\nd</script>",
- "<script>var a/*b*//c\nd</script>",
+ "<script>var a /c\nd</script>",
},
{
"JS mixed comments",
"<script>var a/*b*///c\nd</script>",
- "<script>var a/*b*///c\nd</script>",
+ "<script>var a \nd</script>",
},
{
"CSS comments",
"<style>p// paragraph\n" +
`{border: 1px/* color */{{"#00f"}}}</style>`,
- "<style>p// paragraph\n" +
- "{border: 1px/* color */#00f}</style>",
+ "<style>p\n" +
+ "{border: 1px #00f}</style>",
},
{
"JS attr block comment",
{
"HTML substitution commented out",
"<p><!-- {{.H}} --></p>",
- "<p><!-- --></p>",
+ "<p></p>",
},
{
"Comment ends flush with start",
"<!--{{.}}--><script>/*{{.}}*///{{.}}\n</script><style>/*{{.}}*///{{.}}\n</style><a onclick='/*{{.}}*///{{.}}' style='/*{{.}}*///{{.}}'>",
- "<!----><script>/**///\n</script><style>/**///\n</style><a onclick='/**///' style='/**///'>",
+ "<script> \n</script><style> \n</style><a onclick='/**///' style='/**///'>",
},
{
"typed HTML in text",