Skip to content

Commit

Permalink
fix: replace new lines in constant attributes with escaped new lines (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joerdav committed Aug 16, 2023
1 parent df253a6 commit 5b810fa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ func (g *generator) writeBoolConstantAttribute(indentLevel int, attr parser.Bool
func (g *generator) writeConstantAttribute(indentLevel int, attr parser.ConstantAttribute) (err error) {
name := html.EscapeString(attr.Name)
value := html.EscapeString(attr.Value)
value = strings.ReplaceAll(value, "\n", "\\n")
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s=\"%s\"`, name, value)); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions generator/test-element-attributes/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<div style="width: 100;" class="unimportant_900a">
Else
</div>
<div data-script="on click
do something
end">
</div>

3 changes: 3 additions & 0 deletions generator/test-element-attributes/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ templ render(p person) {
class={ unimportant }
}
>Else</div>
<div data-script="on click
do something
end"></div>
}

2 changes: 1 addition & 1 deletion generator/test-element-attributes/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions parser/v2/elementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,28 @@ if test {
Value: "",
},
},
{
name: "multiline attribute",
input: ` data-script="on click
do something
end"
`,
parser: StripType(constantAttributeParser),
expected: ConstantAttribute{
Name: "data-script",
Value: "on click\n do something\n end",
},
},
{
name: "bool constant attribute",
input: `<div data>`,
parser: StripType(elementOpenTagParser),
expected: elementOpenTag{
Name: "div",
Name: "div",
Attributes: []Attribute{
BoolConstantAttribute{
Name: "data",
},
},
},
},
},
Expand Down
7 changes: 6 additions & 1 deletion parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ type Element struct {
}

var voidElements = map[string]struct{}{
"area": {}, "base": {}, "br": {}, "col": {}, "command": {}, "embed": {}, "hr": {}, "img": {}, "input": {}, "keygen": {}, "link": {}, "meta": {}, "param": {}, "source": {}, "track": {}, "wbr": {}}
"area": {}, "base": {}, "br": {}, "col": {}, "command": {}, "embed": {}, "hr": {}, "img": {}, "input": {}, "keygen": {}, "link": {}, "meta": {}, "param": {}, "source": {}, "track": {}, "wbr": {},
}

// https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html#void-element
func (e Element) IsVoidElement() bool {
Expand Down Expand Up @@ -538,6 +539,7 @@ func (bca BoolConstantAttribute) IsMultilineAttr() bool { return false }
func (bca BoolConstantAttribute) String() string {
return bca.Name
}

func (bca BoolConstantAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, bca.String())
}
Expand All @@ -552,6 +554,7 @@ func (ca ConstantAttribute) IsMultilineAttr() bool { return false }
func (ca ConstantAttribute) String() string {
return ca.Name + `="` + html.EscapeString(ca.Value) + `"`
}

func (ca ConstantAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, ca.String())
}
Expand All @@ -566,6 +569,7 @@ func (ea BoolExpressionAttribute) IsMultilineAttr() bool { return false }
func (ea BoolExpressionAttribute) String() string {
return ea.Name + `?={ ` + ea.Expression.Value + ` }`
}

func (ea BoolExpressionAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, ea.String())
}
Expand All @@ -580,6 +584,7 @@ func (ea ExpressionAttribute) IsMultilineAttr() bool { return false }
func (ea ExpressionAttribute) String() string {
return ea.Name + `={ ` + ea.Expression.Value + ` }`
}

func (ea ExpressionAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, ea.String())
}
Expand Down

0 comments on commit 5b810fa

Please sign in to comment.