Commit 533b3722 authored by Mike Samuel's avatar Mike Samuel

exp/template/html: define isComment helper

Non semantics-changing refactoring in preparation for comment elision.

R=nigeltao
CC=golang-dev
https://golang.org/cl/5071043
parent bf595ba1
...@@ -89,8 +89,8 @@ const ( ...@@ -89,8 +89,8 @@ const (
// stateBeforeValue occurs after the equals sign but before the value. // stateBeforeValue occurs after the equals sign but before the value.
// It occurs between the ^'s in ` name =^ ^value`. // It occurs between the ^'s in ` name =^ ^value`.
stateBeforeValue stateBeforeValue
// stateComment occurs inside an <!-- HTML comment -->. // stateHTMLCmt occurs inside an <!-- HTML comment -->.
stateComment stateHTMLCmt
// stateRCDATA occurs inside an RCDATA element (<textarea> or <title>) // stateRCDATA occurs inside an RCDATA element (<textarea> or <title>)
// as described at http://dev.w3.org/html5/spec/syntax.html#elements-0 // as described at http://dev.w3.org/html5/spec/syntax.html#elements-0
stateRCDATA stateRCDATA
...@@ -137,7 +137,7 @@ var stateNames = [...]string{ ...@@ -137,7 +137,7 @@ var stateNames = [...]string{
stateAttrName: "stateAttrName", stateAttrName: "stateAttrName",
stateAfterName: "stateAfterName", stateAfterName: "stateAfterName",
stateBeforeValue: "stateBeforeValue", stateBeforeValue: "stateBeforeValue",
stateComment: "stateComment", stateHTMLCmt: "stateHTMLCmt",
stateRCDATA: "stateRCDATA", stateRCDATA: "stateRCDATA",
stateAttr: "stateAttr", stateAttr: "stateAttr",
stateURL: "stateURL", stateURL: "stateURL",
...@@ -165,6 +165,16 @@ func (s state) String() string { ...@@ -165,6 +165,16 @@ func (s state) String() string {
return fmt.Sprintf("illegal state %d", int(s)) return fmt.Sprintf("illegal state %d", int(s))
} }
// isComment is true for any state that contains content meant for template
// authors & maintainers, not for end-users or machines.
func isComment(s state) bool {
switch s {
case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateCSSBlockCmt, stateCSSLineCmt:
return true
}
return false
}
// delim is the delimiter that will end the current HTML attribute. // delim is the delimiter that will end the current HTML attribute.
type delim uint8 type delim uint8
......
...@@ -187,11 +187,6 @@ func (e *escaper) escapeAction(c context, n *parse.ActionNode) context { ...@@ -187,11 +187,6 @@ func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
s = append(s, "exp_template_html_jsstrescaper") s = append(s, "exp_template_html_jsstrescaper")
case stateJSRegexp: case stateJSRegexp:
s = append(s, "exp_template_html_jsregexpescaper") s = append(s, "exp_template_html_jsregexpescaper")
case stateComment, stateJSBlockCmt, stateJSLineCmt, stateCSSBlockCmt, stateCSSLineCmt:
return context{
state: stateError,
err: errorf(ErrInsideComment, n.Line, "%s appears inside a comment", n),
}
case stateCSS: case stateCSS:
s = append(s, "exp_template_html_cssvaluefilter") s = append(s, "exp_template_html_cssvaluefilter")
case stateText: case stateText:
...@@ -204,6 +199,12 @@ func (e *escaper) escapeAction(c context, n *parse.ActionNode) context { ...@@ -204,6 +199,12 @@ func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
c.state = stateAttrName c.state = stateAttrName
s = append(s, "exp_template_html_htmlnamefilter") s = append(s, "exp_template_html_htmlnamefilter")
default: default:
if isComment(c.state) {
return context{
state: stateError,
err: errorf(ErrInsideComment, n.Line, "%s appears inside a comment", n),
}
}
panic("unexpected state " + c.state.String()) panic("unexpected state " + c.state.String())
} }
switch c.delim { switch c.delim {
......
...@@ -1123,15 +1123,15 @@ func TestEscapeText(t *testing.T) { ...@@ -1123,15 +1123,15 @@ func TestEscapeText(t *testing.T) {
}, },
{ {
`<!-- foo`, `<!-- foo`,
context{state: stateComment}, context{state: stateHTMLCmt},
}, },
{ {
`<!-->`, `<!-->`,
context{state: stateComment}, context{state: stateHTMLCmt},
}, },
{ {
`<!--->`, `<!--->`,
context{state: stateComment}, context{state: stateHTMLCmt},
}, },
{ {
`<!-- foo -->`, `<!-- foo -->`,
...@@ -1167,7 +1167,7 @@ func TestEscapeText(t *testing.T) { ...@@ -1167,7 +1167,7 @@ func TestEscapeText(t *testing.T) {
}, },
{ {
`<script>foo</script><!--`, `<script>foo</script><!--`,
context{state: stateComment}, context{state: stateHTMLCmt},
}, },
{ {
`<script>document.write("<p>foo</p>");`, `<script>document.write("<p>foo</p>");`,
......
...@@ -21,7 +21,7 @@ var transitionFunc = [...]func(context, []byte) (context, []byte){ ...@@ -21,7 +21,7 @@ var transitionFunc = [...]func(context, []byte) (context, []byte){
stateAttrName: tAttrName, stateAttrName: tAttrName,
stateAfterName: tAfterName, stateAfterName: tAfterName,
stateBeforeValue: tBeforeValue, stateBeforeValue: tBeforeValue,
stateComment: tComment, stateHTMLCmt: tHTMLCmt,
stateRCDATA: tSpecialTagEnd, stateRCDATA: tSpecialTagEnd,
stateAttr: tAttr, stateAttr: tAttr,
stateURL: tURL, stateURL: tURL,
...@@ -52,7 +52,7 @@ func tText(c context, s []byte) (context, []byte) { ...@@ -52,7 +52,7 @@ func tText(c context, s []byte) (context, []byte) {
if i == -1 || i+1 == len(s) { if i == -1 || i+1 == len(s) {
return c, nil return c, nil
} else if i+4 <= len(s) && bytes.Equal(commentStart, s[i:i+4]) { } else if i+4 <= len(s) && bytes.Equal(commentStart, s[i:i+4]) {
return context{state: stateComment}, s[i+4:] return context{state: stateHTMLCmt}, s[i+4:]
} }
i++ i++
if s[i] == '/' { if s[i] == '/' {
...@@ -168,8 +168,8 @@ func tBeforeValue(c context, s []byte) (context, []byte) { ...@@ -168,8 +168,8 @@ func tBeforeValue(c context, s []byte) (context, []byte) {
return c, s[i:] return c, s[i:]
} }
// tComment is the context transition function for stateComment. // tHTMLCmt is the context transition function for stateHTMLCmt.
func tComment(c context, s []byte) (context, []byte) { func tHTMLCmt(c context, s []byte) (context, []byte) {
i := bytes.Index(s, commentEnd) i := bytes.Index(s, commentEnd)
if i != -1 { if i != -1 {
return context{}, s[i+3:] return context{}, s[i+3:]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment