LibWeb: Implement Selection.collapse_to_end

This commit is contained in:
Rafał Babiarz 2022-12-09 22:09:03 +01:00 committed by Andreas Kling
parent bb5da1b4e2
commit 227d860d6b
2 changed files with 17 additions and 3 deletions

View file

@ -219,9 +219,23 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_start()
}
// https://w3c.github.io/selection-api/#dom-selection-collapsetoend
void Selection::collapse_to_end()
WebIDL::ExceptionOr<void> 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

View file

@ -40,7 +40,7 @@ public:
WebIDL::ExceptionOr<void> collapse(JS::GCPtr<DOM::Node>, unsigned offset);
WebIDL::ExceptionOr<void> set_position(JS::GCPtr<DOM::Node>, unsigned offset);
WebIDL::ExceptionOr<void> collapse_to_start();
void collapse_to_end();
WebIDL::ExceptionOr<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);
WebIDL::ExceptionOr<void> select_all_children(JS::NonnullGCPtr<DOM::Node>);