var utils = SpecialPowers.getDOMWindowUtils(window); var Cc = SpecialPowers.Cc; var Ci = SpecialPowers.Ci;
function getLoadContext() {
return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
}
async function runTest() { var pasteCount = 0; var pasteFunc = function() { pasteCount++; };
function verifyContent(s) { var e = document.getElementById("i1"); var doc = e.contentDocument;
if (navigator.platform.includes("Win")) {
// On Windows ignore \n which got left over from the removal of the fragment tags
// <html><body>\n<!--StartFragment--> and <!--EndFragment-->\n</body>\n</html>.
is(doc.body.innerHTML.replace(/\n/g, ""), s, "");
} else {
is(doc.body.innerHTML, s, "");
}
}
function pasteInto(trans, html, target_id) { var e = document.getElementById("i1"); var doc = e.contentDocument;
doc.designMode = "on";
doc.body.innerHTML = html;
doc.defaultView.focus();
if (target_id)
e = doc.getElementById(target_id);
else
e = doc.body; var selection = doc.defaultView.getSelection();
selection.removeAllRanges();
selection.selectAllChildren(e);
selection.collapseToEnd();
function getTransferableFromClipboard(asHTML) { var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
trans.init(getLoadContext());
if (asHTML) {
trans.addDataFlavor("text/html");
} else {
trans.addDataFlavor("text/plain");
} var clip = SpecialPowers.Services.clipboard;
clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard, SpecialPowers.wrap(window).browsingContext.currentWindowContext);
return trans;
}
// Commented out as the test for it below is also commented out.
// function makeTransferable(s, asHTML, target_id) {
// var e = document.getElementById("i2");
// var doc = e.contentDocument;
// if (asHTML) {
// doc.body.innerHTML = s;
// } else {
// var text = doc.createTextNode(s);
// doc.body.appendChild(text);
// }
// doc.designMode = "on";
// doc.defaultView.focus();
// var selection = doc.defaultView.getSelection();
// selection.removeAllRanges();
// if (!target_id) {
// selection.selectAllChildren(doc.body);
// } else {
// var range = document.createRange();
// range.selectNode(doc.getElementById(target_id));
// selection.addRange(range);
// }
//
// // We cannot use plain strings, we have to use nsSupportsString.
// var supportsStringClass = SpecialPowers.Components.classes["@mozilla.org/supports-string;1"];
// var ssData = supportsStringClass.createInstance(Ci.nsISupportsString);
//
// // Create the transferable.
// var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
// trans.init(getLoadContext());
//
// // Add the data to the transferable.
// if (asHTML) {
// trans.addDataFlavor("text/html");
// ssData.data = doc.body.innerHTML;
// trans.setTransferData("text/html", ssData);
// } else {
// trans.addDataFlavor("text/plain");
// ssData.data = doc.body.innerHTML;
// trans.setTransferData("text/plain", ssData);
// }
//
// return trans;
// }
async function copyToClipBoard(s, asHTML, target_id) { var e = document.getElementById("i2"); var doc = e.contentDocument;
if (asHTML) {
doc.body.innerHTML = s;
} else { var text = doc.createTextNode(s);
doc.body.appendChild(text);
}
doc.designMode = "on";
doc.defaultView.focus(); var selection = doc.defaultView.getSelection();
selection.removeAllRanges();
if (!target_id) {
selection.selectAllChildren(doc.body);
} else { var range = document.createRange();
range.selectNode(doc.getElementById(target_id));
selection.addRange(range);
}
await copyToClipBoard("HelloKitty", true); var trans = getTransferableFromClipboard(true);
pasteInto(trans, "");
verifyContent("HelloKitty");
is(pasteCount, 1, "paste event was not triggered");
// this test is not working out exactly like the clipboard test
// has to do with generating the nsITransferable above
// trans = makeTransferable('HelloKitty', true);
// pasteInto(trans, '');
// verifyContent('HelloKitty');
await copyToClipBoard("
Hello Kitty
HelloKitty", true);
trans = getTransferableFromClipboard(true);
pasteInto(trans, '
X
', "paste_here");
verifyContent('
X
Hello Kitty
HelloKitty
');
is(pasteCount, 1, "paste event was not triggered");
// The following test doesn't do what I expected, because the special handling
// of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes
// non-list/item children. See bug 481177.
// await copyToClipBoard("
Hello Kitty
Hello", true);
// pasteInto('
X
',"paste_here");
// verifyContent('
X
Hello Kitty
Hello');
await copyToClipBoard("
Kitty
Hello", true);
trans = getTransferableFromClipboard(true);
pasteInto(trans, '
Hello
', "paste_here");
verifyContent('
Hello KittyHello
');
is(pasteCount, 1, "paste event was not triggered");
await copyToClipBoard('123', true);
trans = getTransferableFromClipboard(true);
pasteInto(trans, '', "paste_here");
verifyContent('
123
');
is(pasteCount, 1, "paste event was not triggered");
// test that we can preventDefault pastes
pasteFunc = function(event) { event.preventDefault(); return false; };
await copyToClipBoard("
Kitty
Hello", true);
trans = getTransferableFromClipboard(true);
pasteInto(trans, '
Hello
', "paste_here");
verifyContent('
Hello
');
is(pasteCount, 0, "paste event was triggered");
}
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.