Skip to content

Commit

Permalink
fix: escape constant attribute Go strings (#725)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Kolmos <robertkolmos@gmail.com>
  • Loading branch information
Robert-Kolmos and Robert-Kolmos authored May 6, 2024
1 parent 702c106 commit 89f216e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
3 changes: 2 additions & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,8 @@ 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")
value = strconv.Quote(value)
value = value[1 : len(value)-1]
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s=\"%s\"`, name, value)); err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions generator/test-constant-attribute-escaping/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div>
<!-- valid go escape sequences -->
<input pattern="\a"/>
<input pattern="\b"/>
<input pattern="\f"/>
<input pattern="\n"/>
<input pattern="\r"/>
<input pattern="\t"/>
<input pattern="\v"/>
<input pattern="\\"/>
<input pattern="\777"/>
<input pattern="\xFF"/>
<input pattern="\u00FF"/>
<input pattern="\u00FF\u00FF\u00FF"/>
<!-- invalid go escape sequences -->
<input pattern="\s"/>
</div>
23 changes: 23 additions & 0 deletions generator/test-constant-attribute-escaping/render_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testconstantattributeescaping

import (
_ "embed"
"testing"

"github.com/a-h/templ/generator/htmldiff"
)

//go:embed expected.html
var expected string

func Test(t *testing.T) {
component := BasicTemplate()

diff, err := htmldiff.Diff(component, expected)
if err != nil {
t.Fatal(err)
}
if diff != "" {
t.Error(diff)
}
}
21 changes: 21 additions & 0 deletions generator/test-constant-attribute-escaping/template.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testconstantattributeescaping

templ BasicTemplate() {
<div>
<!-- valid go escape sequences -->
<input pattern="\a"/>
<input pattern="\b"/>
<input pattern="\f"/>
<input pattern="\n"/>
<input pattern="\r"/>
<input pattern="\t"/>
<input pattern="\v"/>
<input pattern="\\"/>
<input pattern="\777"/>
<input pattern="\xFF"/>
<input pattern="\u00FF"/>
<input pattern="\u00FF\u00FF\u00FF"/>
<!-- invalid go escape sequences -->
<input pattern="\s"/>
</div>
}
34 changes: 34 additions & 0 deletions generator/test-constant-attribute-escaping/template_templ.go

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

0 comments on commit 89f216e

Please sign in to comment.