A wasm test.
This commit is contained in:
parent
16f0be33a4
commit
22e334f414
|
@ -1,5 +1,6 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"typed-html",
|
"typed-html",
|
||||||
"macros"
|
"macros",
|
||||||
|
"wasm"
|
||||||
]
|
]
|
||||||
|
|
|
@ -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"
|
|
@ -0,0 +1 @@
|
||||||
|
default-target = "wasm32-unknown-unknown"
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue