Sign in

    crater-wasm

    Wasm bindings and WIT-facing facade for crater

    Download zip
    Author
    Version
    0.17.1
    License
    Apache-2.0
    Last updated
    5 months ago
    Downloads
    56

    #Crater WASM Component

    This directory contains the WASM component build of Crater for distribution via wa.dev.

    #Installation via wa.dev

    #For MoonBit projects

    Add to your moon.mod.json:

    { "deps": { "mizchi/crater-wasm": "*" } }

    Then run:

    moon update

    #For JavaScript/TypeScript projects

    Install the component and jco:

    npm install @aspect-build/jco

    Download and transpile:

    # Download from wa.dev curl -O https://wa.dev/mizchi/crater/crater.wasm # Transpile to JavaScript npx jco transpile crater.wasm -o ./crater --name crater

    #Usage

    import { core } from './crater/crater.js'; // Compute layout from HTML - returns flat node array const html = '<div style="width: 100px; height: 50px"><span>Hello</span></div>'; const result = JSON.parse(core.computeLayout(html, 800, 600)); console.log(result.rootId); // "body" console.log(result.nodes); // [ // { id: "body", parentId: "", index: 0, x: 0, y: 0, width: 800, height: 50, ... }, // { id: "div", parentId: "body", index: 0, x: 0, y: 0, width: 100, height: 50, ... }, // { id: "span", parentId: "div", index: 0, x: 0, y: 0, width: 100, height: 50, text: "Hello", ... } // ] // Build tree from flat array if needed function buildTree(nodes, rootId) { const nodeMap = new Map(nodes.map(n => [n.id, { ...n, children: [] }])); for (const node of nodes) { if (node.parentId) { nodeMap.get(node.parentId)?.children.push(nodeMap.get(node.id)); } } return nodeMap.get(rootId); }

    #Renderer API (Legacy hierarchical output)

    import { renderer } from './crater/crater.js'; // Render HTML to layout JSON (hierarchical tree) const html = '<div style="width: 100px; display: flex;"><div style="flex: 1;"></div></div>'; const layoutJson = renderer.renderHtmlToJson(html, 800, 600); const layout = JSON.parse(layoutJson); console.log(layout); // { id: 'body', x: 0, y: 0, width: 800, height: 0, children: [...] } // Render to paint tree with colors const paintTree = renderer.renderHtmlToPaintTree(html, 800, 600);

    #Incremental API (For apps with dynamic updates)

    import { incremental } from './crater/crater.js'; // Create layout tree incremental.createTree(html, 800, 600); // Initial layout const result = incremental.computeIncremental(); console.log(JSON.parse(result)); // Update style and recompute (only affected nodes) incremental.updateStyle('div#root', 'width: 200px;'); incremental.markDirty('div#root'); const updated = incremental.computeIncremental(); // Check cache statistics const stats = JSON.parse(incremental.getCacheStats()); console.log(`Cache hit rate: ${stats.hitRate * 100}%`); // Resize viewport incremental.resizeViewport(1024, 768); const resized = incremental.computeIncremental(); // Cleanup incremental.destroyTree();

    #Yoga-compatible API

    import { yoga, incremental } from './crater/crater.js'; // Create layout tree first incremental.createTree('<div id="root"></div>', 800, 600); // Programmatic layout using Yoga-style API yoga.setWidth('div#root', 400); yoga.setHeight('div#root', 300); yoga.setFlexDirection('div#root', 'column'); yoga.setJustifyContent('div#root', 'center'); yoga.setAlignItems('div#root', 'center'); // Calculate and get layout const layoutJson = yoga.calculateLayout(800, 600); const layout = JSON.parse(layoutJson); // Get computed values const width = yoga.getComputedWidth('div#root'); const height = yoga.getComputedHeight('div#root'); console.log(`Computed size: ${width}x${height}`); incremental.destroyTree();

    #Accessibility API

    import { accessibility } from './crater/crater.js'; const html = '<button>Click me</button><input type="text" placeholder="Name">'; // Get ARIA snapshot (Playwright-compatible YAML format) console.log(accessibility.getAriaSnapshot(html)); // - document: // - generic: // - button "Click me": Click me // - textbox // Get ARIA snapshot as JSON const ariaJson = JSON.parse(accessibility.getAriaSnapshotJson(html)); console.log(ariaJson); // Get full accessibility tree const tree = JSON.parse(accessibility.getAccessibilityTree(html)); console.log(tree);

    #Build from Source

    npm run build:wasm-component

    This generates:
    • wasm/target/crater.wasm - The final WASM component

    To generate JavaScript bindings:

    npm run jco:transpile

    #WIT Interface

    The component exports five interfaces:

    Stateless, pure layout computation with flat output format.
    • compute-layout(html, width, height) -> string - Compute layout, returns JSON with { rootId, nodes[] }

    Output node format:
    interface LayoutNode { id: string; parentId: string; // empty for root index: number; // child index in parent x: number; y: number; width: number; height: number; contentX: number; contentY: number; contentWidth: number; contentHeight: number; text?: string; // for text nodes }

    #mizchi:crater/renderer@0.1.0

    Legacy rendering with hierarchical output.
    • render-html(html, width, height) -> string - Render HTML to text layout tree
    • render-html-to-json(html, width, height) -> string - Render HTML to JSON (hierarchical)
    • render-html-to-paint-tree(html, width, height) -> string - Render HTML to paint tree JSON

    #mizchi:crater/incremental@0.1.0

    Stateful layout with caching and diff updates.
    • create-tree(html, width, height) -> u32 - Create layout tree from HTML
    • compute-incremental() -> string - Compute layout incrementally (with cache)
    • compute-full() -> string - Compute full layout (no cache)
    • mark-dirty(node-id) -> bool - Mark node as dirty
    • update-style(node-id, css) -> bool - Update node style
    • resize-viewport(width, height) - Resize viewport
    • get-cache-stats() -> string - Get cache statistics as JSON
    • reset-cache-stats() - Reset cache statistics
    • needs-layout() -> bool - Check if layout is needed
    • destroy-tree() - Destroy current tree

    #mizchi:crater/yoga@0.1.0

    Yoga-compatible style API for programmatic layout:
    • Node management: create-node, add-child, insert-child, remove-child, get-child-count
    • Size: set-width, set-width-percent, set-width-auto, set-height, set-height-percent, set-height-auto
    • Flex: set-flex-grow, set-flex-shrink, set-flex-basis, set-flex-direction, set-flex-wrap
    • Alignment: set-justify-content, set-align-items
    • Display: set-display
    • Spacing: set-margin, set-padding, set-gap
    • Computed values: get-computed-left, get-computed-top, get-computed-width, get-computed-height
    • Layout: calculate-layout, has-new-layout, mark-layout-seen

    #mizchi:crater/accessibility@0.1.0

    Accessibility tree and ARIA snapshot generation.
    • get-aria-snapshot(html) -> string - Get ARIA snapshot in YAML format (Playwright-compatible)
    • get-aria-snapshot-json(html) -> string - Get ARIA snapshot as JSON
    • get-accessibility-tree(html) -> string - Get full accessibility tree as JSON

    #Publishing to wa.dev

    # Login to wa.dev wa login # Publish cd wasm wa publish

    #Requirements

    AlignItems

    Align items enum values

    Display

    Display enum values

    FlatLayoutNode

    Flat layout node (children referenced by parent-id)

    FlatLayoutResult

    Layout result with all nodes flattened (depth-first order)

    FlexDirection

    Flex direction enum values

    FlexWrap

    Flex wrap enum values

    JustifyContent

    Justify content enum values

    add_child

    fn add_child(parent_id : String, child_id : String) -> Bool

    calculate_layout

    fn calculate_layout(width : Double, height : Double) -> String

    calculate_layout_structured

    fn calculate_layout_structured(width : Double, height : Double) ->
    FlatLayoutResult

    compute_full

    fn compute_full() -> String

    compute_full_structured

    compute_incremental

    fn compute_incremental() -> String

    compute_incremental_structured

    fn compute_incremental_structured() ->
    FlatLayoutResult

    compute_layout

    fn compute_layout(html : String, width : UInt, height : UInt) -> String

    create_node

    fn create_node(node_id : String) -> UInt

    create_tree

    fn create_tree(html : String, width : UInt, height : UInt) -> UInt

    destroy_tree

    fn destroy_tree() -> Unit

    extract_main_content

    fn extract_main_content(html : String) -> String

    extract_main_content_with_layout

    fn extract_main_content_with_layout(html : String, width : Int, height : Int) -> String

    get_accessibility_tree

    fn get_accessibility_tree(html : String) -> String

    get_aria_snapshot

    fn get_aria_snapshot(html : String) -> String

    get_aria_snapshot_json

    fn get_aria_snapshot_json(html : String) -> String

    get_cache_stats

    fn get_cache_stats() -> String

    get_child_count

    fn get_child_count(node_id : String) -> UInt

    get_computed_height

    fn get_computed_height(node_id : String) -> Double

    get_computed_left

    fn get_computed_left(node_id : String) -> Double

    get_computed_top

    fn get_computed_top(node_id : String) -> Double

    get_computed_width

    fn get_computed_width(node_id : String) -> Double

    has_new_layout

    fn has_new_layout(node_id : String) -> Bool

    insert_child

    fn insert_child(parent_id : String, child_id : String, index : UInt) -> Bool

    mark_dirty

    fn mark_dirty(node_id : String) -> Bool

    mark_layout_seen

    fn mark_layout_seen(node_id : String) -> Bool

    needs_layout

    fn needs_layout() -> Bool

    remove_child

    fn remove_child(parent_id : String, index : UInt) -> Bool

    render_html

    fn render_html(html : String, width : UInt, height : UInt) -> String

    render_html_to_json

    fn render_html_to_json(html : String, width : UInt, height : UInt) -> String

    render_html_to_paint_tree

    fn render_html_to_paint_tree(html : String, width : UInt, height : UInt) -> String

    reset_cache_stats

    fn reset_cache_stats() -> Unit

    resize_viewport

    fn resize_viewport(width : UInt, height : UInt) -> Unit

    set_align_items

    fn set_align_items(node_id : String, align :
    AlignItems
    ) -> Bool

    set_display

    fn set_display(node_id : String, display :
    Display
    ) -> Bool

    set_flex_basis

    fn set_flex_basis(node_id : String, basis : Double) -> Bool

    set_flex_direction

    fn set_flex_direction(node_id : String, direction :
    FlexDirection
    ) -> Bool

    set_flex_grow

    fn set_flex_grow(node_id : String, grow : Double) -> Bool

    set_flex_shrink

    fn set_flex_shrink(node_id : String, shrink : Double) -> Bool

    set_flex_wrap

    fn set_flex_wrap(node_id : String, wrap :
    FlexWrap
    ) -> Bool

    set_gap

    fn set_gap(node_id : String, gap : Double) -> Bool

    set_height

    fn set_height(node_id : String, height : Double) -> Bool

    set_height_auto

    fn set_height_auto(node_id : String) -> Bool

    set_height_percent

    fn set_height_percent(node_id : String, height : Double) -> Bool

    set_justify_content

    fn set_justify_content(node_id : String, justify :
    JustifyContent
    ) -> Bool

    set_margin

    fn set_margin(node_id : String, margin : Double) -> Bool

    set_padding

    fn set_padding(node_id : String, padding : Double) -> Bool

    set_width

    fn set_width(node_id : String, width : Double) -> Bool

    set_width_auto

    fn set_width_auto(node_id : String) -> Bool

    set_width_percent

    fn set_width_percent(node_id : String, width : Double) -> Bool

    update_style

    fn update_style(node_id : String, css : String) -> Bool

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io