From 57e20404dde0cc210bbedfaf1955bad34decb16b Mon Sep 17 00:00:00 2001 From: Di Zhang Date: Wed, 7 Aug 2024 19:31:45 -0700 Subject: [PATCH] [Selection API] Change getComposedRanges() from rest parameter to a dictionary Per https://github.com/w3c/selection-api/issues/176, we should update selection.getComposedRanges() from using a rest `shadowRoots` parameter to using a dictionary that contains the array `shadowRoots`. Change-Id: I6b5b3fed786cf6d75fa4a20c0b2a583635cd2aca Bug: 40286116 Fixed: 355577223 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5770701 Commit-Queue: Di Zhang Reviewed-by: Siye Liu Cr-Commit-Position: refs/heads/main@{#1338832} --- ...ge-isPointInRange-shadowdom.tentative.html | 3 +- ...ater-become-slotted-content.tentative.html | 2 +- ...lection-collapse-and-extend.tentative.html | 10 +++--- ...getComposedRanges-collapsed.tentative.html | 32 +++++++++---------- ...selection-getComposedRanges.tentative.html | 14 ++++---- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/dom/ranges/Range-isPointInRange-shadowdom.tentative.html b/dom/ranges/Range-isPointInRange-shadowdom.tentative.html index a90ddcf584785d..e6768693ff1c5f 100644 --- a/dom/ranges/Range-isPointInRange-shadowdom.tentative.html +++ b/dom/ranges/Range-isPointInRange-shadowdom.tentative.html @@ -25,7 +25,7 @@ window.getSelection().setBaseAndExtent(start.firstChild, 3, end.firstChild, 3); - const composedRange = window.getSelection().getComposedRanges(shadowRoot)[0]; + const composedRange = window.getSelection().getComposedRanges({ shadowRoots: [shadowRoot] })[0]; // Sanity check to make sure we have selected something across the shadow boundary. assert_true(composedRange.startContainer == start.firstChild); assert_true(composedRange.startOffset == 3); @@ -40,7 +40,6 @@ }, "isPointInRange() test for collapsed selection"); test(function() { - assert_implements(window.getSelection().getComposedRanges, "GetComposedRanges is not supported"); const start = document.getElementById("start"); const shadowRoot = document.getElementById("host").shadowRoot; diff --git a/selection/shadow-dom/select-later-become-slotted-content.tentative.html b/selection/shadow-dom/select-later-become-slotted-content.tentative.html index f38105dd0bb32e..d44930479d4d7c 100644 --- a/selection/shadow-dom/select-later-become-slotted-content.tentative.html +++ b/selection/shadow-dom/select-later-become-slotted-content.tentative.html @@ -17,7 +17,7 @@ assert_equals(sel.focusNode, outer.firstChild); assert_equals(sel.focusOffset, 2); - const composedRange = sel.getComposedRanges(host.shadowRoot)[0]; + const composedRange = sel.getComposedRanges({ shadowRoots: [host.shadowRoot] })[0]; assert_equals(composedRange.startContainer, slotted.firstChild); assert_equals(composedRange.startOffset, 3); assert_equals(composedRange.endContainer, outer.firstChild); diff --git a/shadow-dom/selection-collapse-and-extend.tentative.html b/shadow-dom/selection-collapse-and-extend.tentative.html index 36cc036b3849f8..3d0bc5009d5ff0 100644 --- a/shadow-dom/selection-collapse-and-extend.tentative.html +++ b/shadow-dom/selection-collapse-and-extend.tentative.html @@ -18,7 +18,7 @@ const textNode = shadowRoot.querySelector('p').firstChild; getSelection().removeAllRanges(); getSelection().collapse(textNode, 5); - const ranges = getSelection().getComposedRanges(shadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, textNode); assert_equals(ranges[0].startOffset, 5); @@ -34,7 +34,7 @@ const textNode = shadowRoot.querySelector('p').firstChild; getSelection().removeAllRanges(); getSelection().collapse(textNode, 5); - const ranges = getSelection().getComposedRanges(shadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 0); }, 'collapse abort steps when called with a disconnected node inside a shadow tree'); @@ -46,10 +46,10 @@ const textNode = shadowRoot.querySelector('p').firstChild; getSelection().removeAllRanges(); getSelection().collapse(textNode, 5); - let ranges = getSelection().getComposedRanges(shadowRoot); + let ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); getSelection().extend(textNode, 11); - ranges = getSelection().getComposedRanges(shadowRoot); + ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, textNode); assert_equals(ranges[0].startOffset, 5); @@ -66,7 +66,7 @@ getSelection().removeAllRanges(); getSelection().collapse(container, 0); getSelection().extend(textNode, 5); - const ranges = getSelection().getComposedRanges(shadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, container); assert_equals(ranges[0].startOffset, 0); diff --git a/shadow-dom/selection-getComposedRanges-collapsed.tentative.html b/shadow-dom/selection-getComposedRanges-collapsed.tentative.html index 6af01fd8e0f2a3..99ab82eb39cf44 100644 --- a/shadow-dom/selection-getComposedRanges-collapsed.tentative.html +++ b/shadow-dom/selection-getComposedRanges-collapsed.tentative.html @@ -51,20 +51,20 @@ assert_equals(sel.getComposedRanges()[0].endContainer, container); assert_equals(sel.getComposedRanges()[0].endOffset, 4); - assert_equals(sel.getComposedRanges(c)[0].startContainer, c); - assert_equals(sel.getComposedRanges(c)[0].startOffset, 0); - assert_equals(sel.getComposedRanges(c)[0].endContainer, container); - assert_equals(sel.getComposedRanges(c)[0].endOffset, 4); + assert_equals(sel.getComposedRanges({ shadowRoots: [c] })[0].startContainer, c); + assert_equals(sel.getComposedRanges({ shadowRoots: [c] })[0].startOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [c] })[0].endContainer, container); + assert_equals(sel.getComposedRanges({ shadowRoots: [c] })[0].endOffset, 4); - assert_equals(sel.getComposedRanges(d)[0].startContainer, container); - assert_equals(sel.getComposedRanges(d)[0].startOffset, 1); - assert_equals(sel.getComposedRanges(d)[0].endContainer, d); - assert_equals(sel.getComposedRanges(d)[0].endOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [d] })[0].startContainer, container); + assert_equals(sel.getComposedRanges({ shadowRoots: [d] })[0].startOffset, 1); + assert_equals(sel.getComposedRanges({ shadowRoots: [d] })[0].endContainer, d); + assert_equals(sel.getComposedRanges({ shadowRoots: [d] })[0].endOffset, 0); - assert_equals(sel.getComposedRanges(c, d)[0].startContainer, c); - assert_equals(sel.getComposedRanges(c, d)[0].startOffset, 0); - assert_equals(sel.getComposedRanges(c, d)[0].endContainer, d); - assert_equals(sel.getComposedRanges(c, d)[0].endOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].startContainer, c); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].startOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].endContainer, d); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].endOffset, 0); // Re-setting the same range should never change the output sel.setBaseAndExtent(c, 0, d, 0); @@ -73,9 +73,9 @@ assert_equals(sel.getRangeAt(0).endContainer, d); assert_equals(sel.getRangeAt(0).endOffset, 0); - assert_equals(sel.getComposedRanges(c, d)[0].startContainer, c); - assert_equals(sel.getComposedRanges(c, d)[0].startOffset, 0); - assert_equals(sel.getComposedRanges(c, d)[0].endContainer, d); - assert_equals(sel.getComposedRanges(c, d)[0].endOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].startContainer, c); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].startOffset, 0); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].endContainer, d); + assert_equals(sel.getComposedRanges({ shadowRoots: [c, d] })[0].endOffset, 0); }, 'Setting the range to nodes in different shadow trees collapses ordinary ranges, but does not collapse composed ranges.'); diff --git a/shadow-dom/selection-getComposedRanges.tentative.html b/shadow-dom/selection-getComposedRanges.tentative.html index 9d5fa761627250..a69188a62ee921 100644 --- a/shadow-dom/selection-getComposedRanges.tentative.html +++ b/shadow-dom/selection-getComposedRanges.tentative.html @@ -18,10 +18,10 @@ container.innerHTML = 'hello, world'; getSelection().setBaseAndExtent(container.firstChild, 0, container.firstChild, 5); try { - getSelection().getComposedRanges(container); + getSelection().getComposedRanges({ shadowRoots: [container] }); assert_unreached('executed without error; want error'); } catch (e) { - assert_equals(e.message, "Failed to execute 'getComposedRanges' on 'Selection': parameter 1 is not of type 'ShadowRoot'."); + assert_equals(e.message, "Failed to execute 'getComposedRanges' on 'Selection': Failed to read the 'shadowRoots' property from 'GetComposedRangesOptions': Failed to convert value to 'ShadowRoot'."); } }, 'getComposedRanges should fail if argument is not a shadow root'); @@ -53,7 +53,7 @@ const shadowRoot = host.attachShadow({mode: 'closed'}); shadowRoot.innerHTML = 'hello, world'; getSelection().setBaseAndExtent(shadowRoot.firstChild, 0, shadowRoot.firstChild, 5); - const ranges = getSelection().getComposedRanges(shadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, shadowRoot.firstChild); assert_equals(ranges[0].startOffset, 0); @@ -95,7 +95,7 @@ const shadowRoot = host.attachShadow({mode: 'closed'}); shadowRoot.innerHTML = 'hello, world'; getSelection().setBaseAndExtent(shadowRoot.firstChild, 7, container, 2); - const ranges = getSelection().getComposedRanges(shadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [shadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, shadowRoot.firstChild); assert_equals(ranges[0].startOffset, 7); @@ -129,7 +129,7 @@ const innerShadowRoot = innerHost.attachShadow({mode: 'closed'}); innerShadowRoot.innerHTML = 'some text'; getSelection().setBaseAndExtent(innerShadowRoot.firstChild, 5, innerShadowRoot.firstChild, 9); - const ranges = getSelection().getComposedRanges(innerShadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [innerShadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, innerShadowRoot.firstChild); assert_equals(ranges[0].startOffset, 5); @@ -146,7 +146,7 @@ const innerShadowRoot = innerHost.attachShadow({mode: 'closed'}); innerShadowRoot.innerHTML = 'some text'; getSelection().setBaseAndExtent(innerShadowRoot.firstChild, 5, innerShadowRoot.firstChild, 9); - const ranges = getSelection().getComposedRanges(outerShadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [outerShadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, outerShadowRoot); assert_equals(ranges[0].startOffset, 0); @@ -163,7 +163,7 @@ const innerShadowRoot = innerHost.attachShadow({mode: 'closed'}); innerShadowRoot.innerHTML = 'some text'; getSelection().setBaseAndExtent(container.firstChild, 0, outerShadowRoot, 0); - const ranges = getSelection().getComposedRanges(innerShadowRoot); + const ranges = getSelection().getComposedRanges({ shadowRoots: [innerShadowRoot] }); assert_equals(ranges.length, 1); assert_equals(ranges[0].startContainer, container.firstChild, "A"); assert_equals(ranges[0].startOffset, 0, "B");