tor-browser

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

test_htmlcopyencoder.xhtml (8875B)


      1 <!DOCTYPE HTML>
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <!--
      4 -->
      5 <head>
      6  <title>Test the html copy encoder with XHTML</title>
      7  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      9 </head>
     10 <body>
     11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=422403">Mozilla Bug </a>
     12 <p id="display"></p>
     13 <div id="content" style="display: none">
     14 </div>
     15 <pre id="test">
     16 <script class="testbody" type="text/javascript">
     17 //<![CDATA[
     18 function testHtmlCopyEncoder () {
     19  const de = SpecialPowers.Ci.nsIDocumentEncoder;
     20  var encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
     21  var out, expected;
     22 
     23  var node = document.getElementById('draggable');
     24 
     25 
     26  // in the following tests, we must use the OutputLFLineBreak flag, to avoid
     27  // to have the default line break of the platform in the result, so the test
     28  // can pass on all platform
     29 
     30 
     31  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
     32  encoder.setContainerNode(node);
     33  out = encoder.encodeToString();
     34  expected = 'This is a <em>draggable</em> <br>bit of text.';
     35  is(out, expected, "test container node ");
     36 
     37  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
     38  encoder.setNode(node);
     39  out = encoder.encodeToString();
     40  expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> <br>bit of text.</div>";
     41  is(out, expected, "test node");
     42 
     43  var select = window.getSelection();
     44  select.selectAllChildren(node);
     45  
     46  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
     47  encoder.setSelection(select);
     48  out = encoder.encodeToString();
     49  expected = "<div style=\"display: none;\">\n\n<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> <br>bit of text.</div>\n\n</div>";
     50  todo_is(out, expected, "test selection");
     51 
     52  node.nextSibling.data="\nfoo bar\n";
     53  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
     54  encoder.setSelection(select);
     55  out = encoder.encodeToString();
     56  expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em>\n <br>bit of text.</div>";
     57  todo_is(out, expected, "test selection with additional data");
     58 
     59  node = document.getElementById('aList');
     60 
     61  select = window.getSelection();
     62  select.selectAllChildren(node);  
     63 
     64  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
     65  encoder.setSelection(select);
     66  out = encoder.encodeToString();
     67  expected = '<ol id=\"aList\">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
     68  todo_is(out, expected, "test list selection");
     69 
     70  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
     71  encoder.setContainerNode(node);
     72  out = encoder.encodeToString();
     73  expected = '\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n';
     74  is(out, expected, "test list container node");
     75 
     76  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
     77  encoder.setNode(node);
     78  out = encoder.encodeToString();
     79  expected = "<ol id=\"aList\">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>";
     80  is(out, expected, "test list node");
     81 
     82  var liList = node.getElementsByTagName("li");
     83  var range = document.createRange();
     84 
     85  // selection start at the first child of the ol, and end after the element ol
     86  range.setStart(node, 1);
     87  range.setEnd(node.parentNode, 2);
     88  select.removeAllRanges();
     89  select.addRange(range);
     90  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
     91  encoder.setSelection(select);
     92  out = encoder.encodeToString();
     93  expected = '<ol id="aList">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
     94  todo_is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol");
     95 
     96  // selection start at the third child of the ol, and end after the element ol
     97  range.setStart(node, 3);
     98  range.setEnd(node.parentNode, 2);
     99  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    100  encoder.setSelection(select);
    101  out = encoder.encodeToString();
    102  expected = '<ol id="aList"><li value=\"2\">sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
    103  todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol");
    104 
    105 
    106  // selection start at the third child of the ol, and end after the element ol + ol start at the value 5
    107  range.setStart(node, 3);
    108  range.setEnd(node.parentNode, 2);
    109  node.setAttribute("start","5");
    110  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    111  encoder.setSelection(select);
    112  out = encoder.encodeToString();
    113  expected = '<ol start=\"5\" id=\"aList\"><li value=\"6\">sit amet, <strong>consectetuer</strong>\n </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
    114  todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol + ol start at the value 5");
    115 
    116 
    117  // selection contains only some child of the ol
    118  node.removeAttribute("start");
    119  range.setStart(node, 3);
    120  range.setEnd(node, 5);
    121  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    122  encoder.setSelection(select);
    123  out = encoder.encodeToString();
    124  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
    125  todo_is(out, expected, "test list selection with range: selection contains only some child of the ol");
    126 
    127  // selection contains only some child of the ol  + ol start at the value 5
    128  node.setAttribute("start","5");
    129  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    130  encoder.setSelection(select);
    131  out = encoder.encodeToString();
    132  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
    133  todo_is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");
    134 
    135 
    136  // selection contains only some child of the ol  + a value is set on the first li
    137  node.removeAttribute("start");
    138  liList[0].setAttribute("value","8");
    139  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    140  encoder.setSelection(select);
    141  out = encoder.encodeToString();
    142  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
    143  todo_is(out, expected, "test list selection: contains only some child of the ol  + a value is set on the first li");
    144 
    145  select.selectAllChildren(node);
    146  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
    147  encoder.setSelection(select);
    148  out = encoder.encodeToString();
    149  expected = '<ol id=\"aList\">\n   <li value=\"8\">Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
    150  todo_is(out, expected, "test list selection with a value on a LI");
    151 
    152  SimpleTest.finish();
    153 }
    154 
    155 
    156 SimpleTest.waitForExplicitFinish();
    157 
    158 addLoadEvent(testHtmlCopyEncoder);
    159 //]]>
    160 </script>
    161 </pre>
    162 <div style="display: none">
    163 
    164 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> <br/>bit of text.</div>
    165 
    166 </div>
    167 <div style="display: none">
    168 
    169 <ol id="aList">
    170   <li>Lorem ipsum dolor</li>
    171  <li>sit amet, <strong>consectetuer</strong> </li>
    172  <li>adipiscing elit</li>
    173  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>
    174  <li>aptent taciti</li>
    175 </ol>
    176 foo bar
    177 </div>
    178 </body>
    179 </html>