tor-browser

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

test_htmlcopyencoder_table.html (5031B)


      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  <table>
     16    <caption><span>Title1</span></caption>
     17    <thead>
     18      <tr><th>First</th><th><span>Second</span></th><th><div>Third</div></th></tr>
     19    </thead>
     20    <tbody>
     21      <tr><th>Colume1</th><td>Data11</td><td><span>Data12</span></td></tr>
     22      <tr><th><span>Colume2</span></th><td><div>Data21</div></td></tr>
     23      <tr><th><div>Colume3</div></th></tr>
     24    </tbody>
     25    <tfoot>
     26      <tr><th>Footer</th><td>DataFooter</td></tr>
     27    </tfoot>
     28  </table>
     29 </div>
     30 <script>
     31 
     32 /* global describe, it, beforeEach */
     33 
     34 const alwaysIncludeCommonAncestor = SpecialPowers.getBoolPref(
     35  "dom.serializer.includeCommonAncestor.enabled"
     36 );
     37 
     38 const TESTS = [
     39  {
     40    selector: `caption`,
     41    expectedResult: `<table><caption><span>Title1</span></caption></table>`,
     42    expectedContext: `<html><body><div id="container"><table></table></div></body></html>`,
     43  }, {
     44    selector: `caption span`,
     45    expectedResult: `${alwaysIncludeCommonAncestor ? `<table><caption>` : ``}` +
     46                    `<span>Title1</span>` +
     47                    `${alwaysIncludeCommonAncestor ? `</caption></table>` : ``}`,
     48    expectedContext: `<html><body><div id="container"><table>` +
     49                     `${alwaysIncludeCommonAncestor ? `` : `<caption></caption>`}` +
     50                     `</table></div></body></html>`,
     51  }, {
     52    selector: `thead th`,
     53    expectedResult: `<table><thead><tr><th>First</th></tr></thead></table>`,
     54    expectedContext: `<html><body><div id="container"><table><thead></thead></table></div></body></html>`,
     55  }, {
     56    selector: `thead th span`,
     57    expectedResult: `<span>Second</span>`,
     58    expectedContext: `<html><body><div id="container"><table><thead><tr><th></th></tr></thead></table></div></body></html>`,
     59  }, {
     60    selector: `thead th div`,
     61    expectedResult: `<div>Third</div>`,
     62    expectedContext: `<html><body><div id="container"><table><thead><tr><th></th></tr></thead></table></div></body></html>`,
     63  }, {
     64    selector: `tbody th`,
     65    expectedResult: `<table><tbody><tr><th>Colume1</th></tr></tbody></table>`,
     66    expectedContext: `<html><body><div id="container"><table><tbody></tbody></table></div></body></html>`,
     67  }, {
     68    selector: `tbody th span`,
     69    expectedResult: `<span>Colume2</span>`,
     70    expectedContext: `<html><body><div id="container"><table><tbody><tr><th></th></tr></tbody></table></div></body></html>`,
     71  }, {
     72    selector: `tbody th div`,
     73    expectedResult: `<div>Colume3</div>`,
     74    expectedContext: `<html><body><div id="container"><table><tbody><tr><th></th></tr></tbody></table></div></body></html>`,
     75  }, {
     76    selector: `tbody td`,
     77    expectedResult: `<table><tbody><tr><td>Data11</td></tr></tbody></table>`,
     78    expectedContext: `<html><body><div id="container"><table><tbody></tbody></table></div></body></html>`,
     79  }, {
     80    selector: `tbody td span`,
     81    expectedResult: `<span>Data12</span>`,
     82    expectedContext: `<html><body><div id="container"><table><tbody><tr><td></td></tr></tbody></table></div></body></html>`,
     83  }, {
     84    selector: `tbody td div`,
     85    expectedResult: `<div>Data21</div>`,
     86    expectedContext: `<html><body><div id="container"><table><tbody><tr><td></td></tr></tbody></table></div></body></html>`,
     87  }, {
     88    selector: `tfoot th`,
     89    expectedResult: `<table><tfoot><tr><th>Footer</th></tr></tfoot></table>`,
     90    expectedContext: `<html><body><div id="container"><table><tfoot></tfoot></table></div></body></html>`,
     91  }, {
     92    selector: `tfoot td`,
     93    expectedResult: `<table><tfoot><tr><td>DataFooter</td></tr></tfoot></table>`,
     94    expectedContext: `<html><body><div id="container"><table><tfoot></tfoot></table></div></body></html>`,
     95  }
     96 ];
     97 
     98 const de = SpecialPowers.Ci.nsIDocumentEncoder;
     99 const encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
    100 
    101 TESTS.forEach(testCase => {
    102  it(`Selector: ${testCase.selector}`, () => {
    103    info("Select nodes");
    104    const selection = window.getSelection();
    105    selection.removeAllRanges();
    106 
    107    const range = document.createRange();
    108    selection.addRange(range);
    109 
    110    const target = document.querySelector(testCase.selector);
    111    range.selectNode(target);
    112 
    113    info("Initialize encoder");
    114    encoder.init(document, "text/html", 0);
    115    encoder.setSelection(selection);
    116 
    117    info("Check result");
    118    let htmlContext = { value: '' };
    119    let htmlInfo = { value: '' };
    120    is(encoder.encodeToStringWithContext(htmlContext, htmlInfo), testCase.expectedResult,
    121       `Check serialized output`);
    122    is(htmlContext.value, testCase.expectedContext, `Check serialized context`);
    123    is(htmlInfo.value, `0,0`, `Check serialized info`);
    124  });
    125 });
    126 
    127 </script>
    128 </body>
    129 </html>