1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::{node, StyleSheet};
use elvis_core::Node;
use std::{cell::RefCell, convert::Into, rc::Rc};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Clone, Debug, Default)]
pub struct Page {
tree: Node,
style: Rc<RefCell<StyleSheet>>,
}
impl<N> From<N> for Page
where
N: Into<Node>,
{
fn from(n: N) -> Page {
let mut node: Node = n.into();
node.idx(&mut vec![]);
Page {
tree: node,
style: Rc::new(RefCell::new(StyleSheet::default())),
}
}
}
impl Page {
pub fn calling(&mut self) -> Result<(), JsValue> {
let window = web_sys::window().unwrap();
let dom = window.document().unwrap();
StyleSheet::shared()?;
self.style()?;
let body = dom.query_selector("body")?.unwrap();
body.set_inner_html("");
body.append_child(
&node::to_element(&Rc::new(RefCell::new(self.tree.clone())), &dom)?.into(),
)?;
Ok(())
}
fn style(&mut self) -> Result<bool, JsValue> {
self.style.borrow_mut().batch(&mut self.tree);
Ok(self.style.borrow().ser(self.tree.attr.id.to_string())?)
}
}