mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 04:21:21 +00:00
Add an example of DOM access
This commit is contained in:
39
examples/dom/src/lib.rs
Normal file
39
examples/dom/src/lib.rs
Normal file
@ -0,0 +1,39 @@
|
||||
#![feature(proc_macro)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// Definitions of the functionality available in JS, which wasm-bindgen will
|
||||
// generate shims for today (and eventually these should be near-0 cost!)
|
||||
//
|
||||
// These definitions need to be hand-written today but the current vision is
|
||||
// that we'll use WebIDL to generate this `extern` block into a crate which you
|
||||
// can link and import. There's a tracking issue for this at
|
||||
// https://github.com/alexcrichton/wasm-bindgen/issues/42
|
||||
//
|
||||
// In the meantime these are written out by hand and correspond to the names and
|
||||
// signatures documented on MDN, for example
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
type HTMLDocument;
|
||||
static document: HTMLDocument;
|
||||
#[wasm_bindgen(method)]
|
||||
fn createElement(this: &HTMLDocument, tagName: &str) -> Element;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn body(this: &HTMLDocument) -> Element;
|
||||
|
||||
type Element;
|
||||
#[wasm_bindgen(method, setter)]
|
||||
fn set_innerHTML(this: &Element, html: &str);
|
||||
#[wasm_bindgen(method)]
|
||||
fn appendChild(this: &Element, other: Element);
|
||||
}
|
||||
|
||||
// Called by our JS entry point to run the example
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
let val = document.createElement("p");
|
||||
val.set_innerHTML("Hello from Rust!");
|
||||
document.body().appendChild(val);
|
||||
}
|
Reference in New Issue
Block a user