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
use super::*;
use web_sys::DocumentType;

pub struct UseDocument {
    data: Rc<RefCell<UseDocumentData>>,
}

struct UseDocumentData {
    document: Option<Document>,
}

impl UseDocument {
    pub fn new(cx: &ScopeState) -> Option<Self> {
        let document = window()?.document()?;
        let data = Rc::new(RefCell::new(UseDocumentData { document: Some(document) }));

        Some(Self { data })
    }
}

impl UseDocument {
    /// Title
    pub fn title(&self) -> String {
        let document = &self.data.borrow_mut().document;
        match document {
            None => String::new(),
            Some(e) => e.title(),
        }
    }
    ///
    pub fn set_title(&self, input: &str) -> Option<()> {
        let document = &self.data.borrow_mut().document;
        Some(document.as_ref()?.set_title(input))
    }
    /// **read-only**
    pub fn character_set(&self) -> String {
        let document = &self.data.borrow_mut().document;
        match document {
            None => String::from("utf-8"),
            Some(e) => e.character_set(),
        }
    }
    /// **read-only**
    pub fn doc_type(&self) -> Option<DocumentType> {
        let document = &self.data.borrow_mut().document;
        document.as_ref()?.doctype()
    }
}