function modifySelection(s) { var g = window.getSelection(); var l = g.getRangeAt(0); var d = document.createElement("p");
d.innerHTML = s;
d.appendChild(l.cloneContents());
var e = document.createElement("div");
document.body.appendChild(e);
e.appendChild(d); var a = document.createRange();
a.selectNode(d);
g.removeAllRanges();
g.addRange(a);
window.setTimeout(function () {
e.remove();
g.removeAllRanges();
g.addRange(l);
}, 0);
}
function getLoadContext() { var Ci = SpecialPowers.Ci; return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
}
async function testCopyPaste(isXHTML) { var suppressUnicodeCheckIfHidden = !!isXHTML; var suppressHTMLCheck = !!isXHTML;
var docShell = SpecialPowers.wrap(window).docShell;
var documentViewer = docShell.docViewer.QueryInterface(
SpecialPowers.Ci.nsIDocumentViewerEdit
);
var clipboard = SpecialPowers.Services.clipboard;
var textarea = SpecialPowers.wrap(document.getElementById("input"));
async function copySelectionToClipboard(suppressUnicodeCheck) {
await SimpleTest.promiseClipboardChange(
() => true,
() => {
documentViewer.copySelection();
}
); if (!suppressUnicodeCheck) {
ok(
clipboard.hasDataMatchingFlavors(["text/plain"], 1), "check text/plain"
);
} if (!suppressHTMLCheck) {
ok(clipboard.hasDataMatchingFlavors(["text/html"], 1), "check text/html");
}
} function clear() {
textarea.blur(); var sel = window.getSelection();
sel.removeAllRanges();
}
async function copyToClipboard(node, suppressUnicodeCheck) {
clear(); var r = document.createRange();
r.selectNode(node);
window.getSelection().addRange(r);
await copySelectionToClipboard(suppressUnicodeCheck);
} function addRange(startNode, startIndex, endNode, endIndex) { var sel = window.getSelection(); var r = document.createRange();
r.setStart(startNode, startIndex);
r.setEnd(endNode, endIndex);
sel.addRange(r);
}
async function copyRangeToClipboard(
startNode,
startIndex,
endNode,
endIndex,
suppressUnicodeCheck
) {
clear();
addRange(startNode, startIndex, endNode, endIndex);
await copySelectionToClipboard(suppressUnicodeCheck);
}
async function copyChildrenToClipboard(id) {
clear();
window.getSelection().selectAllChildren(document.getElementById(id));
await copySelectionToClipboard();
} function getClipboardData(mime) { var transferable = SpecialPowers.Cc[ "@mozilla.org/widget/transferable;1"
].createInstance(SpecialPowers.Ci.nsITransferable);
transferable.init(getLoadContext());
transferable.addDataFlavor(mime);
clipboard.getData(
transferable, 1,
SpecialPowers.wrap(window).browsingContext.currentWindowContext
); var data = SpecialPowers.createBlankObject();
transferable.getTransferData(mime, data); return data;
} function testHtmlClipboardValue(mime, expected) { // For Windows, navigator.platform returns "Win32". var expectedValue = expected; if (navigator.platform.includes("Win")) { // Windows has extra content.
expectedValue =
kTextHtmlPrefixClipboardDataWindows +
expected.replace(/\n/g, "\n") +
kTextHtmlSuffixClipboardDataWindows;
}
testClipboardValue(mime, expectedValue);
} function testClipboardValue(mime, expected) { if (suppressHTMLCheck && mime == "text/html") { returnnull;
} var data = SpecialPowers.wrap(getClipboardData(mime));
is(
data.value == null
? data.value
: data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data,
expected,
mime + " value in the clipboard"
); return data.value;
} function testPasteText(expected) {
textarea.value = "";
textarea.focus();
textarea.editor.paste(1);
is(textarea.value, expected, "value of the textarea after the paste");
} function testPasteHTML(id, expected) { var contentEditable = $(id);
contentEditable.focus();
synthesizeKey("v", { accelKey: true });
is(contentEditable.innerHTML, expected, id + ".innerHtml after the paste");
} function testSelectionToString(expected) {
is(
window.getSelection().toString().replace(/\r\n/g, "\n"),
expected, "Selection.toString"
);
} function testInnerHTML(id, expected) { var value = document.getElementById(id).innerHTML;
is(value, expected, id + ".innerHTML");
}
await copyChildrenToClipboard("draggable");
testSelectionToString("This is a draggable bit of text.");
testClipboardValue("text/plain", "This is a draggable bit of text.");
testHtmlClipboardValue( "text/html",
`${includeCommonAncestor ? '<div id="draggable" title="title to have a long HTML line">' : ""}` +
`This is a <em>draggable</em> bit of text.` +
`${includeCommonAncestor ? "</div>" : ""}`
);
testPasteText("This is a draggable bit of text.");
if (!isXHTML) { // ============ copy/paste multi-range selection (bug 1123505) // with text start node var sel = window.getSelection();
sel.removeAllRanges(); var r = document.createRange(); var ul = $("ul1"); var parent = ul.parentNode;
r.setStart(parent, 0);
r.setEnd(parent.firstChild, 15);
sel.addRange(r); // <div>{Copy1then Paste]<ul id="ul1"><li>LI</li>\n</ul></div>
r = document.createRange();
r.setStart(ul, 1);
r.setEnd(parent, 2);
sel.addRange(r); // <div>Copy1then Paste<ul id="ul1"><li>LI{</li>\n</ul>}</div>
await copySelectionToClipboard(true);
testPasteHTML("contentEditable1", "Copy1then Paste"); // The <ul> should not appear because it has no <li>s
// with text end node
sel = window.getSelection();
sel.removeAllRanges();
r = document.createRange();
ul = $("ul2");
parent = ul.parentNode;
r.setStart(parent, 0);
r.setEnd(ul, 1);
sel.addRange(r); // <div>{<ul id="ul2">\n}<li>LI</li></ul>Copy2then Paste</div>
r = document.createRange();
r.setStart(parent.childNodes[1], 0);
r.setEnd(parent, 2);
sel.addRange(r); // <div><ul id="ul2">\n<li>LI</li></ul>[Copy2then Paste}</div>
await copySelectionToClipboard(true);
testPasteHTML("contentEditable2", "Copy2then Paste"); // The <ul> should not appear because it has no <li>s
// with text end node and non-empty start
sel = window.getSelection();
sel.removeAllRanges();
r = document.createRange();
ul = $("ul3");
parent = ul.parentNode;
r.setStart(parent, 0);
r.setEnd(ul, 1);
sel.addRange(r); // <div>{<ul id="ul3"><li>\n</li>}<li>LI</li></ul>Copy3then Paste</div>
r = document.createRange();
r.setStart(parent.childNodes[1], 0);
r.setEnd(parent, 2);
sel.addRange(r); // <div><ul id="ul3"><li>\n</li><li>LI</li></ul>[Copy3then Paste}</div>
await copySelectionToClipboard(true);
testPasteHTML( "contentEditable3", // The <ul> should appear because it has a <li> // The preceding linefeed of the <br> in the empty <li> is an invisible // white-space. Thus, it's not important whether it appears or not in the result. '<ul id="ul3"><li><br></li></ul>Copy3then Paste'
);
// with elements of different depth
sel = window.getSelection();
sel.removeAllRanges();
r = document.createRange(); var div1 = $("div1s");
parent = div1.parentNode;
r.setStart(parent, 0);
r.setEnd(document.getElementById("div1se1"), 1); // after the "inner" DIV
sel.addRange(r);
r = document.createRange();
r.setStart(div1.childNodes[1], 0); // the start of "after"
r.setEnd(parent, 1);
sel.addRange(r);
await copySelectionToClipboard(true);
testPasteHTML( "contentEditable4", '<div id="div1s"><div id="div1se1">before</div></div><div id="div1s">after</div>'
);
// with elements of different depth, and a text node at the end
sel = window.getSelection();
sel.removeAllRanges();
r = document.createRange();
div1 = $("div2s");
parent = div1.parentNode;
r.setStart(parent, 0);
r.setEnd(document.getElementById("div2se1"), 1); // after the "inner" DIV
sel.addRange(r);
r = document.createRange();
r.setStart(div1.childNodes[1], 0); // the start of "after"
r.setEnd(parent, 1);
sel.addRange(r);
await copySelectionToClipboard(true);
testPasteHTML( "contentEditable5", '<div id="div2s"><div id="div2se1">before</div></div><div id="div2s">after</div>'
);
// crash test for bug 1127835 var e1 = document.getElementById("1127835crash1"); var e2 = document.getElementById("1127835crash2"); var e3 = document.getElementById("1127835crash3"); var t1 = e1.childNodes[0]; var t3 = e3.childNodes[0];
sel = window.getSelection();
sel.removeAllRanges();
r = document.createRange();
r.setStart(e2, 1);
r.setEnd(t3, 0);
sel.addRange(r); // <div>\n<span id="1127835crash1">1</span><div id="1127835crash2"><div>\n</div>{</div><a href="..." id="1127835crash3">]3</a>\n</div>
await copySelectionToClipboard(true);
testPasteHTML( "contentEditable6", '<span id="1127835crash1"></span><div id="1127835crash2"><div>\n</div></div><a href="http://www.mozilla.org/" id="1127835crash3"><br></a>'
); // Don't strip the empty `<a href="...">` element because of avoiding any dataloss provided by the element
}
// ============ copy/paste test from/to a textarea
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.