diff --git a/Libraries/LibWeb/Page/Frame.cpp b/Libraries/LibWeb/Page/Frame.cpp index e81e9722b6..6db661242c 100644 --- a/Libraries/LibWeb/Page/Frame.cpp +++ b/Libraries/LibWeb/Page/Frame.cpp @@ -26,7 +26,9 @@ #include #include +#include #include +#include #include #include #include @@ -180,7 +182,7 @@ Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position) return position; } -void Frame::set_cursor_position(const DOM::Position & position) +void Frame::set_cursor_position(const DOM::Position& position) { if (m_cursor_position == position) return; @@ -196,4 +198,51 @@ void Frame::set_cursor_position(const DOM::Position & position) dbg() << "Cursor position: " << m_cursor_position; } +String Frame::selected_text() const +{ + StringBuilder builder; + if (!m_document) + return {}; + auto* layout_root = m_document->layout_node(); + if (!layout_root) + return {}; + if (!layout_root->selection().is_valid()) + return {}; + + auto selection = layout_root->selection().normalized(); + + if (selection.start().layout_node == selection.end().layout_node) { + if (!is(*selection.start().layout_node)) + return ""; + return downcast(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node + 1); + } + + // Start node + auto layout_node = selection.start().layout_node; + if (is(*layout_node)) { + auto& text = downcast(*layout_node).text_for_rendering(); + builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node)); + } + + // Middle nodes + layout_node = layout_node->next_in_pre_order(); + while (layout_node && layout_node != selection.end().layout_node) { + if (is(*layout_node)) + builder.append(downcast(*layout_node).text_for_rendering()); + else if (is(*layout_node) || is(*layout_node)) + builder.append('\n'); + + layout_node = layout_node->next_in_pre_order(); + } + + // End node + ASSERT(layout_node == selection.end().layout_node); + if (is(*layout_node)) { + auto& text = downcast(*layout_node).text_for_rendering(); + builder.append(text.substring(0, selection.end().index_in_node + 1)); + } + + return builder.to_string(); +} + } diff --git a/Libraries/LibWeb/Page/Frame.h b/Libraries/LibWeb/Page/Frame.h index 8ee452a29e..4be5139ee1 100644 --- a/Libraries/LibWeb/Page/Frame.h +++ b/Libraries/LibWeb/Page/Frame.h @@ -91,6 +91,8 @@ public: bool cursor_blink_state() const { return m_cursor_blink_state; } + String selected_text() const; + private: explicit Frame(DOM::Element& host_element, Frame& main_frame); explicit Frame(Page&); diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index b4f6e34a30..8cab744523 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -115,47 +115,8 @@ void PageView::select_all() String PageView::selected_text() const { - StringBuilder builder; - auto* layout_root = this->layout_root(); - if (!layout_root) - return {}; - if (!layout_root->selection().is_valid()) - return {}; - - auto selection = layout_root->selection().normalized(); - - if (selection.start().layout_node == selection.end().layout_node) { - if (!is(*selection.start().layout_node)) - return ""; - return downcast(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node + 1); - } - - // Start node - auto layout_node = selection.start().layout_node; - if (is(*layout_node)) { - auto& text = downcast(*layout_node).text_for_rendering(); - builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node)); - } - - // Middle nodes - layout_node = layout_node->next_in_pre_order(); - while (layout_node && layout_node != selection.end().layout_node) { - if (is(*layout_node)) - builder.append(downcast(*layout_node).text_for_rendering()); - else if (is(*layout_node) || is(*layout_node)) - builder.append('\n'); - - layout_node = layout_node->next_in_pre_order(); - } - - // End node - ASSERT(layout_node == selection.end().layout_node); - if (is(*layout_node)) { - auto& text = downcast(*layout_node).text_for_rendering(); - builder.append(text.substring(0, selection.end().index_in_node + 1)); - } - - return builder.to_string(); + // FIXME: Use focused frame + return page().main_frame().selected_text(); } void PageView::page_did_layout()