Просмотр исходного кода

LibWeb: Implement Selection.collapse

Rafał Babiarz 2 лет назад
Родитель
Сommit
6d46ebfe8a

+ 28 - 5
Userland/Libraries/LibWeb/Selection/Selection.cpp

@@ -162,17 +162,40 @@ void Selection::empty()
 }
 
 // https://w3c.github.io/selection-api/#dom-selection-collapse
-void Selection::collapse(JS::GCPtr<DOM::Node>, unsigned offset)
+WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigned offset)
 {
-    (void)offset;
-    TODO();
+    // 1. If node is null, this method must behave identically as removeAllRanges() and abort these steps.
+    if (!node) {
+        remove_all_ranges();
+        return {};
+    }
+
+    // 2. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
+    if (offset > node->length()) {
+        return WebIDL::IndexSizeError::create(realm(), "Selection.collapse() with offset longer than node's length"sv);
+    }
+
+    // 3. If node's root is not the document associated with this, abort these steps.
+    if (&node->root() != m_document.ptr())
+        return {};
+
+    // 4. Otherwise, let newRange be a new range.
+    auto new_range = DOM::Range::create(*m_document);
+
+    // 5. Set the start the start and the end of newRange to (node, offset).
+    TRY(new_range->set_start(*node, offset));
+
+    // 6. Set this's range to newRange.
+    m_range = new_range;
+
+    return {};
 }
 
 // https://w3c.github.io/selection-api/#dom-selection-setposition
-void Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
+WebIDL::ExceptionOr<void> Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
 {
     // The method must be an alias, and behave identically, to collapse().
-    collapse(node, offset);
+    return collapse(node, offset);
 }
 
 // https://w3c.github.io/selection-api/#dom-selection-collapsetostart

+ 2 - 2
Userland/Libraries/LibWeb/Selection/Selection.h

@@ -37,8 +37,8 @@ public:
     WebIDL::ExceptionOr<void> remove_range(JS::NonnullGCPtr<DOM::Range>);
     void remove_all_ranges();
     void empty();
-    void collapse(JS::GCPtr<DOM::Node>, unsigned offset);
-    void set_position(JS::GCPtr<DOM::Node>, unsigned offset);
+    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();
     void collapse_to_end();
     WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset);