Skip to content

Commit

Permalink
improve lose parsing rules of element attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 22, 2021
1 parent 7dc1886 commit 70aae13
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
9 changes: 7 additions & 2 deletions elementparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ var elementCloseTagParser = parse.All(asElementCloseTag,
)

// Attribute name.
var attributeNameParser = parse.StringUntil(parse.Rune('='))
var attributeNameFirst = "abcdefghijklmnopqrstuvwxyz"
var attributeNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
var attributeNameParser = parse.Then(parse.WithStringConcatCombiner,
parse.RuneIn(elementNameFirst),
parse.Many(parse.WithStringConcatCombiner, 0, 15, parse.RuneIn(elementNameSubsequent)),
)

// Constant attribute.
var attributeConstantValueParser = parse.StringUntil(parse.Rune('"'))
Expand Down Expand Up @@ -270,7 +275,7 @@ func (p elementSelfClosingParser) Parse(pi parse.Input) parse.Result {
parse.Rune('<'),
elementNameParser,
newAttributesParser().Parse,
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser),
optionalWhitespaceParser,
parse.String("/>"),
)(pi)
}
Expand Down
57 changes: 57 additions & 0 deletions templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,63 @@ func TestTemplateParser(t *testing.T) {
},
},
},
{
name: "template: inputs",
input: `{% templ Name(p Parameter) %}
<input type="text" value="a" />
<input type="text" value="b" />
{% endtempl %}`,
expected: Template{
Name: Expression{
Value: "Name",
Range: Range{
From: Position{
Index: 9,
Line: 1,
Col: 9,
},
To: Position{
Index: 12,
Line: 1,
Col: 12,
},
},
},
Parameters: Expression{
Value: "p Parameter",
Range: Range{
From: Position{
Index: 14,
Line: 1,
Col: 14,
},
To: Position{
Index: 25,
Line: 1,
Col: 25,
},
},
},
Children: []Node{
Element{
Name: "input",
Attributes: []Attribute{
ConstantAttribute{Name: "type", Value: "text"},
ConstantAttribute{Name: "value", Value: "a"},
},
},
Whitespace{Value: "\n"},
Element{
Name: "input",
Attributes: []Attribute{
ConstantAttribute{Name: "type", Value: "text"},
ConstantAttribute{Name: "value", Value: "b"},
},
},
Whitespace{Value: "\n"},
},
},
},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit 70aae13

Please sign in to comment.