testing_library_dom/
suggestions.rs

1use web_sys::HtmlElement;
2
3use crate::{
4    types::{Method, Suggestion, Variant},
5    MatcherOptions,
6};
7
8fn make_suggestion(
9    query_name: Method,
10    _element: &HtmlElement,
11    content: String,
12    variant: Variant,
13    _name: Option<String>,
14) -> Suggestion {
15    let warning = None;
16
17    let query_matcher = content.into();
18    let query_options = MatcherOptions::default();
19
20    // if let Some(name) = name {
21    // query_options.name =
22    // }
23
24    // if query_name == Method::Role && is_inaccessible(element) {
25    // query_options.
26    // warning = Some();
27    // }
28
29    let query_method = format!("{variant}_by_{query_name}");
30
31    Suggestion {
32        query_name,
33        query_method,
34        query_matcher,
35        query_options,
36        variant,
37        warning,
38    }
39}
40
41fn can_suggest<T>(
42    current_method: Method,
43    requested_method: Option<Method>,
44    data: Option<T>,
45) -> Option<T> {
46    if requested_method.is_none()
47        || requested_method.is_some_and(|requested_method| requested_method == current_method)
48    {
49        data
50    } else {
51        None
52    }
53}
54
55pub fn get_suggested_query(
56    element: &HtmlElement,
57    variant: Option<Variant>,
58    method: Option<Method>,
59) -> Option<Suggestion> {
60    let variant = variant.unwrap_or(Variant::Get);
61
62    // TODO
63
64    let alt = element.get_attribute("alt");
65    if let Some(alt) = can_suggest(Method::AltText, method, alt) {
66        return Some(make_suggestion(
67            Method::AltText,
68            element,
69            alt,
70            variant,
71            None,
72        ));
73    }
74
75    None
76}