Skip to content

Commit

Permalink
feat: templ.JSONScript: support setting type attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jun 22, 2024
1 parent 69bfdb1 commit 94c1028
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.725
0.2.726
31 changes: 28 additions & 3 deletions jsonscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ var _ Component = JSONScriptElement{}
func JSONScript(id string, data any) JSONScriptElement {
return JSONScriptElement{
ID: id,
Type: "application/json",
Data: data,
Nonce: GetNonce,
}
}

// WithType sets the value of the type attribute of the script element.
func (j JSONScriptElement) WithType(t string) JSONScriptElement {
j.Type = t
return j
}

// WithNonceFromString sets the value of the nonce attribute of the script element to the given string.
func (j JSONScriptElement) WithNonceFromString(nonce string) JSONScriptElement {
j.Nonce = func(context.Context) string {
return nonce
}
return j
}

// WithNonceFrom sets the value of the nonce attribute of the script element to the value returned by the given function.
func (j JSONScriptElement) WithNonceFrom(f func(context.Context) string) JSONScriptElement {
j.Nonce = f
return j
Expand All @@ -34,6 +43,8 @@ func (j JSONScriptElement) WithNonceFrom(f func(context.Context) string) JSONScr
type JSONScriptElement struct {
// ID of the element in the DOM.
ID string
// Type of the script element, defaults to "application/json".
Type string
// Data that will be encoded as JSON.
Data any
// Nonce is a function that returns a CSP nonce.
Expand All @@ -43,11 +54,25 @@ type JSONScriptElement struct {
}

func (j JSONScriptElement) Render(ctx context.Context, w io.Writer) (err error) {
var nonceAttr string
if _, err = io.WriteString(w, "<script"); err != nil {
return err
}
if j.ID != "" {
if _, err = fmt.Fprintf(w, " id=\"%s\"", EscapeString(j.ID)); err != nil {
return err
}
}
if j.Type != "" {
if _, err = fmt.Fprintf(w, " type=\"%s\"", EscapeString(j.Type)); err != nil {
return err
}
}
if nonce := j.Nonce(ctx); nonce != "" {
nonceAttr = fmt.Sprintf(" nonce=\"%s\"", EscapeString(nonce))
if _, err = fmt.Fprintf(w, " nonce=\"%s\"", EscapeString(nonce)); err != nil {
return err
}
}
if _, err = fmt.Fprintf(w, "<script id=\"%s\" type=\"application/json\"%s>", EscapeString(j.ID), nonceAttr); err != nil {
if _, err = io.WriteString(w, ">"); err != nil {
return err
}
if err = json.NewEncoder(w).Encode(j.Data); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions jsonscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestJSONScriptElement(t *testing.T) {
e: templ.JSONScript("idf", data).WithNonceFrom(func(context.Context) string { return "nonce-from-function" }),
expected: "<script id=\"idf\" type=\"application/json\" nonce=\"nonce-from-function\">{\"foo\":\"bar\"}\n</script>",
},
{
name: "if a type is provided, it is used",
e: templ.JSONScript("idt", data).WithType("application/ld+json"),
expected: "<script id=\"idt\" type=\"application/ld+json\">{\"foo\":\"bar\"}\n</script>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 94c1028

Please sign in to comment.