diff options
author | Rafał Babiarz <5783815+Sauler@users.noreply.github.com> | 2022-12-09 22:08:16 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-10 01:45:21 +0100 |
commit | bb5da1b4e28f26b84828ec14d751ce3f18e9295d (patch) | |
tree | deba07749876ac882b3ecfe50b202802ae509244 /Userland/Libraries/LibWeb/Selection | |
parent | 6d46ebfe8a81a5e7cf7f082f7650799b02b8a056 (diff) | |
download | serenity-bb5da1b4e28f26b84828ec14d751ce3f18e9295d.zip |
LibWeb: Implement Selection.collapse_to_start
Diffstat (limited to 'Userland/Libraries/LibWeb/Selection')
-rw-r--r-- | Userland/Libraries/LibWeb/Selection/Selection.cpp | 18 | ||||
-rw-r--r-- | 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 955e93193d..0913b47cee 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.cpp +++ b/Userland/Libraries/LibWeb/Selection/Selection.cpp @@ -199,9 +199,23 @@ WebIDL::ExceptionOr<void> Selection::set_position(JS::GCPtr<DOM::Node> node, uns } // https://w3c.github.io/selection-api/#dom-selection-collapsetostart -void Selection::collapse_to_start() +WebIDL::ExceptionOr<void> Selection::collapse_to_start() { - TODO(); + // 1. The method must throw InvalidStateError exception if the this is empty. + if (!m_range) { + return WebIDL::InvalidStateError::create(realm(), "Selection.collapse_to_start() 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->start_offset())); + TRY(new_range->set_end(*anchor_node(), m_range->start_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-collapsetoend diff --git a/Userland/Libraries/LibWeb/Selection/Selection.h b/Userland/Libraries/LibWeb/Selection/Selection.h index 077a95f4cd..87c1390768 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.h +++ b/Userland/Libraries/LibWeb/Selection/Selection.h @@ -39,7 +39,7 @@ public: void empty(); WebIDL::ExceptionOr<void> collapse(JS::GCPtr<DOM::Node>, unsigned offset); WebIDL::ExceptionOr<void> set_position(JS::GCPtr<DOM::Node>, unsigned offset); - void collapse_to_start(); + WebIDL::ExceptionOr<void> collapse_to_start(); void collapse_to_end(); WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset); WebIDL::ExceptionOr<void> set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_offset); |