content/base/test/copypaste.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/copypaste.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,265 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function modifySelection(s) {
     1.8 +  var g = window.getSelection();
     1.9 +  var l = g.getRangeAt(0);
    1.10 +  var d = document.createElement("p");
    1.11 +  d.innerHTML = s;
    1.12 +  d.appendChild(l.cloneContents());
    1.13 +
    1.14 +  var e = document.createElement("div");
    1.15 +  document.body.appendChild(e);
    1.16 +  e.appendChild(d);
    1.17 +  var a = document.createRange();
    1.18 +  a.selectNode(d);
    1.19 +  g.removeAllRanges();
    1.20 +  g.addRange(a);
    1.21 +  window.setTimeout(function () {
    1.22 +      e.parentNode.removeChild(e);
    1.23 +      g.removeAllRanges();
    1.24 +      g.addRange(l);
    1.25 +  }, 0)
    1.26 +}
    1.27 +
    1.28 +function getLoadContext() {
    1.29 +  var Ci = SpecialPowers.Ci;
    1.30 +  return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
    1.31 +                                   .getInterface(Ci.nsIWebNavigation)
    1.32 +                                   .QueryInterface(Ci.nsILoadContext);
    1.33 +}
    1.34 +
    1.35 +function testCopyPaste (isXHTML) {
    1.36 +  var suppressUnicodeCheckIfHidden = !!isXHTML;
    1.37 +  var suppressHTMLCheck = !!isXHTML;
    1.38 +
    1.39 +  var webnav = SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
    1.40 +                     .getInterface(SpecialPowers.Ci.nsIWebNavigation)
    1.41 +
    1.42 +  var docShell = webnav.QueryInterface(SpecialPowers.Ci.nsIDocShell);
    1.43 +
    1.44 +  var documentViewer = docShell.contentViewer
    1.45 +                               .QueryInterface(SpecialPowers.Ci.nsIContentViewerEdit);
    1.46 +
    1.47 +  var clipboard = SpecialPowers.Services.clipboard;
    1.48 +
    1.49 +  var textarea = SpecialPowers.wrap(document.getElementById('input'));
    1.50 +
    1.51 +  function copySelectionToClipboard(suppressUnicodeCheck) {
    1.52 +    documentViewer.copySelection();
    1.53 +    if (!suppressUnicodeCheck)
    1.54 +      ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), "check text/unicode");
    1.55 +    if (!suppressHTMLCheck)
    1.56 +      ok(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), "check text/html");
    1.57 +  }
    1.58 +  function copyToClipboard(node, suppressUnicodeCheck) {
    1.59 +    textarea.blur();
    1.60 +    clipboard.emptyClipboard(1);
    1.61 +    var sel = window.getSelection();
    1.62 +    sel.removeAllRanges();
    1.63 +    var r = document.createRange();
    1.64 +    r.selectNode(node);
    1.65 +    window.getSelection().addRange(r);
    1.66 +    copySelectionToClipboard(suppressUnicodeCheck);
    1.67 +  }
    1.68 +  function copyRangeToClipboard(startNode,startIndex,endNode,endIndex,suppressUnicodeCheck) {
    1.69 +    textarea.blur();
    1.70 +    clipboard.emptyClipboard(1);
    1.71 +    var sel = window.getSelection();
    1.72 +    sel.removeAllRanges();
    1.73 +    var r = document.createRange();
    1.74 +    r.setStart(startNode,startIndex)
    1.75 +    r.setEnd(endNode,endIndex)
    1.76 +    window.getSelection().addRange(r);
    1.77 +    copySelectionToClipboard(suppressUnicodeCheck);
    1.78 +  }
    1.79 +  function copyChildrenToClipboard(id) {
    1.80 +    textarea.blur();
    1.81 +    clipboard.emptyClipboard(1);
    1.82 +    window.getSelection().selectAllChildren(document.getElementById(id));
    1.83 +    copySelectionToClipboard();
    1.84 +  }
    1.85 +  function getClipboardData(mime) {
    1.86 +    var transferable = SpecialPowers.Cc['@mozilla.org/widget/transferable;1']
    1.87 +                                    .createInstance(SpecialPowers.Ci.nsITransferable);
    1.88 +    transferable.init(getLoadContext());
    1.89 +    transferable.addDataFlavor(mime);
    1.90 +    clipboard.getData(transferable, 1);
    1.91 +    var data = SpecialPowers.createBlankObject();
    1.92 +    transferable.getTransferData(mime, data, {}) ;
    1.93 +    return data;
    1.94 +  }
    1.95 +  function testClipboardValue(mime, expected) {
    1.96 +    if (suppressHTMLCheck && mime == "text/html")
    1.97 +      return null;
    1.98 +    var data = SpecialPowers.wrap(getClipboardData(mime));
    1.99 +    is (data.value == null ? data.value :
   1.100 +        data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data,
   1.101 +      expected,
   1.102 +      mime + " value in the clipboard");
   1.103 +    return data.value;
   1.104 +  }
   1.105 +  function testPasteText(expected) {
   1.106 +    textarea.value="";
   1.107 +    textarea.focus();
   1.108 +    textarea.editor.paste(1);
   1.109 +    is(textarea.value, expected, "value of the textarea after the paste");
   1.110 +  }
   1.111 +  function testSelectionToString(expected) {
   1.112 +    is(window.getSelection().toString().replace(/\r\n/g,"\n"), expected, "Selection.toString");
   1.113 +  }
   1.114 +  function testInnerHTML(id, expected) {
   1.115 +    var value = document.getElementById(id).innerHTML;
   1.116 +    is(value, expected, id + ".innerHTML");
   1.117 +  }
   1.118 +  function testEmptyChildren(id) {
   1.119 +    copyChildrenToClipboard(id);
   1.120 +    testSelectionToString("");
   1.121 +    testClipboardValue("text/unicode", null);
   1.122 +    testClipboardValue("text/html", null);
   1.123 +    testPasteText("");
   1.124 +  }
   1.125 +
   1.126 +  copyChildrenToClipboard("draggable");
   1.127 +  testSelectionToString("This is a draggable bit of text.");
   1.128 +  testClipboardValue("text/unicode",
   1.129 +                     "This is a draggable bit of text.");
   1.130 +  testClipboardValue("text/html",
   1.131 +                     "<div id=\"draggable\" title=\"title to have a long HTML line\">This is a <em>draggable</em> bit of text.</div>");
   1.132 +  testPasteText("This is a draggable bit of text.");
   1.133 +
   1.134 +  copyChildrenToClipboard("alist");
   1.135 +  testSelectionToString(" bla\n\n    foo\n    bar\n\n");
   1.136 +  testClipboardValue("text/unicode", " bla\n\n    foo\n    bar\n\n");
   1.137 +  testClipboardValue("text/html", "<div id=\"alist\">\n    bla\n    <ul>\n      <li>foo</li>\n      \n      <li>bar</li>\n    </ul>\n  </div>");
   1.138 +  testPasteText(" bla\n\n    foo\n    bar\n\n");
   1.139 +
   1.140 +  copyChildrenToClipboard("blist");
   1.141 +  testSelectionToString(" mozilla\n\n    foo\n    bar\n\n");
   1.142 +  testClipboardValue("text/unicode", " mozilla\n\n    foo\n    bar\n\n");
   1.143 +  testClipboardValue("text/html", "<div id=\"blist\">\n    mozilla\n    <ol>\n      <li>foo</li>\n      \n      <li>bar</li>\n    </ol>\n  </div>");
   1.144 +  testPasteText(" mozilla\n\n    foo\n    bar\n\n");
   1.145 +
   1.146 +  copyChildrenToClipboard("clist");
   1.147 +  testSelectionToString(" mzla\n\n    foo\n        bazzinga!\n    bar\n\n");
   1.148 +  testClipboardValue("text/unicode", " mzla\n\n    foo\n        bazzinga!\n    bar\n\n");
   1.149 +  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>");
   1.150 +  testPasteText(" mzla\n\n    foo\n        bazzinga!\n    bar\n\n");
   1.151 +
   1.152 +  copyChildrenToClipboard("div4");
   1.153 +  testSelectionToString(" Tt t t ");
   1.154 +  testClipboardValue("text/unicode", " Tt t t ");
   1.155 +  if (isXHTML) {
   1.156 +    testClipboardValue("text/html", "<div id=\"div4\">\n  T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n</div>");
   1.157 +    testInnerHTML("div4", "\n  T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n");
   1.158 +  }
   1.159 +  else {
   1.160 +    testClipboardValue("text/html", "<div id=\"div4\">\n  T<textarea>t t t</textarea>\n</div>");
   1.161 +    testInnerHTML("div4", "\n  T<textarea>t t t</textarea>\n");
   1.162 +  }
   1.163 +  testPasteText(" Tt t t ");
   1.164 +
   1.165 +  copyChildrenToClipboard("div5");
   1.166 +  testSelectionToString(" T ");
   1.167 +  testClipboardValue("text/unicode", " T ");
   1.168 +  if (isXHTML) {
   1.169 +    testClipboardValue("text/html", "<div id=\"div5\">\n  T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">     </textarea>\n</div>");
   1.170 +    testInnerHTML("div5", "\n  T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">     </textarea>\n");
   1.171 +  }
   1.172 +  else {
   1.173 +    testClipboardValue("text/html", "<div id=\"div5\">\n  T<textarea>     </textarea>\n</div>");
   1.174 +    testInnerHTML("div5", "\n  T<textarea>     </textarea>\n");
   1.175 +  }
   1.176 +  testPasteText(" T ");
   1.177 +
   1.178 +  copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1,suppressUnicodeCheckIfHidden);
   1.179 +  testSelectionToString("");
   1.180 +// START Disabled due to bug 564688
   1.181 +if (false) {
   1.182 +  testClipboardValue("text/unicode", "");
   1.183 +  testClipboardValue("text/html", "");
   1.184 +}
   1.185 +// END Disabled due to bug 564688
   1.186 +  testInnerHTML("div6", "div6");
   1.187 +
   1.188 +  copyRangeToClipboard($("div7").childNodes[0],0, $("div7").childNodes[0],4,suppressUnicodeCheckIfHidden);
   1.189 +  testSelectionToString("");
   1.190 +// START Disabled due to bug 564688
   1.191 +if (false) {
   1.192 +  testClipboardValue("text/unicode", "");
   1.193 +  testClipboardValue("text/html", "");
   1.194 +}
   1.195 +// END Disabled due to bug 564688
   1.196 +  testInnerHTML("div7", "div7");
   1.197 +
   1.198 +  copyRangeToClipboard($("div8").childNodes[0],0, $("div8").childNodes[0],4,suppressUnicodeCheckIfHidden);
   1.199 +  testSelectionToString("");
   1.200 +// START Disabled due to bug 564688
   1.201 +if (false) {
   1.202 +  testClipboardValue("text/unicode", "");
   1.203 +  testClipboardValue("text/html", "");
   1.204 +}
   1.205 +// END Disabled due to bug 564688
   1.206 +  testInnerHTML("div8", "div8");
   1.207 +
   1.208 +  copyRangeToClipboard($("div9").childNodes[0],0, $("div9").childNodes[0],4,suppressUnicodeCheckIfHidden);
   1.209 +  testSelectionToString("div9");
   1.210 +  testClipboardValue("text/unicode", "div9");
   1.211 +  testClipboardValue("text/html", "div9");
   1.212 +  testInnerHTML("div9", "div9");
   1.213 +
   1.214 +  copyToClipboard($("div10"), suppressUnicodeCheckIfHidden);
   1.215 +  testSelectionToString("");
   1.216 +  testInnerHTML("div10", "div10");
   1.217 +
   1.218 +  copyToClipboard($("div10").firstChild, suppressUnicodeCheckIfHidden);
   1.219 +  testSelectionToString("");
   1.220 +
   1.221 +  copyRangeToClipboard($("div10").childNodes[0],0, $("div10").childNodes[0],1,suppressUnicodeCheckIfHidden);
   1.222 +  testSelectionToString("");
   1.223 +
   1.224 +  copyRangeToClipboard($("div10").childNodes[1],0, $("div10").childNodes[1],1,suppressUnicodeCheckIfHidden);
   1.225 +  testSelectionToString("");
   1.226 +
   1.227 +  // ============ copy/paste test from/to a textarea
   1.228 +
   1.229 +  var val = "1\n 2\n  3";
   1.230 +  textarea.value=val;
   1.231 +  textarea.select();
   1.232 +  textarea.editor.copy();
   1.233 +  
   1.234 +  textarea.value="";
   1.235 +  textarea.editor.paste(1);
   1.236 +  is(textarea.value, val);
   1.237 +  textarea.value="";
   1.238 +
   1.239 +  // ============ NOSCRIPT should not be copied
   1.240 +
   1.241 +  copyChildrenToClipboard("div13");
   1.242 +  testSelectionToString("__");
   1.243 +  testClipboardValue("text/unicode", "__");
   1.244 +  testClipboardValue("text/html", "<div id=\"div13\">__</div>");
   1.245 +  testPasteText("__");
   1.246 +
   1.247 +  // ============ converting cell boundaries to tabs in tables
   1.248 +
   1.249 +  copyToClipboard($("tr1"));
   1.250 +  testClipboardValue("text/unicode", "foo\tbar");
   1.251 +
   1.252 +  // ============ manipulating Selection in oncopy
   1.253 +
   1.254 +  copyRangeToClipboard($("div11").childNodes[0],0, $("div11").childNodes[1],2);
   1.255 +  testClipboardValue("text/unicode", "Xdiv11");
   1.256 +  testClipboardValue("text/html", "<div><p>X<span>div</span>11</p></div>");
   1.257 +  setTimeout(function(){testSelectionToString("div11")},0);
   1.258 +
   1.259 +  setTimeout(function(){
   1.260 +    copyRangeToClipboard($("div12").childNodes[0],0, $("div12").childNodes[1],2);
   1.261 +    testClipboardValue("text/unicode", "Xdiv12");
   1.262 +    testClipboardValue("text/html", "<div><p>X<span>div</span>12</p></div>");
   1.263 +    setTimeout(function(){ 
   1.264 +      testSelectionToString("div12"); 
   1.265 +      setTimeout(SimpleTest.finish,0);
   1.266 +    },0);
   1.267 +  },0);
   1.268 +}

mercurial