Skip to content

Commit

Permalink
HTML: fix bug with unexpected ending in template
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Dec 18, 2023
1 parent 686b74d commit 835ff6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 12 additions & 10 deletions html/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (l *Lexer) Next() (TokenType, []byte) {
isEndTag := c == '/' && l.r.Peek(2) != '>' && (l.r.Peek(2) != 0 || l.r.PeekErr(2) == nil)
if !isEndTag && (c < 'a' || 'z' < c) && (c < 'A' || 'Z' < c) && c != '!' && c != '?' {
// not a tag
l.r.Move(1)
} else if 0 < l.r.Pos() {
// return currently buffered texttoken so that we can return tag next iteration
l.text = l.r.Shift()
Expand Down Expand Up @@ -202,8 +203,9 @@ func (l *Lexer) Next() (TokenType, []byte) {
return TextToken, l.text
}
return ErrorToken, nil
} else {
l.r.Move(1)
}
l.r.Move(1)
}
}

Expand Down Expand Up @@ -539,19 +541,19 @@ func (l *Lexer) shiftXML(rawTag Hash) []byte {

func (l *Lexer) moveTemplate() {
for {
if c := l.r.Peek(0); l.at(l.tmplEnd...) || c == 0 && l.r.Err() != nil {
if c != 0 {
l.r.Move(len(l.tmplEnd))
}
break
if c := l.r.Peek(0); c == 0 && l.r.Err() != nil {
return
} else if l.at(l.tmplEnd...) {
l.r.Move(len(l.tmplEnd))
return
} else if c == '"' || c == '\'' {
l.r.Move(1)
escape := false
for {
if c2 := l.r.Peek(0); !escape && c2 == c || c2 == 0 && l.r.Err() != nil {
if c2 != 0 {
l.r.Move(1)
}
if c2 := l.r.Peek(0); c2 == 0 && l.r.Err() != nil {
return
} else if !escape && c2 == c {
l.r.Move(1)
break
} else if c2 == '\\' {
escape = !escape
Expand Down
2 changes: 2 additions & 0 deletions html/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ func TestTemplates(t *testing.T) {
{"<input type={{.}} />", []bool{true}},
{"<input {{if eq .Type 0}}selected{{end}}>", []bool{true}},
{"<input {{if eq .Type 0}} selected {{end}}>", []bool{true, false, true}},
{"{{", []bool{true}},
{"{{'", []bool{true}},
}
for _, tt := range tests {
t.Run(tt.html, func(t *testing.T) {
Expand Down

0 comments on commit 835ff6b

Please sign in to comment.