text

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TextNode

type TextNode struct {
	// contains filtered or unexported fields
}

TextNode represents text content that can be either HTML-escaped (safe) or raw (unescaped). It implements the node.Node interface and is used internally by Text() and RawText() constructor functions to handle different security models.

func RawText

func RawText(str string) *TextNode

RawText creates an unescaped text component for trusted HTML content. This is now marked as dynamic by default to handle variables and dynamic content. Use this ONLY for content you control, such as pre-built HTML strings.

Example:

text.RawText(htmlContent) // Renders unescaped, marked as dynamic

func RawTextf

func RawTextf(format string, a ...any) *TextNode

RawTextf creates a formatted text component without HTML escaping. It should only be used with trusted format strings and arguments.

Example:

text.RawTextf("<a href='%s'>%s</a>", "/home", "Home") // Renders as: <a href='/home'>Home</a>

func Static

func Static(str string) *TextNode

Static creates a text component that is explicitly marked as static content. This should be used for content that never changes, allowing JIT optimisation. Warning: Static content is not html encoded/escaped.

Example:

text.Static("Copyright 2024") // Renders as: Copyright 2024

func Text

func Text(str string) *TextNode

Text creates a safe text component with automatic HTML escaping for security. This is now marked as dynamic by default to handle variables and user content.

Special HTML characters like <, >, &, and quotes are automatically escaped.

Example:

text.Text(userName) // Renders with HTML escaping, marked as dynamic

func Textf

func Textf(format string, a ...any) *TextNode

Textf creates a safe, formatted text component with automatic HTML escaping. It works like fmt.Sprintf but ensures the final string is properly escaped to prevent XSS attacks.

Example:

text.Textf("Hello, %s!", "<world>") // Renders as: Hello, &lt;world&gt;!

func (*TextNode) Dynamic

func (tn *TextNode) Dynamic() bool

Dynamic returns true if this text content is dynamically generated (created with Textf or RawTextf)

func (*TextNode) Nodes

func (tn *TextNode) Nodes() []node.Node

Nodes returns an empty slice as text nodes do not have children.

func (*TextNode) Render

func (tn *TextNode) Render(w ...io.Writer) []byte

Render returns the text content as a byte slice or writes to the provided writer.

func (*TextNode) RenderBuilder

func (tn *TextNode) RenderBuilder(buf *bytes.Buffer)

RenderBuilder writes the text content directly to the provided buffer. This method provides efficient rendering for large node trees.

func (*TextNode) SetAttribute

func (tn *TextNode) SetAttribute(key string, value string)

Base returns nil as RawText nodes do not have attributes. SetAttribute is a no-op for TextNode as it does not have attributes.

func (*TextNode) String

func (tn *TextNode) String() string

String returns the text content as a string. This allows RawText to be used in contexts that require string values.

Jump to

Keyboard shortcuts

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