From fadb14d31d0d3127e80121ac695fe839908efaa1 Mon Sep 17 00:00:00 2001 From: John Diamond Date: Sat, 12 Oct 2024 02:42:55 +0200 Subject: [PATCH] LibWeb: Compare anchor/focus offsets in selection.isCollapsed The "isCollapsed" attribute on a selection must "return true if and only if the anchor and focus are the same". In addition to checking that the anchor and focus belonged to the same DOM node, we now also check that they refer to the same position within the node. With this change Ladybird passes all the subtests in the "isCollapsed" WPT suite. https://wpt.live/selection/isCollapsed.html --- Tests/LibWeb/Text/expected/is-collapsed.txt | 1 + Tests/LibWeb/Text/input/is-collapsed.html | 27 +++++++++++++++++++ .../Libraries/LibWeb/Selection/Selection.cpp | 5 +++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/is-collapsed.txt create mode 100644 Tests/LibWeb/Text/input/is-collapsed.html diff --git a/Tests/LibWeb/Text/expected/is-collapsed.txt b/Tests/LibWeb/Text/expected/is-collapsed.txt new file mode 100644 index 00000000000..7ef22e9a431 --- /dev/null +++ b/Tests/LibWeb/Text/expected/is-collapsed.txt @@ -0,0 +1 @@ +PASS diff --git a/Tests/LibWeb/Text/input/is-collapsed.html b/Tests/LibWeb/Text/input/is-collapsed.html new file mode 100644 index 00000000000..5a12704953d --- /dev/null +++ b/Tests/LibWeb/Text/input/is-collapsed.html @@ -0,0 +1,27 @@ + +

Simply selectable

+ + diff --git a/Userland/Libraries/LibWeb/Selection/Selection.cpp b/Userland/Libraries/LibWeb/Selection/Selection.cpp index e783a1533df..741a674228a 100644 --- a/Userland/Libraries/LibWeb/Selection/Selection.cpp +++ b/Userland/Libraries/LibWeb/Selection/Selection.cpp @@ -96,7 +96,10 @@ bool Selection::is_collapsed() const { // The attribute must return true if and only if the anchor and focus are the same // (including if both are null). Otherwise it must return false. - return const_cast(this)->anchor_node() == const_cast(this)->focus_node(); + if (!m_range) + return true; + return const_cast(this)->anchor_node() == const_cast(this)->focus_node() + && m_range->start_offset() == m_range->end_offset(); } // https://w3c.github.io/selection-api/#dom-selection-rangecount