summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Selection
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibWeb/Selection')
-rw-r--r--Userland/Libraries/LibWeb/Selection/Selection.cpp38
-rw-r--r--Userland/Libraries/LibWeb/Selection/Selection.h5
2 files changed, 34 insertions, 9 deletions
diff --git a/Userland/Libraries/LibWeb/Selection/Selection.cpp b/Userland/Libraries/LibWeb/Selection/Selection.cpp
index 6305d5fec2..72aa62b0e6 100644
--- a/Userland/Libraries/LibWeb/Selection/Selection.cpp
+++ b/Userland/Libraries/LibWeb/Selection/Selection.cpp
@@ -136,7 +136,7 @@ void Selection::add_range(JS::NonnullGCPtr<DOM::Range> range)
return;
// 3. Set this's range to range by a strong reference (not by making a copy).
- m_range = range;
+ set_range(range);
}
// https://w3c.github.io/selection-api/#dom-selection-removerange
@@ -144,7 +144,7 @@ WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> r
{
// The method must make this empty by disassociating its range if this's range is range.
if (m_range == range) {
- m_range = nullptr;
+ set_range(nullptr);
return {};
}
@@ -156,7 +156,7 @@ WebIDL::ExceptionOr<void> Selection::remove_range(JS::NonnullGCPtr<DOM::Range> r
void Selection::remove_all_ranges()
{
// The method must make this empty by disassociating its range if this has an associated range.
- m_range = nullptr;
+ set_range(nullptr);
}
// https://w3c.github.io/selection-api/#dom-selection-empty
@@ -191,7 +191,7 @@ WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigne
TRY(new_range->set_start(*node, offset));
// 6. Set this's range to newRange.
- m_range = new_range;
+ set_range(new_range);
return {};
}
@@ -219,7 +219,7 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_start()
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;
+ set_range(new_range);
return {};
}
@@ -239,7 +239,8 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_end()
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;
+ set_range(new_range);
+
return {};
}
@@ -280,7 +281,7 @@ WebIDL::ExceptionOr<void> Selection::extend(JS::NonnullGCPtr<DOM::Node> node, un
}
// 8. Set this's range to newRange.
- m_range = new_range;
+ set_range(new_range);
// 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
if (new_focus_node->is_before(old_anchor_node)) {
@@ -325,7 +326,7 @@ WebIDL::ExceptionOr<void> Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::N
}
// 6. Set this's range to newRange.
- m_range = new_range;
+ set_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".
@@ -355,7 +356,7 @@ WebIDL::ExceptionOr<void> Selection::select_all_children(JS::NonnullGCPtr<DOM::N
TRY(new_range->set_end(node, child_count));
// 5. Set this's range to newRange.
- m_range = new_range;
+ set_range(new_range);
// 6. Set this's direction to forwards.
m_direction = Direction::Forwards;
@@ -429,9 +430,28 @@ DeprecatedString Selection::to_deprecated_string() const
return m_range->to_deprecated_string();
}
+JS::NonnullGCPtr<DOM::Document> Selection::document() const
+{
+ return m_document;
+}
+
JS::GCPtr<DOM::Range> Selection::range() const
{
return m_range;
}
+void Selection::set_range(JS::GCPtr<DOM::Range> range)
+{
+ if (m_range == range)
+ return;
+
+ if (m_range)
+ m_range->set_associated_selection({}, nullptr);
+
+ m_range = range;
+
+ if (m_range)
+ m_range->set_associated_selection({}, this);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/Selection/Selection.h b/Userland/Libraries/LibWeb/Selection/Selection.h
index a96a6e4fc1..581336e39b 100644
--- a/Userland/Libraries/LibWeb/Selection/Selection.h
+++ b/Userland/Libraries/LibWeb/Selection/Selection.h
@@ -53,6 +53,9 @@ public:
// Non-standard convenience accessor for the selection's range.
JS::GCPtr<DOM::Range> range() const;
+ // Non-standard accessor for the selection's document.
+ JS::NonnullGCPtr<DOM::Document> document() const;
+
private:
Selection(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
@@ -61,6 +64,8 @@ private:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
+ void set_range(JS::GCPtr<DOM::Range>);
+
// https://w3c.github.io/selection-api/#dfn-empty
JS::GCPtr<DOM::Range> m_range;