diff --git a/src/pkg/exp/template/html/escape.go b/src/pkg/exp/template/html/escape.go index 28019f2525..650a6acd28 100644 --- a/src/pkg/exp/template/html/escape.go +++ b/src/pkg/exp/template/html/escape.go @@ -568,12 +568,43 @@ func (e *escaper) escapeText(c context, n *parse.TextNode) context { 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 { + // "{{.C}}", - // TODO: Elide comment. - "Hello, <Cincinatti>", + "Hello, <Cincinatti>", }, { "HTML comment not first < in text node.", "<!--", - "<!--", + "<!--", }, { "HTML normalization 1", @@ -384,18 +383,18 @@ func TestEscape(t *testing.T) { { "HTML normalization 3", "a<b", - "a<b", + "a<b", }, { "Split HTML comment", "Hello, {{.C}}{{else}}world -->{{.W}}{{end}}", - "Hello, <Cincinatti>", + "Hello, <Cincinatti>", }, { "JS line comment", "", - "", }, { @@ -405,8 +404,8 @@ func TestEscape(t *testing.T) { // Newline separates break from call. If newline // removed, then break will consume label leaving // code invalid. - "", + "", }, { "JS single-line block comment", @@ -417,25 +416,25 @@ func TestEscape(t *testing.T) { // removed, then break will consume label leaving // code invalid. "", }, { "JS block comment flush with mathematical division", "", - "", + "", }, { "JS mixed comments", "", - "", + "", }, { "CSS comments", "`, - "", + "", }, { "JS attr block comment", @@ -462,12 +461,12 @@ func TestEscape(t *testing.T) { { "HTML substitution commented out", "

", - "

", + "

", }, { "Comment ends flush with start", "", - "", + "", }, { "typed HTML in text",