Bug 1948279 - Text Fragments: Allow creating text fragments for multiple ranges. r=mconley

Differential Revision: https://phabricator.services.mozilla.com/D238215
This commit is contained in:
Jan-Niklas Jaeschke
2025-02-14 19:52:20 +00:00
parent 67af9389b6
commit e8420f7d69
2 changed files with 30 additions and 16 deletions

View File

@@ -277,21 +277,31 @@ export class ContextMenuChild extends JSWindowActorChild {
}
case "ContextMenu:GetTextDirective": {
return this.contentWindow?.getSelection().rangeCount
? this.contentWindow?.document?.fragmentDirective
.createTextDirective(
this.contentWindow.getSelection().getRangeAt(0)
if (this.contentWindow?.getSelection().rangeCount) {
const textDirectives = [];
for (
let rangeIndex = 0;
rangeIndex < this.contentWindow.getSelection().rangeCount;
rangeIndex++
) {
textDirectives.push(
this.contentWindow.document?.fragmentDirective.createTextDirective(
this.contentWindow.getSelection().getRangeAt(rangeIndex)
)
.then(textDirective => {
if (textDirective != null) {
textDirective =
(this.contentWindow.location.hash.length ? "" : "#") +
`:~:${textDirective}`;
return `${this.contentWindow.location}${textDirective}`;
}
return null;
})
: null;
);
}
return Promise.all(textDirectives).then(directives => {
const validDirectives = directives.filter(d => d);
const textFragment = validDirectives.join("&");
if (textFragment) {
let url = URL.parse(this.contentWindow.location);
url.hash += `:~:${textFragment}`;
return url.href;
}
return null;
});
}
return null;
}
case "ContextMenu:RemoveAllTextFragments": {
this.contentWindow?.document?.fragmentDirective.removeAllTextDirectives();

View File

@@ -75,7 +75,7 @@ add_task(async function copiesToClipboard() {
isTextSelected: true,
runTests: async ({ copyLinkToHighlight }) => {
await SimpleTest.promiseClipboardChange(
"https://www.example.com/?stripParam=1234#:~:text=eiusmod%20tempor%20incididunt",
"https://www.example.com/?stripParam=1234#:~:text=eiusmod%20tempor%20incididunt&text=labore",
async () => {
await BrowserTestUtils.waitForCondition(
() =>
@@ -98,7 +98,7 @@ add_task(async function copiesToClipboard() {
isTextSelected: true,
runTests: async ({ copyCleanLinkToHighlight }) => {
await SimpleTest.promiseClipboardChange(
"https://www.example.com/#:~:text=eiusmod%20tempor%20incididunt",
"https://www.example.com/#:~:text=eiusmod%20tempor%20incididunt&text=labore",
async () => {
await BrowserTestUtils.waitForCondition(
() =>
@@ -160,6 +160,10 @@ async function testCopyLinkToHighlight({ isTextSelected, runTests }) {
const range = content.document.createRange();
range.selectNodeContents(span);
selection.addRange(range);
const range2 = content.document.createRange();
range2.setStart(textEnd, 4);
range2.setEnd(textEnd, 10);
selection.addRange(range2);
}
}
);