2009-11-09 17:41:12 +03:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>Test for copy/paste</title>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=524975">Mozilla Bug </a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript">
|
2010-10-11 02:07:00 +04:00
|
|
|
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.parentNode.removeChild(e);
|
|
|
|
g.removeAllRanges();
|
|
|
|
g.addRange(l);
|
|
|
|
}, 0)
|
|
|
|
}
|
2009-11-09 17:41:12 +03:00
|
|
|
|
2012-04-17 06:14:01 +04:00
|
|
|
function getLoadContext() {
|
|
|
|
var Ci = SpecialPowers.wrap(Components).interfaces;
|
|
|
|
return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsILoadContext);
|
|
|
|
}
|
|
|
|
|
2009-11-09 17:41:12 +03:00
|
|
|
function testCopyPaste () {
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
|
|
|
var webnav = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
|
|
|
|
var docShell = webnav.QueryInterface(Components.interfaces.nsIDocShell);
|
|
|
|
|
|
|
|
var documentViewer = docShell.contentViewer
|
|
|
|
.QueryInterface(Components.interfaces.nsIContentViewerEdit);
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2009-11-09 17:41:12 +03:00
|
|
|
var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"]
|
|
|
|
.getService(Components.interfaces.nsIClipboard);
|
2010-03-18 18:19:36 +03:00
|
|
|
|
|
|
|
var textarea = document.getElementById('input');
|
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
function copySelectionToClipboard() {
|
|
|
|
documentViewer.copySelection();
|
2011-08-18 17:22:52 +04:00
|
|
|
ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), "check text/unicode");
|
|
|
|
ok(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), "check text/html");
|
2010-05-25 03:24:15 +04:00
|
|
|
}
|
|
|
|
function copyToClipboard(node) {
|
|
|
|
textarea.blur();
|
|
|
|
clipboard.emptyClipboard(1);
|
|
|
|
var sel = window.getSelection();
|
|
|
|
sel.removeAllRanges();
|
|
|
|
var r = document.createRange();
|
|
|
|
r.selectNode(node);
|
|
|
|
window.getSelection().addRange(r);
|
|
|
|
copySelectionToClipboard();
|
|
|
|
}
|
|
|
|
function copyRangeToClipboard(startNode,startIndex,endNode,endIndex) {
|
|
|
|
textarea.blur();
|
|
|
|
clipboard.emptyClipboard(1);
|
|
|
|
var sel = window.getSelection();
|
|
|
|
sel.removeAllRanges();
|
|
|
|
var r = document.createRange();
|
|
|
|
r.setStart(startNode,startIndex)
|
|
|
|
r.setEnd(endNode,endIndex)
|
|
|
|
window.getSelection().addRange(r);
|
|
|
|
copySelectionToClipboard();
|
|
|
|
}
|
|
|
|
function copyChildrenToClipboard(id) {
|
|
|
|
textarea.blur();
|
|
|
|
clipboard.emptyClipboard(1);
|
|
|
|
window.getSelection().selectAllChildren(document.getElementById(id));
|
|
|
|
copySelectionToClipboard();
|
|
|
|
}
|
|
|
|
function getClipboardData(mime) {
|
|
|
|
var transferable = Components.classes['@mozilla.org/widget/transferable;1']
|
|
|
|
.createInstance(Components.interfaces.nsITransferable);
|
2012-04-17 06:14:01 +04:00
|
|
|
transferable.init(getLoadContext());
|
2010-05-25 03:24:15 +04:00
|
|
|
transferable.addDataFlavor(mime);
|
|
|
|
clipboard.getData(transferable, 1);
|
|
|
|
var data = {};
|
|
|
|
transferable.getTransferData(mime, data, {}) ;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
function testClipboardValue(mime, expected) {
|
|
|
|
var data = getClipboardData(mime);
|
|
|
|
is (data.value == null ? data.value :
|
|
|
|
data.value.QueryInterface(Components.interfaces.nsISupportsString).data,
|
|
|
|
expected,
|
|
|
|
mime + " value in the clipboard");
|
|
|
|
return data.value;
|
|
|
|
}
|
|
|
|
function testPasteText(expected) {
|
|
|
|
textarea.value="";
|
|
|
|
textarea.focus();
|
|
|
|
textarea.QueryInterface(Components.interfaces.nsIDOMNSEditableElement)
|
|
|
|
.editor.paste(1);
|
|
|
|
is(textarea.value, expected, "value of the textarea 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");
|
|
|
|
}
|
|
|
|
function testEmptyChildren(id) {
|
|
|
|
copyChildrenToClipboard(id);
|
|
|
|
testSelectionToString("");
|
|
|
|
testClipboardValue("text/unicode", null);
|
|
|
|
testClipboardValue("text/html", null);
|
|
|
|
testPasteText("");
|
|
|
|
}
|
|
|
|
|
|
|
|
copyChildrenToClipboard("draggable");
|
|
|
|
testSelectionToString("This is a draggable bit of text.");
|
|
|
|
testClipboardValue("text/unicode",
|
|
|
|
"This is a draggable bit of text.");
|
|
|
|
testClipboardValue("text/html",
|
|
|
|
"<div id=\"draggable\" title=\"title to have a long HTML line\">This is a <em>draggable</em> bit of text.</div>");
|
|
|
|
testPasteText("This is a draggable bit of text.");
|
|
|
|
|
|
|
|
copyChildrenToClipboard("alist");
|
2010-09-21 17:02:55 +04:00
|
|
|
testSelectionToString(" bla\n\n foo\n bar\n\n");
|
2012-04-03 10:44:15 +04:00
|
|
|
testClipboardValue("text/unicode", " bla\n\n foo\n bar\n\n");
|
2010-05-25 03:24:15 +04:00
|
|
|
testClipboardValue("text/html", "<div id=\"alist\">\n bla\n <ul>\n <li>foo</li>\n \n <li>bar</li>\n </ul>\n </div>");
|
2012-04-03 10:44:15 +04:00
|
|
|
testPasteText(" bla\n\n foo\n bar\n\n");
|
2010-06-21 23:56:18 +04:00
|
|
|
|
|
|
|
copyChildrenToClipboard("blist");
|
2010-09-21 17:02:55 +04:00
|
|
|
testSelectionToString(" mozilla\n\n foo\n bar\n\n");
|
2012-04-03 10:44:15 +04:00
|
|
|
testClipboardValue("text/unicode", " mozilla\n\n foo\n bar\n\n");
|
2010-06-21 23:56:18 +04:00
|
|
|
testClipboardValue("text/html", "<div id=\"blist\">\n mozilla\n <ol>\n <li>foo</li>\n \n <li>bar</li>\n </ol>\n </div>");
|
2012-04-03 10:44:15 +04:00
|
|
|
testPasteText(" mozilla\n\n foo\n bar\n\n");
|
2010-06-21 23:56:18 +04:00
|
|
|
|
|
|
|
copyChildrenToClipboard("clist");
|
2010-09-21 17:02:55 +04:00
|
|
|
testSelectionToString(" mzla\n\n foo\n bazzinga!\n bar\n\n");
|
2012-04-03 10:44:15 +04:00
|
|
|
testClipboardValue("text/unicode", " mzla\n\n foo\n bazzinga!\n bar\n\n");
|
2010-06-21 23:56:18 +04:00
|
|
|
testClipboardValue("text/html", "<div id=\"clist\">\n mzla\n <ul>\n <li>foo<ul>\n <li>bazzinga!</li>\n </ul></li>\n \n <li>bar</li>\n </ul>\n </div>");
|
2012-04-03 10:44:15 +04:00
|
|
|
testPasteText(" mzla\n\n foo\n bazzinga!\n bar\n\n");
|
2010-05-25 03:24:15 +04:00
|
|
|
|
|
|
|
copyChildrenToClipboard("div4");
|
2010-09-21 17:02:55 +04:00
|
|
|
testSelectionToString(" Tt t t ");
|
2012-04-03 10:44:15 +04:00
|
|
|
testClipboardValue("text/unicode", " Tt t t ");
|
2010-05-25 03:24:15 +04:00
|
|
|
testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea>t t t</textarea>\n</div>");
|
|
|
|
testInnerHTML("div4", "\n T<textarea>t t t</textarea>\n");
|
2012-04-03 10:44:15 +04:00
|
|
|
testPasteText(" Tt t t ");
|
2010-05-25 03:24:15 +04:00
|
|
|
|
|
|
|
copyChildrenToClipboard("div5");
|
2010-09-21 17:02:55 +04:00
|
|
|
testSelectionToString(" T ");
|
2012-04-03 10:44:15 +04:00
|
|
|
testClipboardValue("text/unicode", " T ");
|
2010-05-25 03:24:15 +04:00
|
|
|
testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea> </textarea>\n</div>");
|
|
|
|
testInnerHTML("div5", "\n T<textarea> </textarea>\n");
|
2012-04-03 10:44:15 +04:00
|
|
|
testPasteText(" T ");
|
2010-05-25 03:24:15 +04:00
|
|
|
|
|
|
|
copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1);
|
|
|
|
testSelectionToString("");
|
|
|
|
// START Disabled due to bug 564688
|
|
|
|
if (false) {
|
|
|
|
testClipboardValue("text/unicode", "");
|
|
|
|
testClipboardValue("text/html", "");
|
|
|
|
}
|
|
|
|
// END Disabled due to bug 564688
|
|
|
|
testInnerHTML("div6", "div6");
|
|
|
|
|
|
|
|
copyRangeToClipboard($("div7").childNodes[0],0, $("div7").childNodes[0],4);
|
|
|
|
testSelectionToString("");
|
|
|
|
// START Disabled due to bug 564688
|
|
|
|
if (false) {
|
|
|
|
testClipboardValue("text/unicode", "");
|
|
|
|
testClipboardValue("text/html", "");
|
|
|
|
}
|
|
|
|
// END Disabled due to bug 564688
|
|
|
|
testInnerHTML("div7", "div7");
|
|
|
|
|
|
|
|
copyRangeToClipboard($("div8").childNodes[0],0, $("div8").childNodes[0],4);
|
|
|
|
testSelectionToString("");
|
|
|
|
// START Disabled due to bug 564688
|
|
|
|
if (false) {
|
|
|
|
testClipboardValue("text/unicode", "");
|
|
|
|
testClipboardValue("text/html", "");
|
|
|
|
}
|
|
|
|
// END Disabled due to bug 564688
|
|
|
|
testInnerHTML("div8", "div8");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
copyRangeToClipboard($("div9").childNodes[0],0, $("div9").childNodes[0],4);
|
|
|
|
testSelectionToString("div9");
|
|
|
|
testClipboardValue("text/unicode", "div9");
|
|
|
|
testClipboardValue("text/html", "div9");
|
|
|
|
testInnerHTML("div9", "div9");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
copyToClipboard($("div10"));
|
|
|
|
testSelectionToString("");
|
|
|
|
testInnerHTML("div10", "div10");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
copyToClipboard($("div10").firstChild);
|
|
|
|
testSelectionToString("");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
copyRangeToClipboard($("div10").childNodes[0],0, $("div10").childNodes[0],1);
|
|
|
|
testSelectionToString("");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
copyRangeToClipboard($("div10").childNodes[1],0, $("div10").childNodes[1],1);
|
|
|
|
testSelectionToString("");
|
2010-03-18 18:19:36 +03:00
|
|
|
|
|
|
|
// ============ copy/paste test from/to a textarea
|
|
|
|
|
|
|
|
var val = "1\n 2\n 3";
|
2009-11-09 17:41:12 +03:00
|
|
|
textarea.value=val;
|
|
|
|
textarea.select();
|
|
|
|
textarea.editor.copy();
|
|
|
|
|
|
|
|
textarea.value="";
|
|
|
|
textarea.editor.paste(1);
|
|
|
|
is(textarea.value, val);
|
2010-03-18 18:19:36 +03:00
|
|
|
textarea.value="";
|
2009-11-09 17:41:12 +03:00
|
|
|
|
2011-08-09 19:14:37 +04:00
|
|
|
// ============ NOSCRIPT should not be copied
|
|
|
|
|
|
|
|
copyChildrenToClipboard("div13");
|
|
|
|
testSelectionToString("__");
|
|
|
|
testClipboardValue("text/unicode", "__");
|
|
|
|
testClipboardValue("text/html", "<div id=\"div13\">__</div>");
|
|
|
|
testPasteText("__");
|
|
|
|
|
2012-03-31 18:10:34 +04:00
|
|
|
// ============ converting cell boundaries to tabs in tables
|
|
|
|
|
|
|
|
copyToClipboard($("tr1"));
|
|
|
|
testClipboardValue("text/unicode", "foo\tbar");
|
|
|
|
|
2010-10-11 02:07:00 +04:00
|
|
|
// ============ manipulating Selection in oncopy
|
|
|
|
|
|
|
|
copyRangeToClipboard($("div11").childNodes[0],0, $("div11").childNodes[1],2);
|
|
|
|
testClipboardValue("text/unicode", "Xdiv11");
|
|
|
|
testClipboardValue("text/html", "<div><p>X<span>div</span>11</p></div>");
|
|
|
|
setTimeout(function(){testSelectionToString("div11")},0);
|
|
|
|
|
|
|
|
setTimeout(function(){
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
copyRangeToClipboard($("div12").childNodes[0],0, $("div12").childNodes[1],2);
|
|
|
|
testClipboardValue("text/unicode", "Xdiv12");
|
|
|
|
testClipboardValue("text/html", "<div><p>X<span>div</span>12</p></div>");
|
2012-06-01 22:52:37 +04:00
|
|
|
setTimeout(function(){
|
|
|
|
testSelectionToString("div12");
|
|
|
|
setTimeout(SimpleTest.finish,0);
|
|
|
|
},0);
|
2010-10-11 02:07:00 +04:00
|
|
|
},0);
|
2009-11-09 17:41:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
addLoadEvent(testCopyPaste);
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
<div>
|
|
|
|
|
|
|
|
<div id="draggable" title="title to have a long HTML line">This is a <em>draggable</em> bit of text.</div>
|
2010-03-18 18:19:36 +03:00
|
|
|
<textarea id="input" cols="40" rows="10"></textarea>
|
|
|
|
|
|
|
|
<div id="alist">
|
|
|
|
bla
|
|
|
|
<ul>
|
|
|
|
<li>foo</li>
|
2010-05-25 03:24:15 +04:00
|
|
|
<li style="display: none;">baz</li>
|
2010-03-18 18:19:36 +03:00
|
|
|
<li>bar</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2009-11-09 17:41:12 +03:00
|
|
|
|
2010-06-21 23:56:18 +04:00
|
|
|
<div id="blist">
|
|
|
|
mozilla
|
|
|
|
<ol>
|
|
|
|
<li>foo</li>
|
|
|
|
<li style="display: none;">baz</li>
|
|
|
|
<li>bar</li>
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="clist">
|
|
|
|
mzla
|
|
|
|
<ul>
|
|
|
|
<li>foo<ul>
|
|
|
|
<li>bazzinga!</li>
|
|
|
|
</ul></li>
|
|
|
|
<li style="display: none;">baz</li>
|
|
|
|
<li>bar</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
2010-05-25 03:24:15 +04:00
|
|
|
<div id="div4">
|
|
|
|
T<textarea>t t t</textarea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="div5">
|
|
|
|
T<textarea> </textarea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="div6" style="display:none"></div>
|
|
|
|
<script>
|
|
|
|
var x = $("div6")
|
|
|
|
x.appendChild(document.createTextNode('di'))
|
|
|
|
x.appendChild(document.createTextNode('v6'))
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div id="div7" style="display:none">div7</div>
|
|
|
|
<div id="div8" style="visibility:hidden">div8</div>
|
|
|
|
<div style="visibility:hidden"><div id="div9" style="visibility:visible">div9</div></div>
|
|
|
|
<div style="visibility:hidden"><div><div><div id="div10"></div></div></div></div>
|
|
|
|
<script>
|
|
|
|
var x = $("div10")
|
|
|
|
x.appendChild(document.createTextNode('div'))
|
|
|
|
x.appendChild(document.createTextNode('10'))
|
|
|
|
</script>
|
|
|
|
|
2010-10-11 02:07:00 +04:00
|
|
|
<div id="div11" oncopy="modifySelection('X')"><span>div</span>11</div>
|
|
|
|
<div id="div12" oncopy="modifySelection('X<b style=\'display:none\'>Y</b>')"><span>div</span>12</div>
|
2010-05-25 03:24:15 +04:00
|
|
|
|
2011-08-09 19:14:37 +04:00
|
|
|
<div id="div13">_<noscript>FAIL</noscript>_</div>
|
|
|
|
|
2012-03-31 18:10:34 +04:00
|
|
|
<table><tr id=tr1><td>foo</td><td>bar</td></tr></table>
|
|
|
|
|
2009-11-09 17:41:12 +03:00
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|