From: Mike Samuel Date: Thu, 22 Sep 2011 02:04:41 +0000 (-0700) Subject: exp/template/html: simplify transition functions X-Git-Tag: weekly.2011-09-21~3 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1f577d26d7c7a935c792dacc6cc50826cfd77af0;p=gostls13.git exp/template/html: simplify transition functions This simplifies transition functions to make it easier to reliably elide comments in a later CL. Before: - transition functions are responsible for detecting special end tags. After: - the code to detect special end tags is done in one place. We were relying on end tags being skipped which meant we were not noticing comments inside script/style elements that contain no substitutions. This change means we will notice all such comments where necessary, but stripTags will notice none since it does not need to. This speeds up stripTags. R=nigeltao CC=golang-dev https://golang.org/cl/5074041 --- diff --git a/src/pkg/exp/template/html/context.go b/src/pkg/exp/template/html/context.go index 57d44938ca..de073f134a 100644 --- a/src/pkg/exp/template/html/context.go +++ b/src/pkg/exp/template/html/context.go @@ -175,6 +175,15 @@ func isComment(s state) bool { return false } +// isInTag return whether s occurs solely inside an HTML tag. +func isInTag(s state) bool { + switch s { + case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr: + return true + } + return false +} + // delim is the delimiter that will end the current HTML attribute. type delim uint8 diff --git a/src/pkg/exp/template/html/escape.go b/src/pkg/exp/template/html/escape.go index 050746c1b2..28019f2525 100644 --- a/src/pkg/exp/template/html/escape.go +++ b/src/pkg/exp/template/html/escape.go @@ -583,7 +583,14 @@ func (e *escaper) escapeText(c context, n *parse.TextNode) context { // s, then returns the context after those tokens and the unprocessed suffix. func contextAfterText(c context, s []byte) (context, int) { if c.delim == delimNone { - return transitionFunc[c.state](c, s) + c1, i := tSpecialTagEnd(c, s) + if i == 0 { + // A special end tag (``) has been seen and + // all content preceding it has been consumed. + return c1, 0 + } + // Consider all content up to any end tag. + return transitionFunc[c.state](c, s[:i]) } i := bytes.IndexAny(s, delimEnds[c.delim]) diff --git a/src/pkg/exp/template/html/escape_test.go b/src/pkg/exp/template/html/escape_test.go index cf1c828002..84bf6b7a4a 100644 --- a/src/pkg/exp/template/html/escape_test.go +++ b/src/pkg/exp/template/html/escape_test.go @@ -814,7 +814,7 @@ func TestErrors(t *testing.T) { }, { `