Skip to main content

Module wasm

Module wasm 

Source
Available on crate feature wasm only.
Expand description

WebAssembly bindings.

§Why this does not follow docs/history/MOLT_SPEC.md §4.1

The spec’s WasmDocument holds body_nodes: Vec<String>: it renders each node to a string on insertion and concatenates at the end. That throws the tree away, so render_pretty() becomes impossible, nothing can be modified after insertion, and the API is limited to the two element types the spec hard-codes.

These bindings wrap the real Element and Document behind opaque handles instead. JavaScript therefore drives the same renderer as Rust does, which is what lets tests/wasm.rs build one document through both APIs and compare the bytes. If native and WASM ever diverge, that comparison catches it.

§Handles are consumed

Every builder method takes its receiver and any element argument by value, which in JavaScript means the handle you passed in is gone afterwards — calling a method on it throws null pointer passed to rust. Chain calls, or keep the returned handle:

const a = element("div");
a.addClass("x");          // consumes `a`
a.render();               // throws
const b = element("div").addClass("x");   // right

Taking arguments by value is what lets child(el) move the subtree instead of cloning it. Cloning made building a page quadratic in its depth, overflowed the wasm stack on a deep one, and left the original handle alive and independent, so a later change to it was silently not part of the page.

Structs§

WDocument
A complete HTML document, usable from JavaScript.
WElement
An HTML element, usable from JavaScript.
WSeo
A page’s SEO metadata block, usable from JavaScript.

Functions§

element
Creates an element. The idiomatic JS entry point — element("div") reads better than new WElement("div").
escape_text
Escapes text for HTML content, exposed for callers doing their own assembly.
json_ld
A <script type="application/ld+json"> with the document made safe to embed: <, > and & become JSON escapes, so a </script> in a value cannot end the element.
version
The crate version, so a page can report which build produced it.