mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:57:35 +00:00
LibWeb: Implement hit testing a bit closer to spec
We now perform hit testing in reverse paint order as described in CSS2's section about the z-index property.
This commit is contained in:
parent
0401d7da36
commit
15ed0ebdc6
4 changed files with 74 additions and 26 deletions
|
@ -148,27 +148,81 @@ void StackingContext::paint(PaintContext& context)
|
|||
|
||||
HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
||||
{
|
||||
// NOTE: Hit testing basically happens in reverse painting order.
|
||||
// https://www.w3.org/TR/CSS22/visuren.html#z-index
|
||||
|
||||
// 7. the child stacking contexts with positive stack levels (least positive first).
|
||||
for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
|
||||
auto const& child = *m_children[i];
|
||||
if (child.m_box.computed_values().z_index().value_or(0) < 0)
|
||||
break;
|
||||
auto result = child.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return result;
|
||||
}
|
||||
|
||||
HitTestResult result;
|
||||
if (!is<InitialContainingBlock>(m_box)) {
|
||||
result = m_box.hit_test(position, type);
|
||||
} else {
|
||||
// NOTE: InitialContainingBlock::hit_test() merely calls StackingContext::hit_test()
|
||||
// so we call its base class instead.
|
||||
result = verify_cast<InitialContainingBlock>(m_box).BlockContainer::hit_test(position, type);
|
||||
// 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
|
||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||
if (box.is_positioned() && !box.stacking_context()) {
|
||||
result = box.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
if (result.layout_node)
|
||||
return result;
|
||||
|
||||
// 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
|
||||
if (m_box.children_are_inline() && is<BlockContainer>(m_box)) {
|
||||
auto result = m_box.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return result;
|
||||
}
|
||||
|
||||
int z_index = m_box.computed_values().z_index().value_or(0);
|
||||
// 4. the non-positioned floats.
|
||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||
if (box.is_floating()) {
|
||||
result = box.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
for (auto* child : m_children) {
|
||||
int child_z_index = child->m_box.computed_values().z_index().value_or(0);
|
||||
if (result.layout_node && (child_z_index < z_index))
|
||||
continue;
|
||||
|
||||
auto result_here = child->hit_test(position, type);
|
||||
if (result_here.layout_node)
|
||||
result = result_here;
|
||||
// 3. the in-flow, non-inline-level, non-positioned descendants.
|
||||
if (!m_box.children_are_inline()) {
|
||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||
if (!box.is_absolutely_positioned() && !box.is_floating()) {
|
||||
result = box.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
if (result.layout_node)
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
|
||||
// 2. the child stacking contexts with negative stack levels (most negative first).
|
||||
for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
|
||||
auto const& child = *m_children[i];
|
||||
if (child.m_box.computed_values().z_index().value_or(0) < 0)
|
||||
break;
|
||||
auto result = child.hit_test(position, type);
|
||||
if (result.layout_node)
|
||||
return result;
|
||||
}
|
||||
|
||||
// 1. the background and borders of the element forming the stacking context.
|
||||
if (m_box.absolute_border_box_rect().contains(position.to_type<float>())) {
|
||||
return HitTestResult {
|
||||
.layout_node = m_box,
|
||||
};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void StackingContext::dump(int indent) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue