frontest 0.3.0

A crate for querying, matching and asserting DOM
Documentation
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
use gloo::utils::document;
use wasm_bindgen::JsCast;
use web_sys::{
    Element, HtmlButtonElement, HtmlElement, HtmlInputElement, HtmlLabelElement, HtmlMeterElement,
    HtmlOutputElement, HtmlProgressElement, HtmlSelectElement, HtmlTextAreaElement,
};

/// Returns the list of aria roles for a given [`HtmlElement`].
///
/// Aria role is a semantic meaning of an element.
/// It provides a web site with an [`accessibility`].
/// List of assigned roles was shamelessly taken from [aria-query](https://www.npmjs.com/package/aria-query).
///
/// | Tag                             | Roles             |
/// |---------------------------------|-------------------|
/// | `<article>`                     | article           |
/// | `<button>`                      | button            |
/// | `<td>`                          | cell, gridcell    |
/// | `<select>`                      | combobox, listbox |
/// | `<menuitem>`                    | command, menuitem |
/// | `<dd>`                          | definition        |
/// | `<figure>`                      | figure            |
/// | `<form>`                        | form              |
/// | `<table>`                       | grid, table       |
/// | `<fieldset>`                    | group             |
/// | `<h1> <h2> <h3> <h4> <h5> <h6>` | heading           |
/// | `<img>`                         | img               |
/// | `<a> <link>`                    | link              |
/// | `<ol> <ul>`                     | list              |
/// | `<li>`                          | listitem          |
/// | `<nav>`                         | navigation        |
/// | `<option>`                      | option            |
/// | `<frame>`                       | region            |
/// | `<rel>`                         | roletype          |
/// | `<tr>`                          | row               |
/// | `<tbody> <tfoot> <thead>`       | rowgroup          |
/// | `<hr>`                          | separator         |
/// | `<dt> <dfn>`                    | term              |
/// | `<textarea>`                    | textbox           |
/// | `<input type=button>`           | button            |
/// | `<input type=checkbox>`         | checkbox          |
/// | `<input type=radio>`            | radio             |
/// | `<input type=search>`           | searchbox         |
/// | `<input type=text>`             | textbox           |
/// | `<th scope=row>`                | rowheader         |
/// | `<th>`                          | columnheader      |
///
/// [`accessibility`]: https://developer.mozilla.org/en-US/docs/Web/Accessibility
pub fn element_to_aria_roles(elem: &HtmlElement) -> Vec<&'static str> {
    match elem.tag_name().to_lowercase().as_str() {
        "article" => vec!["article"],
        "button" => vec!["button"],
        "td" => vec!["cell", "gridcell"],
        "select" => vec!["combobox", "listbox"],
        "menuitem" => vec!["command", "menuitem"],
        "dd" => vec!["definition"],
        "figure" => vec!["figure"],
        "form" => vec!["form"],
        "table" => vec!["grid", "table"],
        "fieldset" => vec!["group"],
        "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => vec!["heading"],
        "img" => vec!["img"],
        "a" | "link" => vec!["link"],
        "ol" | "ul" => vec!["list"],
        "li" => vec!["listitem"],
        "nav" => vec!["navigation"],
        "option" => vec!["option"],
        "frame" => vec!["region"],
        "rel" => vec!["roletype"],
        "tr" => vec!["row"],
        "tbody" | "tfoot" | "thead" => vec!["rowgroup"],
        "hr" => vec!["separator"],
        "dt" | "dfn" => vec!["term"],
        "textarea" => vec!["textbox"],
        "input" => match elem.get_attribute("type").as_deref().unwrap_or("") {
            "button" => vec!["button"],
            "checkbox" => vec!["checkbox"],
            "radio" => vec!["radio"],
            "search" => vec!["searchbox"],
            "text" => vec!["textbox"],
            _ => vec![],
        },
        "th" => match elem.get_attribute("scope").as_deref().unwrap_or("") {
            "row" => vec!["rowheader"],
            _ => vec!["columnheader"],
        },
        _ => vec![],
    }
}

/// Trait implemented by types that can be used as a predicate for [`HtmlElement`].
///
/// One can implement this trait to create custom [`Matcher`]s.
///
/// # Example:
/// ```no_run
/// # use web_sys::HtmlElement;
/// # use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// struct IsHidden;
///
/// impl Matcher for IsHidden {
///     fn matches(&self, elem: &HtmlElement) -> bool {
///         elem.hidden()
///     }
/// }
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<button hidden>
///         Yayyy frontend in rust!
///     </button>"#
/// );
/// body().append_child(&div).unwrap();
///
/// let hidden_button = div.get(&IsHidden.and(HasRole("button"))).unwrap();
///
/// assert!(hidden_button.inner_html().contains("in rust"));
///
/// body().remove_child(&div).unwrap();
/// ```
pub trait Matcher {
    /// Returns `true` if the element was matched by [`Matcher`].
    fn matches(&self, elem: &HtmlElement) -> bool;
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_matcher() {
    use crate::query::Matcher;
    use crate::query::{HasRole, Joinable, Query};
    use gloo::utils::{body, document};
    use web_sys::HtmlElement;

    struct IsHidden;

    impl Matcher for IsHidden {
        fn matches(&self, elem: &HtmlElement) -> bool {
            elem.hidden()
        }
    }

    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<button hidden>
            Yayyy frontend in rust!
        </button>"#,
    );
    body().append_child(&div).unwrap();

    let hidden_button = div.get(&IsHidden.and(HasRole("button"))).unwrap();

    assert!(hidden_button.inner_html().contains("in rust"));

    body().remove_child(&div).unwrap();
}

/// Consumes a [`Matcher`] and returns a negation of it.
///
/// Utility wrapper that performs a logical `not` operation on a matcher.
///
/// # Example:
///
/// ```no_run
/// use gloo::utils::{body, document};
/// use frontest::prelude::*;

/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <p>what</p>
///         <a href="/foo">is</a>
///         <button>this</button>
///     </div>"#,
/// );
/// body().append_child(&div).unwrap();
///
/// let link = div.get(&HasText("is").and(Not(HasRole("button")))).unwrap();
/// assert_eq!(&link.get_attribute("href").unwrap(), "/foo");
///
/// body().remove_child(&div).unwrap();
/// ```
pub struct Not<M: Matcher>(pub M);

impl<M: Matcher> Matcher for Not<M> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        !self.0.matches(elem)
    }
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_not() {
    use crate::query::{HasRole, HasText, Joinable, Not, Query};
    use gloo::utils::{body, document};
    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <p>what</p>
            <a href="/foo">is</a>
            <button>this</button>
        </div>"#,
    );
    body().append_child(&div).unwrap();

    let link = div.get(&HasText("is").and(Not(HasRole("button")))).unwrap();
    assert_eq!(&link.get_attribute("href").unwrap(), "/foo");

    body().remove_child(&div).unwrap();
}

/// Matches components that have visible text that contains given substring.
///
/// [`HasText`] uses [`inner_text`] under the hood and is case-sensitive.
/// It will match elements by their content as presented for user.
/// All css rules applies eg. those switching text content, case or visibility.
/// Remember that for this experience you need to insert an element somewhere into DOM.
///
/// # Example:
///
/// ```no_run
/// # use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <button>I am</button>
///         <button style="visibility: hidden;">Blue</button>
///     </div>"#,
/// );
/// // Without this line, the last assert will panic as css rules won't be applied.
/// body().append_child(&div).unwrap();
///
/// assert!(div.get(&HasText("I am")).is_some());
/// assert!(div.get(&HasText("i am")).is_none());
/// assert!(div.get(&HasText("Blue")).is_none());
///
/// body().remove_child(&div).unwrap();
/// ```
/// [`inner_text`]: web_sys::HtmlElement::inner_text
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HasText<'a>(pub &'a str);

impl<'a> Matcher for HasText<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        elem.inner_text().contains(self.0) && {
            let children_len = elem.children().length();
            !(0..children_len)
                .filter_map(|n| elem.children().item(n))
                .filter_map(|child| child.dyn_into::<HtmlElement>().ok())
                .any(|child| child.inner_text().contains(self.0))
        }
    }
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_has_text() {
    use crate::query::{HasText, Query};
    use gloo::utils::{body, document};
    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <button>I am</button>
            <button style="visibility: hidden;">Blue</button>
        </div>"#,
    );
    // Without this line, the last assert will panic as css rules won't be applied.
    body().append_child(&div).unwrap();

    assert!(div.get(&HasText("I am")).is_some());
    assert!(div.get(&HasText("i am")).is_none());
    assert!(div.get(&HasText("Blue")).is_none());

    body().remove_child(&div).unwrap();
}

/// Matches components that have given aria role.
///
/// This is by far the best method for finding components as it searches for elements in the [`accessibility tree`].
/// You should always prefer something like `.get(&HasRole("button").and(HasText("Add")))` over the alternavies.
/// Currently only supports user assigned roles and semantic tag to role deduction with [`element_to_aria_roles`].
/// It currently doesn't support any of [`aria_attribute_types`] or implicit role deduction.
/// Support for those is planned as much as it can be at this age of project.
///
/// # Example:
///
/// ```no_run
/// # use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <button>Rust</button>
///         <input type="button">Is</input>
///         <div role="button">Fun</input>
///     </div>"#,
/// );
/// body().append_child(&div).unwrap();
///
/// assert_eq!(div.get_all(&HasRole("button")).len(), 3);
///
/// body().remove_child(&div).unwrap();
/// ```
/// [`accessibility_tree`]: https://developer.mozilla.org/en-US/docs/Glossary/Accessibility_tree
/// [`aria_attribute_types`]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes#aria_attribute_types
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HasRole<'a>(pub &'a str);

impl<'a> Matcher for HasRole<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        if element_to_aria_roles(elem).contains(&self.0) {
            true
        } else if let Some(role) = elem.get_attribute("role") {
            role == self.0
        } else {
            false
        }
    }
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_has_role() {
    use crate::query::{HasRole, Query};
    use gloo::utils::{body, document};
    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <button>Rust</button>
            <input type="button">Is</input>
            <div role="button">Fun</input>
        </div>"#,
    );
    body().append_child(&div).unwrap();

    assert_eq!(div.get_all(&HasRole("button")).len(), 3);

    body().remove_child(&div).unwrap();
}

/// Matches components that have given label.
///
/// This is also a great method for interacting with DOM in the way as a user would.
/// Labels are not only visually connected with elements but also programatically.
/// Screen readers will read out the labels when given component is selected as well as
/// clicking on a label results in selecting labeled component.
///
/// [`Labeling'] is supported for input elements (except type="hidden"), button, meter,
/// output, progress, select and text area.
///
/// # Example:
///
/// ```no_run
/// # use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <!-- for id attributes -->
///         <label for="best-language">Type rust</label>
///         <input id="best-language" />
///
///         <!-- implicit labels -->
///         <label>Type rust <meter /></label>
///
///         <!-- wrapped implicit labels -->
///         <label>
///           <span>Type rust</span>
///           <button />
///         </label>
///
///         <!-- aria-labelledby attributes -->
///         <label id="best-language">Type rust</label>
///         <input aria-labelledby="best-language" />
///
///         <!-- aria-label attributes are not supported as they are not visible to user -->
///         <input aria-label="Type rust" />
///     </div>"#,
/// );
/// body().append_child(&div).unwrap();
///
/// assert_eq!(div.get_all(&HasLabel("Type rust")).len(), 4);
///
/// body().remove_child(&div).unwrap();
/// ```
/// [`Labeling`]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HasLabel<'a>(pub &'a str);

impl<'a> Matcher for HasLabel<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        // Check if element is one of types that support labeling
        // and if so, extract labels
        // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
        let labels = if let Some(elem) = elem.dyn_ref::<HtmlInputElement>() {
            // input type="hidden" doesn't support labels
            if elem.type_() == "hidden" {
                return false;
            }
            elem.labels().unwrap()
        } else if let Some(elem) = elem.dyn_ref::<HtmlButtonElement>() {
            elem.labels()
        } else if let Some(elem) = elem.dyn_ref::<HtmlMeterElement>() {
            elem.labels()
        } else if let Some(elem) = elem.dyn_ref::<HtmlOutputElement>() {
            elem.labels()
        } else if let Some(elem) = elem.dyn_ref::<HtmlProgressElement>() {
            elem.labels()
        } else if let Some(elem) = elem.dyn_ref::<HtmlSelectElement>() {
            elem.labels()
        } else if let Some(elem) = elem.dyn_ref::<HtmlTextAreaElement>() {
            elem.labels()
        } else {
            return false;
        };
        // Check if element is labeled by requested label
        if (0..labels.length())
            .filter_map(|idx| labels.get(idx))
            .any(|label| label.text_content().as_deref() == Some(self.0))
        {
            return true;
        }
        // Check if element is implicitly wrapped with label
        if let Some(parent) = elem.parent_element() {
            if let Some(label) = parent.dyn_ref::<HtmlLabelElement>() {
                let child_nodes = label.child_nodes();
                if (0..child_nodes.length())
                    .filter_map(|idx| child_nodes.get(idx))
                    .filter(|child| Some(elem) != child.dyn_ref())
                    .any(|child| child.text_content().as_deref().map(str::trim) == Some(self.0))
                {
                    return true;
                }
            }
        }
        // Check if element is aria-labelledby a label
        if let Some(label) = elem.get_attribute("aria-labelledby") {
            if document().get_element_by_id(&label).is_some() {
                return true;
            }
        }
        false
    }
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_has_label() {
    use crate::query::{HasLabel, Query};
    use gloo::utils::{body, document};

    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <!-- for id attributes -->
            <label for="best-language">Type rust</label>
            <input id="best-language" />

            <!-- implicit labels -->
            <label>Type rust <meter /></label>

            <!-- wrapped implicit labels -->
            <label>
              <span>Type rust</span>
              <button />
            </label>

            <!-- aria-labelledby attributes -->
            <label id="best-language">Type rust</label>
            <input aria-labelledby="best-language" />

            <!-- aria-label attributes are not supported as they are not visible to user -->
            <input aria-label="Type rust" />
        </div>"#,
    );
    body().append_child(&div).unwrap();

    assert_eq!(div.get_all(&HasLabel("Type rust")).len(), 4);

    body().remove_child(&div).unwrap();
}

/// Matches components that have given placeholder text.
///
/// Placeholders are not a substitute for labels. If placeholder is the only identifier
/// for an input, any assistive technology will not be able to identify them.
/// It is still a better fallback than just using [`HasText`] for accessible elements.
///
/// # Example:
///
/// ```no_run
/// # use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <button>tests rocks</button>
///         <input placeholder="tests rocks" />
///     </div>"#,
/// );
/// body().append_child(&div).unwrap();
///
/// assert_eq!(
///     div.get(&HasPlaceholder("tests")).unwrap().tag_name(),
///     "INPUT"
/// );
///
/// body().remove_child(&div).unwrap();
/// ```
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HasPlaceholder<'a>(pub &'a str);

impl<'a> Matcher for HasPlaceholder<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        let placeholder = if let Some(elem) = elem.dyn_ref::<HtmlInputElement>() {
            elem.placeholder()
        } else if let Some(elem) = elem.dyn_ref::<HtmlTextAreaElement>() {
            elem.placeholder()
        } else {
            return false;
        };
        placeholder.contains(self.0)
    }
}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_has_placeholder() {
    use crate::query::{HasPlaceholder, Query};
    use gloo::utils::{body, document};

    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <button>tests rocks</button>
            <input placeholder="tests rocks" />
        </div>"#,
    );
    body().append_child(&div).unwrap();

    assert_eq!(
        div.get(&HasPlaceholder("tests")).unwrap().tag_name(),
        "INPUT"
    );

    body().remove_child(&div).unwrap();
}

/// A trait for joining multiple matchers.
///
/// It is automatically implemented for all matchers.
/// It allows for joining matchers using `or` and `and` methods that consume both matchers
/// and returns a joined matcher. It can be chained with multiple calls.
///
/// # Example:
/// ```no_run
/// use gloo::utils::{body, document};
/// use frontest::prelude::*;
///
/// let div = document().create_element("div").unwrap();
/// div.set_inner_html(
///     r#"<div>
///         <button>I eat cookies</button>
///     </div>"#,
/// );
/// body().append_child(&div).unwrap();
///
/// assert!(div
///     .get(
///         &HasRole("button")
///             .and(HasText("bananas").or(HasText("apples")))
///             .or(HasText("cookies"))
///     )
///     .is_some());
///
/// body().remove_child(&div).unwrap();
/// ```
pub trait Joinable {
    /// Join two matchers by applying logical `and` operation.
    fn and<'a, 'b, M>(self, other: M) -> And<'b>
    where
        'a: 'b,
        Self: Sized + Matcher + 'a,
        M: Matcher + 'a,
    {
        And {
            filters: [Box::new(self), Box::new(other)],
        }
    }

    /// Join two matchers by applying logical `or` operation.
    fn or<'a, 'b, M>(self, other: M) -> Or<'b>
    where
        'a: 'b,
        Self: Sized + Matcher + 'a,
        M: Matcher + 'a,
    {
        Or {
            filters: [Box::new(self), Box::new(other)],
        }
    }
}

impl<M> Joinable for M where M: Matcher {}

#[cfg(test)]
#[wasm_bindgen_test::wasm_bindgen_test]
async fn doctest_joinable() {
    use crate::query::{HasRole, HasText, Joinable, Query};
    use gloo::utils::{body, document};
    let div = document().create_element("div").unwrap();
    div.set_inner_html(
        r#"<div>
            <button>I eat cookies</button>
        </div>"#,
    );
    body().append_child(&div).unwrap();

    assert!(div
        .get(
            &HasRole("button")
                .and(HasText("bananas").or(HasText("apples")))
                .or(HasText("cookies"))
        )
        .is_some());

    body().remove_child(&div).unwrap();
}

/// Result of joining two [`Matcher`]s by applyng a logical [`and`] operation on them.
///
/// [`and`]: Joinable::and
pub struct And<'a> {
    filters: [Box<dyn Matcher + 'a>; 2],
}

impl<'a> Matcher for And<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        self.filters.iter().all(|f| f.matches(elem))
    }
}

/// Result of combining two [`Matcher`]s by applyng a logical [`or`] operation on them.
///
/// [`or`]: Joinable::or
pub struct Or<'a> {
    filters: [Box<dyn Matcher + 'a>; 2],
}

impl<'a> Matcher for Or<'a> {
    fn matches(&self, elem: &HtmlElement) -> bool {
        self.filters.iter().any(|f| f.matches(elem))
    }
}

/// Allows selecting [`HtmlElement`]s using [`Matcher`]s.
///
/// By default implemented for [`Element`] where it selects it's children matching provided pattern.
pub trait Query {
    /// Tries to get a unique component. Returns [`None`] on failure and [`HtmlElement`] on success.
    ///
    /// # Panics:
    /// If more than one element is found.
    fn get<M: Matcher>(&self, rules: &M) -> Option<HtmlElement>;

    /// Returns a [`Vec`] of all components matched by a [`Matcher`].
    fn get_all<M: Matcher>(&self, rules: &M) -> Vec<HtmlElement>;
}

impl Query for Element {
    fn get<M: Matcher>(&self, matcher: &M) -> Option<HtmlElement> {
        let selected = self.query_selector_all("*").unwrap();
        // Get all nodes matching given text
        let mut preprocessed = (0..selected.length())
            .filter_map(|idx| selected.get(idx))
            .filter_map(|node| node.dyn_into::<HtmlElement>().ok())
            .filter(|e| matcher.matches(e))
            .collect::<Vec<_>>();

        match preprocessed.len() {
            0 => None,
            1 => Some(preprocessed.pop().unwrap()),
            _ => panic!("Found more than one element."),
        }
    }

    fn get_all<M: Matcher>(&self, matcher: &M) -> Vec<HtmlElement> {
        let selected = self.query_selector_all("*").unwrap();
        // Get all nodes matching given text
        (0..selected.length())
            .filter_map(|idx| selected.get(idx))
            .filter_map(|node| node.dyn_into::<HtmlElement>().ok())
            .filter(|e| matcher.matches(e))
            .collect::<Vec<_>>()
    }
}