tor-browser

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

test_htmlcopyencoder_list.html (4889B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 -->
      5 <head>
      6 <title>Test on the html copy encoder for table selection</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=1953174">Mozilla Bug 1953174</a>
     12 <p id="display"></p>
     13 <div id="content" style="display: none"></div>
     14 <div id="container">
     15  <ul>
     16    <li>Item1</li>
     17    <li><span>Item2</span></li>
     18    <li><div>Item3</div></li>
     19    <li><ol><li>SubItem1</li></ol></li>
     20    <li><ol><li><span>SubItem2</span></li><li><div>SubItem3</div></li></ol></li>
     21    <div><li>ItemInDiv1</li></div>
     22    <span><li>ItemInSpan1</li></span>
     23    <div><li><span>ItemInDiv1</span></li></div>
     24  </ul>
     25 </div>
     26 <script>
     27 
     28 /* global describe, it, beforeEach */
     29 
     30 const alwaysIncludeCommonAncestor = SpecialPowers.getBoolPref(
     31  "dom.serializer.includeCommonAncestor.enabled"
     32 );
     33 
     34 const TESTS = [
     35  {
     36    selector: `ul li`,
     37    expectedResult: `<li>Item1</li>`,
     38    expectedContext: `<html><body><div id="container"><ul></ul></div></body></html>`,
     39  }, {
     40    selector: `ul li span`,
     41    expectedResult: `${alwaysIncludeCommonAncestor ? `<li>` : ``}` +
     42                    `<span>Item2</span>` +
     43                    `${alwaysIncludeCommonAncestor ? `</li>` : ``}`,
     44    expectedContext: `<html><body><div id="container"><ul>` +
     45                     `${alwaysIncludeCommonAncestor ? `` : `<li></li>`}` +
     46                     `</ul></div></body></html>`,
     47  }, {
     48    selector: `ul li div`,
     49    expectedResult: `${alwaysIncludeCommonAncestor ? `<li>` : ``}` +
     50                    `<div>Item3</div>` +
     51                    `${alwaysIncludeCommonAncestor ? `</li>` : ``}`,
     52    expectedContext: `<html><body><div id="container"><ul>` +
     53                     `${alwaysIncludeCommonAncestor ? `` : `<li></li>`}` +
     54                     `</ul></div></body></html>`,
     55  }, {
     56    selector: `ul ol li`,
     57    expectedResult: `${alwaysIncludeCommonAncestor ? `<li><ol>` : ``}` +
     58                    `<li>SubItem1</li>` +
     59                    `${alwaysIncludeCommonAncestor ? `</ol></li>` : ``}`,
     60    expectedContext: `<html><body><div id="container"><ul>` +
     61                     `${alwaysIncludeCommonAncestor ? `` : `<li><ol></ol></li>`}` +
     62                     `</ul></div></body></html>`,
     63  }, {
     64    selector: `ul ol li span`,
     65    expectedResult: `${alwaysIncludeCommonAncestor ? `<li>` : ``}` +
     66                    `<span>SubItem2</span>` +
     67                    `${alwaysIncludeCommonAncestor ? `</li>` : ``}`,
     68    expectedContext: `<html><body><div id="container"><ul><li><ol>` +
     69                     `${alwaysIncludeCommonAncestor ? `` : `<li></li>`}` +
     70                     `</ol></li></ul></div></body></html>`,
     71  }, {
     72    selector: `ul ol li div`,
     73    expectedResult: `${alwaysIncludeCommonAncestor ? `<li>` : ``}` +
     74                    `<div>SubItem3</div>` +
     75                    `${alwaysIncludeCommonAncestor ? `</li>` : ``}`,
     76    expectedContext: `<html><body><div id="container"><ul><li><ol>` +
     77                     `${alwaysIncludeCommonAncestor ? `` : `<li value="2"></li>`}` +
     78                     `</ol></li></ul></div></body></html>`,
     79  }, {
     80    selector: `ul span li`,
     81    expectedResult: `<span><li>ItemInSpan1</li></span>`,
     82    expectedContext: `<html><body><div id="container"><ul>` +
     83                     `${alwaysIncludeCommonAncestor ? `` : `<span></span>`}` +
     84                     `</ul></div></body></html>`,
     85  }, {
     86    selector: `ul div span`,
     87    expectedResult: `${alwaysIncludeCommonAncestor ? `<div><li>` : ``}` +
     88                    `<span>ItemInDiv1</span>` +
     89                    `${alwaysIncludeCommonAncestor ? `</li></div>` : ``}`,
     90    expectedContext: `<html><body><div id="container"><ul>` +
     91                     `${alwaysIncludeCommonAncestor ? `` : `<div><li></li></div>`}` +
     92                     `</ul></div></body></html>`,
     93  },
     94 ];
     95 
     96 const de = SpecialPowers.Ci.nsIDocumentEncoder;
     97 const encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
     98 
     99 TESTS.forEach(testCase => {
    100  it(`Selector: ${testCase.selector}`, () => {
    101    info("Select nodes");
    102    const selection = window.getSelection();
    103    selection.removeAllRanges();
    104 
    105    const range = document.createRange();
    106    selection.addRange(range);
    107 
    108    const target = document.querySelector(testCase.selector);
    109    range.selectNode(target);
    110 
    111    info("Initialize encoder");
    112    encoder.init(document, "text/html", 0);
    113    encoder.setSelection(selection);
    114 
    115    info("Check result");
    116    let htmlContext = { value: '' };
    117    let htmlInfo = { value: '' };
    118    is(encoder.encodeToStringWithContext(htmlContext, htmlInfo), testCase.expectedResult,
    119       `Check serialized output`);
    120    is(htmlContext.value, testCase.expectedContext, `Check serialized context`);
    121    is(htmlInfo.value, `0,0`, `Check serialized info`);
    122  });
    123 });
    124 
    125 </script>
    126 </body>
    127 </html>