htmlspec

package
v0.5.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package htmlspec is the single source of truth for how Krate serializes JSX elements to HTML: void elements, boolean / booleanish / enumerated attributes, the React prop-name to attribute-name mapping (HTML, SVG, xlink and xml namespaces), form-control value handling, style objects, URL sanitization and tag/attribute name validation.

Every HTML emitter (the IR tree builder, the SSR evaluator, the renderer) must use this package instead of keeping its own lists, so the paths cannot drift apart. The semantics follow react-dom/server (React 19).

The package deliberately has no dependency on the AST: callers convert their evaluated values into a Value, which carries the JS type when known.

Index

Constants

View Source
const BlockedURL = "javascript:throw new Error('React has blocked a javascript: URL as a security precaution.')"

BlockedURL replaces a javascript: URL, matching React 19.

Variables

This section is empty.

Functions

func Attr

func Attr(tag, jsxName string, v Value) string

Attr serializes one attribute of element tag. jsxName is the prop name as written in JSX (className, htmlFor, xlinkHref, ...). The result is either "" (the attribute is omitted) or ` name`, or ` name="escaped value"`.

Callers handle `style` objects (Style), textarea/select values (ContentValue / SelectValues) and event handlers before calling Attr.

func AttrName

func AttrName(name string) string

AttrName maps a JSX prop name to the emitted attribute name. Unknown names (including already-correct SVG camelCase names such as viewBox and markerHeight) pass through unchanged.

func CSSPropertyName

func CSSPropertyName(key string) string

CSSPropertyName converts a style-object key to its CSS property name: custom properties (--x) and already-hyphenated names are kept, camelCase is hyphenated, and vendor prefixes become leading-dash forms (WebkitTransition -> -webkit-transition, msTransition -> -ms-transition).

func ContentValue

func ContentValue(tag, jsxName string) bool

ContentValue reports whether jsxName on tag is rendered as element content rather than an attribute: `value`/`defaultValue` on <textarea>.

func IsBoolean

func IsBoolean(name string) bool

IsBoolean reports whether name (an emitted attribute name, or a JSX prop name) is a boolean attribute.

func IsBooleanish

func IsBooleanish(name string) bool

IsBooleanish reports whether name is a booleanish enumerated attribute (contentEditable, draggable, spellCheck, ...).

func IsJavaScriptURL

func IsJavaScriptURL(u string) bool

IsJavaScriptURL reports whether u is a javascript: URL the way a browser parses it: leading C0 controls and spaces are ignored, ASCII tab/CR/LF may appear anywhere inside the scheme, and the scheme is case-insensitive. This mirrors React's isJavaScriptProtocol regular expression.

func IsPropertyAttr

func IsPropertyAttr(name string) bool

IsPropertyAttr reports whether a reactive update to name must go through the DOM property (el.value / el.checked / el.selected / el.muted).

func IsRawText

func IsRawText(tag string) bool

IsRawText reports whether tag's children are raw text (script/style).

func IsReservedProp

func IsReservedProp(name string) bool

IsReservedProp reports props that never become attributes.

func IsURLAttr

func IsURLAttr(name string) bool

IsURLAttr reports whether name (emitted form) is a URL-valued attribute.

func IsVoid

func IsVoid(tag string) bool

IsVoid reports whether tag is an HTML void element.

func RawText

func RawText(tag, s string) string

RawText makes s safe inside a <script> or <style> element by breaking up any `</script` / `</style` (case-insensitive) sequence that would close the element early. In JS and CSS `<\/` is equivalent to `</`.

func SanitizeURL

func SanitizeURL(u string) string

SanitizeURL returns u, or BlockedURL when u is a javascript: URL.

func SelectValueProp

func SelectValueProp(tag, jsxName string) bool

SelectValueProp reports whether jsxName on tag selects options instead of being emitted: `value`/`defaultValue` on <select>.

func SelectValues

func SelectValues(v Value) []string

SelectValues splits an evaluated select value into the option values it selects. Arrays (for <select multiple>) arrive as Items, or as a JSON/JS array literal from untyped callers.

func Style

func Style(entries []StyleEntry) string

Style serializes a style object to a CSS declaration list ("font-size:12px;margin-top:4px"). The result is unescaped; Attr-style callers escape it with the rest of the attribute. It returns "" when no declaration survives, in which case the style attribute is omitted.

func StyleValue

func StyleValue(prop string, v Value) (string, bool)

StyleValue renders one style value for the hyphenated property prop. ok is false when React would skip the declaration (null, undefined, booleans and empty strings).

func Text

func Text(s string) string

Text escapes decoded JSX text for an HTML text position.

func ValidAttrName

func ValidAttrName(name string) bool

ValidAttrName reports whether name is a safe attribute name, following React's ATTRIBUTE_NAME_START_CHAR / ATTRIBUTE_NAME_CHAR grammar (XML names).

func ValidTagName

func ValidTagName(name string) bool

ValidTagName reports whether name is an acceptable element name (React's VALID_TAG_REGEX: a letter followed by letters, digits, ':', '.', '_', '-').

Types

type Kind

type Kind int

Kind is the JS type of an attribute or style value, as far as the caller knows it.

const (
	// Loose is an evaluated value whose JS type is unknown (the evaluator
	// works on strings). "null"/"undefined" are treated as nullish and
	// boolean attributes use JS truthiness of the text; everything else is a
	// string.
	Loose Kind = iota
	// String is a value known to be a JS string (e.g. a string literal).
	String
	// Number is a value known to be a JS number.
	Number
	// Bool is a value known to be a JS boolean; Str is "true" or "false".
	Bool
	// Null is null or undefined.
	Null
	// Bare is an attribute written without a value (`<input disabled>`),
	// which JSX defines as `true`. Non-boolean bare attributes are emitted
	// bare as well so internal marker attributes stay compact.
	Bare
)

type StyleEntry

type StyleEntry struct {
	Key   string
	Value Value
}

StyleEntry is one `key: value` pair of a JSX style object.

type Value

type Value struct {
	Kind Kind
	Str  string
	// Items holds the element texts when the value is a JS array (used by
	// <select multiple> selections).
	Items []string
}

Value is an attribute or style value with its (possibly unknown) JS type.

func BoolVal

func BoolVal(b bool) Value

BoolVal builds a known-boolean Value.

func LooseVal

func LooseVal(s string) Value

LooseVal builds an untyped evaluated Value.

func StrVal

func StrVal(s string) Value

StrVal builds a known-string Value.

func (Value) IsNullish

func (v Value) IsNullish() bool

IsNullish reports whether v renders as nothing (null/undefined).

func (Value) Truthy

func (v Value) Truthy() bool

Truthy reports the JS truthiness of v.

Jump to

Keyboard shortcuts

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