diff --git a/Cargo.toml b/Cargo.toml index c77ad20..02a6d72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ "typed-html", - "macros" + "macros", + "wasm" ] diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml new file mode 100644 index 0000000..79c4dd6 --- /dev/null +++ b/wasm/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "typed-html-wasm-test" +version = "0.1.0" +authors = ["Bodil Stokke "] + +[dependencies] +typed-html-macros = { path = "../macros" } +typed-html = { path = "../typed-html" } +stdweb = "0.4.10" diff --git a/wasm/Web.toml b/wasm/Web.toml new file mode 100644 index 0000000..813e273 --- /dev/null +++ b/wasm/Web.toml @@ -0,0 +1 @@ +default-target = "wasm32-unknown-unknown" diff --git a/wasm/src/main.rs b/wasm/src/main.rs new file mode 100644 index 0000000..0d317c5 --- /dev/null +++ b/wasm/src/main.rs @@ -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 { + 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!( +
+

"Hello Kitty"

+

+ "She is not a ""cat" + ". She is a ""human girl""." +

+
+ ); + 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); +}