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
//! Traits for nodes in the DOM tree.

/// This module is pub(crate) to ensure only the correct wrapper types access
/// untyped nodes via the traits defined here.
pub(crate) mod sealed {
    /// Implemented by types in this crate which wrap CachedNode.
    pub trait Memoized {
        /// Return a reference to the inner value.
        fn node(&self) -> &crate::cached_node::CachedNode;
    }
}

/// Node is an interface from which various types of DOM API objects inherit,
/// allowing those types to be treated similarly; for example, inheriting the
/// same set of methods, or being testable in the same way.
///
/// Note: this trait cannot be implemented outside of this crate.
pub trait Node: sealed::Memoized + Sized {
    /// Retrieves access to the raw HTML element underlying the (CachedNode).
    ///
    /// Because this offers an escape hatch around the memoized mutations, it
    /// should be used with caution. Also because of this, it has a silly
    /// name intended to loudly announce that care must be taken.
    ///
    /// Code called by the root function of your application will be run quite
    /// frequently and so the tools for memoization are important for
    /// keeping your application responsive. If you have legitimate needs
    /// for this API, please consider filing an issue with your use case so
    /// the maintainers of this crate can consider "official" ways to support
    /// it.
    fn raw_node_that_has_sharp_edges_please_be_careful(&self) -> &augdom::Node {
        self.node().raw_node()
    }
}

/// A value which can be bound as a child to a DOM node.
pub trait Child {
    /// Returns the "raw" node for this child to bind to its parent.
    fn to_bind(&self) -> &augdom::Node;
}

impl<N> Child for N
where
    N: Node,
{
    fn to_bind(&self) -> &augdom::Node {
        self.raw_node_that_has_sharp_edges_please_be_careful()
    }
}

/// A node which accepts children.
///
/// > Note: `C` is constrained by `Child` rather than `Node` to allow custom
/// components to be bound directly to DOM types.
pub trait Parent<C: Child>: Node {
    /// Add a child to this node.
    fn child(self, child: C) -> Self {
        self.node().ensure_child_attached(child.to_bind());
        self
    }
}