Skip to content

Commit

Permalink
Bug 1776879, replace text/unicode for clipboard and drag and drop and…
Browse files Browse the repository at this point in the history
… use text/plain directly, r=edgar,mak,stransky,geckoview-reviewers,extension-reviewers,zombie,m_kato

Most usage is a straight replacement but gtk needs extra changes as it transfers plain text in UTF8 natively and needs to be converted into UTF16, and Windows uses single-byte characters for RTF and CF_HTML formats so we preserve this.

Differential Revision: https://phabricator.services.mozilla.com/D158587
  • Loading branch information
EnnDeakin2 committed Feb 1, 2023
1 parent 888d6ef commit 394bb79
Show file tree
Hide file tree
Showing 101 changed files with 349 additions and 448 deletions.
4 changes: 2 additions & 2 deletions accessible/tests/mochitest/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,11 @@ function getTextFromClipboard() {
return "";
}

trans.addDataFlavor("text/unicode");
trans.addDataFlavor("text/plain");
Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);

var str = {};
trans.getTransferData("text/unicode", str);
trans.getTransferData("text/plain", str);

if (str) {
str = str.value.QueryInterface(Ci.nsISupportsString);
Expand Down
2 changes: 1 addition & 1 deletion browser/base/content/browser-places.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ var PlacesToolbarHelper = {
}

addData(PlacesUtils.TYPE_X_MOZ_URL, 0);
addData(PlacesUtils.TYPE_UNICODE, 0);
addData(PlacesUtils.TYPE_PLAINTEXT, 0);
addData(PlacesUtils.TYPE_HTML, 0);
},
};
Expand Down
4 changes: 2 additions & 2 deletions browser/base/content/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,7 @@ function readFromClipboard() {
);
trans.init(getLoadContext());

trans.addDataFlavor("text/unicode");
trans.addDataFlavor("text/plain");

// If available, use selection clipboard, otherwise global one
let clipboard = Services.clipboard;
Expand All @@ -3187,7 +3187,7 @@ function readFromClipboard() {
}

var data = {};
trans.getTransferData("text/unicode", data);
trans.getTransferData("text/plain", data);

if (data) {
data = data.value.QueryInterface(Ci.nsISupportsString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ add_task(async function test_contenteditable() {
"text/html",
PlacesUtils.toISupportsString("<strong>Bold text</strong>")
);
xferable.addDataFlavor("text/unicode");
xferable.addDataFlavor("text/plain");
xferable.setTransferData(
"text/unicode",
"text/plain",
PlacesUtils.toISupportsString("Bold text")
);
Services.clipboard.setData(
Expand Down
2 changes: 1 addition & 1 deletion browser/base/content/test/general/browser_bug1297539.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getTransferableFromClipboard(asHTML) {
if (asHTML) {
trans.addDataFlavor("text/html");
} else {
trans.addDataFlavor("text/unicode");
trans.addDataFlavor("text/plain");
}
Services.clipboard.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
return trans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function setClipboard(path) {
trans.addDataFlavor("application/x-moz-file");
trans.setTransferData("application/x-moz-file", file);

trans.addDataFlavor("text/unicode");
trans.addDataFlavor("text/plain");
const str = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
str.data = "Alternate";
trans.setTransferData("text/unicode", str);
trans.setTransferData("text/plain", str);

// Write to clipboard.
Services.clipboard.setData(trans, null, Ci.nsIClipboard.kGlobalClipboard);
Expand Down
2 changes: 1 addition & 1 deletion browser/components/downloads/content/allDownloadsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ DownloadsPlacesView.prototype = {
);
trans.init(null);

let flavors = ["text/x-moz-url", "text/unicode"];
let flavors = ["text/x-moz-url", "text/plain"];
flavors.forEach(trans.addDataFlavor);

Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
Expand Down
2 changes: 1 addition & 1 deletion browser/components/downloads/content/downloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ var DownloadsPanel = {
Ci.nsITransferable
);
trans.init(null);
let flavors = ["text/x-moz-url", "text/unicode"];
let flavors = ["text/x-moz-url", "text/plain"];
flavors.forEach(trans.addDataFlavor);
Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
// Getting the data or creating the nsIURI might fail
Expand Down
6 changes: 3 additions & 3 deletions browser/components/places/PlacesUIUtils.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ export var PlacesUIUtils = {
let contents = [
{ type: lazy.PlacesUtils.TYPE_X_MOZ_URL, entries: [] },
{ type: lazy.PlacesUtils.TYPE_HTML, entries: [] },
{ type: lazy.PlacesUtils.TYPE_UNICODE, entries: [] },
{ type: lazy.PlacesUtils.TYPE_PLAINTEXT, entries: [] },
];

contents.forEach(function(content) {
Expand Down Expand Up @@ -2034,7 +2034,7 @@ XPCOMUtils.defineLazyGetter(PlacesUIUtils, "URI_FLAVORS", () => {
return [
lazy.PlacesUtils.TYPE_X_MOZ_URL,
TAB_DROP_TYPE,
lazy.PlacesUtils.TYPE_UNICODE,
lazy.PlacesUtils.TYPE_PLAINTEXT,
];
});
XPCOMUtils.defineLazyGetter(PlacesUIUtils, "SUPPORTED_FLAVORS", () => {
Expand Down Expand Up @@ -2271,7 +2271,7 @@ function getTransactionsForCopy(items, insertionIndex, insertionParentGuid) {
});
} else {
let title =
item.type != lazy.PlacesUtils.TYPE_UNICODE ? item.title : item.uri;
item.type != lazy.PlacesUtils.TYPE_PLAINTEXT ? item.title : item.uri;
transaction = lazy.PlacesTransactions.NewBookmark({
index,
parentGuid: insertionParentGuid,
Expand Down
19 changes: 6 additions & 13 deletions browser/components/places/content/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PlacesController.prototype = {
case "cmd_paste":
case "placesCmd_paste":
// If the clipboard contains a Places flavor it is definitely pasteable,
// otherwise we also allow pasting "text/unicode" and "text/x-moz-url" data.
// otherwise we also allow pasting "text/plain" and "text/x-moz-url" data.
// We don't check if the data is valid here, because the clipboard may
// contain very large blobs that would largely slowdown commands updating.
// Of course later paste() should ignore any invalid data.
Expand All @@ -178,7 +178,7 @@ PlacesController.prototype = {
[
...PlacesUIUtils.PLACES_FLAVORS,
PlacesUtils.TYPE_X_MOZ_URL,
PlacesUtils.TYPE_UNICODE,
PlacesUtils.TYPE_PLAINTEXT,
],
Ci.nsIClipboard.kGlobalClipboard
)
Expand Down Expand Up @@ -1049,7 +1049,7 @@ PlacesController.prototype = {

function addURIData(index) {
addData(PlacesUtils.TYPE_X_MOZ_URL, index);
addData(PlacesUtils.TYPE_UNICODE, index);
addData(PlacesUtils.TYPE_PLAINTEXT, index);
addData(PlacesUtils.TYPE_HTML, index);
}

Expand Down Expand Up @@ -1131,7 +1131,7 @@ PlacesController.prototype = {
{ type: PlacesUtils.TYPE_X_MOZ_PLACE, entries: [] },
{ type: PlacesUtils.TYPE_X_MOZ_URL, entries: [] },
{ type: PlacesUtils.TYPE_HTML, entries: [] },
{ type: PlacesUtils.TYPE_UNICODE, entries: [] },
{ type: PlacesUtils.TYPE_PLAINTEXT, entries: [] },
];

// Avoid handling descendants of a copied node, the transactions take care
Expand Down Expand Up @@ -1264,7 +1264,7 @@ PlacesController.prototype = {
[
PlacesUtils.TYPE_X_MOZ_PLACE,
PlacesUtils.TYPE_X_MOZ_URL,
PlacesUtils.TYPE_UNICODE,
PlacesUtils.TYPE_PLAINTEXT,
].forEach(type => xferable.addDataFlavor(type));

Services.clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
Expand Down Expand Up @@ -1494,13 +1494,6 @@ var PlacesControllerDragHelper = {
}
}

// If no supported flavor is found, check if data includes text/plain
// contents. If so, request them as text/unicode, a conversion will happen
// automatically.
if (aFlavors.contains("text/plain")) {
return PlacesUtils.TYPE_UNICODE;
}

return null;
},

Expand Down Expand Up @@ -1623,7 +1616,7 @@ var PlacesControllerDragHelper = {

// Following flavors may contain duplicated data.
let duplicable = new Map();
duplicable.set(PlacesUtils.TYPE_UNICODE, new Set());
duplicable.set(PlacesUtils.TYPE_PLAINTEXT, new Set());
duplicable.set(PlacesUtils.TYPE_X_MOZ_URL, new Set());

// Collect all data from the DataTransfer before processing it, as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ add_task(async function test() {
};

// Simulate a bookmark drop for all of the mime types and effects.
let mimeTypes = ["text/plain", "text/unicode", "text/x-moz-url"];
let mimeTypes = ["text/plain", "text/x-moz-url"];
let effects = ["move", "copy", "link"];
for (let effect of effects) {
for (let mimeType of mimeTypes) {
Expand Down
4 changes: 2 additions & 2 deletions browser/components/textrecognition/tests/browser/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function getTextFromClipboard() {
Ci.nsITransferable
);
transferable.init(window.docShell.QueryInterface(Ci.nsILoadContext));
transferable.addDataFlavor("text/unicode");
transferable.addDataFlavor("text/plain");
Services.clipboard.getData(transferable, Services.clipboard.kGlobalClipboard);

const results = {};
transferable.getTransferData("text/unicode", results);
transferable.getTransferData("text/plain", results);
return results.value.QueryInterface(Ci.nsISupportsString)?.data ?? "";
}

Expand Down
4 changes: 2 additions & 2 deletions browser/components/urlbar/UrlbarInput.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,7 @@ export class UrlbarInput {
let title = this.window.gBrowser.contentTitle || href;

event.dataTransfer.setData("text/x-moz-url", `${href}\n${title}`);
event.dataTransfer.setData("text/unicode", href);
event.dataTransfer.setData("text/plain", href);
event.dataTransfer.setData("text/html", `<a href="${href}">${title}</a>`);
event.dataTransfer.effectAllowed = "copyLink";
event.stopPropagation();
Expand Down Expand Up @@ -3709,7 +3709,7 @@ function getDroppableData(event) {
}
}
// Handle as text.
return event.dataTransfer.getData("text/unicode");
return event.dataTransfer.getData("text/plain");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ add_task(async function() {
}

let primaryAsText = SpecialPowers.getClipboardData(
"text/unicode",
"text/plain",
SpecialPowers.Ci.nsIClipboard.kSelectionClipboard
);
Assert.equal(primaryAsText, TEXT_FOR_PRIMARY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ add_task(async function test_selectByKey() {

function assertClipboard() {
Assert.equal(
SpecialPowers.getClipboardData("text/unicode"),
SpecialPowers.getClipboardData("text/plain"),
"100 cm",
"The result of conversion is copied to clipboard"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function checkPrimarySelection(expectedVal = "") {
)
) {
let primaryAsText = SpecialPowers.getClipboardData(
"text/unicode",
"text/plain",
SpecialPowers.Ci.nsIClipboard.kSelectionClipboard
);
Assert.equal(primaryAsText, expectedVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ add_task(async function testSimpleSourcesWithManualClickExpand() {
info("Test the copy to clipboard context menu");
const mathMinTreeNode = findSourceNodeWithText(dbg, "math.min.js");
await triggerCopySourceContextMenu(dbg, mathMinTreeNode);
const clipboardData = SpecialPowers.getClipboardData("text/unicode");
const clipboardData = SpecialPowers.getClipboardData("text/plain");
is(
clipboardData,
EXAMPLE_URL + "math.min.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ add_task(async function() {
});

function checkClipboardData(expected) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
return decodeURIComponent(actual).trim() === expected.trim();
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ add_task(async function() {
});

function checkClipboardData(expected) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
return actual.trim() === expected.trim();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ add_task(async function() {
});

function checkClipboardData(expected) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
return actual.trim() === expected.trim();
}
4 changes: 2 additions & 2 deletions devtools/client/inspector/computed/test/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async function copySomeTextAndCheckClipboard(view, positions, expectedPattern) {
}

function checkClipboard(expectedPattern) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
const expectedRegExp = new RegExp(expectedPattern, "g");
return expectedRegExp.test(actual);
}
Expand All @@ -249,7 +249,7 @@ function failClipboardCheck(expectedPattern) {
expectedPattern = expectedPattern.replace(/\\\(/g, "(");
expectedPattern = expectedPattern.replace(/\\\)/g, ")");

let actual = SpecialPowers.getClipboardData("text/unicode");
let actual = SpecialPowers.getClipboardData("text/plain");

// Trim the right hand side of our strings. This is because expectedPattern
// accounts for windows sometimes adding a newline to our copied data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ async function disableProperty(view, index) {
}

function checkClipboardData(expectedPattern) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
const expectedRegExp = new RegExp(expectedPattern, "g");
return expectedRegExp.test(actual);
}
Expand All @@ -342,7 +342,7 @@ function failedClipboard(expectedPattern) {
expectedPattern = expectedPattern.replace(/\\\(/g, "(");
expectedPattern = expectedPattern.replace(/\\\)/g, ")");

let actual = SpecialPowers.getClipboardData("text/unicode");
let actual = SpecialPowers.getClipboardData("text/plain");

// Trim the right hand side of our strings. This is because expectedPattern
// accounts for windows sometimes adding a newline to our copied data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async function checkCopyEditorValue(view) {
}

function checkClipboardData(expectedPattern) {
const actual = SpecialPowers.getClipboardData("text/unicode");
const actual = SpecialPowers.getClipboardData("text/plain");
const expectedRegExp = new RegExp(expectedPattern, "g");
return expectedRegExp.test(actual);
}
Expand All @@ -194,7 +194,7 @@ function failedClipboard(expectedPattern) {
expectedPattern = expectedPattern.replace(/\\\(/g, "(");
expectedPattern = expectedPattern.replace(/\\\)/g, ")");

let actual = SpecialPowers.getClipboardData("text/unicode");
let actual = SpecialPowers.getClipboardData("text/plain");

// Trim the right hand side of our strings. This is because expectedPattern
// accounts for windows sometimes adding a newline to our copied data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ add_task(async function() {
ok(true, "The eyedropper is now hidden");

info("Check that the clipboard still contains the copied color");
is(SpecialPowers.getClipboardData("text/unicode"), "#ff0000");
is(SpecialPowers.getClipboardData("text/plain"), "#ff0000");

info("Replace the clipboard content with another text");
SpecialPowers.clipboardCopyString("not-a-color");
is(SpecialPowers.getClipboardData("text/unicode"), "not-a-color");
is(SpecialPowers.getClipboardData("text/plain"), "not-a-color");

info("Click on the page again, check the clipboard was not updated");
await BrowserTestUtils.synthesizeMouseAtCenter(
Expand All @@ -57,7 +57,7 @@ add_task(async function() {
);
// Wait 500ms because nothing is observable when the test is successful.
await wait(500);
is(SpecialPowers.getClipboardData("text/unicode"), "not-a-color");
is(SpecialPowers.getClipboardData("text/plain"), "not-a-color");

finalize();
});
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async function testClearedRequests({ tab, monitor, toolbox }) {
connector
);

const jsonString = SpecialPowers.getClipboardData("text/unicode");
const jsonString = SpecialPowers.getClipboardData("text/plain");
const har = JSON.parse(jsonString);
is(har.log.entries.length, 2, "There must be two requests");
is(
Expand Down Expand Up @@ -233,6 +233,6 @@ async function reloadAndCopyAllAsHar({
connector
);

const jsonString = SpecialPowers.getClipboardData("text/unicode");
const jsonString = SpecialPowers.getClipboardData("text/plain");
return JSON.parse(jsonString);
}
4 changes: 2 additions & 2 deletions devtools/shared/platform/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ function copyString(string) {
}

/**
* Retrieve the current clipboard data matching the flavor "text/unicode".
* Retrieve the current clipboard data matching the flavor "text/plain".
*
* @return {String} Clipboard text content, null if no text clipboard data is available.
*/
function getText() {
const flavor = "text/unicode";
const flavor = "text/plain";

const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
Ci.nsITransferable
Expand Down
Loading

0 comments on commit 394bb79

Please sign in to comment.