From 5b810fab9e3d9d29980a3bb46168eaa28dec4f8b Mon Sep 17 00:00:00 2001 From: Joe Davidson Date: Wed, 16 Aug 2023 10:30:14 +0100 Subject: [PATCH] fix: replace new lines in constant attributes with escaped new lines (#124) --- generator/generator.go | 1 + generator/test-element-attributes/expected.html | 4 ++++ generator/test-element-attributes/template.templ | 3 +++ .../test-element-attributes/template_templ.go | 2 +- parser/v2/elementparser_test.go | 16 ++++++++++++++-- parser/v2/types.go | 7 ++++++- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index bcf532ef1..642ba22d2 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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 } diff --git a/generator/test-element-attributes/expected.html b/generator/test-element-attributes/expected.html index 2f203facb..3520103a6 100644 --- a/generator/test-element-attributes/expected.html +++ b/generator/test-element-attributes/expected.html @@ -13,4 +13,8 @@
Else
+
+
diff --git a/generator/test-element-attributes/template.templ b/generator/test-element-attributes/template.templ index 75ba58040..166cd7626 100644 --- a/generator/test-element-attributes/template.templ +++ b/generator/test-element-attributes/template.templ @@ -26,5 +26,8 @@ templ render(p person) { class={ unimportant } } >Else +
} diff --git a/generator/test-element-attributes/template_templ.go b/generator/test-element-attributes/template_templ.go index bb49a0cf0..46f802400 100644 --- a/generator/test-element-attributes/template_templ.go +++ b/generator/test-element-attributes/template_templ.go @@ -165,7 +165,7 @@ func render(p person) templ.Component { if err != nil { return err } - _, err = templBuffer.WriteString("") + _, err = templBuffer.WriteString("
") if err != nil { return err } diff --git a/parser/v2/elementparser_test.go b/parser/v2/elementparser_test.go index b83140a77..473b18a76 100644 --- a/parser/v2/elementparser_test.go +++ b/parser/v2/elementparser_test.go @@ -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: `
`, parser: StripType(elementOpenTagParser), expected: elementOpenTag{ - Name: "div", + Name: "div", Attributes: []Attribute{ BoolConstantAttribute{ Name: "data", - }, + }, }, }, }, diff --git a/parser/v2/types.go b/parser/v2/types.go index 618800034..2a91268bb 100644 --- a/parser/v2/types.go +++ b/parser/v2/types.go @@ -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 { @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) }