htmlutil

package
v0.85.0-pre.8 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package htmlutil provides safe HTML element construction and rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddNonce

func AddNonce(el *Element, length uint8) (string, error)

AddNonce generates a random nonce and sets it on the element.

func ComputeContentSha256

func ComputeContentSha256(el *Element) (string, error)

ComputeContentSha256 returns the base64-encoded SHA-256 hash of the element's DangerousInnerHTML content.

func RenderElement

func RenderElement(el *Element) (template.HTML, error)

RenderElement renders an element to template.HTML.

func RenderElementToBuilder

func RenderElementToBuilder(el *Element, b *strings.Builder) error

RenderElementToBuilder renders an element into the provided builder.

func RenderModuleScriptToBuilder

func RenderModuleScriptToBuilder(src string, b *strings.Builder) error

RenderModuleScriptToBuilder renders a <script type="module"> tag.

func SetSha256Integrity

func SetSha256Integrity(el *Element, external_hash string) (string, error)

SetSha256Integrity sets the integrity attribute to the provided external hash.

Types

type Element

type Element struct {
	Tag                 string            `json:"tag,omitempty"`
	Attributes          map[string]string `json:"attributes,omitempty"`
	AttributesKnownSafe map[string]string `json:"attributesKnownSafe,omitempty"`
	BooleanAttributes   []string          `json:"booleanAttributes,omitempty"`
	TextContent         string            `json:"textContent,omitempty"`
	DangerousInnerHTML  string            `json:"dangerousInnerHTML,omitempty"`
	SelfClosing         bool              `json:"-"`
}

Element represents a single HTML element with attributes and content.

func EscapeIntoTrusted

func EscapeIntoTrusted(el *Element) Element

EscapeIntoTrusted returns a copy of el with all attributes escaped and merged into AttributesKnownSafe, and TextContent escaped into DangerousInnerHTML. The result is safe for rendering without further escaping.

Jump to

Keyboard shortcuts

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