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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! An implementation of `augdom`'s APIs on top of the actual web using the
//! `web-sys` crate and `wasm-bindgen`.

use super::Node;
use futures::{channel::mpsc::UnboundedReceiver, Stream};
use prettiest::Pretty;
use std::{
    any::type_name,
    io::Write,
    pin::Pin,
    task::{Context, Poll},
};
use tracing::error;
use wasm_bindgen::{prelude::*, JsCast};
use web_sys as sys;

/// A dynamically-allocated closure that is callable from JavaScript.
pub struct Callback {
    cb: Closure<dyn FnMut(JsValue)>,
}

impl Callback {
    /// Allocate a new JS-compatible callback.
    pub fn new<T>(mut cb: impl FnMut(T) + 'static) -> Self
    where
        T: JsCast,
    {
        let cb = Closure::wrap(Box::new(move |raw: JsValue| match raw.dyn_into() {
            Ok(value) => cb(value),
            Err(v) => {
                error!(
                    failed_cast_to = %type_name::<T>(),
                    value = %v.pretty(),
                    "received unexpected value in callback",
                );
            }
        }) as Box<dyn FnMut(JsValue)>);
        Self { cb }
    }

    /// Returns an reference to the underlying JS function. If the reference is
    /// used after this `Callback` is dropped it will panic.
    pub fn as_fn(&self) -> &js_sys::Function {
        self.cb.as_ref().unchecked_ref()
    }
}

impl crate::Dom for sys::Node {
    type MutationRecord = sys::MutationRecord;
    type Nodes = NodeList;
    type Observer = Mutations;

    fn write_xml<W: Write>(&self, writer: &mut quick_xml::Writer<W>) {
        use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
        if let Some(elem) = self.dyn_ref::<sys::Element>() {
            let name = elem.tag_name().to_lowercase();
            let attrs = elem.attributes();
            let attrs = (0..attrs.length())
                .filter_map(|i| attrs.item(i))
                .map(|a| (a.name(), a.value()))
                .collect::<Vec<_>>();

            writer
                .write_event(Event::Start(
                    BytesStart::borrowed_name(name.as_bytes())
                        .with_attributes(attrs.iter().map(|(n, v)| (n.as_str(), v.as_str()))),
                ))
                .expect("writing start of element");

            let children = sys::Node::child_nodes(elem.as_ref());
            for i in 0..children.length() {
                children.item(i).unwrap().write_xml(writer);
            }

            writer
                .write_event(Event::End(BytesEnd::owned(name.into_bytes())))
                .expect("writing start of element");
        } else if let Some(text) = self.dyn_ref::<sys::Text>() {
            writer
                .write_event(quick_xml::events::Event::Text(BytesText::from_plain_str(
                    &text.data(),
                )))
                .expect("writing text node");
        } else {
            unreachable!("augdom only creates elements and text nodes. this is a bug.");
        }
    }

    fn first_child(&self) -> Option<Self> {
        self.first_child()
    }

    fn append_child(&self, child: &Self) {
        self.append_child(child).unwrap();
    }

    fn next_sibling(&self) -> Option<Self> {
        self.next_sibling()
    }

    fn remove_child(&self, to_remove: &Self) -> Option<Self> {
        self.remove_child(to_remove).ok()
    }

    fn replace_child(&self, new_child: &Self, existing: &Self) {
        self.replace_child(new_child, existing).unwrap();
    }

    fn get_attribute(&self, name: &str) -> Option<String> {
        let e: Option<&sys::Element> = self.dyn_ref();
        e.map(|e| sys::Element::get_attribute(e, name)).flatten()
    }

    fn set_attribute(&self, name: &str, value: &str) {
        let e: &sys::Element = self.dyn_ref().unwrap();
        e.set_attribute(name, value).unwrap();
    }

    fn remove_attribute(&self, name: &str) {
        let e: &sys::Element = self.dyn_ref().unwrap();
        e.remove_attribute(name).ok();
    }

    fn get_inner_text(&self) -> String {
        let e: Option<&sys::HtmlElement> = self.dyn_ref();
        e.map(sys::HtmlElement::inner_text).unwrap_or_default()
    }

    fn dispatch<E: crate::event::Event>(&self, event: E) {
        event.dispatch(self);
    }

    fn query_selector(&self, selectors: &str) -> Option<Self> {
        let e: &sys::Element = self.dyn_ref().unwrap();
        sys::Element::query_selector(e, selectors).unwrap().map(Into::into)
    }

    fn query_selector_all(&self, selectors: &str) -> Self::Nodes {
        let e: &sys::Element = self.dyn_ref().unwrap();
        NodeList { idx: 0, inner: sys::Element::query_selector_all(e, selectors).unwrap() }
    }

    fn observe_mutations(&self) -> Self::Observer {
        Mutations::new(self)
    }
}

impl From<sys::Node> for Node {
    fn from(e: sys::Node) -> Self {
        Node::Concrete(e)
    }
}

impl From<sys::Element> for Node {
    fn from(e: sys::Element) -> Self {
        Node::Concrete(e.into())
    }
}

impl From<sys::HtmlElement> for Node {
    fn from(e: sys::HtmlElement) -> Self {
        Node::Concrete(e.into())
    }
}

impl From<sys::Text> for Node {
    fn from(e: sys::Text) -> Self {
        Node::Concrete(e.into())
    }
}

impl Node {
    /// Returns a reference to a concrete DOM node, panics if this is a virtual
    /// node.
    pub fn expect_concrete(&self) -> &sys::Node {
        match self {
            Node::Concrete(n) => n,

            #[cfg(feature = "rsdom")]
            Node::Virtual(_) => panic!("expected a Node::Concrete, found a Node::Virtual"),
        }
    }
}

/// Wraps [`sys::NodeList`] to implement `Iterator`.
pub struct NodeList {
    inner: sys::NodeList,
    idx: u32,
}

impl Iterator for NodeList {
    type Item = sys::Node;

    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.inner.item(self.idx);
        self.idx += 1;
        ret
    }
}

impl std::iter::ExactSizeIterator for NodeList {
    fn len(&self) -> usize {
        self.inner.length() as _
    }
}

/// Wraps a [`web_sys::MutationObserver`], providing a `Stream`.
pub struct Mutations {
    observer: crate::sys::MutationObserver,
    _callback: Callback,
    records: UnboundedReceiver<Vec<sys::MutationRecord>>,
}

impl Mutations {
    fn new(node: &crate::sys::Node) -> Self {
        let (sender, records) = futures::channel::mpsc::unbounded();
        let _callback = Callback::new(move |arr: js_sys::Array| {
            let records = arr
                .iter()
                .map(|val| val.dyn_into::<crate::sys::MutationRecord>().unwrap())
                .collect();
            sender.unbounded_send(records).unwrap();
        });
        let observer = crate::sys::MutationObserver::new(_callback.as_fn()).unwrap();
        let mut options = crate::sys::MutationObserverInit::new();
        options.attributes(true);
        options.character_data(true);
        options.child_list(true);
        options.subtree(true);
        observer.observe_with_options(node, &options).unwrap();

        Self { observer, _callback, records }
    }
}

impl Stream for Mutations {
    type Item = Vec<sys::MutationRecord>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let records = &mut self.get_mut().records;
        futures::pin_mut!(records);
        records.poll_next(cx)
    }
}

impl Drop for Mutations {
    fn drop(&mut self) {
        self.observer.disconnect();
    }
}