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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! `augdom` provides an "augmented DOM" implementation that can run almost
//! anywhere Rust can. By default the `webdom` feature is enabled and this crate
//! is a wrapper around `web-sys` for creating and manipulating HTML elements.
//! See the [crate::Dom] trait for the provided behavior.
//!
//! The `rsdom` feature enables a DOM emulation layer written in pure Rust which
//! can be used for testing or to render HTML strings.
//!
//! # Known Limitations
//!
//! As of today the `<web_sys::Element as Dom>::*_attribute` methods will panic
//! if called on a text node. This cost seems appropriate today because this is
//! a dependency for other crates which enforce this requirement themselves.
//! `web_sys` enforces this restriction statically.

#![deny(clippy::all, missing_docs)]

static_assertions::assert_cfg!(
    any(feature = "webdom", feature = "rsdom"),
    "At least one DOM implementation's feature must be enabled (`webdom`, `rsdom`)"
);

#[cfg(feature = "webdom")]
pub use {wasm_bindgen::JsCast, web_sys as sys};

#[cfg(feature = "rsdom")]
use {rsdom::VirtNode, std::rc::Rc};

use futures::Stream;
use quick_xml::Writer as XmlWriter;
use std::{
    fmt::{Debug, Display, Formatter, Result as FmtResult},
    io::{prelude::*, Cursor},
    pin::Pin,
    task::{Context, Poll},
};

#[cfg(feature = "rsdom")]
pub mod rsdom;
#[cfg(feature = "webdom")]
pub mod webdom;

pub mod event;
pub mod testing;

/// Returns the current document. Panics if called outside a valid context.
pub fn document() -> Document {
    #[cfg(feature = "rsdom")]
    match illicit::get::<Document>() {
        Ok(d) => d.clone(),
        _e => {
            #[cfg(not(feature = "webdom"))]
            {
                _e.unwrap()
            }
            #[cfg(feature = "webdom")]
            {
                concrete_document()
            }
        }
    }

    #[cfg(all(feature = "webdom", not(feature = "rsdom")))]
    concrete_document()
}

/// Wrap the provided `root` function in a virtual document and ensures that all
/// nodes created within `root` will create virtual nodes.
#[cfg(feature = "rsdom")]
pub fn in_virtual_document<Root>(mut root: impl FnMut() -> Root) -> impl FnMut() -> Root {
    use illicit::AsContext;
    let document = Document::new_virtual();
    move || document.clone().offer(&mut root)
}

/// Create a new virtual element as part of a throwaway document. Useful for
/// testing.
#[cfg(feature = "rsdom")]
pub fn create_virtual_element(ty: &str) -> Node {
    Document::new_virtual().create_element(ty)
}

#[cfg(feature = "webdom")]
fn concrete_document() -> Document {
    Document::Concrete(
        sys::window()
            .expect("must run from within a `window`")
            .document()
            .expect("must run from within a `window` with a valid `document`"),
    )
}

/// A value which implements a subset of the web's document object model.
pub trait Dom: Sized {
    /// The type returned by `query_selector_all`.
    type Nodes: IntoIterator<Item = Self>;

    /// The type returned in batches by [`Dom::Observer`].
    type MutationRecord;

    /// The type returned by `observe`.
    type Observer: Stream<Item = Vec<Self::MutationRecord>> + Unpin;

    /// Write this value as XML via the provided writer. Consider using
    /// [Dom::outer_html] or [Dom::pretty_outer_html] unless you need the
    /// performance.
    fn write_xml<W: Write>(&self, writer: &mut XmlWriter<W>);

    /// Returns a string of serialized XML without newlines or indentation.
    fn outer_html(&self) -> String {
        let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
        {
            let mut writer = XmlWriter::new(&mut buf);
            self.write_xml(&mut writer);
        }
        String::from_utf8(buf.into_inner()).unwrap()
    }

    /// Returns a string of "prettified" serialized XML with the provided
    /// indentation.
    fn pretty_outer_html(&self, indent: usize) -> String {
        let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
        {
            let mut writer = XmlWriter::new_with_indent(&mut buf, b' ', indent);
            self.write_xml(&mut writer);
        }
        String::from_utf8(buf.into_inner()).unwrap()
    }

    /// Get an attribute from this DOM node.
    fn get_attribute(&self, name: &str) -> Option<String>;

    /// Set an attribute on this DOM node.
    fn set_attribute(&self, name: &str, value: &str);

    /// Ensure the provided attribute has been removed from this DOM node.
    fn remove_attribute(&self, name: &str);

    /// Returns the next child of this node's parent after this node itself.
    fn next_sibling(&self) -> Option<Self>;

    /// Returns the first child of this node.
    fn first_child(&self) -> Option<Self>;

    /// Adds a new child to the end of this node's children.
    fn append_child(&self, child: &Self);

    /// Replaces the provided child of this node with a new one.
    fn replace_child(&self, new_child: &Self, existing: &Self);

    /// Removes the provided child from this node.
    fn remove_child(&self, to_remove: &Self) -> Option<Self>;

    /// Represents the "rendered" text content of a node and its descendants. It
    /// approximates the text the user would get if they highlighted the
    /// contents of the element with the cursor and then copied it to the
    /// clipboard.
    fn get_inner_text(&self) -> String;

    /// Synchronously invokes the affected EventListeners in the appropriate
    /// order. The normal event processing rules (including the capturing
    /// and optional bubbling phase) also apply to events dispatched
    /// manually with `dispatchEvent()`.
    fn dispatch<E: event::Event>(&self, event: E);

    /// Returns the first descendant of `self` which matches the specified
    /// [selectors].
    ///
    /// [selectors]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
    fn query_selector(&self, selectors: &str) -> Option<Self>;

    /// Returns a static (not live) Vec of descendents of `self` which match the
    /// specified [selectors].
    ///
    /// [selectors]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
    fn query_selector_all(&self, selectors: &str) -> Self::Nodes;

    /// Return a stream of mutations related to the subtree under this node.
    fn observe_mutations(&self) -> Self::Observer;
}

/// The current document.
#[derive(Clone)]
pub enum Document {
    /// A handle to a concrete document.
    #[cfg(feature = "webdom")]
    Concrete(sys::Document),

    /// A virtual environment's document.
    #[cfg(feature = "rsdom")]
    Virtual {
        /// The virtual document's head.
        head: Rc<VirtNode>,
        /// The virtual document's body.
        body: Rc<VirtNode>,
    },
}

impl Document {
    /// Create a new document for virtual rendering, inside or outside of the
    /// browser.
    #[cfg(feature = "rsdom")]
    pub fn new_virtual() -> Self {
        Document::Virtual {
            head: VirtNode::create_element("head"),
            body: VirtNode::create_element("body"),
        }
    }

    /// Return this document's head.
    pub fn head(&self) -> Node {
        match self {
            #[cfg(feature = "webdom")]
            Document::Concrete(d) => {
                let head = d.head().unwrap();
                let node: &sys::Node = head.as_ref();
                Node::Concrete(node.clone())
            }
            #[cfg(feature = "rsdom")]
            Document::Virtual { head, .. } => Node::Virtual(head.clone()),
        }
    }

    /// Return this document's body.
    pub fn body(&self) -> Node {
        match self {
            #[cfg(feature = "webdom")]
            Document::Concrete(d) => {
                let body = d.body().unwrap();
                let node: &sys::Node = body.as_ref();
                Node::Concrete(node.clone())
            }
            #[cfg(feature = "rsdom")]
            Document::Virtual { body, .. } => Node::Virtual(body.clone()),
        }
    }

    /// Returns the currently focused element, if any.
    pub fn active_element(&self) -> Option<Node> {
        match self {
            #[cfg(feature = "webdom")]
            Document::Concrete(d) => d.active_element().map(Into::into),
            #[cfg(feature = "rsdom")]
            Document::Virtual { body, .. } => Some(body.clone().into()),
        }
    }

    /// Create a new element in this document.
    pub fn create_element(&self, ty: &str) -> Node {
        match self {
            #[cfg(feature = "webdom")]
            Document::Concrete(d) => {
                let elem = d.create_element(ty).unwrap();
                let node: &sys::Node = elem.as_ref();
                Node::Concrete(node.clone())
            }
            #[cfg(feature = "rsdom")]
            Document::Virtual { .. } => Node::Virtual(VirtNode::create_element(ty)),
        }
    }

    /// Create a new text node in this document.
    pub fn create_text_node(&self, contents: &str) -> Node {
        match self {
            #[cfg(feature = "webdom")]
            Document::Concrete(d) => {
                let text = d.create_text_node(contents);
                let node: &sys::Node = text.as_ref();
                Node::Concrete(node.clone())
            }
            #[cfg(feature = "rsdom")]
            Document::Virtual { .. } => Node::Virtual(VirtNode::create_text_node(contents)),
        }
    }
}

impl Debug for Document {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        <Node as Debug>::fmt(&self.head(), f)?;
        <Node as Debug>::fmt(&self.body(), f)
    }
}

/// A `Node` in the augmented DOM.
#[derive(Clone)]
pub enum Node {
    /// A handle to a concrete DOM node running in the browser.
    #[cfg(feature = "webdom")]
    Concrete(sys::Node),

    /// A handle to a "virtual" DOM node, emulating the web in memory. While
    /// this implementation lacks many features, it can run on any target
    /// that Rust supports.
    #[cfg(feature = "rsdom")]
    Virtual(Rc<VirtNode>),
}

impl Debug for Node {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        let s = if f.alternate() { self.pretty_outer_html(4) } else { self.outer_html() };
        f.write_str(&s)
    }
}

impl Display for Node {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str(&self.pretty_outer_html(2))
    }
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            #[cfg(feature = "webdom")]
            (Node::Concrete(s), Node::Concrete(o)) => s.is_same_node(Some(o)),

            #[cfg(feature = "rsdom")]
            (Node::Virtual(s), Node::Virtual(o)) => Rc::ptr_eq(s, o),

            #[cfg(all(feature = "webdom", feature = "rsdom"))]
            _ => unreachable!("if moxie-dom is comparing two different types of nodes...uh-oh."),
        }
    }
}

impl Dom for Node {
    type MutationRecord = MutationRecord;
    type Nodes = Vec<Self>;
    type Observer = MutationObserver;

    fn write_xml<W: Write>(&self, writer: &mut XmlWriter<W>) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => {
                n.write_xml(writer);
            }

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => {
                n.write_xml(writer);
            }
        }
    }

    fn first_child(&self) -> Option<Self> {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::first_child(n).map(Node::Concrete),

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.first_child().map(Node::Virtual),
        }
    }

    fn append_child(&self, child: &Self) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => {
                <sys::Node as Dom>::append_child(n, child.expect_concrete());
            }

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => {
                n.append_child(child.expect_virtual());
            }
        }
    }

    fn next_sibling(&self) -> Option<Self> {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::next_sibling(n).map(Node::Concrete),

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.next_sibling().map(Node::Virtual),
        }
    }

    fn remove_child(&self, to_remove: &Self) -> Option<Self> {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => {
                <sys::Node as Dom>::remove_child(n, to_remove.expect_concrete()).map(Node::Concrete)
            }

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.remove_child(to_remove.expect_virtual()).map(Node::Virtual),
        }
    }

    fn replace_child(&self, new_child: &Self, existing: &Self) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => {
                <sys::Node as Dom>::replace_child(
                    n,
                    new_child.expect_concrete(),
                    existing.expect_concrete(),
                );
            }

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => {
                n.replace_child(new_child.expect_virtual(), existing.expect_virtual());
            }
        }
    }

    fn get_attribute(&self, name: &str) -> Option<String> {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::get_attribute(n, name),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => <Rc<VirtNode> as Dom>::get_attribute(n, name),
        }
    }

    fn set_attribute(&self, name: &str, value: &str) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::set_attribute(n, name, value),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.set_attribute(name, value),
        }
    }

    fn remove_attribute(&self, name: &str) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::remove_attribute(n, name),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.remove_attribute(name),
        }
    }

    fn get_inner_text(&self) -> String {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::get_inner_text(n),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => <Rc<VirtNode> as Dom>::get_inner_text(n),
        }
    }

    fn dispatch<E: event::Event>(&self, event: E) {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => <sys::Node as Dom>::dispatch(n, event),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => <Rc<VirtNode> as Dom>::dispatch(n, event),
        }
    }

    fn query_selector(&self, selectors: &str) -> Option<Self> {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => n.query_selector(selectors).map(Node::Concrete),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => n.query_selector(selectors).map(Node::Virtual),
        }
    }

    fn query_selector_all(&self, selectors: &str) -> Self::Nodes {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => n.query_selector_all(selectors).map(Node::Concrete).collect(),
            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => {
                n.query_selector_all(selectors).into_iter().map(Node::Virtual).collect()
            }
        }
    }

    fn observe_mutations(&self) -> Self::Observer {
        match self {
            #[cfg(feature = "webdom")]
            Node::Concrete(n) => MutationObserver::Concrete(n.observe_mutations()),

            #[cfg(feature = "rsdom")]
            Node::Virtual(n) => MutationObserver::Virtual(n.observe_mutations()),
        }
    }
}

/// Wraps streams of mutation events from a given DOM backend.
pub enum MutationObserver {
    /// Results from a MutationObserver.
    #[cfg(feature = "webdom")]
    Concrete(webdom::Mutations),

    /// A stream of mutations from the virtual backend.
    #[cfg(feature = "rsdom")]
    Virtual(futures::channel::mpsc::UnboundedReceiver<Vec<rsdom::Mutation>>),
}

impl Stream for MutationObserver {
    type Item = Vec<MutationRecord>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.get_mut() {
            #[cfg(feature = "webdom")]
            MutationObserver::Concrete(mutations) => {
                futures::pin_mut!(mutations);
                let next = futures::ready!(mutations.poll_next(cx));
                let batch = next.map(|n| n.into_iter().map(MutationRecord::Concrete).collect());
                Poll::Ready(batch)
            }

            #[cfg(feature = "rsdom")]
            MutationObserver::Virtual(mutations) => {
                futures::pin_mut!(mutations);
                let next = futures::ready!(mutations.poll_next(cx));
                let batch = next.map(|n| n.into_iter().map(MutationRecord::Virtual).collect());
                Poll::Ready(batch)
            }
        }
    }
}

/// Wraps individual mutation records from a given DOM backend.
pub enum MutationRecord {
    /// A mutation record from the web backend.
    #[cfg(feature = "webdom")]
    Concrete(sys::MutationRecord),

    /// A mutation record from the virtual backend.
    #[cfg(feature = "rsdom")]
    Virtual(rsdom::Mutation),
}

#[cfg(test)]
mod tests {
    use super::{
        testing::{Query, TargetExt},
        *,
    };
    use std::mem::forget as cleanup_with_test;
    use wasm_bindgen::prelude::*;
    use wasm_bindgen_test::*;
    wasm_bindgen_test_configure!(run_in_browser);

    fn example_dom() -> Node {
        let document = document();
        let div = document.create_element("div");

        let label = document.create_element("label");
        label.set_attribute("for", "username");
        label.append_child(&document.create_text_node("Username"));
        div.append_child(&label);

        let input = document.create_element("input");
        input.set_attribute("id", "username");
        div.append_child(&input);

        let button = document.create_element("button");
        button.append_child(&document.create_text_node("Print Username"));
        div.append_child(&button);

        let container_for_callback = div.clone();
        let onclick = event::EventHandle::new(&button, move |_: event::Click| {
            // on a click, add this dom node to the parent in a callback
            let (input, document) = (input.clone(), document.clone());
            let div = container_for_callback.clone();
            let cb = move || {
                let printed_name_container = document.create_element("div");
                printed_name_container.set_attribute("data-testid", "printed-username");
                let input_text = document.create_text_node(&input.get_attribute("value").unwrap());
                printed_name_container.append_child(&input_text);
                div.append_child(&printed_name_container);
            };

            // fire the callback on a timer
            let cb = Closure::wrap(Box::new(cb) as Box<dyn FnMut()>);
            let empty_args = js_sys::Array::new();
            sys::window()
                .expect("must be able to retrieve window")
                .set_timeout_with_callback_and_timeout_and_arguments(
                    cb.as_ref().unchecked_ref(),
                    500,
                    &empty_args,
                )
                .unwrap();
            cleanup_with_test(cb);
        });
        cleanup_with_test(onclick);

        div
    }

    #[wasm_bindgen_test]
    async fn basic_matchers() {
        let container = example_dom();

        let ada = "Ada Lovelace";
        let input = container.find().by_label_text("Username").one().unwrap();
        input.set_attribute("value", ada);

        container.find().by_text("Print Username").one().unwrap().click();
        let printed = container.find().by_test_id("printed-username").until().one().await.unwrap();

        assert_eq!(printed.get_inner_text(), ada);

        let container_html = container.to_string();
        let expected = "<div>
  <label for=\"username\">Username</label>
  <input id=\"username\" value=\"Ada Lovelace\">
  </input>
  <button>Print Username</button>
  <div data-testid=\"printed-username\">Ada Lovelace</div>
</div>";

        assert_eq!(container_html, expected);
    }
}