Document safe.JSStr function

This commit is contained in:
Joe Mooring 2023-09-28 13:48:50 -07:00 committed by GitHub
parent e77993be08
commit 7551ba28f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 0 deletions

View File

@ -12,6 +12,7 @@ relatedFuncs:
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.CSS INPUT

View File

@ -12,6 +12,7 @@ relatedFuncs:
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.HTML INPUT

View File

@ -12,6 +12,7 @@ relatedFuncs:
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.HTMLAttr INPUT

View File

@ -12,6 +12,7 @@ relatedFuncs:
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.JS INPUT

View File

@ -0,0 +1,59 @@
---
title: safeJSStr
description: Declares the provided string as a known safe JavaScript string.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: safe
relatedFuncs:
- safe.CSS
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.JSStr INPUT
- safeJSStr INPUT
---
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:
```go-html-template
{{ $title := "Lilo & Stitch" }}
<script>
const a = "Title: " + {{ $title }};
</script>
```
Rendered:
```html
<script>
const a = "Title: " + "Lilo \u0026 Stitch";
</script>
```
To avoid escaping by Go's [html/template] package:
```go-html-template
{{ $title := "Lilo & Stitch" }}
<script>
const a = "Title: " + {{ $title | safeJSStr }};
</script>
```
Rendered:
```html
<script>
const a = "Title: " + "Lilo & Stitch";
</script>
```
[html/template]: https://pkg.go.dev/html/template

View File

@ -13,6 +13,7 @@ relatedFuncs:
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
signature:
- safe.URL INPUT