Documentation
¶
Overview ¶
Package htmlsanitize sanitizes user-submitted HTML (comments, forum posts, private messages, profile signatures) before it is stored, stripping dangerous markup (<script>, event handlers, javascript: URLs, <iframe>, etc.) while keeping the common safe formatting tags a rich-text editor produces: bold/italic/underline, links, images, lists, and so on.
Index ¶
- Constants
- func Sanitize(s string) string
- func SanitizeAdmin(s string) string
- func SanitizeAdminRowTemplate(s string) string
- func SanitizeEditor(s string) string
- func SanitizeWithFormat(s string, allowedTags string, allowClass bool) string
- func StripToLines(s string) []string
- func StripToText(s string) string
Constants ¶
const BreakMarker = "<!--break-->"
BreakMarker - the "read more" cut (<!--break-->): the editor's button inserts it and teasers are cut at it, so every HTML format keeps it, though comments in general are stripped.
const RowTemplateURLToken = "{{url}}"
RowTemplateURLToken - the item path token of a Views row template, kept by SanitizeAdminRowTemplate even inside href.
Variables ¶
This section is empty.
Functions ¶
func Sanitize ¶
Sanitize strips unsafe HTML from s, keeping common formatting tags with their URLs validated. Safe to call concurrently.
func SanitizeAdmin ¶
SanitizeAdmin is like Sanitize, but for rich text only admins/moderators can author: it also keeps "class" and a restricted "style" attribute (see adminPolicy). Safe to call concurrently.
func SanitizeAdminRowTemplate ¶
SanitizeAdminRowTemplate sanitizes a Views row template like SanitizeAdmin, keeping the {{url}} token (also as an href value) and the {{field:…}} tokens (plain text to the policy); internal links stay without rel="nofollow". Safe to call concurrently.
func SanitizeEditor ¶
SanitizeEditor sanitizes the HTML of a visual editor that has no text format to decide its tags (a text field's legacy editor widget): SanitizeAdmin's allowlist plus https embeds, the "read more" cut kept. Safe to call concurrently.
func SanitizeWithFormat ¶
SanitizeWithFormat sanitizes s per an entity.TextFormatDef config: allowedTags is a space-separated, case-insensitive list of tag names (blank means plain text, every tag stripped); allowClass permits "class". A fresh policy is built per call, which is fine since formats are applied on save, not on render. a/img/list/table tags use bluemonday's dedicated helpers (URL validation, rel="nofollow", structural attributes) rather than a bare AllowElements. Safe to call concurrently.
func StripToLines ¶
StripToLines is StripToText keeping the paragraphs: each block (a paragraph, a heading, a list item, a line after <br>…) is one line, its white space collapsed; empty ones are dropped. Never use the result as HTML. Safe to call concurrently.
func StripToText ¶
StripToText renders already-sanitized HTML s as plain text: tags removed (block ones - paragraphs, headings, list items, line breaks… - leave a space, so their texts don't run together), entities decoded, so html/template's auto-escaping doesn't double-encode them ("&" shown literally), runs of white space collapsed, the embed tags ([[block:…]], embedtag) left out: what they embed isn't the text's. Never use the result as HTML. Safe to call concurrently.
Types ¶
This section is empty.