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
//! Utility functions for the DOM. Requires `dom` feature.

use web_sys::{
    css, wasm_bindgen::JsCast, window, CssStyleDeclaration, Document, Element, HtmlElement, Node,
    ShadowRoot, Window,
};

use crate::ElementOrWindow;

#[derive(Clone, Debug)]
pub enum DomNodeOrWindow<'a> {
    Node(&'a Node),
    Window(&'a Window),
}

impl<'a> From<&'a Node> for DomNodeOrWindow<'a> {
    fn from(value: &'a Node) -> Self {
        DomNodeOrWindow::Node(value)
    }
}

impl<'a> From<&'a Element> for DomNodeOrWindow<'a> {
    fn from(value: &'a Element) -> Self {
        DomNodeOrWindow::Node(value)
    }
}

impl<'a> From<&'a Window> for DomNodeOrWindow<'a> {
    fn from(value: &'a Window) -> Self {
        DomNodeOrWindow::Window(value)
    }
}

impl<'a> From<ElementOrWindow<'a, Element, Window>> for DomNodeOrWindow<'a> {
    fn from(value: ElementOrWindow<'a, Element, Window>) -> Self {
        match value {
            ElementOrWindow::Element(element) => DomNodeOrWindow::Node(element),
            ElementOrWindow::Window(window) => DomNodeOrWindow::Window(window),
        }
    }
}

impl<'a> From<&ElementOrWindow<'a, Element, Window>> for DomNodeOrWindow<'a> {
    fn from(value: &ElementOrWindow<'a, Element, Window>) -> Self {
        match value {
            ElementOrWindow::Element(element) => DomNodeOrWindow::Node(element),
            ElementOrWindow::Window(window) => DomNodeOrWindow::Window(window),
        }
    }
}

impl<'a> From<&'a DomElementOrWindow<'a>> for DomNodeOrWindow<'a> {
    fn from(value: &'a DomElementOrWindow) -> Self {
        match value {
            DomElementOrWindow::Element(element) => DomNodeOrWindow::Node(element),
            DomElementOrWindow::Window(window) => DomNodeOrWindow::Window(window),
        }
    }
}

#[derive(Clone, Debug)]
pub enum DomElementOrWindow<'a> {
    Element(&'a Element),
    Window(&'a Window),
}

impl<'a> From<&'a Element> for DomElementOrWindow<'a> {
    fn from(value: &'a Element) -> Self {
        DomElementOrWindow::Element(value)
    }
}

impl<'a> From<&'a Window> for DomElementOrWindow<'a> {
    fn from(value: &'a Window) -> Self {
        DomElementOrWindow::Window(value)
    }
}

impl<'a> From<ElementOrWindow<'a, Element, Window>> for DomElementOrWindow<'a> {
    fn from(value: ElementOrWindow<'a, Element, Window>) -> Self {
        match value {
            ElementOrWindow::Element(element) => DomElementOrWindow::Element(element),
            ElementOrWindow::Window(window) => DomElementOrWindow::Window(window),
        }
    }
}

impl<'a> From<&ElementOrWindow<'a, Element, Window>> for DomElementOrWindow<'a> {
    fn from(value: &ElementOrWindow<'a, Element, Window>) -> Self {
        match value {
            ElementOrWindow::Element(element) => DomElementOrWindow::Element(element),
            ElementOrWindow::Window(window) => DomElementOrWindow::Window(window),
        }
    }
}

pub fn get_node_name(node_or_window: DomNodeOrWindow) -> String {
    match node_or_window {
        DomNodeOrWindow::Node(node) => node.node_name().to_lowercase(),
        DomNodeOrWindow::Window(_) => "#document".into(),
    }
}

pub fn get_window(node: Option<&Node>) -> Window {
    match node {
        Some(node) => match node.owner_document() {
            Some(document) => document.default_view(),
            None => window(),
        },
        None => window(),
    }
    .expect("Window should exist.")
}

pub fn get_document_element(node_or_window: Option<DomNodeOrWindow>) -> Element {
    let document = match node_or_window {
        Some(DomNodeOrWindow::Node(node)) => node.owner_document(),
        Some(DomNodeOrWindow::Window(window)) => window.document(),
        None => get_window(None).document(),
    }
    .expect("Node or window should have document.");

    document
        .document_element()
        .expect("Document should have document element.")
}

pub fn is_html_element(node: &Node) -> bool {
    node.is_instance_of::<HtmlElement>()
}

const OVERFLOW_VALUES: [&str; 5] = ["auto", "scroll", "overlay", "hidden", "clip"];
const DISPLAY_VALUES: [&str; 2] = ["inline", "contents"];

pub fn is_overflow_element(element: &Element) -> bool {
    let style = get_computed_style(element);
    let overflow = style.get_property_value("overflow").unwrap_or("".into());
    let overflow_x = style.get_property_value("overflow-x").unwrap_or("".into());
    let overflow_y = style.get_property_value("overflow-y").unwrap_or("".into());
    let display = style.get_property_value("display").unwrap_or("".into());

    let overflow_combined = format!("{}{}{}", overflow, overflow_x, overflow_y);

    OVERFLOW_VALUES
        .into_iter()
        .any(|s| overflow_combined.contains(s))
        && !DISPLAY_VALUES.into_iter().any(|s| display == s)
}

pub fn is_table_element(element: &Element) -> bool {
    let node_name = get_node_name(element.into());
    ["table", "td", "th"].into_iter().any(|s| node_name == s)
}

const WILL_CHANGE_VALUES: [&str; 3] = ["transform", "perspective", "filter"];
const CONTAIN_VALUES: [&str; 4] = ["paint", "layout", "strict", "content"];

pub fn is_containing_block(element: &Element) -> bool {
    let webkit = is_web_kit();
    let css = get_computed_style(element);

    css.get_property_value("transform").unwrap_or("none".into()) != "none"
        || css
            .get_property_value("perspective")
            .unwrap_or("none".into())
            != "none"
        || css
            .get_property_value("container-type")
            .map(|value| value != "normal")
            .unwrap_or(false)
        || (!webkit
            && css
                .get_property_value("backdrop-filter")
                .map(|value| value != "none")
                .unwrap_or(false))
        || (!webkit
            && css
                .get_property_value("filter")
                .map(|value| value != "none")
                .unwrap_or(false))
        || css
            .get_property_value("will-change")
            .map(|value| WILL_CHANGE_VALUES.into_iter().any(|v| v == value))
            .unwrap_or(false)
        || css
            .get_property_value("contain")
            .map(|value| CONTAIN_VALUES.into_iter().any(|v| v == value))
            .unwrap_or(false)
}

pub fn get_containing_block(element: &Element) -> Option<HtmlElement> {
    let mut current_node = get_parent_node(element);

    while !is_last_traversable_node(&current_node) {
        if let Ok(element) = current_node.dyn_into::<HtmlElement>() {
            if is_containing_block(&element) {
                return Some(element);
            }

            current_node = get_parent_node(&element);
        } else {
            break;
        }
    }

    None
}

pub fn is_web_kit() -> bool {
    css::supports_with_value("-webkit-backdrop-filter", "none").unwrap_or(false)
}

pub fn is_last_traversable_node(node: &Node) -> bool {
    let node_name = get_node_name(node.into());
    ["html", "body", "#document"]
        .into_iter()
        .any(|s| node_name == s)
}

pub fn get_computed_style(element: &Element) -> CssStyleDeclaration {
    get_window(Some(element))
        .get_computed_style(element)
        .expect("Valid element.")
        .expect("Element should have computed style.")
}

#[derive(Clone, Debug)]
pub struct NodeScroll {
    pub scroll_left: f64,
    pub scroll_top: f64,
}

impl NodeScroll {
    pub fn new(value: f64) -> Self {
        Self {
            scroll_left: value,
            scroll_top: value,
        }
    }
}

pub fn get_node_scroll(element_or_window: DomElementOrWindow) -> NodeScroll {
    match element_or_window {
        DomElementOrWindow::Element(element) => NodeScroll {
            scroll_left: element.scroll_left() as f64,
            scroll_top: element.scroll_top() as f64,
        },
        DomElementOrWindow::Window(window) => NodeScroll {
            scroll_left: window
                .page_x_offset()
                .expect("Window should have page x offset."),
            scroll_top: window
                .page_y_offset()
                .expect("Window should have page y offset."),
        },
    }
}

pub fn get_parent_node(node: &Node) -> Node {
    if get_node_name(node.into()) == "html" {
        return node.clone();
    }

    let element = node.dyn_ref::<Element>();

    let result: Node;
    if let Some(slot) = element.and_then(|element| element.assigned_slot()) {
        // Step into the shadow DOM of the parent of a slotted node.
        result = slot.into();
    } else if let Some(parent_node) = node.parent_node() {
        // DOM Element detected.
        result = parent_node;
    } else if let Some(shadow_root) = node.dyn_ref::<ShadowRoot>() {
        // ShadowRoot detected.
        result = shadow_root.host().into();
    } else {
        // Fallback.
        result = get_document_element(Some(node.into())).into();
    }

    match node.dyn_ref::<ShadowRoot>() {
        Some(shadow_root) => shadow_root.host().into(),
        None => result,
    }
}

pub fn get_nearest_overflow_ancestor(node: &Node) -> HtmlElement {
    let parent_node = get_parent_node(node);

    if is_last_traversable_node(&parent_node) {
        node.owner_document()
            .as_ref()
            .or(node.dyn_ref::<Document>())
            .expect("Node should be document or have owner document.")
            .body()
            .expect("Document should have body.")
    } else if is_html_element(&parent_node)
        && is_overflow_element(parent_node.unchecked_ref::<Element>())
    {
        parent_node.unchecked_into()
    } else {
        get_nearest_overflow_ancestor(&parent_node)
    }
}

#[derive(Clone, Debug)]
pub enum OverflowAncestor {
    Element(Element),
    Window(Window),
    // TODO
    // VisualViewport(VisualViewport)
}

pub fn get_overflow_ancestors(
    node: &Node,
    mut list: Vec<OverflowAncestor>,
    traverse_iframe: bool,
) -> Vec<OverflowAncestor> {
    let scrollable_ancestor = get_nearest_overflow_ancestor(node);
    let is_body = node
        .owner_document()
        .and_then(|document| document.body())
        .is_some_and(|body| scrollable_ancestor == body);
    let window = get_window(Some(&scrollable_ancestor));

    if is_body {
        let frame_element = window
            .frame_element()
            .expect("Window should have frame element option.");

        list.push(OverflowAncestor::Window(window));
        // TODO: visual viewport

        if is_overflow_element(&scrollable_ancestor) {
            list.push(OverflowAncestor::Element(scrollable_ancestor.into()));
        }

        if let Some(frame_element) = frame_element {
            if traverse_iframe {
                list.append(&mut get_overflow_ancestors(&frame_element, vec![], true))
            }
        }

        list
    } else {
        let mut other_list = get_overflow_ancestors(&scrollable_ancestor, vec![], traverse_iframe);

        list.push(OverflowAncestor::Element(scrollable_ancestor.into()));
        list.append(&mut other_list);

        list
    }
}