htmlsanitize

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
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.

View Source
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

func Sanitize(s string) string

Sanitize strips unsafe HTML from s, keeping common formatting tags with their URLs validated. Safe to call concurrently.

func SanitizeAdmin

func SanitizeAdmin(s string) string

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

func SanitizeAdminRowTemplate(s string) string

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

func SanitizeEditor(s string) string

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

func SanitizeWithFormat(s string, allowedTags string, allowClass bool) string

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

func StripToLines(s string) []string

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

func StripToText(s string) string

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 ("&amp;" 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL