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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use super::{
    attribute::{Attribute, NextAttribute},
    element::ElementType,
};
use crate::{
    html::element::HtmlElement, prelude::Render, renderer::Renderer,
    view::add_attr::AddAnyAttr,
};
use std::marker::PhantomData;

/// Describes a container that can be used to hold a reference to an HTML element.
pub trait NodeRefContainer<E, Rndr>: Send + Clone
where
    E: ElementType,
    Rndr: Renderer,
{
    /// Fills the container with the element.
    fn load(self, el: &Rndr::Element);
}

/// An [`Attribute`] that will fill a [`NodeRefContainer`] with an HTML element.
#[derive(Debug)]
pub struct NodeRefAttr<E, C, Rndr> {
    container: C,
    ty: PhantomData<E>,
    rndr: PhantomData<Rndr>,
}

impl<E, C, Rndr> Clone for NodeRefAttr<E, C, Rndr>
where
    C: Clone,
{
    fn clone(&self) -> Self {
        Self {
            container: self.container.clone(),
            ty: PhantomData,
            rndr: PhantomData,
        }
    }
}

/// Creates an attribute that will fill a [`NodeRefContainer`] with the element it is applied to.
pub fn node_ref<E, C, Rndr>(container: C) -> NodeRefAttr<E, C, Rndr>
where
    E: ElementType,
    C: NodeRefContainer<E, Rndr>,
    Rndr: Renderer,
{
    NodeRefAttr {
        container,
        ty: PhantomData,
        rndr: PhantomData,
    }
}

impl<E, C, Rndr> Attribute<Rndr> for NodeRefAttr<E, C, Rndr>
where
    E: ElementType,
    C: NodeRefContainer<E, Rndr>,
    Rndr: Renderer,
    Rndr::Element: PartialEq,
{
    const MIN_LENGTH: usize = 0;
    type AsyncOutput = Self;
    type State = Rndr::Element;
    type Cloneable = ();
    type CloneableOwned = ();

    #[inline(always)]
    fn html_len(&self) -> usize {
        0
    }

    fn to_html(
        self,
        _buf: &mut String,
        _class: &mut String,
        _style: &mut String,
        _inner_html: &mut String,
    ) {
    }

    fn hydrate<const FROM_SERVER: bool>(
        self,
        el: &<Rndr as Renderer>::Element,
    ) -> Self::State {
        self.container.load(el);
        el.to_owned()
    }

    fn build(self, el: &<Rndr as Renderer>::Element) -> Self::State {
        self.container.load(el);
        el.to_owned()
    }

    fn rebuild(self, state: &mut Self::State) {
        self.container.load(state);
    }

    fn into_cloneable(self) -> Self::Cloneable {
        panic!("node_ref should not be spread across multiple elements.");
    }

    fn into_cloneable_owned(self) -> Self::Cloneable {
        panic!("node_ref should not be spread across multiple elements.");
    }

    fn dry_resolve(&mut self) {}

    async fn resolve(self) -> Self::AsyncOutput {
        self
    }
}

impl<E, C, Rndr> NextAttribute<Rndr> for NodeRefAttr<E, C, Rndr>
where
    E: ElementType,
    C: NodeRefContainer<E, Rndr>,
    Rndr: Renderer,
    Rndr::Element: PartialEq,
{
    type Output<NewAttr: Attribute<Rndr>> = (Self, NewAttr);

    fn add_any_attr<NewAttr: Attribute<Rndr>>(
        self,
        new_attr: NewAttr,
    ) -> Self::Output<NewAttr> {
        (self, new_attr)
    }
}

/// Adds the `node_ref` attribute to an element.
pub trait NodeRefAttribute<E, C, Rndr>
where
    E: ElementType,
    C: NodeRefContainer<E, Rndr>,
    Rndr: Renderer,
    Rndr::Element: PartialEq,
{
    /// Binds this HTML element to a [`NodeRefContainer`].
    fn node_ref(
        self,
        container: C,
    ) -> <Self as AddAnyAttr<Rndr>>::Output<NodeRefAttr<E, C, Rndr>>
    where
        Self: Sized + AddAnyAttr<Rndr>,
        <Self as AddAnyAttr<Rndr>>::Output<NodeRefAttr<E, C, Rndr>>:
            Render<Rndr>,
    {
        self.add_any_attr(node_ref(container))
    }
}

impl<E, At, Ch, C, Rndr> NodeRefAttribute<E, C, Rndr>
    for HtmlElement<E, At, Ch, Rndr>
where
    E: ElementType,
    At: Attribute<Rndr>,
    Ch: Render<Rndr>,
    C: NodeRefContainer<E, Rndr>,
    Rndr: Renderer,
    Rndr::Element: PartialEq,
{
}