Skip to main content

strata/runtime/
hit_test.rs

1//! Spatial hit-testing for pointer events, clicks, cursors, and CSS properties.
2
3use super::*;
4
5#[derive(Clone, Copy, Debug)]
6pub(crate) struct HitClipStep {
7    pub clip: Option<strata_layout::Rect>,
8    pub is_abs_cb: bool,
9    pub is_fixed_cb: bool,
10}
11
12impl AppRuntime {
13    /// Unprojects a world pointer coordinate into an element's local coordinate system if transformed.
14    #[inline]
15    pub fn unproject_point(layout: &LayoutNode, px: f32, py: f32) -> (f32, f32) {
16        if layout.style.has_transform() {
17            let mat = layout.style.compute_transform_matrix(
18                layout.geometry.border_box.width,
19                layout.geometry.border_box.height,
20            );
21            if let Some(inv) = mat.inverse() {
22                let lx = px - layout.geometry.border_box.x;
23                let ly = py - layout.geometry.border_box.y;
24                let (un_lx, un_ly) = inv.transform_point(lx, ly);
25                (
26                    layout.geometry.border_box.x + un_lx,
27                    layout.geometry.border_box.y + un_ly,
28                )
29            } else {
30                (px, py)
31            }
32        } else {
33            (px, py)
34        }
35    }
36
37    /// Ancestor clip step tracked along the DOM path so `position: fixed` and `position: absolute`
38    /// elements escape clips from ancestors that do not contain their CSS containing block.
39    #[inline]
40    pub(crate) fn hit_clip_step_for(layout: &LayoutNode) -> HitClipStep {
41        let is_fixed_cb = layout.style.establishes_fixed_containing_block();
42        let is_abs_cb =
43            layout.style.position != strata_style::types::Position::Static || is_fixed_cb;
44        let clips = layout.style.overflow_x != strata_style::types::Overflow::Visible
45            || layout.style.overflow_y != strata_style::types::Overflow::Visible;
46        HitClipStep {
47            clip: if clips {
48                Some(layout.geometry.border_box)
49            } else {
50                None
51            },
52            is_abs_cb,
53            is_fixed_cb,
54        }
55    }
56
57    #[inline]
58    pub(crate) fn effective_hit_clip_steps<'a>(
59        node: &LayoutNode,
60        steps: &'a [HitClipStep],
61    ) -> &'a [HitClipStep] {
62        match node.style.position {
63            strata_style::types::Position::Fixed => {
64                if let Some(last_idx) = steps.iter().rposition(|s| s.is_fixed_cb) {
65                    &steps[..=last_idx]
66                } else {
67                    &[]
68                }
69            }
70            strata_style::types::Position::Absolute => {
71                if let Some(last_idx) = steps.iter().rposition(|s| s.is_abs_cb) {
72                    &steps[..=last_idx]
73                } else {
74                    &[]
75                }
76            }
77            _ => steps,
78        }
79    }
80
81    #[inline]
82    pub(crate) fn hit_clip_steps_allow(steps: &[HitClipStep], px: f32, py: f32) -> bool {
83        steps
84            .iter()
85            .all(|s| s.clip.is_none_or(|r| r.contains_point(px, py)))
86    }
87
88    /// Returns true if `node` establishes an atomic stacking context for paint and hit-testing.
89    #[inline]
90    pub fn establishes_stacking_context(node: &LayoutNode, is_root: bool) -> bool {
91        node.establishes_stacking_context_in(None, is_root)
92    }
93
94    /// Returns true if `node` establishes an atomic stacking context given `parent_display`.
95    #[inline]
96    pub fn establishes_stacking_context_in(
97        node: &LayoutNode,
98        parent_display: Option<strata_style::types::Display>,
99        is_root: bool,
100    ) -> bool {
101        node.establishes_stacking_context_in(parent_display, is_root)
102    }
103
104    /// Pairs flattened blueprint children with `node_layout.children`, preserving flattened child indices
105    /// and resolving keyed `EntityId` children when reordered.
106    pub(crate) fn paired_blueprint_children<'a>(
107        node_bp: &'a NodeBlueprint,
108        node_layout: &'a LayoutNode,
109    ) -> Vec<(usize, &'a NodeBlueprint, &'a LayoutNode)> {
110        let mut flat_children = Vec::new();
111        match node_bp {
112            NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
113                for c in children {
114                    Self::flatten_blueprints(c, &mut flat_children);
115                }
116            }
117            _ => {}
118        }
119
120        let mut out = Vec::with_capacity(node_layout.children.len());
121        for (idx, child_layout) in node_layout.children.iter().enumerate() {
122            let resolved_bp = if child_layout.entity_id.is_valid() {
123                if let Some(&cand) = flat_children.get(idx) {
124                    if cand.entity_id() == Some(child_layout.entity_id) {
125                        Some(cand)
126                    } else {
127                        node_bp
128                            .find_by_entity_id(child_layout.entity_id)
129                            .or(Some(cand))
130                    }
131                } else {
132                    node_bp.find_by_entity_id(child_layout.entity_id)
133                }
134            } else {
135                flat_children.get(idx).copied()
136            };
137
138            if let Some(child_bp) = resolved_bp {
139                out.push((idx, child_bp, child_layout));
140            }
141        }
142        out
143    }
144
145    fn collect_top_layer_portals<'a>(
146        bp: &'a NodeBlueprint,
147        layout: &'a LayoutNode,
148        out: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode)>,
149        depth: usize,
150    ) {
151        if depth >= 256 {
152            return;
153        }
154        for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
155            if child_layout.style.display == strata_style::types::Display::None {
156                continue;
157            }
158            if child_layout.is_top_layer {
159                out.push((child_bp, child_layout));
160            }
161            if !child_layout.children.is_empty() {
162                Self::collect_top_layer_portals(child_bp, child_layout, out, depth + 1);
163            }
164        }
165    }
166
167    fn collect_hoisted_stacking_participants<'a>(
168        bp: &'a NodeBlueprint,
169        layout: &'a LayoutNode,
170        steps: &[HitClipStep],
171        negative_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, Vec<HitClipStep>)>,
172        auto_or_zero_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, bool, Vec<HitClipStep>)>,
173        positive_z: &mut Vec<(&'a NodeBlueprint, &'a LayoutNode, Vec<HitClipStep>)>,
174        depth: usize,
175    ) {
176        if depth >= 256 {
177            return;
178        }
179        let parent_display = Some(layout.style.display);
180        for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
181            if child_layout.style.display == strata_style::types::Display::None
182                || child_layout.is_top_layer
183            {
184                continue;
185            }
186            if Self::establishes_stacking_context_in(child_layout, parent_display, false) {
187                let eff_steps = Self::effective_hit_clip_steps(child_layout, steps).to_vec();
188                let z = child_layout.style.z_index.unwrap_or(0);
189                if z < 0 {
190                    negative_z.push((child_bp, child_layout, eff_steps));
191                } else if z > 0 {
192                    positive_z.push((child_bp, child_layout, eff_steps));
193                } else {
194                    auto_or_zero_z.push((child_bp, child_layout, true, eff_steps));
195                }
196            } else {
197                let eff_for_child = Self::effective_hit_clip_steps(child_layout, steps);
198                if child_layout.style.position != strata_style::types::Position::Static {
199                    auto_or_zero_z.push((child_bp, child_layout, false, eff_for_child.to_vec()));
200                }
201                let mut child_steps = Vec::with_capacity(eff_for_child.len() + 1);
202                child_steps.extend_from_slice(eff_for_child);
203                let step = Self::hit_clip_step_for(child_layout);
204                if step.clip.is_some() || step.is_abs_cb || step.is_fixed_cb {
205                    child_steps.push(step);
206                }
207                Self::collect_hoisted_stacking_participants(
208                    child_bp,
209                    child_layout,
210                    &child_steps,
211                    negative_z,
212                    auto_or_zero_z,
213                    positive_z,
214                    depth + 1,
215                );
216            }
217        }
218    }
219
220    fn hit_test_direct_in_flow_children<'a, T, F>(
221        bp: &'a NodeBlueprint,
222        layout: &'a LayoutNode,
223        px: f32,
224        py: f32,
225        depth: usize,
226        test_self: &mut F,
227    ) -> Option<T>
228    where
229        F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
230    {
231        let paired = Self::paired_blueprint_children(bp, layout);
232        if paired.is_empty() {
233            return None;
234        }
235
236        let parent_display = Some(layout.style.display);
237        let is_in_flow_non_sc = |c: &LayoutNode| {
238            c.style.display != strata_style::types::Display::None
239                && !c.is_top_layer
240                && !Self::establishes_stacking_context_in(c, parent_display, false)
241                && c.style.position == strata_style::types::Position::Static
242        };
243        let is_inline_level = |c: &LayoutNode| {
244            matches!(
245                c.style.display,
246                strata_style::types::Display::Inline
247                    | strata_style::types::Display::InlineBlock
248                    | strata_style::types::Display::InlineFlex
249                    | strata_style::types::Display::InlineGrid
250            )
251        };
252
253        // Layer 5: In-flow inline-level descendants (front-to-back)
254        for &(_idx, child_bp, child_layout) in paired.iter().rev() {
255            if is_in_flow_non_sc(child_layout)
256                && child_layout.style.float == strata_style::types::Float::None
257                && is_inline_level(child_layout)
258            {
259                if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
260                    child_bp,
261                    child_layout,
262                    px,
263                    py,
264                    &[],
265                    depth + 1,
266                    test_self,
267                ) {
268                    return Some(hit);
269                }
270            }
271        }
272
273        // Layer 4: Non-positioned floats (front-to-back)
274        for &(_idx, child_bp, child_layout) in paired.iter().rev() {
275            if is_in_flow_non_sc(child_layout)
276                && child_layout.style.float != strata_style::types::Float::None
277            {
278                if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
279                    child_bp,
280                    child_layout,
281                    px,
282                    py,
283                    &[],
284                    depth + 1,
285                    test_self,
286                ) {
287                    return Some(hit);
288                }
289            }
290        }
291
292        // Layer 3: In-flow block-level descendants (front-to-back)
293        for &(_idx, child_bp, child_layout) in paired.iter().rev() {
294            if is_in_flow_non_sc(child_layout)
295                && child_layout.style.float == strata_style::types::Float::None
296                && !is_inline_level(child_layout)
297            {
298                if let Some(hit) = Self::hit_test_in_flow_non_hoisted(
299                    child_bp,
300                    child_layout,
301                    px,
302                    py,
303                    &[],
304                    depth + 1,
305                    test_self,
306                ) {
307                    return Some(hit);
308                }
309            }
310        }
311
312        None
313    }
314
315    fn hit_test_in_flow_non_hoisted<'a, T, F>(
316        bp: &'a NodeBlueprint,
317        layout: &'a LayoutNode,
318        px: f32,
319        py: f32,
320        inherited_steps: &[HitClipStep],
321        depth: usize,
322        test_self: &mut F,
323    ) -> Option<T>
324    where
325        F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
326    {
327        if depth >= 256 || layout.style.display == strata_style::types::Display::None {
328            return None;
329        }
330        let eff_inherited = Self::effective_hit_clip_steps(layout, inherited_steps);
331        if !Self::hit_clip_steps_allow(eff_inherited, px, py) {
332            return None;
333        }
334        if (layout.style.overflow_x != strata_style::types::Overflow::Visible
335            || layout.style.overflow_y != strata_style::types::Overflow::Visible)
336            && !layout.geometry.border_box.contains_point(px, py)
337        {
338            return None;
339        }
340        if let Some(hit) =
341            Self::hit_test_direct_in_flow_children(bp, layout, px, py, depth, test_self)
342        {
343            return Some(hit);
344        }
345        test_self(bp, layout, px, py)
346    }
347
348    pub(crate) fn is_portal_backdrop_bp(bp: &NodeBlueprint) -> bool {
349        if let NodeBlueprint::Element { attributes, .. } = bp {
350            attributes.iter().any(|a| {
351                matches!(
352                    a,
353                    strata_dom::Attribute::Property { key, value }
354                        if key == "data-portal-backdrop" && value == "true"
355                )
356            })
357        } else {
358            false
359        }
360    }
361
362    pub(crate) fn top_layer_portal_node_is_visual_content(
363        bp: &NodeBlueprint,
364        layout: &LayoutNode,
365        px: f32,
366        py: f32,
367        depth: usize,
368    ) -> bool {
369        if depth >= 256
370            || layout.style.display == strata_style::types::Display::None
371            || layout.style.visibility == strata_style::types::Visibility::Hidden
372            || layout.style.pointer_events == strata_style::types::PointerEvents::None
373        {
374            return false;
375        }
376        if Self::is_portal_backdrop_bp(bp) {
377            return false;
378        }
379        let (px, py) = Self::unproject_point(layout, px, py);
380        let in_box = layout.geometry.border_box.contains_point(px, py);
381        if in_box {
382            for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
383                if Self::top_layer_portal_node_is_visual_content(
384                    child_bp,
385                    child_layout,
386                    px,
387                    py,
388                    depth + 1,
389                ) {
390                    return true;
391                }
392            }
393            if depth > 0
394                || layout.style.background_color.is_some_and(|c| c.a > 0)
395                || !layout.style.box_shadow.is_empty()
396            {
397                return true;
398            }
399        } else {
400            for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(bp, layout) {
401                if Self::top_layer_portal_node_is_visual_content(
402                    child_bp,
403                    child_layout,
404                    px,
405                    py,
406                    depth + 1,
407                ) {
408                    return true;
409                }
410            }
411        }
412        false
413    }
414
415    pub(crate) fn point_hits_non_backdrop_top_layer_portal(&self, px: f32, py: f32) -> bool {
416        let mut top_layer_portals = Vec::new();
417        Self::collect_top_layer_portals(
418            &self.root_blueprint,
419            &self.layout_root,
420            &mut top_layer_portals,
421            0,
422        );
423        top_layer_portals.into_iter().rev().any(|(p_bp, p_layout)| {
424            !Self::is_portal_backdrop_bp(p_bp)
425                && Self::top_layer_portal_node_is_visual_content(p_bp, p_layout, px, py, 0)
426        })
427    }
428
429    fn hit_test_stacking_context_with<'a, T, F>(
430        bp: &'a NodeBlueprint,
431        layout: &'a LayoutNode,
432        px: f32,
433        py: f32,
434        inherited_steps: &[HitClipStep],
435        depth: usize,
436        test_self: &mut F,
437    ) -> Option<T>
438    where
439        F: FnMut(&'a NodeBlueprint, &'a LayoutNode, f32, f32) -> Option<T>,
440    {
441        if depth >= 256 || layout.style.display == strata_style::types::Display::None {
442            return None;
443        }
444
445        if depth == 0 {
446            let mut top_layer_portals = Vec::new();
447            Self::collect_top_layer_portals(bp, layout, &mut top_layer_portals, 0);
448            let mut blocked_backdrops = false;
449            for &(p_bp, p_layout) in top_layer_portals.iter().rev() {
450                let is_backdrop = Self::is_portal_backdrop_bp(p_bp);
451                if blocked_backdrops && is_backdrop {
452                    continue;
453                }
454                if let Some(hit) =
455                    Self::hit_test_stacking_context_with(p_bp, p_layout, px, py, &[], 1, test_self)
456                {
457                    return Some(hit);
458                }
459                if !is_backdrop
460                    && Self::top_layer_portal_node_is_visual_content(p_bp, p_layout, px, py, 0)
461                {
462                    blocked_backdrops = true;
463                }
464            }
465        }
466
467        let eff_inherited = Self::effective_hit_clip_steps(layout, inherited_steps);
468        let inherited_allows = Self::hit_clip_steps_allow(eff_inherited, px, py);
469
470        let (px, py) = Self::unproject_point(layout, px, py);
471
472        let own_clips = layout.style.overflow_x != strata_style::types::Overflow::Visible
473            || layout.style.overflow_y != strata_style::types::Overflow::Visible;
474        let own_allows = !own_clips || layout.geometry.border_box.contains_point(px, py);
475
476        let mut base_steps = Vec::with_capacity(eff_inherited.len() + 1);
477        base_steps.extend_from_slice(eff_inherited);
478        let own_step = Self::hit_clip_step_for(layout);
479        if own_step.clip.is_some() || own_step.is_abs_cb || own_step.is_fixed_cb {
480            base_steps.push(own_step);
481        }
482
483        let mut negative_z = Vec::new();
484        let mut auto_or_zero_z = Vec::new();
485        let mut positive_z = Vec::new();
486        Self::collect_hoisted_stacking_participants(
487            bp,
488            layout,
489            &base_steps,
490            &mut negative_z,
491            &mut auto_or_zero_z,
492            &mut positive_z,
493            depth + 1,
494        );
495
496        negative_z.sort_by_key(|(_, l, _)| l.style.z_index.unwrap_or(0));
497        positive_z.sort_by_key(|(_, l, _)| l.style.z_index.unwrap_or(0));
498
499        // Layer 7: Child stacking contexts with positive z-index (highest z-index first, reverse DOM on ties)
500        for (c_bp, c_layout, c_steps) in positive_z.iter().rev() {
501            if let Some(hit) = Self::hit_test_stacking_context_with(
502                c_bp,
503                c_layout,
504                px,
505                py,
506                c_steps,
507                depth + 1,
508                test_self,
509            ) {
510                return Some(hit);
511            }
512        }
513
514        // Layer 6: Positioned descendants with z-index: auto and child stacking contexts with z-index: 0
515        for (c_bp, c_layout, is_sc, c_steps) in auto_or_zero_z.iter().rev() {
516            let hit = if *is_sc {
517                Self::hit_test_stacking_context_with(
518                    c_bp,
519                    c_layout,
520                    px,
521                    py,
522                    c_steps,
523                    depth + 1,
524                    test_self,
525                )
526            } else {
527                Self::hit_test_in_flow_non_hoisted(
528                    c_bp,
529                    c_layout,
530                    px,
531                    py,
532                    c_steps,
533                    depth + 1,
534                    test_self,
535                )
536            };
537            if hit.is_some() {
538                return hit;
539            }
540        }
541
542        // Layers 5, 4, 3: In-flow inline, float, and block descendants
543        if inherited_allows && own_allows {
544            if let Some(hit) =
545                Self::hit_test_direct_in_flow_children(bp, layout, px, py, depth, test_self)
546            {
547                return Some(hit);
548            }
549        }
550
551        // Layer 2: Child stacking contexts with negative z-index (closest to 0 first, reverse DOM on ties)
552        for (c_bp, c_layout, c_steps) in negative_z.iter().rev() {
553            if let Some(hit) = Self::hit_test_stacking_context_with(
554                c_bp,
555                c_layout,
556                px,
557                py,
558                c_steps,
559                depth + 1,
560                test_self,
561            ) {
562                return Some(hit);
563            }
564        }
565
566        // Layer 1: Stacking context root element itself
567        if inherited_allows {
568            test_self(bp, layout, px, py)
569        } else {
570            None
571        }
572    }
573
574    /// Recursively hit tests a coordinate point against the blueprint and computed layout hierarchy,
575    /// returning the nearest listener registered for `event_type`, its element border box, and its `EntityId`.
576    pub fn hit_test_event_with_box_and_entity<'a>(
577        blueprint: &'a NodeBlueprint,
578        layout: &'a LayoutNode,
579        px: f32,
580        py: f32,
581        event_type: strata_dom::EventType,
582    ) -> Option<(
583        std::rc::Rc<dyn Fn(strata_dom::Event)>,
584        &'a strata_layout::Rect,
585        strata_primitives::EntityId,
586    )> {
587        Self::hit_test_stacking_context_with(
588            blueprint,
589            layout,
590            px,
591            py,
592            &[],
593            0,
594            &mut |bp, node_layout, lx, ly| {
595                if matches!(
596                    node_layout.style.visibility,
597                    strata_style::types::Visibility::Hidden
598                ) || matches!(
599                    node_layout.style.pointer_events,
600                    strata_style::types::PointerEvents::None
601                ) {
602                    return None;
603                }
604                if let NodeBlueprint::Element { attributes, .. } = bp {
605                    if node_layout.geometry.border_box.contains_point(lx, ly) {
606                        for attr in attributes {
607                            if let strata_dom::Attribute::EventListener { event: ev, handler } =
608                                attr
609                            {
610                                if *ev == event_type {
611                                    return Some((
612                                        handler.clone(),
613                                        &node_layout.geometry.border_box,
614                                        node_layout.entity_id,
615                                    ));
616                                }
617                            }
618                        }
619                    }
620                }
621                None
622            },
623        )
624    }
625
626    /// Recursively hit tests a coordinate point against the blueprint and computed layout hierarchy,
627    /// returning the nearest listener registered for `event_type` and its element border box.
628    pub fn hit_test_event_with_box<'a>(
629        blueprint: &'a NodeBlueprint,
630        layout: &'a LayoutNode,
631        px: f32,
632        py: f32,
633        event_type: strata_dom::EventType,
634    ) -> Option<(
635        std::rc::Rc<dyn Fn(strata_dom::Event)>,
636        &'a strata_layout::Rect,
637    )> {
638        Self::hit_test_event_with_box_and_entity(blueprint, layout, px, py, event_type)
639            .map(|(h, r, _)| (h, r))
640    }
641
642    /// Walks from `path` up toward the root to find the nearest ancestor (or self)
643    /// registering a listener for `event_type`, returning `(handler, border_box, entity_id)`.
644    pub fn find_event_at_or_above_path<'a>(
645        blueprint: &'a NodeBlueprint,
646        layout: &'a LayoutNode,
647        path: &[usize],
648        event_type: strata_dom::EventType,
649    ) -> Option<(
650        std::rc::Rc<dyn Fn(strata_dom::Event)>,
651        &'a strata_layout::Rect,
652        strata_primitives::EntityId,
653    )> {
654        for len in (0..=path.len()).rev() {
655            let prefix = &path[..len];
656            let Some(bp) = Self::find_blueprint_at_path(blueprint, prefix) else {
657                continue;
658            };
659            let mut node_layout = layout;
660            let mut valid = true;
661            for &idx in prefix {
662                if let Some(child) = node_layout.children.get(idx) {
663                    node_layout = child;
664                } else {
665                    valid = false;
666                    break;
667                }
668            }
669            if !valid {
670                continue;
671            }
672            if let NodeBlueprint::Element { attributes, .. } = bp {
673                for attr in attributes {
674                    if let strata_dom::Attribute::EventListener { event: ev, handler } = attr {
675                        if *ev == event_type {
676                            return Some((
677                                handler.clone(),
678                                &node_layout.geometry.border_box,
679                                node_layout.entity_id,
680                            ));
681                        }
682                    }
683                }
684            }
685        }
686        None
687    }
688
689    /// Recursively hit tests a coordinate point against the blueprint and computed layout hierarchy,
690    /// returning the nearest (deepest, front-most) listener registered for `event_type`.
691    pub fn hit_test_event(
692        blueprint: &NodeBlueprint,
693        layout: &LayoutNode,
694        px: f32,
695        py: f32,
696        event_type: strata_dom::EventType,
697    ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
698        Self::hit_test_event_with_box(blueprint, layout, px, py, event_type).map(|(h, _)| h)
699    }
700
701    /// Hit tests for the nearest `onclick` listener at a coordinate point.
702    pub fn hit_test_click(
703        blueprint: &NodeBlueprint,
704        layout: &LayoutNode,
705        px: f32,
706        py: f32,
707    ) -> Option<std::rc::Rc<dyn Fn(strata_dom::Event)>> {
708        Self::hit_test_event(blueprint, layout, px, py, strata_dom::EventType::Click)
709    }
710
711    /// Recursively hit tests a coordinate point against the blueprint and computed layout hierarchy to determine cursor style.
712    pub fn hit_test_cursor(
713        blueprint: &NodeBlueprint,
714        layout: &LayoutNode,
715        px: f32,
716        py: f32,
717    ) -> strata_platform::window::CursorIcon {
718        Self::hit_test_stacking_context_with(
719            blueprint,
720            layout,
721            px,
722            py,
723            &[],
724            0,
725            &mut |bp, node_layout, lx, ly| {
726                if matches!(
727                    node_layout.style.visibility,
728                    strata_style::types::Visibility::Hidden
729                ) || matches!(
730                    node_layout.style.pointer_events,
731                    strata_style::types::PointerEvents::None
732                ) {
733                    return None;
734                }
735                if let NodeBlueprint::Element {
736                    tag,
737                    classes,
738                    attributes,
739                    ..
740                } = bp
741                {
742                    if node_layout.geometry.border_box.contains_point(lx, ly) {
743                        match node_layout.style.cursor {
744                            strata_style::types::Cursor::Pointer => {
745                                return Some(strata_platform::window::CursorIcon::Pointer)
746                            }
747                            strata_style::types::Cursor::Text => {
748                                return Some(strata_platform::window::CursorIcon::Text)
749                            }
750                            strata_style::types::Cursor::Crosshair => {
751                                return Some(strata_platform::window::CursorIcon::Crosshair)
752                            }
753                            strata_style::types::Cursor::Move => {
754                                return Some(strata_platform::window::CursorIcon::Move)
755                            }
756                            strata_style::types::Cursor::NotAllowed => {
757                                return Some(strata_platform::window::CursorIcon::NotAllowed)
758                            }
759                            strata_style::types::Cursor::Wait => {
760                                return Some(strata_platform::window::CursorIcon::Wait)
761                            }
762                            strata_style::types::Cursor::Grab => {
763                                return Some(strata_platform::window::CursorIcon::Grab)
764                            }
765                            strata_style::types::Cursor::Grabbing => {
766                                return Some(strata_platform::window::CursorIcon::Grabbing)
767                            }
768                            strata_style::types::Cursor::ColResize => {
769                                return Some(strata_platform::window::CursorIcon::ColResize)
770                            }
771                            strata_style::types::Cursor::RowResize => {
772                                return Some(strata_platform::window::CursorIcon::RowResize)
773                            }
774                            strata_style::types::Cursor::Help => {
775                                return Some(strata_platform::window::CursorIcon::Help)
776                            }
777                            strata_style::types::Cursor::None => {
778                                return Some(strata_platform::window::CursorIcon::None)
779                            }
780                            strata_style::types::Cursor::Default => {}
781                        }
782
783                        let has_pointer = matches!(tag, Tag::Button | Tag::A)
784                            || classes.iter().any(|c| {
785                                c.split_whitespace()
786                                    .any(|tok| tok == "cursor-pointer" || tok == "pointer")
787                            })
788                            || attributes.iter().any(|a| {
789                                matches!(
790                                    a,
791                                    strata_dom::Attribute::EventListener {
792                                        event: strata_dom::EventType::Click,
793                                        ..
794                                    }
795                                )
796                            });
797
798                        if has_pointer {
799                            return Some(strata_platform::window::CursorIcon::Pointer);
800                        }
801
802                        let has_text_cursor = matches!(tag, Tag::Input | Tag::Textarea)
803                            || classes.iter().any(|c| {
804                                c.split_whitespace()
805                                    .any(|tok| tok == "cursor-text" || tok == "select-text")
806                            });
807
808                        if has_text_cursor {
809                            return Some(strata_platform::window::CursorIcon::Text);
810                        }
811                    }
812                }
813                None
814            },
815        )
816        .unwrap_or(strata_platform::window::CursorIcon::Default)
817    }
818
819    /// Finds the value of the deepest element property `key` whose border box contains the point.
820    /// Elements are tested front-to-back according to CSS stacking contexts so overlays win over what they cover.
821    pub fn hit_test_property(
822        blueprint: &NodeBlueprint,
823        layout: &LayoutNode,
824        px: f32,
825        py: f32,
826        key: &str,
827    ) -> Option<String> {
828        Self::hit_test_stacking_context_with(
829            blueprint,
830            layout,
831            px,
832            py,
833            &[],
834            0,
835            &mut |bp, node_layout, lx, ly| {
836                if matches!(
837                    node_layout.style.visibility,
838                    strata_style::types::Visibility::Hidden
839                ) || matches!(
840                    node_layout.style.pointer_events,
841                    strata_style::types::PointerEvents::None
842                ) {
843                    return None;
844                }
845                if let NodeBlueprint::Element { attributes, .. } = bp {
846                    if node_layout.geometry.border_box.contains_point(lx, ly) {
847                        for attr in attributes {
848                            if let strata_dom::Attribute::Property { key: k, value } = attr {
849                                if k == key {
850                                    return Some(value.clone());
851                                }
852                            }
853                        }
854                    }
855                }
856                None
857            },
858        )
859    }
860}