[data-reveal]{opacity:1!important;transform:none!important}
AntelopeJS DMS
DocumentationSketch check

Sketch check

How the mockup compiler and the preview_blocks tool check a sketch's HTML and CSS, and how another process runs the same check through the DMS package's sketch export.

A sketch is static HTML and CSS that the viewer draws in a sandboxed frame. The frame's CSP stops loads, but not a link out, so the check below is what keeps a sketch from navigating or loading outside the frame.

The mockup compiler parses a sketch's html with an HTML parser, as the frame's body and with scripting off, the way the sandboxed frame reads it. It then checks the tree it read against an allowlist:

  • Elements: text and layout HTML (headings, paragraphs, lists, tables, div, span, img, figure, details, button, input, textarea, style and the like), an SVG subset for drawings (svg, g, path, shapes, text, gradients, clipPath, mask, pattern, marker, use, image, a, style), and a MathML subset for formulas. Any other element is refused by name, <script>, <iframe>, <object>, <embed>, <link>, <base>, <meta>, <form>, <template>, <noscript>, <foreignObject>, <set> and <animate> among them.
  • Attributes: id, class, style, title, lang, dir, role, hidden, tabindex, aria-* and data-* on any element, the SVG presentation and geometry attributes, and the few each HTML element needs (alt and width on <img>, colspan on a cell, value on <progress> and the like). Any other attribute is refused by name, every on* handler among them.
  • URLs: href on <a> and SVG <a> or <use> may only be an #anchor. src on <img> and href on SVG <image> may only be a data:image/ URL.
  • At most 524,288 characters in each of html and css, 4,096 tags, 64 attributes on one tag, 8,195 elements built by the parser (it rebuilds open formatting elements such as <b> after a block closes them) and 256 levels of nesting. A css holding </style is refused before it is parsed.

CSS is read from css, from the <style> elements and style attributes of the parsed tree, and from every SVG and MathML attribute, which SVG reads as CSS. A CSS tokenizer reads it as the CSS Syntax spec does, escapes, newlines and comments included. Everything in it must be on these lists:

  • At-rules: @media, @supports, @container, @layer, @keyframes, @font-face, @page, @scope and @starting-style. None of them loads anything by itself.
  • Functions, by name once unescaped and stripped of a vendor prefix such as -webkit-:
    • Values, which may hold no string: colours (rgb(), rgba(), hsl(), hsla(), hwb(), lab(), lch(), oklab(), oklch(), color(), color-mix(), light-dark()); math (calc(), min(), max(), clamp(), round(), mod(), rem(), the trigonometric functions, pow(), sqrt(), hypot(), log(), exp(), abs(), sign()); the linear, radial and conic gradients and their repeating forms; transforms; filter functions; counter(); repeat(), minmax(), fit-content(); cubic-bezier(), steps(), linear(); the shapes inset(), circle(), ellipse(), polygon(), rect(), xywh(); and the selector functions :not(), :is(), :where(), :has(), :nth-child(), :nth-last-child(), :nth-of-type(), :nth-last-of-type(), :dir() and selector().
    • Text, which may hold strings: var(), env(), attr() (but not an attr() typed as a URL), counters(), format(), local(), tech(), :lang() and path().
    • Loaders: url() and src(), whose URL must be a quoted #anchor or data:image/ URL, and image(), image-set() and cross-fade(), whose strings must be one too. Inside a loader there may only be those strings, numbers, words such as 2x, commas and nested image functions, so no var(), env() or attr().
  • A url token, such as url(data:image/png;base64,…) written without quotes, must be an #anchor or a data:image/ URL, read as a browser reads it: url(\22 #a\22 ) is the relative URL "#a".
  • Strings outside any function, such as in content, font-family or quotes.
  • At most 64 nested brackets and functions.

Anything else is refused: an at-rule or a function off these lists (@import, @namespace, @property, @function, a dashed call such as --u(), if(), image() holding a remote URL), a string or url() a browser reads as broken, CSS the tokenizer cannot read, such as a name too long for it, and in css, markup such as </style> that would end the style element it is drawn in.

A sketch that passes is emitted with its html written back from the tree the parser read, without comments, so the frame draws what was checked. The written-back markup can differ from the source byte for byte: quotes are normalized, and an element the parser closed or reopened is written as it read it. Markup the parser would read differently once written back is refused. A refused sketch is emitted with an empty html and no css, and each refusal is an issue.

The MCP preview_blocks tool refuses a sketch by the same check. Another process runs it through @antelopejs/dms/sketch, which needs only zod, parse5 and @csstools/css-tokenizer, loads without the AntelopeJS runtime, and runs on Node ^20.19.0 || >=22.12.0, the floor @antelopejs/dms and @antelopejs/core declare, since the CSS tokenizer is an ES module it loads through require(esm):

ts
import { checkSketch } from "@antelopejs/dms/sketch";

const checked = checkSketch({
  html: '<p class="note">Packed</p>',
  css: ".note{color:var(--sketch-accent)}",
});
if (!checked.ok) {
  throw new Error(checked.refusals.map(({ what }) => what).join("; "));
}
const drawn = checked.sketch;

checkSketch answers the sketch to draw, its html written back from the parsed tree, or one refusal per field and rule. sketchRefusals answers only the refusals, and an empty list for a sketch that may be drawn.