mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:37:34 +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
|
@ -136,7 +136,7 @@ void Selection::add_range(JS::NonnullGCPtr<DOM::Range> range)
|
|||
return;
|
||||
|
||||
// 3. Set this's range to range by a strong reference (not by making a copy).
|
||||
m_range = range;
|
||||
set_range(range);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/selection-api/#dom-selection-removerange
|
||||
|
@ -144,7 +144,7 @@ WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> r
|
|||
{
|
||||
// The method must make this empty by disassociating its range if this's range is range.
|
||||
if (m_range == range) {
|
||||
m_range = nullptr;
|
||||
set_range(nullptr);
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,7 @@ WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> r
|
|||
void Selection::remove_all_ranges()
|
||||
{
|
||||
// The method must make this empty by disassociating its range if this has an associated range.
|
||||
m_range = nullptr;
|
||||
set_range(nullptr);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/selection-api/#dom-selection-empty
|
||||
|
@ -191,7 +191,7 @@ WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigne
|
|||
TRY(new_range->set_start(*node, offset));
|
||||
|
||||
// 6. Set this's range to newRange.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
@ -219,7 +219,7 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_start()
|
|||
TRY(new_range->set_end(*anchor_node(), m_range->start_offset()));
|
||||
|
||||
// 4. Then set this's range to the newly-created range.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -239,7 +239,8 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_end()
|
|||
TRY(new_range->set_end(*anchor_node(), m_range->end_offset()));
|
||||
|
||||
// 4. Then set this's range to the newly-created range.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -280,7 +281,7 @@ WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, un
|
|||
}
|
||||
|
||||
// 8. Set this's range to newRange.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
|
||||
// 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
|
||||
if (new_focus_node->is_before(old_anchor_node)) {
|
||||
|
@ -325,7 +326,7 @@ WebIDL::ExceptionOr<void> Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::N
|
|||
}
|
||||
|
||||
// 6. Set this's range to newRange.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
|
||||
// 7. If focus is before anchor, set this's direction to backwards. Otherwise, set it to forwards
|
||||
// NOTE: "Otherwise" can be seen as "focus is equal to or after anchor".
|
||||
|
@ -355,7 +356,7 @@ WebIDL::ExceptionOr<void> Selection::select_all_children(JS::NonnullGCPtr<DOM::N
|
|||
TRY(new_range->set_end(node, child_count));
|
||||
|
||||
// 5. Set this's range to newRange.
|
||||
m_range = new_range;
|
||||
set_range(new_range);
|
||||
|
||||
// 6. Set this's direction to forwards.
|
||||
m_direction = Direction::Forwards;
|
||||
|
@ -429,9 +430,28 @@ DeprecatedString Selection::to_deprecated_string() const
|
|||
return m_range->to_deprecated_string();
|
||||
}
|
||||
|
||||
JS::NonnullGCPtr<DOM::Document> Selection::document() const
|
||||
{
|
||||
return m_document;
|
||||
}
|
||||
|
||||
JS::GCPtr<DOM::Range> Selection::range() const
|
||||
{
|
||||
return m_range;
|
||||
}
|
||||
|
||||
void Selection::set_range(JS::GCPtr<DOM::Range> range)
|
||||
{
|
||||
if (m_range == range)
|
||||
return;
|
||||
|
||||
if (m_range)
|
||||
m_range->set_associated_selection({}, nullptr);
|
||||
|
||||
m_range = range;
|
||||
|
||||
if (m_range)
|
||||
m_range->set_associated_selection({}, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue