tor-browser

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

test_bug166235.html (6495B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=166235
      5 https://bugzilla.mozilla.org/show_bug.cgi?id=816298
      6 -->
      7 <head>
      8  <title>Test for Bug 166235</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11 </head>
     12 <body>
     13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=166235">Mozilla Bug 166235 and Bug 816298</a>
     14 <p id="test0">This text should be copied.</p>
     15 <p id="test1">This text should<span style="user-select: none;"> NOT</span> be copied.</p>
     16 <p id="test2">This<span style="user-select: none;"><span style="user-select: text"> text should</span> NOT</span> be copied.</p>
     17 <p id="test3">This text should<span style="user-select: -moz-none;"> NOT</span> be copied.</p>
     18 <p id="test4">This<span style="user-select: -moz-none;"><span style="user-select: text"> text should</span> NOT</span> be copied.</p>
     19 <p id="test5">This<span style="user-select: all"> text should</span> be copied.</p>
     20 <div id="content" style="display: none">
     21 
     22 </div>
     23 <textarea id="input"></textarea>
     24 <pre id="test">
     25 <script type="application/javascript">
     26  "use strict";
     27 
     28 /** Test for Bug 166235 */
     29  var Cc = SpecialPowers.Cc;
     30  var Ci = SpecialPowers.Ci;
     31 
     32  var docShell = SpecialPowers.wrap(window).docShell;
     33 
     34  var documentViewer = docShell.docViewer
     35                               .QueryInterface(SpecialPowers.Ci.nsIDocumentViewerEdit);
     36 
     37  var clipboard = Cc["@mozilla.org/widget/clipboard;1"]
     38                    .getService(SpecialPowers.Ci.nsIClipboard);
     39 
     40  var textarea = SpecialPowers.wrap(document.getElementById('input'));
     41 
     42  function getLoadContext() {
     43    return SpecialPowers.wrap(window).docShell
     44                                     .QueryInterface(Ci.nsILoadContext);
     45  }
     46 
     47  function copyChildrenToClipboard(id) {
     48    textarea.blur();
     49    clipboard.emptyClipboard(1);
     50    window.getSelection().selectAllChildren(document.getElementById(id));
     51    documentViewer.copySelection();
     52 
     53    is(clipboard.hasDataMatchingFlavors(["text/plain"], 1), true);
     54    is(clipboard.hasDataMatchingFlavors(["text/html"], 1), true);
     55  }
     56  function getClipboardData(mime) {
     57    var transferable = Cc['@mozilla.org/widget/transferable;1']
     58                         .createInstance(SpecialPowers.Ci.nsITransferable);
     59    transferable.init(getLoadContext());
     60    transferable.addDataFlavor(mime);
     61    clipboard.getData(transferable, 1, SpecialPowers.wrap(window).browsingContext.currentWindowContext);
     62    var data = SpecialPowers.createBlankObject();
     63    transferable.getTransferData(mime, data) ;
     64    return SpecialPowers.wrap(data);
     65  }
     66  function testHtmlClipboardValue(mime, expected, test) {
     67    var expectedValue = expected;
     68    // For Windows, navigator.platform returns "Win32".
     69    if (navigator.platform.includes("Win")) {
     70      expectedValue = kTextHtmlPrefixClipboardDataWindows + expected + kTextHtmlSuffixClipboardDataWindows;
     71    }
     72    testClipboardValue(mime, expectedValue, test);
     73  }
     74  function testClipboardValue(mime, expected, test) {
     75    var data = getClipboardData(mime);
     76    is (data.value == null ? data.value :
     77        data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data,
     78      expected,
     79      mime + " value in the clipboard");
     80    return data.value;
     81  }
     82  function testPasteText(expected, test) {
     83    textarea.value="";
     84    textarea.focus();
     85    textarea.editor.paste(1);
     86    is(textarea.value, expected, test + ": textarea paste");
     87  }
     88  function testInnerHTML(id, expected) {
     89    var value = document.getElementById(id).innerHTML;
     90    is(value, expected, id + ".innerHTML");
     91  }
     92 
     93 // expected results for Selection.toString()
     94 var originalStrings = [
     95  'This text should be copied.',
     96  'This text should be copied.',
     97  'This text should be copied.',
     98  'This text should be copied.',
     99  'This text should be copied.',
    100  'This text should be copied.'
    101 ];
    102 
    103 // expected results for clipboard text/html
    104 const includeCommonAncestor = SpecialPowers.getBoolPref(
    105  "dom.serializer.includeCommonAncestor.enabled"
    106 );
    107 var clipboardHTML = [
    108  `${includeCommonAncestor ? "<p id=\"test0\">" : ""}This text should be copied.${includeCommonAncestor ? "</p>" : ""}`,
    109  `${includeCommonAncestor ? "<p id=\"test1\">" : ""}This text should be copied.${includeCommonAncestor ? "</p>" : ""}`,
    110  `${includeCommonAncestor ? "<p id=\"test2\">" : ""}This<span style=\"user-select: text\"> text should</span> be copied.${includeCommonAncestor ? "</p>" : ""}`,
    111  `${includeCommonAncestor ? "<p id=\"test3\">" : ""}This text should be copied.${includeCommonAncestor ? "</p>" : ""}`,
    112  `${includeCommonAncestor ? "<p id=\"test4\">" : ""}This<span style=\"user-select: text\"> text should</span> be copied.${includeCommonAncestor ? "</p>" : ""}`,
    113  `${includeCommonAncestor ? "<p id=\"test5\">" : ""}This<span style=\"user-select: all\"> text should</span> be copied.${includeCommonAncestor ? "</p>" : ""}`,
    114 ];
    115 
    116 // expected results for clipboard text/plain
    117 var clipboardUnicode = [
    118  'This text should be copied.',
    119  'This text should be copied.',
    120  'This text should be copied.',
    121  'This text should be copied.',
    122  'This text should be copied.',
    123  'This text should be copied.'
    124 ];
    125 
    126 // expected results for .innerHTML
    127 var innerHTMLStrings = [
    128  'This text should be copied.',
    129  'This text should<span style=\"user-select: none;\"> NOT</span> be copied.',
    130  'This<span style=\"user-select: none;\"><span style=\"user-select: text\"> text should</span> NOT</span> be copied.',
    131  'This text should<span style=\"user-select: -moz-none;\"> NOT</span> be copied.',
    132  'This<span style=\"user-select: -moz-none;\"><span style=\"user-select: text\"> text should</span> NOT</span> be copied.',
    133  'This<span style=\"user-select: all\"> text should</span> be copied.',
    134 ];
    135 
    136 // expected results for pasting into a TEXTAREA
    137 var textareaStrings = [
    138  'This text should be copied.',
    139  'This text should be copied.',
    140  'This text should be copied.',
    141  'This text should be copied.',
    142  'This text should be copied.',
    143  'This text should be copied.'
    144 ];
    145 
    146 for (var i = 0; i < originalStrings.length; i++) {
    147  var id = 'test' + i;
    148  copyChildrenToClipboard(id);
    149  is(window.getSelection().toString(), originalStrings[i], id + ' Selection.toString()');
    150  testHtmlClipboardValue("text/html", clipboardHTML[i], id);
    151  testClipboardValue("text/plain", clipboardUnicode[i], id);
    152  testInnerHTML(id, innerHTMLStrings[i]);
    153  testPasteText(textareaStrings[i], id + '.innerHTML');
    154 }
    155 
    156 </script>
    157 </pre>
    158 </body>
    159 </html>