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,styleand 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-*anddata-*on any element, the SVG presentation and geometry attributes, and the few each HTML element needs (altandwidthon<img>,colspanon a cell,valueon<progress>and the like). Any other attribute is refused by name, everyon*handler among them. - URLs:
hrefon<a>and SVG<a>or<use>may only be an#anchor.srcon<img>andhrefon SVG<image>may only be adata:image/URL. - At most 524,288 characters in each of
htmlandcss, 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. Acssholding</styleis 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,@scopeand@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 shapesinset(),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()andselector(). - Text, which may hold strings:
var(),env(),attr()(but not anattr()typed as a URL),counters(),format(),local(),tech(),:lang()andpath(). - Loaders:
url()andsrc(), whose URL must be a quoted#anchorordata:image/URL, andimage(),image-set()andcross-fade(), whose strings must be one too. Inside a loader there may only be those strings, numbers, words such as2x, commas and nested image functions, so novar(),env()orattr().
- Values, which may hold no string: colours (
- A url token, such as
url(data:image/png;base64,…)written without quotes, must be an#anchoror adata: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-familyorquotes. - 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):
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.