mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:38:12 +00:00
LibWeb: Use DOM Selection instead of ad-hoc layout tree selection
Before this patch, we were expressing the current selection as a range between two points in the layout tree. This was a made-up concept I called LayoutRange (2x LayoutPosition) and as it turns out, we don't actually need it! Instead, we can just use the Selection API from the Selection API spec. This API expresses selection in terms of the DOM, and we already had many of the building blocks implemented. To ensure that selections get visually updated when the underlying Range of an active Selection is programmatically manipulated, Range now has an "associated Selection". If a range is updated while associated with a selection, we recompute layout tree selection states and repaint the page to make it user-visible.
This commit is contained in:
parent
1c4328902d
commit
b79bc25a1f
9 changed files with 161 additions and 71 deletions
|
@ -17,6 +17,7 @@
|
|||
#include <LibWeb/DOM/Text.h>
|
||||
#include <LibWeb/Geometry/DOMRect.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/Layout/InitialContainingBlock.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
|
@ -71,6 +72,28 @@ void Range::initialize(JS::Realm& realm)
|
|||
set_prototype(&Bindings::ensure_web_prototype<Bindings::RangePrototype>(realm, "Range"));
|
||||
}
|
||||
|
||||
void Range::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_associated_selection);
|
||||
}
|
||||
|
||||
void Range::set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection> selection)
|
||||
{
|
||||
m_associated_selection = selection;
|
||||
update_associated_selection();
|
||||
}
|
||||
|
||||
void Range::update_associated_selection()
|
||||
{
|
||||
if (!m_associated_selection)
|
||||
return;
|
||||
if (auto* layout_root = m_associated_selection->document()->layout_node()) {
|
||||
layout_root->recompute_selection_states();
|
||||
layout_root->set_needs_display();
|
||||
}
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-range-root
|
||||
Node& Range::root()
|
||||
{
|
||||
|
@ -173,6 +196,7 @@ WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartO
|
|||
m_end_offset = offset;
|
||||
}
|
||||
|
||||
update_associated_selection();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -353,6 +377,7 @@ WebIDL::ExceptionOr<void> Range::select(Node& node)
|
|||
m_end_container = *parent;
|
||||
m_end_offset = index + 1;
|
||||
|
||||
update_associated_selection();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -370,11 +395,11 @@ void Range::collapse(bool to_start)
|
|||
if (to_start) {
|
||||
m_end_container = m_start_container;
|
||||
m_end_offset = m_start_offset;
|
||||
return;
|
||||
} else {
|
||||
m_start_container = m_end_container;
|
||||
m_start_offset = m_end_offset;
|
||||
}
|
||||
|
||||
m_start_container = m_end_container;
|
||||
m_start_offset = m_end_offset;
|
||||
update_associated_selection();
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-range-selectnodecontents
|
||||
|
@ -395,6 +420,7 @@ WebIDL::ExceptionOr<void> Range::select_node_contents(Node const& node)
|
|||
m_end_container = node;
|
||||
m_end_offset = length;
|
||||
|
||||
update_associated_selection();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue