The mockup element¶
Realizes A-WEB-2 (P1, K1, K3–K6) and implements R-WEB-002: <mockitdown-mockup>, the custom
element that renders one mockup in a web page, over the TypeScript core (A-WEB-1, see
TypeScript core) and the browser asset loader (see
Web development).
Files¶
File (typescript/) |
What |
|---|---|
src/elements/mockup.ts |
MockupElement, defineElements(), ELEMENT_TAGS, MOCKUP_TAG, MOCKUP_ATTRIBUTES, SCRIPT_TYPE, DIAGNOSTICS_EVENT, scriptCode(text), shadowCss(css), clearImportCache() |
src/mockitdown.ts |
the package entry: re-exports the core, the asset loader and the element module, and calls defineElements() |
pages/mockup.html |
"Mockup element": the demo and test page (markup characters, pack switch, live editor, imports, diagnostics) |
tests/browser/mockup.spec.mjs |
IT-14, browser part (Playwright) |
tests/browser/fixtures/markup-characters.html |
the Python variant's HTML for the TC-1 source |
tests/unit/elements.test.ts |
IT-14, unit part (node:test) |
tsconfig.json now includes the DOM and DOM.Iterable libraries for src/elements/; the core
stays DOM-free by test (A-WEB-1.K1), and src/types/node-shim.d.ts no longer declares URL.
Use¶
<script type="module" src="../dist/mockitdown.js"></script>
<mockitdown-mockup pack="mui" device="fit" import-base="mockups/" asset-base="images/">
<script type="text/mockitdown">
import "components/pill"
VStack { Text("a < b && c > d"); StatusPill(text: "Online") }
</script>
</mockitdown-mockup>
const m = document.querySelector("mockitdown-mockup");
m.setAttribute("pack", "swiftui");
await m.rendered; // after the render that change scheduled
m.code = 'Text("</script> is fine here")';
await m.rendered;
m.diagnostics; // Diagnostic[] of the last render
document.addEventListener("mockitdown-diagnostics", (e) => console.log(e.target, e.detail.diagnostics));
</script cannot appear in the script child (R-WEB-002.C9)
The HTML parser ends the <script type="text/mockitdown"> element at the first </script, even
inside a string of the mockup. Set such code from a script with the code property; the DOM
carries it unchanged.
How a render runs¶
flowchart TD
C["attributeChangedCallback · MutationObserver<br/>(child list, character data, subtree) · code setter"] --> S["#schedule()<br/>generation++, dirty"]
S -->|"one microtask per burst (K6)"| P["#pump(): while dirty"]
P --> A{"assets loaded?"}
A -->|no| L["await loadAssetsFromUrl()"]
A -->|yes| O["attributes → coerceFenceOptions<br/>import-base, asset-base"]
L --> O
O --> R["render(code, {…, import_resolver, asset_base})"]
R -->|"resolver missed URLs"| F["fetch them (cache by URL, K3)"] --> R
R -->|"nothing missing"| H["fence errors box + result.html<br/>adoptedStyleSheets = [host, sheet(packs)] (K4, K5)"]
H --> E["diagnostics → mockitdown-diagnostics event<br/>(bubbles, composed)"]
E --> W["resolve rendered waiters"]
- Code (
scriptCode): the firstscriptchild element whosetypeistext/mockitdown; a whitespace-only first line (after<script>) and last line (before</script>) are dropped, the common indentation is removed character by character and blank lines are emptied, astextwrap.dedent. The result is the body a Markdown fence with the same lines would hand the renderer — no trailing line feed — so the figure's anchor prefix matches the fence's. Other children are ignored and, with no<slot>in the shadow root, never shown. - Attributes: the keys of
FENCE_OPTIONS(the fence's twelve pluswindow) andimport-base,asset-baseare observed. An empty boolean attribute is"true"; thencoerceFenceOptionstypes them, and its problems becomefenceerrors after the render's diagnostics, with their own error box above the rendering — whatmockitdown.fence.formatdoes in Python. Other attributes are not observed. - Bases:
import-base(made a directory URL) andasset-baseresolve againstdocument.baseURI; without them imports resolve against the document URL and images against its directory. - Imports (K3): the core's resolver is synchronous. The element's resolver answers from
importTexts(URL → text, or null for a failed fetch) and records every URL it does not have; the element fetches them and renders again, at most 32 rounds. A path is tried as written, then with.mockitdown(R-WEB-001.C6, C10), so an import costs one round per candidate and nesting level, andimport "components/pill"requestscomponents/pill(404) beforecomponents/pill.mockitdown. Registered components (registerComponents) are found first and never fetched.clearImportCache()forgets fetched files. - Styles (K4, K5): one constructed
CSSStyleSheetper pack set (packs.join(",")), replaced in place when the CSS text changes, plus a host sheet:host{display:block}.shadowCssrewritesbody:not([data-md-color-scheme])to:hostand[data-md-color-scheme="slate"]to:host-context([data-md-color-scheme="slate"]), soappearance="auto"follows the OS scheme in a shadow root (:host-contextworks in Chromium only; elsewhere the Material slate rule is dropped). Without constructed sheets the CSS goes into a<style>in front of the HTML. - Failures (R-WEB-002.C12): an asset load failure or an exception during rendering becomes one
error of code
element(cannot render: …) in an error box. Every element renders in its own async chain, so nothing one element throws reaches another (C7). rendered(C8): pending observer records are taken first; then it resolves at once when the element is connected, rendered and not dirty, else when the pump ends clean. It never rejects. A render overtaken by a newer change (the generation moved while awaiting assets or imports) is discarded without touching the shadow root.
Debugging¶
npm run serve, openhttp://127.0.0.1:8173/pages/mockup.html; in the console,$0.shadowRoot.innerHTML,$0.diagnostics,$0.codeon a selected element.- A mockup that never renders: check that
dist/mockitdown.jsloaded as a module and thatdist/shared/manifest.jsonis reachable (the error box names the URL and status). - An import not found: the network panel shows the URLs tried, relative to
import-base. - Dark tokens with
appearance="auto"not applied: the OS scheme drives it inside the shadow root; Material's palette toggle is followed in Chromium only.
Revision history¶
| Version | Date | Change |
|---|---|---|
| 0.3.7 | 2026-11-25 | Page written: mockitdown-mockup element, demo page, IT-14 6 |