Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/docs/03-syntax-and-usage/03-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ It's possible to spread any variable of type `templ.Attributes`. `templ.Attribut
* If the value is a `templ.KeyValue[bool, bool]`, the attribute is added if both boolean values are true, as `<div name>`.
* If the value is a `templ.SafeURL`, the attribute is added with the URL value, without URL sanitization.
* If the value is a `templ.ComponentScript`, i.e. the result of `templ.JSFuncCall` or `templ.JSUnsafeFuncCall`, the attribute is added with the JavaScript as the value.
* If the name is `style`, and the value is a `map[string]string`, `map[string]templ.SafeCSSProperty`, or `templ.KeyValue[string, string]`, the attribute is added with the CSS properties as the value, sanitized in the same way as a [style attribute](/syntax-and-usage/css-style-management#style-attribute).

Spread attributes are sanitized in the same way as [attribute key expressions](#attribute-key-expressions), so `templ.Attributes{"href": url}` is sanitized as a URL, and `templ.Attributes{"onclick": "alert(1)"}` is replaced, because the value is a `string`, not a `templ.ComponentScript`. To set an event handler, use `templ.JSFuncCall` to call a JavaScript function with Go data, e.g. `templ.Attributes{"onclick": templ.JSFuncCall("alert", message)}`, or `templ.JSUnsafeFuncCall` for JavaScript from a trusted source, e.g. `templ.Attributes{"onclick": templ.JSUnsafeFuncCall("alert('hello')")}`.

Expand Down
4 changes: 4 additions & 0 deletions runtime/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func RenderAttributes(ctx context.Context, w io.Writer, scriptWriter io.Writer,
if htmlattr.Classify(elementName, name) != htmlattr.ContextSrcdoc {
continue
}
case map[string]string, map[string]templ.SafeCSSProperty, templ.KeyValue[string, string]:
if htmlattr.Classify(elementName, name) != htmlattr.ContextStyle {
continue
}
case templ.KeyValue[string, bool]:
if !v.Value {
continue
Expand Down
44 changes: 44 additions & 0 deletions runtime/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ func TestRenderAttributes(t *testing.T) {
},
expected: ` style="color: red&#39;\000022&gt;\00003Cscript&gt;;"`,
},
{
name: "style maps are rendered as CSS",
attributes: templ.Attributes{
"style": map[string]string{
"color": "red",
"aspect-ratio": "210/297",
},
},
expected: ` style="aspect-ratio:210/297;color:red;"`,
},
{
name: "style map values are sanitized",
attributes: templ.Attributes{
"style": map[string]string{
"color": "</style>",
},
},
expected: ` style="color:zTemplUnsafeCSSPropertyValue;"`,
},
{
name: "style SafeCSSProperty maps are rendered as CSS",
attributes: templ.Attributes{
"style": map[string]templ.SafeCSSProperty{
"color": "red",
},
},
expected: ` style="color:red;"`,
},
{
name: "style KeyValue[string, string] values are rendered as CSS",
attributes: templ.Attributes{
"style": templ.KV("color", "red"),
},
expected: ` style="color:red;"`,
},
{
name: "style maps are ignored on other attributes",
attributes: templ.Attributes{
"data-style": map[string]string{
"color": "red",
},
},
expected: ``,
},
{
name: "integer types are rendered as strings",
attributes: templ.Attributes{
Expand Down