Skip to content

Commit

Permalink
compiler: fix lexing of {{, {% and {# after an attribute name
Browse files Browse the repository at this point in the history
This change fixes the lexing of a show, statement or comment after an
attribute name in a template.
  • Loading branch information
gazerro committed Sep 30, 2020
1 parent 6ea0de8 commit 8cd1caf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,12 @@ func (l *lexer) scanAttribute(p int) (string, int) {
}
p += size - 1
}
if c == '{' && p+1 < len(l.src) {
switch l.src[p+1] {
case '{', '%', '#':
return "", p
}
}
l.column++
}
if p == s || p == len(l.src) {
Expand Down
9 changes: 9 additions & 0 deletions compiler/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ var typeTests = map[string][]tokenTyp{
"{{ a and b }}": {tokenStartValue, tokenIdentifier, tokenRelaxedAnd, tokenIdentifier, tokenEndValue},
"{{ a or b }}": {tokenStartValue, tokenIdentifier, tokenRelaxedOr, tokenIdentifier, tokenEndValue},
"{{ a or not b }}": {tokenStartValue, tokenIdentifier, tokenRelaxedOr, tokenRelaxedNot, tokenIdentifier, tokenEndValue},

"<a {% if a %}{% end %}>": {tokenText, tokenStartBlock, tokenIf, tokenIdentifier, tokenEndBlock, tokenStartBlock, tokenEnd, tokenEndBlock, tokenText},
"<a {% if a %}b{% end %}>": {tokenText, tokenStartBlock, tokenIf, tokenIdentifier, tokenEndBlock, tokenText, tokenStartBlock, tokenEnd, tokenEndBlock, tokenText},
"<a {% if a %}b=\"\"{% end %}>": {tokenText, tokenStartBlock, tokenIf, tokenIdentifier, tokenEndBlock, tokenText, tokenStartBlock, tokenEnd, tokenEndBlock, tokenText},
"<a {% if a %}b=''{% end %}>": {tokenText, tokenStartBlock, tokenIf, tokenIdentifier, tokenEndBlock, tokenText, tokenStartBlock, tokenEnd, tokenEndBlock, tokenText},
}

var tagWithURLTypes = []tokenTyp{tokenText, tokenStartURL, tokenText, tokenEndURL, tokenText}
Expand Down Expand Up @@ -290,6 +295,10 @@ var contextTests = map[ast.Context]map[string][]ast.Context{
`<a href={{ u }}>`: {ast.ContextText, ast.ContextUnquotedAttribute, ast.ContextUnquotedAttribute, ast.ContextUnquotedAttribute, ast.ContextUnquotedAttribute, ast.ContextUnquotedAttribute, ast.ContextText},
`<a href="a{{ p }}">`: {ast.ContextText, ast.ContextAttribute, ast.ContextText, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextText},
`<a href="{% if a %}b{% end %}">`: {ast.ContextText, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextText, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextText},
`<a {% if a %}{% end %}>`: {ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText},
`<a {% if a %}b{% end %}>`: {ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText},
`<a {% if a %}b=""{% end %}>`: {ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText},
`<a {% if a %}b=''{% end %}>`: {ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText, ast.ContextTag, ast.ContextTag, ast.ContextTag, ast.ContextText},
`<a class="{{ a }}">`: {ast.ContextText, ast.ContextAttribute, ast.ContextAttribute, ast.ContextAttribute, ast.ContextText},
`<a class="c">{{ a }}`: {ast.ContextText, ast.ContextHTML, ast.ContextHTML, ast.ContextHTML},
`<a class='c'>{{ a }}`: {ast.ContextText, ast.ContextHTML, ast.ContextHTML, ast.ContextHTML},
Expand Down

1 comment on commit 8cd1caf

@zapateo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this commit and it seems ok.

Please sign in to comment.