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
- func Attr(tag, jsxName string, v Value) string
- func AttrName(name string) string
- func CSSPropertyName(key string) string
- func ContentValue(tag, jsxName string) bool
- func IsBoolean(name string) bool
- func IsBooleanish(name string) bool
- func IsJavaScriptURL(u string) bool
- func IsPropertyAttr(name string) bool
- func IsRawText(tag string) bool
- func IsReservedProp(name string) bool
- func IsURLAttr(name string) bool
- func IsVoid(tag string) bool
- func RawText(tag, s string) string
- func SanitizeURL(u string) string
- func SelectValueProp(tag, jsxName string) bool
- func SelectValues(v Value) []string
- func Style(entries []StyleEntry) string
- func StyleValue(prop string, v Value) (string, bool)
- func Text(s string) string
- func ValidAttrName(name string) bool
- func ValidTagName(name string) bool
- type Kind
- type StyleEntry
- type Value
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
ContentValue reports whether jsxName on tag is rendered as element content rather than an attribute: `value`/`defaultValue` on <textarea>.
func IsBoolean ¶
IsBoolean reports whether name (an emitted attribute name, or a JSX prop name) is a boolean attribute.
func IsBooleanish ¶
IsBooleanish reports whether name is a booleanish enumerated attribute (contentEditable, draggable, spellCheck, ...).
func IsJavaScriptURL ¶
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 ¶
IsPropertyAttr reports whether a reactive update to name must go through the DOM property (el.value / el.checked / el.selected / el.muted).
func IsReservedProp ¶
IsReservedProp reports props that never become attributes.
func RawText ¶
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 ¶
SanitizeURL returns u, or BlockedURL when u is a javascript: URL.
func SelectValueProp ¶
SelectValueProp reports whether jsxName on tag selects options instead of being emitted: `value`/`defaultValue` on <select>.
func SelectValues ¶
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 ¶
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 ValidAttrName ¶
ValidAttrName reports whether name is a safe attribute name, following React's ATTRIBUTE_NAME_START_CHAR / ATTRIBUTE_NAME_CHAR grammar (XML names).
func ValidTagName ¶
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 ¶
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.