Sign in

    crater

    CSS Layout Engine that implements Box/Flex/Grid

    css
    browser
    Download zip
    Author
    Version
    0.4.4
    License
    Apache-2.0
    Last updated
    8 months ago
    Downloads
    60K

    #Crater

    A CSS layout engine written in MoonBit, ported from Taffy.

    #Overview

    Crater aims to provide layout computation similar to Yoga - calculating positions and dimensions of elements based on CSS properties like Flexbox and Grid, without requiring a full browser environment.

    This library focuses purely on layout calculation - it computes the x, y, width, and height of each element in your tree. It does not handle:

    • Font loading or text shaping
    • Text rendering or measurement (uses approximate character-based sizing)
    • Painting or drawing
    • DOM manipulation

    #Test Status

    #Taffy Compatibility Tests

    Layout algorithm tests ported from Taffy:

    ModulePassedTotalRate
    Flexbox54360989.2%
    Block20422690.3%
    Grid26833181.0%
    Total1015116687.0%

    #Parser Tests

    ModulePassedTotalRate
    CSS Parser332332100%
    CSS Selector6262100%
    CSS Media Query4141100%

    #Web Platform Tests (WPT)

    CSS tests from web-platform-tests:

    ModulePassedTotalRate
    css-flexbox23636764.3%
    css-grid263966.7%
    css-tables137417.6%
    css-display149315.1%
    css-sizing4810346.6%
    css-position5911750.4%
    css-overflow5611847.5%
    css-contain12531539.7%
    css-variables2515216.4%
    Total602137843.7%

    Run WPT tests:
    npm run wpt:fetch-all # Fetch all WPT tests npm run wpt:run-all # Run all WPT tests

    #Features

    #Layout Modes

    • Flexbox - direction, wrap, wrap-reverse, alignment, grow/shrink
    • CSS Grid - templates, auto-placement, areas, fr units, minmax, repeat
    • Block layout - margin collapsing
    • Inline layout - inline formatting context, inline-block

    #Box Model

    • margin, padding, border
    • Percentage and fixed dimensions
    • min-width, max-width, min-height, max-height
    • Intrinsic sizing: min-content, max-content, fit-content
    • box-sizing: border-box

    #Positioning

    • position: static, relative, absolute, fixed
    • top, right, bottom, left (inset properties)

    #Other Properties

    • gap (row-gap, column-gap)
    • aspect-ratio
    • overflow-x, overflow-y
    • visibility: hidden (with child override)
    • CSS Variables (--var, var())
    • calc() CSS function

    #Accessibility

    • Accessible Name Computation - WAI-ARIA accname-1.2 algorithm
    • ARIA Snapshot - Playwright-compatible YAML/JSON format
    • Accessibility Tree - Full tree structure with roles, states, and aria-owns support

    #Performance

    HTML parser benchmarks (on Apple Silicon):

    BenchmarkTime
    Simple HTML (100 elements)~27 µs
    Large document (100 sections × 20 paragraphs)~5.2 ms
    Attribute-heavy (200 elements, 6 attrs each)~390 µs
    Table (100×20 cells)~990 µs

    Run benchmarks:
    moon bench -p html

    #Limitations

    • No font rendering: Text measurement uses approximate monospace character sizing
    • No real text layout: Word wrapping is simplified
    • Baseline alignment: Partial implementation
    • Writing modes: Limited support for vertical text

    #Documentation

    #Installation

    moon add mizchi/crater

    #Usage

    // Parse HTML and render layout

    ///|
    let html = "<div style=\"display: flex; width: 300px;\"><div style=\"flex: 1\">A</div><div style=\"flex: 2\">B</div></div>"

    ///|
    let ctx = @renderer.RenderContext::default()

    ///|
    let layout = @renderer.render(html, ctx)

    // layout contains x, y, width, height for each element

    #License

    MIT

    compute_block_layout

    Compute block layout directly

    compute_element_shift

    Compute element shift (CLS)

    compute_flex_layout

    Compute flex layout directly

    compute_layout

    Compute layout for a node tree with given viewport Dispatches to the appropriate layout algorithm based on display type

    compute_layout_with_warnings

    Compute layout with warnings

    compute_total_cls

    Compute total CLS score

    Source Files