1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +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:
Andreas Kling 2023-01-11 19:48:53 +01:00
parent 1c4328902d
commit b79bc25a1f
9 changed files with 161 additions and 71 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/Range.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Painting/PaintableBox.h>
@ -18,6 +19,11 @@ InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullR
InitialContainingBlock::~InitialContainingBlock() = default;
JS::GCPtr<Selection::Selection> InitialContainingBlock::selection() const
{
return const_cast<DOM::Document&>(document()).get_selection();
}
void InitialContainingBlock::build_stacking_context_tree_if_needed()
{
if (paint_box()->stacking_context())
@ -56,40 +62,52 @@ void InitialContainingBlock::paint_all_phases(PaintContext& context)
void InitialContainingBlock::recompute_selection_states()
{
SelectionState state = SelectionState::None;
auto selection = this->selection().normalized();
// 1. Start by resetting the selection state of all layout nodes to None.
for_each_in_inclusive_subtree([&](auto& layout_node) {
if (!selection.is_valid()) {
// Everything gets SelectionState::None.
} else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
state = SelectionState::StartAndEnd;
} else if (&layout_node == selection.start().layout_node) {
state = SelectionState::Start;
} else if (&layout_node == selection.end().layout_node) {
state = SelectionState::End;
} else {
if (state == SelectionState::Start)
state = SelectionState::Full;
else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
state = SelectionState::None;
}
layout_node.set_selection_state(state);
layout_node.set_selection_state(SelectionState::None);
return IterationDecision::Continue;
});
}
void InitialContainingBlock::set_selection(LayoutRange const& selection)
{
m_selection = selection;
recompute_selection_states();
}
// 2. If there is no active Selection or selected Range, return.
auto selection = document().get_selection();
if (!selection)
return;
auto range = selection->range();
if (!range)
return;
void InitialContainingBlock::set_selection_end(LayoutPosition const& position)
{
m_selection.set_end(position);
recompute_selection_states();
auto* start_container = range->start_container();
auto* end_container = range->end_container();
// 3. If the selection starts and ends in the same text node, mark it as StartAndEnd and return.
if (start_container == end_container && is<DOM::Text>(*start_container)) {
if (auto* layout_node = start_container->layout_node()) {
layout_node->set_selection_state(SelectionState::StartAndEnd);
}
return;
}
// 4. Mark the selection start node as Start (if text) or Full (if anything else).
if (auto* layout_node = start_container->layout_node()) {
if (is<DOM::Text>(*start_container))
layout_node->set_selection_state(SelectionState::Start);
else
layout_node->set_selection_state(SelectionState::Full);
}
// 5. Mark the selection end node as End (if text) or Full (if anything else).
if (auto* layout_node = end_container->layout_node()) {
if (is<DOM::Text>(*end_container))
layout_node->set_selection_state(SelectionState::End);
else
layout_node->set_selection_state(SelectionState::Full);
}
// 6. Mark the nodes between start node and end node (in tree order) as Full.
for (auto* node = start_container->next_in_pre_order(); node && node != end_container; node = node->next_in_pre_order()) {
if (auto* layout_node = node->layout_node())
layout_node->set_selection_state(SelectionState::Full);
}
}
}

View file

@ -9,6 +9,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/LayoutPosition.h>
#include <LibWeb/Selection/Selection.h>
namespace Web::Layout {
@ -23,9 +24,7 @@ public:
void paint_all_phases(PaintContext&);
LayoutRange const& selection() const { return m_selection; }
void set_selection(LayoutRange const&);
void set_selection_end(LayoutPosition const&);
JS::GCPtr<Selection::Selection> selection() const;
void build_stacking_context_tree_if_needed();
void recompute_selection_states();
@ -33,8 +32,6 @@ public:
private:
void build_stacking_context_tree();
virtual bool is_initial_containing_block_box() const override { return true; }
LayoutRange m_selection;
};
template<>

View file

@ -1,10 +1,11 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf8View.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/LayoutState.h>
#include <LibWeb/Layout/LineBoxFragment.h>
@ -73,28 +74,33 @@ CSSPixelRect LineBoxFragment::selection_rect(Gfx::Font const& font) const
if (layout_node().selection_state() == Node::SelectionState::Full)
return absolute_rect();
auto selection = layout_node().root().selection().normalized();
if (!selection.is_valid())
return {};
if (!is<TextNode>(layout_node()))
return {};
auto const start_index = m_start;
auto const end_index = m_start + m_length;
auto selection = layout_node().root().selection();
if (!selection)
return {};
auto range = selection->range();
if (!range)
return {};
// FIXME: m_start and m_length should be unsigned and then we won't need these casts.
auto const start_index = static_cast<unsigned>(m_start);
auto const end_index = static_cast<unsigned>(m_start) + static_cast<unsigned>(m_length);
auto text = this->text();
if (layout_node().selection_state() == Node::SelectionState::StartAndEnd) {
// we are in the start/end node (both the same)
if (start_index > selection.end().index_in_node)
if (start_index > range->end_offset())
return {};
if (end_index < selection.start().index_in_node)
if (end_index < range->start_offset())
return {};
if (selection.start().index_in_node == selection.end().index_in_node)
if (range->start_offset() == range->end_offset())
return {};
auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
auto selection_end_in_this_fragment = min(m_length, selection.end().index_in_node - m_start);
auto selection_start_in_this_fragment = max(0, range->start_offset() - m_start);
auto selection_end_in_this_fragment = min(m_length, range->end_offset() - m_start);
auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;
@ -106,10 +112,10 @@ CSSPixelRect LineBoxFragment::selection_rect(Gfx::Font const& font) const
}
if (layout_node().selection_state() == Node::SelectionState::Start) {
// we are in the start node
if (end_index < selection.start().index_in_node)
if (end_index < range->start_offset())
return {};
auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
auto selection_start_in_this_fragment = max(0, range->start_offset() - m_start);
auto selection_end_in_this_fragment = m_length;
auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;
@ -122,11 +128,11 @@ CSSPixelRect LineBoxFragment::selection_rect(Gfx::Font const& font) const
}
if (layout_node().selection_state() == Node::SelectionState::End) {
// we are in the end node
if (start_index > selection.end().index_in_node)
if (start_index > range->end_offset())
return {};
auto selection_start_in_this_fragment = 0;
auto selection_end_in_this_fragment = min(selection.end().index_in_node - m_start, m_length);
auto selection_end_in_this_fragment = min(range->end_offset() - m_start, m_length);
auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;