LibWeb: Don't update selection if start and end node roots differ

This allows selection to work within shadow roots and prevents the
selection being cleared if the mouse moves outside of the current
document or shadow root.
This commit is contained in:
Tim Ledbetter 2024-07-02 10:17:47 +01:00 committed by Andreas Kling
commit e5d1261640
Notes: sideshowbarker 2024-07-17 08:38:37 +09:00

View file

@ -546,15 +546,22 @@ bool EventHandler::handle_mousemove(CSSPixelPoint viewport_position, CSSPixelPoi
}
if (m_in_mouse_selection) {
auto hit = paint_root()->hit_test(position, Painting::HitTestType::TextCursor);
auto should_set_cursor_position = true;
if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
m_navigable->set_cursor_position(DOM::Position::create(realm, *hit->dom_node(), *start_index));
if (auto selection = document.get_selection()) {
auto anchor_node = selection->anchor_node();
if (anchor_node)
(void)selection->set_base_and_extent(*anchor_node, selection->anchor_offset(), *hit->paintable->dom_node(), hit->index_in_node);
else
if (anchor_node) {
if (&anchor_node->root() == &hit->dom_node()->root())
(void)selection->set_base_and_extent(*anchor_node, selection->anchor_offset(), *hit->paintable->dom_node(), hit->index_in_node);
else
should_set_cursor_position = false;
} else {
(void)selection->set_base_and_extent(*hit->paintable->dom_node(), hit->index_in_node, *hit->paintable->dom_node(), hit->index_in_node);
}
}
if (should_set_cursor_position)
m_navigable->set_cursor_position(DOM::Position::create(realm, *hit->dom_node(), *start_index));
document.navigable()->set_needs_display();
}
}