@ts0/style
v0.1.0-beta.0
Published
Safe inline CSS validation and serialization for untrusted styles.
Readme
@ts0/style
Beta. Until v1 a release can break the previous one, with no migration path.
Validate inline CSS and serialize it to a style attribute. The package has zero runtime dependencies and works in the browser, on the server, and in Edge runtimes.
Install with pnpm add @ts0/[email protected].
Vanilla JavaScript
import { styleAttribute } from "@ts0/style"
/** @param {Pick<HTMLElement, "setAttribute">} element */
function paintPreview(element) {
const style = styleAttribute({ backgroundColor: "tomato", width: 120 })
if (style) {
element.setAttribute("style", style)
}
}
export { paintPreview }React
sanitizeStyle returns a frozen map with canonical CSS property names. Map those names to the React style props your component uses. The same mapping works in a host component registered with @ts0/react.
import { sanitizeStyle, type StyleDeclarations } from "@ts0/style"
function Preview({ authored }: { authored: string | StyleDeclarations }) {
const css = sanitizeStyle(authored)
const background = css?.["background-color"]
return (
<div
style={{
backgroundColor:
typeof background === "string" ? background : undefined,
width: css?.width ?? undefined,
}}
/>
)
}
export { Preview }Policies and limits
Styles are declaration maps with one value per canonical property. Standard
property names normalize to lowercase kebab case. Custom properties keep their
case. CSS strings reject duplicate canonical names, including aliases such as
backgroundColor and background-color. Map aliases resolve in enumeration
order, with the last value kept. Supply a single declaration per property when
you need the map to convey your intent regardless of key order.
The default policy accepts property names from Webref and CSS custom properties. Resource-capable properties and custom properties accept plain values without quotes or functions. All declarations reject resource URLs, escapes, comments, control characters, and unbalanced delimiters. A custom policy receives validated declarations and can keep, rewrite, or omit them. Both the input and the returned declarations must pass the same safety checks.
import { styleAttribute, type StylePolicy } from "@ts0/style"
const colorOnly: StylePolicy = (declarations) => ({
color: declarations.color ?? null,
})
const style = styleAttribute("color: red; width: 10px", {
policy: colorOnly,
maxDeclarations: 8,
maxLength: 256,
})
export { style }sanitizeStyle and defaultStylePolicy return declarations or null. formatStyle serializes declarations with structural validation and adds px to nonzero numbers for dimensional properties. styleAttribute combines sanitization and formatting. Empty styles serialize to undefined. Invalid inputs throw StyleError with a code. The default ceilings are 128 declarations and 16,384 characters. Options can lower either ceiling.
Run pnpm style:sync in this package to regenerate the property lists from its pinned Webref dataset.
See style policy by package for the DOM, React, and UI SDK contracts.
