-
-
Notifications
You must be signed in to change notification settings - Fork 280
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
Allowing the script tag to be used as a data block (<script type="application/json">…</script>
) would be helpful
#292
Comments
Thanks for raising the issue. Can you expand on this a bit please? What is How can we allow raw javascript to be interpolated in to templates whilst protecting from XSS attacks? |
Thanks for the response.
barData, err := json.Marshal(barValue)
if err != nil { /* … */ }
bar := string(barData) And you may read const barJSON = document.getElementById("foo").textContent;
const bar = JSON.parse(barJSON); As long as the json string is carefully escaped in the Since my knowledge on this is very limited, I will just leave what I found on stack exchange: |
Okay! I think I understand your scenario now. You are looking to place JSON in a script tag. I think a code-only component would work for your use case here, with validation for templ Template() {
@JsonScript("foo", bar)
}
func JsonScript(id, rawJson string) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
_, err := fmt.Fprintf(w, `<script id="%s" type="application/json">%s</script>`,
templ.EscapeString(id),
rawJson)
return err
})
} Although, as you mention, if templ Template() {
@JsonScript("foo", struct{Some string}{"json"})
}
func JsonScript(id string, obj any) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
if _, err := fmt.Fprintf(w, `<script id="%s" type="application/json">`, templ.EscapeString(id)); err != nil {
return err
}
if err := json.NewEncoder(w).Encode(obj); err != nil {
return err
}
if _, err := w.Write([]byte("</script>")); err != nil {
return err
}
return nil
})
} If neither of these solutions fit your needs, then there may be a change needed here to accomodate. |
These solutions definitely fit my need. Thank you. In the future, would it be possible to make it easier to embed data in script tags with types that definitely won't make the content be processed by the browser? (like |
This might be solved now that #285 has been merged. I'd be interested in knowing if that update doesn't fix things 👀 |
I'm not sure it does, here @umajho is talking about a script tag containing JSON rather than a JS script. But it sounds like it's resolved:
|
Ahh, yep! Read this issue a little too fast 😅 |
Closing, as resolved. |
For example:
The “Code-only components” approach mentioned here is very uncomfortable for me to use, since:
id
) by myself, and</script>
) by myself.It would be very helpful if the library itself provide this functionality.
The text was updated successfully, but these errors were encountered: