A wasm test.

This commit is contained in:
Bodil Stokke 2018-11-12 23:21:27 +00:00
parent 16f0be33a4
commit 22e334f414
4 changed files with 59 additions and 1 deletions

View File

@ -1,5 +1,6 @@
[workspace]
members = [
"typed-html",
"macros"
"macros",
"wasm"
]

9
wasm/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "typed-html-wasm-test"
version = "0.1.0"
authors = ["Bodil Stokke <bodil@bodil.org>"]
[dependencies]
typed-html-macros = { path = "../macros" }
typed-html = { path = "../typed-html" }
stdweb = "0.4.10"

1
wasm/Web.toml Normal file
View File

@ -0,0 +1 @@
default-target = "wasm32-unknown-unknown"

47
wasm/src/main.rs Normal file
View File

@ -0,0 +1,47 @@
#![feature(try_from)]
#![feature(proc_macro_hygiene)]
extern crate stdweb;
extern crate typed_html;
extern crate typed_html_macros;
use stdweb::web::{self, IElement, INode};
use typed_html::elements::{Node, VNode};
use typed_html_macros::html;
fn build(
document: &web::Document,
vnode: VNode,
) -> Result<web::Node, web::error::InvalidCharacterError> {
match vnode {
VNode::Text(text) => Ok(document.create_text_node(&text).into()),
VNode::Element(element) => {
let mut node = document.create_element(element.name)?;
for (key, value) in element.attributes {
node.set_attribute(&key, &value)?;
}
for child in element.children {
let child_node = build(document, child)?;
node.append_child(&child_node);
}
Ok(node.into())
}
}
}
fn main() {
let doc = html!(
<div>
<h1>"Hello Kitty"</h1>
<p>
"She is not a "<em><a href="https://en.wikipedia.org/wiki/Cat">"cat"</a></em>
". She is a "<em>"human girl"</em>"."
</p>
</div>
);
let vdom = doc.vnode();
let document = web::document();
let body = document.body().expect("no body element in doc");
let tree = build(&document, vdom).unwrap();
body.append_child(&tree);
}