From 835ff6b696cf2711dd29592eaeac10906ae029af Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Mon, 18 Dec 2023 08:46:30 -0300 Subject: [PATCH] HTML: fix bug with unexpected ending in template --- html/lex.go | 22 ++++++++++++---------- html/lex_test.go | 2 ++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/html/lex.go b/html/lex.go index e3cb9bd..c000edc 100644 --- a/html/lex.go +++ b/html/lex.go @@ -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() @@ -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) } } @@ -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 diff --git a/html/lex_test.go b/html/lex_test.go index be19607..2ec9f01 100644 --- a/html/lex_test.go +++ b/html/lex_test.go @@ -191,6 +191,8 @@ func TestTemplates(t *testing.T) { {"", []bool{true}}, {"", []bool{true}}, {"", []bool{true, false, true}}, + {"{{", []bool{true}}, + {"{{'", []bool{true}}, } for _, tt := range tests { t.Run(tt.html, func(t *testing.T) {