diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-12-09 19:00:18 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-10 00:21:10 +0000 |
commit | 919aa4501780283062ffe10d1f45dd473feb6081 (patch) | |
tree | 34e8b114fa399cd8afd8ebce4d5ba8accabe9f91 | |
parent | b47aceb480fff5aa6cab7f23553d6fa39840b577 (diff) | |
download | serenity-919aa4501780283062ffe10d1f45dd473feb6081.zip |
LibWeb: Implement Selection.setBaseAndExtent
-rw-r--r-- | Userland/Libraries/LibWeb/Selection/Selection.cpp | 47 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Selection/Selection.h | 2 |
2 files changed, 42 insertions, 7 deletions
diff --git a/Userland/Libraries/LibWeb/Selection/Selection.cpp b/Userland/Libraries/LibWeb/Selection/Selection.cpp index 8f6cc66a58..8bbbff5a25 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.cpp +++ b/Userland/Libraries/LibWeb/Selection/Selection.cpp @@ -237,13 +237,48 @@ WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, un } // https://w3c.github.io/selection-api/#dom-selection-setbaseandextent -void Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_offset) +WebIDL::ExceptionOr<void> Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_offset) { - (void)anchor_node; - (void)anchor_offset; - (void)focus_node; - (void)focus_offset; - TODO(); + // 1. If anchorOffset is longer than anchorNode's length or if focusOffset is longer than focusNode's length, throw an IndexSizeError exception and abort these steps. + if (anchor_offset > anchor_node->length()) + return WebIDL::IndexSizeError::create(realm(), "Anchor offset points outside of the anchor node"); + + if (focus_offset > focus_node->length()) + return WebIDL::IndexSizeError::create(realm(), "Focus offset points outside of the focus node"); + + // 2. If the roots of anchorNode or focusNode are not the document associated with this, abort these steps. + if (&anchor_node->root() != m_document.ptr()) + return {}; + + if (&focus_node->root() != m_document.ptr()) + return {}; + + // 3. Let anchor be the boundary point (anchorNode, anchorOffset) and let focus be the boundary point (focusNode, focusOffset). + + // 4. Let newRange be a new range. + auto new_range = DOM::Range::create(*m_document); + + // 5. If anchor is before focus, set the start the newRange's start to anchor and its end to focus. Otherwise, set the start them to focus and anchor respectively. + auto position_of_anchor_relative_to_focus = DOM::position_of_boundary_point_relative_to_other_boundary_point(anchor_node, anchor_offset, focus_node, focus_offset); + if (position_of_anchor_relative_to_focus == DOM::RelativeBoundaryPointPosition::Before) { + TRY(new_range->set_start(anchor_node, anchor_offset)); + TRY(new_range->set_end(focus_node, focus_offset)); + } else { + TRY(new_range->set_start(focus_node, focus_offset)); + TRY(new_range->set_end(anchor_node, anchor_offset)); + } + + // 6. Set this's range to newRange. + m_range = new_range; + + // 7. If focus is before anchor, set this's direction to backwards. Otherwise, set it to forwards + // NOTE: "Otherwise" can be seen as "focus is equal to or after anchor". + if (position_of_anchor_relative_to_focus == DOM::RelativeBoundaryPointPosition::After) + m_direction = Direction::Backwards; + else + m_direction = Direction::Forwards; + + return {}; } // https://w3c.github.io/selection-api/#dom-selection-selectallchildren diff --git a/Userland/Libraries/LibWeb/Selection/Selection.h b/Userland/Libraries/LibWeb/Selection/Selection.h index fbccc7e780..a0d89b1932 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.h +++ b/Userland/Libraries/LibWeb/Selection/Selection.h @@ -42,7 +42,7 @@ public: void collapse_to_start(); void collapse_to_end(); WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset); - void set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_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); WebIDL::ExceptionOr<void> select_all_children(JS::NonnullGCPtr<DOM::Node>); WebIDL::ExceptionOr<void> delete_from_document(); |