tor-browser

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

test_plaintext_linebreak_compress.html (4831B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4 <title>Test line breaks for plaintext serializer</title>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <script src="/tests/SimpleTest/EventUtils.js"></script>
      7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1650720">Mozilla Bug 1650720</a>
     11 <p id="display"></p>
     12 <div id="content" style="display: none">
     13 </div>
     14 <pre id="preformatted"></pre>
     15 <div id="container"></div>
     16 <script>
     17 
     18 const de = SpecialPowers.Ci.nsIDocumentEncoder;
     19 const platformLineBreak = navigator.platform.indexOf("Win") == 0 ? "\r\n" : "\n";
     20 // XXX: I'm not sure if the line break compression behavior entirely makes sense,
     21 // but this is what we have for now.
     22 const TESTS = [
     23  {innerHTMLs: [`<pre>First <div>Second </div></pre><h3>Third</h3>`,
     24                `<pre>First <div>Second </div></pre>\n<h3>Third</h3>`,
     25                `<pre>First <div>Second </div></pre>\n\n<h3>Third</h3>`,
     26                `<pre>First <div>Second </div></pre>\n\n\n<h3>Third</h3>`,
     27                `<pre>First <div>Second </div></pre>\n\n\n\n<h3>Third</h3>`,
     28                `<pre>First <div>Second </div>\n</pre><h3>Third</h3>`,
     29                `<pre>First <div>Second </div>\n</pre>\n<h3>Third</h3>`,
     30                `<pre>First <div>Second </div>\n</pre>\n\n<h3>Third</h3>`,
     31                `<pre>First <div>Second </div>\n</pre>\n\n\n<h3>Third</h3>`,
     32                `<pre>First <div>Second </div>\n</pre>\n\n\n\n<h3>Third</h3>`,
     33                `<pre>First \n<div>Second </div></pre><h3>Third</h3>`],
     34   expectedResult: `First ${platformLineBreak}Second ${platformLineBreak}${platformLineBreak}Third`},
     35  {innerHTMLs: [`<pre>First <pre>Second </pre></pre>Third`,
     36                `<pre>First <pre>Second </pre></pre>\nThird`,
     37                `<pre>First <pre>Second </pre></pre>\n\nThird`,
     38                `<pre>First <pre>Second </pre></pre>\n\n\nThird`,
     39                `<pre>First <pre>Second </pre></pre>\n\n\n\nThird`,
     40                `<pre>First \n<pre>Second </pre></pre>Third`,
     41                `<pre>First \n<pre>Second </pre></pre>\nThird`,
     42                // XXX: Not sure below two cases should have another line break
     43                // before the "Second". However, <pre> might have some special
     44                // rules against normal elements with white-space:pre.
     45                `<pre>First \n\n<pre>Second </pre></pre>Third`,
     46                `<pre>First \n\n<pre>Second </pre></pre>\nThird`],
     47   expectedResult: `First ${platformLineBreak}${platformLineBreak}Second ${platformLineBreak}${platformLineBreak}Third`},
     48  {innerHTMLs: [`<pre>First </pre><pre>Second</pre>`,
     49                `<pre>First </pre>\n<pre>Second</pre>`,
     50                `<pre>First </pre>\n\n<pre>Second</pre>`,
     51                `<pre>First </pre>\n\n\n<pre>Second</pre>`,
     52                `<pre>First </pre>\n\n\n\n<pre>Second</pre>`,
     53                `<pre>First \n</pre><pre>Second</pre>`,
     54                `<pre>First \n</pre>\n<pre>Second</pre>`,
     55                `<pre>First \n</pre>\n\n<pre>Second</pre>`,
     56                `<pre>First \n</pre>\n\n\n<pre>Second</pre>`,
     57                `<pre>First \n</pre>\n\n\n\n<pre>Second</pre>`,
     58                // XXX: Not sure below five cases should have another line break
     59                // before the "Second". However, <pre> might have some special
     60                // rules against normal elements with white-space:pre.
     61                `<pre>First \n\n</pre><pre>Second</pre>`,
     62                `<pre>First \n\n</pre>\n<pre>Second</pre>`,
     63                `<pre>First \n\n</pre>\n\n<pre>Second</pre>`,
     64                `<pre>First \n\n</pre>\n\n\n<pre>Second</pre>`,
     65                `<pre>First \n\n</pre>\n\n\n\n<pre>Second</pre>`],
     66   expectedResult: `First ${platformLineBreak}${platformLineBreak}Second`},
     67 ];
     68 
     69 function selectAndEncode(aElement, aEncoderFlags = 0) {
     70  // Select the element.
     71  const selection = window.getSelection();
     72  selection.removeAllRanges();
     73  selection.selectAllChildren(aElement);
     74 
     75  const encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
     76  encoder.init(document, "text/plain", de.OutputSelectionOnly | aEncoderFlags);
     77  encoder.setSelection(selection);
     78  return encoder.encodeToString();
     79 }
     80 
     81 TESTS.forEach((test) => {
     82  test.innerHTMLs.forEach((innerHTML) => {
     83    add_task(async function test_line_break_compress() {
     84      info(`Testing ${JSON.stringify(innerHTML)}`);
     85 
     86      const div = document.getElementById("container");
     87      div.innerHTML = innerHTML;
     88 
     89      is(selectAndEncode(div), test.expectedResult,
     90         `Encoded data for ${JSON.stringify(innerHTML)}`);
     91      is(selectAndEncode(div, de.OutputForPlainTextClipboardCopy), test.expectedResult,
     92         `Encoded data for ${JSON.stringify(innerHTML)}`);
     93    });
     94  });
     95 });
     96 
     97 </script>
     98 </body>
     99 </html>