-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tpl/transform: Add transform.XMLEscape template function
Fixes #3268
- Loading branch information
Showing
6 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: transform.XMLEscape | ||
description: Returns the given string, removing disallowed characters then escaping the result to its XML equivalent. | ||
categories: [] | ||
keywords: [] | ||
action: | ||
aliases: [] | ||
related: [] | ||
returnType: string | ||
signatures: [transform.XMLEscape INPUT] | ||
--- | ||
|
||
The `transform.XMLEscape` function removes [disallowed characters] as defined in the XML specification, then escapes the result by replacing the following characters with [HTML entities]: | ||
|
||
- `"` → `"` | ||
- `'` → `'` | ||
- `&` → `&` | ||
- `<` → `<` | ||
- `>` → `>` | ||
- `\t` → `	` | ||
- `\n` → `
` | ||
- `\r` → `
` | ||
|
||
For example: | ||
|
||
```go-html-template | ||
transform.XMLEscape "<p>abc</p>" → <p>abc</p> | ||
``` | ||
|
||
When using `transform.XMLEscape` in a template rendered by Go's [html/template] package, declare the string to be safe HTML to avoid double escaping. For example, in an RSS template: | ||
|
||
{{< code file="layouts/_default/rss.xml" >}} | ||
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description> | ||
{{< /code >}} | ||
|
||
[disallowed characters]: https://www.w3.org/TR/xml/#charsets | ||
[html entities]: https://developer.mozilla.org/en-us/docs/glossary/entity | ||
[html/template]: https://pkg.go.dev/html/template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters