Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: go escape constant attributes #725

Merged
merged 8 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.