mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:47:44 +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
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue