content/base/test/copypaste.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 function modifySelection(s) {
michael@0 5 var g = window.getSelection();
michael@0 6 var l = g.getRangeAt(0);
michael@0 7 var d = document.createElement("p");
michael@0 8 d.innerHTML = s;
michael@0 9 d.appendChild(l.cloneContents());
michael@0 10
michael@0 11 var e = document.createElement("div");
michael@0 12 document.body.appendChild(e);
michael@0 13 e.appendChild(d);
michael@0 14 var a = document.createRange();
michael@0 15 a.selectNode(d);
michael@0 16 g.removeAllRanges();
michael@0 17 g.addRange(a);
michael@0 18 window.setTimeout(function () {
michael@0 19 e.parentNode.removeChild(e);
michael@0 20 g.removeAllRanges();
michael@0 21 g.addRange(l);
michael@0 22 }, 0)
michael@0 23 }
michael@0 24
michael@0 25 function getLoadContext() {
michael@0 26 var Ci = SpecialPowers.Ci;
michael@0 27 return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 28 .getInterface(Ci.nsIWebNavigation)
michael@0 29 .QueryInterface(Ci.nsILoadContext);
michael@0 30 }
michael@0 31
michael@0 32 function testCopyPaste (isXHTML) {
michael@0 33 var suppressUnicodeCheckIfHidden = !!isXHTML;
michael@0 34 var suppressHTMLCheck = !!isXHTML;
michael@0 35
michael@0 36 var webnav = SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
michael@0 37 .getInterface(SpecialPowers.Ci.nsIWebNavigation)
michael@0 38
michael@0 39 var docShell = webnav.QueryInterface(SpecialPowers.Ci.nsIDocShell);
michael@0 40
michael@0 41 var documentViewer = docShell.contentViewer
michael@0 42 .QueryInterface(SpecialPowers.Ci.nsIContentViewerEdit);
michael@0 43
michael@0 44 var clipboard = SpecialPowers.Services.clipboard;
michael@0 45
michael@0 46 var textarea = SpecialPowers.wrap(document.getElementById('input'));
michael@0 47
michael@0 48 function copySelectionToClipboard(suppressUnicodeCheck) {
michael@0 49 documentViewer.copySelection();
michael@0 50 if (!suppressUnicodeCheck)
michael@0 51 ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), "check text/unicode");
michael@0 52 if (!suppressHTMLCheck)
michael@0 53 ok(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), "check text/html");
michael@0 54 }
michael@0 55 function copyToClipboard(node, suppressUnicodeCheck) {
michael@0 56 textarea.blur();
michael@0 57 clipboard.emptyClipboard(1);
michael@0 58 var sel = window.getSelection();
michael@0 59 sel.removeAllRanges();
michael@0 60 var r = document.createRange();
michael@0 61 r.selectNode(node);
michael@0 62 window.getSelection().addRange(r);
michael@0 63 copySelectionToClipboard(suppressUnicodeCheck);
michael@0 64 }
michael@0 65 function copyRangeToClipboard(startNode,startIndex,endNode,endIndex,suppressUnicodeCheck) {
michael@0 66 textarea.blur();
michael@0 67 clipboard.emptyClipboard(1);
michael@0 68 var sel = window.getSelection();
michael@0 69 sel.removeAllRanges();
michael@0 70 var r = document.createRange();
michael@0 71 r.setStart(startNode,startIndex)
michael@0 72 r.setEnd(endNode,endIndex)
michael@0 73 window.getSelection().addRange(r);
michael@0 74 copySelectionToClipboard(suppressUnicodeCheck);
michael@0 75 }
michael@0 76 function copyChildrenToClipboard(id) {
michael@0 77 textarea.blur();
michael@0 78 clipboard.emptyClipboard(1);
michael@0 79 window.getSelection().selectAllChildren(document.getElementById(id));
michael@0 80 copySelectionToClipboard();
michael@0 81 }
michael@0 82 function getClipboardData(mime) {
michael@0 83 var transferable = SpecialPowers.Cc['@mozilla.org/widget/transferable;1']
michael@0 84 .createInstance(SpecialPowers.Ci.nsITransferable);
michael@0 85 transferable.init(getLoadContext());
michael@0 86 transferable.addDataFlavor(mime);
michael@0 87 clipboard.getData(transferable, 1);
michael@0 88 var data = SpecialPowers.createBlankObject();
michael@0 89 transferable.getTransferData(mime, data, {}) ;
michael@0 90 return data;
michael@0 91 }
michael@0 92 function testClipboardValue(mime, expected) {
michael@0 93 if (suppressHTMLCheck && mime == "text/html")
michael@0 94 return null;
michael@0 95 var data = SpecialPowers.wrap(getClipboardData(mime));
michael@0 96 is (data.value == null ? data.value :
michael@0 97 data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data,
michael@0 98 expected,
michael@0 99 mime + " value in the clipboard");
michael@0 100 return data.value;
michael@0 101 }
michael@0 102 function testPasteText(expected) {
michael@0 103 textarea.value="";
michael@0 104 textarea.focus();
michael@0 105 textarea.editor.paste(1);
michael@0 106 is(textarea.value, expected, "value of the textarea after the paste");
michael@0 107 }
michael@0 108 function testSelectionToString(expected) {
michael@0 109 is(window.getSelection().toString().replace(/\r\n/g,"\n"), expected, "Selection.toString");
michael@0 110 }
michael@0 111 function testInnerHTML(id, expected) {
michael@0 112 var value = document.getElementById(id).innerHTML;
michael@0 113 is(value, expected, id + ".innerHTML");
michael@0 114 }
michael@0 115 function testEmptyChildren(id) {
michael@0 116 copyChildrenToClipboard(id);
michael@0 117 testSelectionToString("");
michael@0 118 testClipboardValue("text/unicode", null);
michael@0 119 testClipboardValue("text/html", null);
michael@0 120 testPasteText("");
michael@0 121 }
michael@0 122
michael@0 123 copyChildrenToClipboard("draggable");
michael@0 124 testSelectionToString("This is a draggable bit of text.");
michael@0 125 testClipboardValue("text/unicode",
michael@0 126 "This is a draggable bit of text.");
michael@0 127 testClipboardValue("text/html",
michael@0 128 "<div id=\"draggable\" title=\"title to have a long HTML line\">This is a <em>draggable</em> bit of text.</div>");
michael@0 129 testPasteText("This is a draggable bit of text.");
michael@0 130
michael@0 131 copyChildrenToClipboard("alist");
michael@0 132 testSelectionToString(" bla\n\n foo\n bar\n\n");
michael@0 133 testClipboardValue("text/unicode", " bla\n\n foo\n bar\n\n");
michael@0 134 testClipboardValue("text/html", "<div id=\"alist\">\n bla\n <ul>\n <li>foo</li>\n \n <li>bar</li>\n </ul>\n </div>");
michael@0 135 testPasteText(" bla\n\n foo\n bar\n\n");
michael@0 136
michael@0 137 copyChildrenToClipboard("blist");
michael@0 138 testSelectionToString(" mozilla\n\n foo\n bar\n\n");
michael@0 139 testClipboardValue("text/unicode", " mozilla\n\n foo\n bar\n\n");
michael@0 140 testClipboardValue("text/html", "<div id=\"blist\">\n mozilla\n <ol>\n <li>foo</li>\n \n <li>bar</li>\n </ol>\n </div>");
michael@0 141 testPasteText(" mozilla\n\n foo\n bar\n\n");
michael@0 142
michael@0 143 copyChildrenToClipboard("clist");
michael@0 144 testSelectionToString(" mzla\n\n foo\n bazzinga!\n bar\n\n");
michael@0 145 testClipboardValue("text/unicode", " mzla\n\n foo\n bazzinga!\n bar\n\n");
michael@0 146 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>");
michael@0 147 testPasteText(" mzla\n\n foo\n bazzinga!\n bar\n\n");
michael@0 148
michael@0 149 copyChildrenToClipboard("div4");
michael@0 150 testSelectionToString(" Tt t t ");
michael@0 151 testClipboardValue("text/unicode", " Tt t t ");
michael@0 152 if (isXHTML) {
michael@0 153 testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n</div>");
michael@0 154 testInnerHTML("div4", "\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n");
michael@0 155 }
michael@0 156 else {
michael@0 157 testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea>t t t</textarea>\n</div>");
michael@0 158 testInnerHTML("div4", "\n T<textarea>t t t</textarea>\n");
michael@0 159 }
michael@0 160 testPasteText(" Tt t t ");
michael@0 161
michael@0 162 copyChildrenToClipboard("div5");
michael@0 163 testSelectionToString(" T ");
michael@0 164 testClipboardValue("text/unicode", " T ");
michael@0 165 if (isXHTML) {
michael@0 166 testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\"> </textarea>\n</div>");
michael@0 167 testInnerHTML("div5", "\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\"> </textarea>\n");
michael@0 168 }
michael@0 169 else {
michael@0 170 testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea> </textarea>\n</div>");
michael@0 171 testInnerHTML("div5", "\n T<textarea> </textarea>\n");
michael@0 172 }
michael@0 173 testPasteText(" T ");
michael@0 174
michael@0 175 copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1,suppressUnicodeCheckIfHidden);
michael@0 176 testSelectionToString("");
michael@0 177 // START Disabled due to bug 564688
michael@0 178 if (false) {
michael@0 179 testClipboardValue("text/unicode", "");
michael@0 180 testClipboardValue("text/html", "");
michael@0 181 }
michael@0 182 // END Disabled due to bug 564688
michael@0 183 testInnerHTML("div6", "div6");
michael@0 184
michael@0 185 copyRangeToClipboard($("div7").childNodes[0],0, $("div7").childNodes[0],4,suppressUnicodeCheckIfHidden);
michael@0 186 testSelectionToString("");
michael@0 187 // START Disabled due to bug 564688
michael@0 188 if (false) {
michael@0 189 testClipboardValue("text/unicode", "");
michael@0 190 testClipboardValue("text/html", "");
michael@0 191 }
michael@0 192 // END Disabled due to bug 564688
michael@0 193 testInnerHTML("div7", "div7");
michael@0 194
michael@0 195 copyRangeToClipboard($("div8").childNodes[0],0, $("div8").childNodes[0],4,suppressUnicodeCheckIfHidden);
michael@0 196 testSelectionToString("");
michael@0 197 // START Disabled due to bug 564688
michael@0 198 if (false) {
michael@0 199 testClipboardValue("text/unicode", "");
michael@0 200 testClipboardValue("text/html", "");
michael@0 201 }
michael@0 202 // END Disabled due to bug 564688
michael@0 203 testInnerHTML("div8", "div8");
michael@0 204
michael@0 205 copyRangeToClipboard($("div9").childNodes[0],0, $("div9").childNodes[0],4,suppressUnicodeCheckIfHidden);
michael@0 206 testSelectionToString("div9");
michael@0 207 testClipboardValue("text/unicode", "div9");
michael@0 208 testClipboardValue("text/html", "div9");
michael@0 209 testInnerHTML("div9", "div9");
michael@0 210
michael@0 211 copyToClipboard($("div10"), suppressUnicodeCheckIfHidden);
michael@0 212 testSelectionToString("");
michael@0 213 testInnerHTML("div10", "div10");
michael@0 214
michael@0 215 copyToClipboard($("div10").firstChild, suppressUnicodeCheckIfHidden);
michael@0 216 testSelectionToString("");
michael@0 217
michael@0 218 copyRangeToClipboard($("div10").childNodes[0],0, $("div10").childNodes[0],1,suppressUnicodeCheckIfHidden);
michael@0 219 testSelectionToString("");
michael@0 220
michael@0 221 copyRangeToClipboard($("div10").childNodes[1],0, $("div10").childNodes[1],1,suppressUnicodeCheckIfHidden);
michael@0 222 testSelectionToString("");
michael@0 223
michael@0 224 // ============ copy/paste test from/to a textarea
michael@0 225
michael@0 226 var val = "1\n 2\n 3";
michael@0 227 textarea.value=val;
michael@0 228 textarea.select();
michael@0 229 textarea.editor.copy();
michael@0 230
michael@0 231 textarea.value="";
michael@0 232 textarea.editor.paste(1);
michael@0 233 is(textarea.value, val);
michael@0 234 textarea.value="";
michael@0 235
michael@0 236 // ============ NOSCRIPT should not be copied
michael@0 237
michael@0 238 copyChildrenToClipboard("div13");
michael@0 239 testSelectionToString("__");
michael@0 240 testClipboardValue("text/unicode", "__");
michael@0 241 testClipboardValue("text/html", "<div id=\"div13\">__</div>");
michael@0 242 testPasteText("__");
michael@0 243
michael@0 244 // ============ converting cell boundaries to tabs in tables
michael@0 245
michael@0 246 copyToClipboard($("tr1"));
michael@0 247 testClipboardValue("text/unicode", "foo\tbar");
michael@0 248
michael@0 249 // ============ manipulating Selection in oncopy
michael@0 250
michael@0 251 copyRangeToClipboard($("div11").childNodes[0],0, $("div11").childNodes[1],2);
michael@0 252 testClipboardValue("text/unicode", "Xdiv11");
michael@0 253 testClipboardValue("text/html", "<div><p>X<span>div</span>11</p></div>");
michael@0 254 setTimeout(function(){testSelectionToString("div11")},0);
michael@0 255
michael@0 256 setTimeout(function(){
michael@0 257 copyRangeToClipboard($("div12").childNodes[0],0, $("div12").childNodes[1],2);
michael@0 258 testClipboardValue("text/unicode", "Xdiv12");
michael@0 259 testClipboardValue("text/html", "<div><p>X<span>div</span>12</p></div>");
michael@0 260 setTimeout(function(){
michael@0 261 testSelectionToString("div12");
michael@0 262 setTimeout(SimpleTest.finish,0);
michael@0 263 },0);
michael@0 264 },0);
michael@0 265 }

mercurial