From 163b8a69e33977bb283f3db57b34aaa911937127 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Mon, 2 Dec 2024 14:41:07 +0000 Subject: [PATCH] LibWeb: Assert that nodes share shadow-including root in BP calculation Previously, it was assumed that nodes must share the same root, prior to the calculation of their relative boundary point positions. This is no longer the case, since `Selection.setBaseAndExtent()` now accepts anchor and focus nodes that may be in different shadow trees. --- Libraries/LibWeb/DOM/Range.cpp | 4 +- .../tentative/Selection-direction.txt | 13 ++++ .../tentative/Selection-direction.html | 66 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/selection/shadow-dom/tentative/Selection-direction.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/selection/shadow-dom/tentative/Selection-direction.html diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cpp index d51f2ad5432..aad4a0098db 100644 --- a/Libraries/LibWeb/DOM/Range.cpp +++ b/Libraries/LibWeb/DOM/Range.cpp @@ -120,7 +120,9 @@ GC::Ref Range::root() const RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(GC::Ref node_a, u32 offset_a, GC::Ref node_b, u32 offset_b) { // 1. Assert: nodeA and nodeB have the same root. - VERIFY(&node_a->root() == &node_b->root()); + // NOTE: Nodes may not share the same root if they belong to different shadow trees, + // so we assert that they share the same shadow-including root instead. + VERIFY(&node_a->shadow_including_root() == &node_b->shadow_including_root()); // 2. If nodeA is nodeB, then return equal if offsetA is offsetB, before if offsetA is less than offsetB, and after if offsetA is greater than offsetB. if (node_a == node_b) { diff --git a/Tests/LibWeb/Text/expected/wpt-import/selection/shadow-dom/tentative/Selection-direction.txt b/Tests/LibWeb/Text/expected/wpt-import/selection/shadow-dom/tentative/Selection-direction.txt new file mode 100644 index 00000000000..d6928d0e3ba --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/selection/shadow-dom/tentative/Selection-direction.txt @@ -0,0 +1,13 @@ +Harness status: OK + +Found 7 tests + +6 Pass +1 Fail +Pass direction returns "none" when there is no selection +Pass direction returns "forward" when there is a forward-direction selection in the document tree +Pass direction returns "backward" when there is a backward-direction selection in the document tree +Pass direction returns "forward" when there is a forward selection in the shadow tree +Pass direction returns "backward" when there is a backward selection in the shadow tree +Pass direction returns "forward" when there is a forward selection that crosses shadow boundaries +Fail direction returns "backward" when there is a forward selection that crosses shadow boundaries \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/selection/shadow-dom/tentative/Selection-direction.html b/Tests/LibWeb/Text/input/wpt-import/selection/shadow-dom/tentative/Selection-direction.html new file mode 100644 index 00000000000..2d8baf546cd --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/selection/shadow-dom/tentative/Selection-direction.html @@ -0,0 +1,66 @@ + + + + + + + + + + + +
+ + +