Rework safe functions

Closes #2252
Improves #2196
This commit is contained in:
Joe Mooring authored and Joe Mooring committed 2024-05-26 11:07:29 -07:00
1 parent e30d179471
commit 6bca7bc77f
7 files changed
+222 -89

No files matched your search

+21 -8
View File
@@ -13,12 +13,27 @@ action:
- functions/safe/URL
returnType: template.JSStr
signatures: [safe.JSStr INPUT]
toc: true
aliases: [/functions/safejsstr]
---
Encapsulates a sequence of characters meant to be embedded between quotes in a JavaScript expression. Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output.
Without declaring a variable to be a safe JavaScript string:
## Introduction
{{% include "functions/_common/go-html-template-package.md" %}}
## Usage
Use the `safe.JSStr` function to encapsulate a sequence of characters meant to be embedded between quotes in a JavaScript expression.
Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output.
See the [Go documentation] for details.
[Go documentation]: https://pkg.go.dev/html/template#JSStr
## Example
Without a safe declaration:
```go-html-template
{{ $title := "Lilo & Stitch" }}
@@ -27,7 +42,7 @@ Without declaring a variable to be a safe JavaScript string:
</script>
```
Rendered:
Hugo renders the above to:
```html
<script>
@@ -35,7 +50,7 @@ Rendered:
</script>
```
To avoid escaping by Go's [html/template] package:
To declare the string as safe:
```go-html-template
{{ $title := "Lilo & Stitch" }}
@@ -44,12 +59,10 @@ To avoid escaping by Go's [html/template] package:
</script>
```
Rendered:
Hugo renders the above to:
```html
<script>
const a = "Title: " + "Lilo & Stitch";
</script>
```
[html/template]: https://pkg.go.dev/html/template