tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug525389.html (7486B)


      1 <!DOCTYPE HTML>
      2 <html><head>
      3 <title>Test for bug 525389</title>
      4 <style src="/tests/SimpleTest/test.css" type="text/css"></style>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <script src="/tests/SimpleTest/EventUtils.js"></script>
      7 
      8 <script class="testbody" type="application/javascript">
      9 
     10  var utils = SpecialPowers.getDOMWindowUtils(window);
     11  var Cc = SpecialPowers.Cc;
     12  var Ci = SpecialPowers.Ci;
     13 
     14 function getLoadContext() {
     15  return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
     16 }
     17 
     18 async function runTest() {
     19  var pasteCount = 0;
     20  var pasteFunc = function() { pasteCount++; };
     21 
     22  function verifyContent(s) {
     23    var e = document.getElementById("i1");
     24    var doc = e.contentDocument;
     25    if (navigator.platform.includes("Win")) {
     26      // On Windows ignore \n which got left over from the removal of the fragment tags
     27      // <html><body>\n<!--StartFragment--> and <!--EndFragment-->\n</body>\n</html>.
     28      is(doc.body.innerHTML.replace(/\n/g, ""), s, "");
     29    } else {
     30      is(doc.body.innerHTML, s, "");
     31    }
     32  }
     33 
     34  function pasteInto(trans, html, target_id) {
     35    var e = document.getElementById("i1");
     36    var doc = e.contentDocument;
     37    doc.designMode = "on";
     38    doc.body.innerHTML = html;
     39    doc.defaultView.focus();
     40    if (target_id)
     41      e = doc.getElementById(target_id);
     42    else
     43      e = doc.body;
     44    var selection = doc.defaultView.getSelection();
     45    selection.removeAllRanges();
     46    selection.selectAllChildren(e);
     47    selection.collapseToEnd();
     48 
     49    pasteCount = 0;
     50    e.addEventListener("paste", pasteFunc);
     51    utils.sendContentCommandEvent("pasteTransferable", trans);
     52    e.removeEventListener("paste", pasteFunc);
     53 
     54    return e;
     55  }
     56 
     57  function getTransferableFromClipboard(asHTML) {
     58    var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
     59    trans.init(getLoadContext());
     60    if (asHTML) {
     61      trans.addDataFlavor("text/html");
     62    } else {
     63      trans.addDataFlavor("text/plain");
     64    }
     65    var clip = SpecialPowers.Services.clipboard;
     66    clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard, SpecialPowers.wrap(window).browsingContext.currentWindowContext);
     67    return trans;
     68  }
     69 
     70  // Commented out as the test for it below is also commented out.
     71  // function makeTransferable(s, asHTML, target_id) {
     72  //   var e = document.getElementById("i2");
     73  //   var doc = e.contentDocument;
     74  //   if (asHTML) {
     75  //     doc.body.innerHTML = s;
     76  //   } else {
     77  //     var text = doc.createTextNode(s);
     78  //     doc.body.appendChild(text);
     79  //   }
     80  //   doc.designMode = "on";
     81  //   doc.defaultView.focus();
     82  //   var selection = doc.defaultView.getSelection();
     83  //   selection.removeAllRanges();
     84  //   if (!target_id) {
     85  //     selection.selectAllChildren(doc.body);
     86  //   } else {
     87  //     var range = document.createRange();
     88  //     range.selectNode(doc.getElementById(target_id));
     89  //     selection.addRange(range);
     90  //   }
     91  //
     92  //   // We cannot use plain strings, we have to use nsSupportsString.
     93  //   var supportsStringClass = SpecialPowers.Components.classes["@mozilla.org/supports-string;1"];
     94  //   var ssData = supportsStringClass.createInstance(Ci.nsISupportsString);
     95  //
     96  //   // Create the transferable.
     97  //   var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
     98  //   trans.init(getLoadContext());
     99  //
    100  //   // Add the data to the transferable.
    101  //   if (asHTML) {
    102  //     trans.addDataFlavor("text/html");
    103  //     ssData.data = doc.body.innerHTML;
    104  //     trans.setTransferData("text/html", ssData);
    105  //   } else {
    106  //     trans.addDataFlavor("text/plain");
    107  //     ssData.data = doc.body.innerHTML;
    108  //     trans.setTransferData("text/plain", ssData);
    109  //   }
    110  //
    111  //   return trans;
    112  // }
    113 
    114  async function copyToClipBoard(s, asHTML, target_id) {
    115    var e = document.getElementById("i2");
    116    var doc = e.contentDocument;
    117    if (asHTML) {
    118      doc.body.innerHTML = s;
    119    } else {
    120      var text = doc.createTextNode(s);
    121      doc.body.appendChild(text);
    122    }
    123    doc.designMode = "on";
    124    doc.defaultView.focus();
    125    var selection = doc.defaultView.getSelection();
    126    selection.removeAllRanges();
    127    if (!target_id) {
    128      selection.selectAllChildren(doc.body);
    129    } else {
    130      var range = document.createRange();
    131      range.selectNode(doc.getElementById(target_id));
    132      selection.addRange(range);
    133    }
    134 
    135    await SimpleTest.promiseClipboardChange(() => true,
    136      () => { SpecialPowers.wrap(doc).execCommand("copy", false, null); });
    137 
    138    return e;
    139  }
    140 
    141  await copyToClipBoard("<span>Hello</span><span>Kitty</span>", true);
    142  var trans = getTransferableFromClipboard(true);
    143  pasteInto(trans, "");
    144  verifyContent("<span>Hello</span><span>Kitty</span>");
    145  is(pasteCount, 1, "paste event was not triggered");
    146 
    147  // this test is not working out exactly like the clipboard test
    148  // has to do with generating the nsITransferable above
    149  // trans = makeTransferable('<span>Hello</span><span>Kitty</span>', true);
    150  // pasteInto(trans, '');
    151  // verifyContent('<span>Hello</span><span>Kitty</span>');
    152 
    153  await copyToClipBoard("<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span>", true);
    154  trans = getTransferableFromClipboard(true);
    155  pasteInto(trans, '<ol><li id="paste_here">X</li></ol>', "paste_here");
    156  verifyContent('<ol><li id="paste_here">X<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span></li></ol>');
    157  is(pasteCount, 1, "paste event was not triggered");
    158 
    159 // The following test doesn't do what I expected, because the special handling
    160 // of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes
    161 // non-list/item children.  See bug 481177.
    162 //  await copyToClipBoard("<ol><li>Hello Kitty</li><span>Hello</span></ol>", true);
    163 //  pasteInto('<ol><li id="paste_here">X</li></ol>',"paste_here");
    164 //  verifyContent('<ol><li id="paste_here">X</li><li>Hello Kitty</li><span>Hello</span></ol>');
    165 
    166  await copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
    167  trans = getTransferableFromClipboard(true);
    168  pasteInto(trans, '<pre id="paste_here">Hello </pre>', "paste_here");
    169  verifyContent('<pre id="paste_here">Hello Kitty<span>Hello</span></pre>');
    170  is(pasteCount, 1, "paste event was not triggered");
    171 
    172  await copyToClipBoard('1<span style="display: contents">2</span>3', true);
    173  trans = getTransferableFromClipboard(true);
    174  pasteInto(trans, '<div id="paste_here"></div>', "paste_here");
    175  verifyContent('<div id="paste_here">1<span style="display: contents">2</span>3</div>');
    176  is(pasteCount, 1, "paste event was not triggered");
    177 
    178  // test that we can preventDefault pastes
    179  pasteFunc = function(event) { event.preventDefault(); return false; };
    180  await copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
    181  trans = getTransferableFromClipboard(true);
    182  pasteInto(trans, '<pre id="paste_here">Hello </pre>', "paste_here");
    183  verifyContent('<pre id="paste_here">Hello </pre>');
    184  is(pasteCount, 0, "paste event was triggered");
    185 }
    186 
    187 SimpleTest.waitForExplicitFinish();
    188 addLoadEvent(() => {
    189  add_task(async function test_copy() {
    190    await runTest();
    191  });
    192 });
    193 
    194 </script>
    195 </head>
    196 <body>
    197 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=525389">Mozilla Bug 525389</a>
    198 <p id="display"></p>
    199 
    200 <pre id="test">
    201 </pre>
    202 
    203 <iframe id="i1" width="200" height="100" src="about:blank"></iframe><br>
    204 <iframe id="i2" width="200" height="100" src="about:blank"></iframe><br>
    205 
    206 </body>
    207 </html>