From 227d860d6badd838718c6f162699e73f5ad5d2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Babiarz?= <5783815+Sauler@users.noreply.github.com> Date: Fri, 9 Dec 2022 22:09:03 +0100 Subject: LibWeb: Implement Selection.collapse_to_end --- Userland/Libraries/LibWeb/Selection/Selection.cpp | 18 ++++++++++++++++-- Userland/Libraries/LibWeb/Selection/Selection.h | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/Selection/Selection.cpp b/Userland/Libraries/LibWeb/Selection/Selection.cpp index 0913b47cee..7aaacd4854 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.cpp +++ b/Userland/Libraries/LibWeb/Selection/Selection.cpp @@ -219,9 +219,23 @@ WebIDL::ExceptionOr Selection::collapse_to_start() } // https://w3c.github.io/selection-api/#dom-selection-collapsetoend -void Selection::collapse_to_end() +WebIDL::ExceptionOr Selection::collapse_to_end() { - TODO(); + // 1. The method must throw InvalidStateError exception if the this is empty. + if (!m_range) { + return WebIDL::InvalidStateError::create(realm(), "Selection.collapse_to_end() on empty range"sv); + } + + // 2. Otherwise, it must create a new range + auto new_range = DOM::Range::create(*m_document); + + // 3. Set the start both its start and end to the start of this's range + TRY(new_range->set_start(*anchor_node(), m_range->end_offset())); + 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; + return {}; } // https://w3c.github.io/selection-api/#dom-selection-extend diff --git a/Userland/Libraries/LibWeb/Selection/Selection.h b/Userland/Libraries/LibWeb/Selection/Selection.h index 87c1390768..db75a876c9 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.h +++ b/Userland/Libraries/LibWeb/Selection/Selection.h @@ -40,7 +40,7 @@ public: WebIDL::ExceptionOr collapse(JS::GCPtr, unsigned offset); WebIDL::ExceptionOr set_position(JS::GCPtr, unsigned offset); WebIDL::ExceptionOr collapse_to_start(); - void collapse_to_end(); + WebIDL::ExceptionOr collapse_to_end(); WebIDL::ExceptionOr extend(JS::NonnullGCPtr, unsigned offset); WebIDL::ExceptionOr set_base_and_extent(JS::NonnullGCPtr anchor_node, unsigned anchor_offset, JS::NonnullGCPtr focus_node, unsigned focus_offset); WebIDL::ExceptionOr select_all_children(JS::NonnullGCPtr); -- cgit v1.2.3