tor-browser

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

test_htmlcopyencoder_common_ancestor.html (5849B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 -->
      5 <head>
      6 <title>Test on the html copy encoder for common ancestor</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"></div>
     15 <script>
     16 
     17 /* global describe, it, beforeEach */
     18 
     19 const ELEMENTS = [
     20  "address", "article", "aside", "bdi", "bdo", "blockquote", "data", "dd",
     21  "del", "div", "dl", "dt", "figcaption", "figure", "footer", "header",
     22  "hgroup", "ins", "kdb", "main", "mark", "nav", "p", "q", "rp", "rt", "ruby",
     23  "samp", "section", "sub", "sup", "time"
     24 ];
     25 
     26 const ALWAYS_INCLUDE_ELEMENTS = [
     27  "a", "abbr", "b", "big", "cite", "code", "dfn", "em", "font", "h1", "h2",
     28  "h3", "h4", "h5", "h6", "i", "pre", "s", "script", "small", "span", "strike",
     29  "strong", "tt", "u", "var"
     30 ];
     31 
     32 const alwaysIncludeCommonAncestor = SpecialPowers.getBoolPref(
     33  "dom.serializer.includeCommonAncestor.enabled"
     34 );
     35 
     36 const de = SpecialPowers.Ci.nsIDocumentEncoder;
     37 const encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
     38 function testSelectAndCopy(aNode, aInclude) {
     39  const innerHTML = `<${aNode} id="target">First Second</${aNode}>`;
     40  describe(innerHTML, () => {
     41    beforeEach(() => {
     42      info("Construct nodes");
     43      const container = document.getElementById("container");
     44      container.innerHTML = innerHTML;
     45    });
     46 
     47    it(`Select node`, async () => {
     48      info("Select nodes");
     49      const selection = window.getSelection();
     50      selection.removeAllRanges();
     51 
     52      const target = document.getElementById("target");
     53      const range = document.createRange();
     54      range.selectNode(target);
     55      selection.addRange(range);
     56 
     57      info("Initialize encoder");
     58      encoder.init(document, "text/html", 0);
     59      encoder.setSelection(selection);
     60 
     61      let htmlContext = { value: '' };
     62      let htmlInfo = { value: '' };
     63      is(encoder.encodeToStringWithContext(htmlContext, htmlInfo),
     64         alwaysIncludeCommonAncestor ? `<div id="container">${innerHTML}</div>` : innerHTML,
     65         `Check serialized output`);
     66      is(htmlContext.value,
     67         alwaysIncludeCommonAncestor ? `<html><body></body></html>` : `<html><body><div id="container"></div></body></html>`,
     68         `Check serialized context`);
     69      is(htmlInfo.value, `0,0`, `Check serialized info`);
     70    });
     71 
     72    it(`Select node contents`, async () => {
     73      info("Select nodes");
     74      const selection = window.getSelection();
     75      selection.removeAllRanges();
     76 
     77      const target = document.getElementById("target");
     78      const range = document.createRange();
     79      range.selectNodeContents(target);
     80      selection.addRange(range);
     81 
     82      info("Initialize encoder");
     83      encoder.init(document, "text/html", 0);
     84      encoder.setSelection(selection);
     85 
     86      let expectedResult;
     87      if (alwaysIncludeCommonAncestor) {
     88        expectedResult = `<div id="container">${innerHTML}</div>`;
     89      } else if (aInclude) {
     90        expectedResult = innerHTML;
     91      } else {
     92        expectedResult = target.textContent;
     93      }
     94 
     95      let htmlContext = { value: '' };
     96      let htmlInfo = { value: '' };
     97      is(encoder.encodeToStringWithContext(htmlContext, htmlInfo), expectedResult,
     98         `Check serialized output`);
     99      is(htmlContext.value,
    100         alwaysIncludeCommonAncestor ? `<html><body></body></html>`
    101                                     : `<html><body><div id="container"><${aNode} id="target"></${aNode}></div></body></html>`,
    102         `Check serialized context`);
    103      is(htmlInfo.value, `0,0`, `Check serialized info`);
    104    });
    105 
    106    it(`Select whole text`, async () => {
    107      info("Select text");
    108      const selection = window.getSelection();
    109      selection.removeAllRanges();
    110 
    111      const target = document.getElementById("target").firstChild;
    112      const range = document.createRange();
    113      range.setStart(target, 0);
    114      range.setEnd(target, target.length);
    115      selection.addRange(range);
    116 
    117      info("Initialize encoder");
    118      encoder.init(document, "text/html", 0);
    119      encoder.setSelection(selection);
    120 
    121      let htmlContext = { value: '' };
    122      let htmlInfo = { value: '' };
    123      is(encoder.encodeToStringWithContext(htmlContext, htmlInfo),
    124         aInclude ? innerHTML : `First Second`,
    125         `Check serialized output`);
    126      is(htmlContext.value,
    127         `<html><body><div id="container"><${aNode} id="target"></${aNode}></div></body></html>`,
    128         `Check serialized context`);
    129      is(htmlInfo.value, `0,0`, `Check serialized info`);
    130    });
    131 
    132    it(`Select partial text`, async () => {
    133      info("Select text");
    134      const selection = window.getSelection();
    135      selection.removeAllRanges();
    136 
    137      const target = document.getElementById("target").firstChild;
    138      const range = document.createRange();
    139      range.setStart(target, 0);
    140      range.setEnd(target, 5);
    141      selection.addRange(range);
    142 
    143      info("Initialize encoder");
    144      encoder.init(document, "text/html", 0);
    145      encoder.setSelection(selection);
    146 
    147      let htmlContext = { value: '' };
    148      let htmlInfo = { value: '' };
    149      is(encoder.encodeToStringWithContext(htmlContext, htmlInfo),
    150         aInclude ? `<${aNode} id="target">First</${aNode}>` : `First`,
    151         `Check serialized output`);
    152      is(htmlContext.value,
    153         `<html><body><div id="container"><${aNode} id="target"></${aNode}></div></body></html>`,
    154         `Check serialized context`);
    155      is(htmlInfo.value, `0,0`, `Check serialized info`);
    156    });
    157  });
    158 }
    159 
    160 ELEMENTS.forEach(aNode => {
    161  testSelectAndCopy(aNode, false);
    162 });
    163 
    164 ALWAYS_INCLUDE_ELEMENTS.forEach(aNode => {
    165  testSelectAndCopy(aNode, true);
    166 });
    167 
    168 </script>
    169 </body>
    170 </html>