From d6963884405f67dc1d61ac6374b7ec3c5c869ce4 Mon Sep 17 00:00:00 2001 From: njarrar Date: Mon, 16 Nov 2026 15:50:12 +0000 Subject: [PATCH] fix: render style maps in spread attributes, fixes #1523 --- .../docs/03-syntax-and-usage/03-attributes.md | 1 + runtime/attribute.go | 4 ++ runtime/attribute_test.go | 44 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/docs/docs/03-syntax-and-usage/03-attributes.md b/docs/docs/03-syntax-and-usage/03-attributes.md index 657cc34a8..351893345 100644 --- a/docs/docs/03-syntax-and-usage/03-attributes.md +++ b/docs/docs/03-syntax-and-usage/03-attributes.md @@ -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 `
`. * 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')")}`. diff --git a/runtime/attribute.go b/runtime/attribute.go index 6d67f917e..ba13880d4 100644 --- a/runtime/attribute.go +++ b/runtime/attribute.go @@ -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 diff --git a/runtime/attribute_test.go b/runtime/attribute_test.go index e37a2a62c..c0a3c9da3 100644 --- a/runtime/attribute_test.go +++ b/runtime/attribute_test.go @@ -40,6 +40,50 @@ func TestRenderAttributes(t *testing.T) { }, expected: ` style="color: red'\000022>\00003Cscript>;"`, }, + { + 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": "", + }, + }, + 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{