|
@@ -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;
|
|
|
|