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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Wrapped representation of a DOM Element. See [node](../index.html) module for distinction from
//! raw representation.

use downcast_rs::DowncastSync;

use std::convert::TryFrom;
use std::result::Result;
use std::sync::{Arc, Weak};

use crate::node::raw;
use crate::sandbox::Sandbox;

/// Any wrapped Element
pub struct Element(Arc<dyn raw::AnyRawElement>);

/// Any wrapped Node
pub struct Node(Arc<dyn raw::AnyRawNode>);

macro_rules! impl_node_base {
    ($ty:ident, $raw_ty:ty) => {
        impl From<$ty> for Node {
            fn from(source: $ty) -> Node {
                Node(source.0)
            }
        }

        impl TryFrom<Node> for $ty {
            type Error = Node;

            fn try_from(elem: Node) -> Result<$ty, Node> {
                elem.0
                    .downcast_arc::<$raw_ty>()
                    .map($ty)
                    .map_err(Node)
            }
        }
    };
}

macro_rules! impl_node {
    ($ty:ident, $raw_ty:ty) => {

        impl_node_base!($ty, $raw_ty);
    };
}

macro_rules! impl_element {
    ($ty:ident, $raw_ty:ty) => {
        impl_node_base!($ty, $raw_ty);

        impl From<$ty> for Element {
            fn from(source: $ty) -> Element {
                Element(source.0)
            }
        }

        impl TryFrom<Element> for $ty {
            type Error = Element;

            fn try_from(elem: Element) -> Result<$ty, Element> {
                elem.0
                    .downcast_arc::<$raw_ty>()
                    .map($ty)
                    .map_err(Element)
            }
        }
    };
}

/// A wrapped Body element
pub struct BodyElement(pub Arc<raw::BodyElement>);
impl_element!(BodyElement, raw::BodyElement);

/// A wrapped Document element (<HTML />)
pub struct DocumentElement(pub Arc<raw::DocumentElement>);
impl_element!(DocumentElement, raw::DocumentElement);

/// A wrapped Document node
pub struct Document(pub Arc<raw::Document>);
impl_node!(Document, raw::Document);
impl Document {
    pub(crate) fn new(context: Weak<Sandbox>) -> Self {
        Document(Arc::new(raw::Document::new(context)))
    }
}