tor-browser

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

test_handle_new_lines.html (5225B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <title>Test for TextEditor::HandleNewLinesInStringForSingleLineEditor()</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 <p id="display"></p>
     11 <div id="content" style="display: none;">
     12 
     13 </div>
     14 
     15 <div id="container"></div>
     16 
     17 <textarea id="toCopyPlaintext" style="display: none;"></textarea>
     18 
     19 <pre id="test">
     20 
     21 <script class="testbody" type="application/javascript">
     22 SimpleTest.waitForExplicitFinish();
     23 
     24 async function copyPlaintext(aText) {
     25  return new Promise(resolve => {
     26    SimpleTest.waitForClipboard(
     27      aText.replace(/\r\n?/g, "\n"),
     28      () => {
     29        let element = document.getElementById("toCopyPlaintext");
     30        element.style.display = "block";
     31        element.focus();
     32        element.value = aText;
     33        synthesizeKey("a", {accelKey: true});
     34        synthesizeKey("c", {accelKey: true});
     35      },
     36      () => {
     37        ok(true, `Succeeded to copy "${aText.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/ /g, "\u00A0")}" to clipboard`);
     38        let element = document.getElementById("toCopyPlaintext");
     39        element.style.display = "none";
     40        resolve();
     41      },
     42      () => {
     43        SimpleTest.finish();
     44      });
     45  });
     46 }
     47 
     48 async function doTests() {
     49  // nsIEditor::eNewlinesPasteIntact (0):
     50  //   only remove the leading and trailing newlines.
     51  // nsIEditor::eNewlinesPasteToFirst (1) or any other value:
     52  //   remove the first newline and all characters following it.
     53  // nsIEditor::eNewlinesReplaceWithSpaces (2, Firefox default):
     54  //   replace newlines with spaces.
     55  // nsIEditor::eNewlinesStrip (3):
     56  //   remove newlines from the string.
     57  // nsIEditor::eNewlinesReplaceWithCommas (4, Thunderbird default):
     58  //   replace newlines with commas.
     59  // nsIEditor::eNewlinesStripSurroundingWhitespace (5):
     60  //   collapse newlines and surrounding whitespace characters and
     61  //   remove them from the string.
     62 
     63  // value: setting or pasting text.
     64  // expected: array of final values for each above pref value.
     65  //   setValue: expected result when HTMLInputElement.value is set to the value.
     66  //   pasteValue: expected result when pasting the value from clipboard.
     67  //
     68  // Note that HTMLInputElement strips both \r and \n.  Therefore, each expected
     69  // result is different from pasting the value.
     70  const kTests = [
     71    { value: "\nabc\ndef\n",
     72      expected: [{ setValue: "abcdef", pasteValue: "abc\ndef" },
     73                 { setValue: "abcdef", pasteValue: "abc" },
     74                 { setValue: "abcdef", pasteValue: " abc def" },
     75                 { setValue: "abcdef", pasteValue: "abcdef" },
     76                 { setValue: "abcdef", pasteValue: "abc,def" },
     77                 { setValue: "abcdef", pasteValue: "abcdef" }],
     78    },
     79    { value: "\n   abc   \n   def   \n",
     80      expected: [{ setValue: "   abc      def   ", pasteValue: "   abc   \n   def   " },
     81                 { setValue: "   abc      def   ", pasteValue: "   abc   " },
     82                 { setValue: "   abc      def   ", pasteValue: "    abc       def   " },
     83                 { setValue: "   abc      def   ", pasteValue: "   abc      def   " },
     84                 { setValue: "   abc      def   ", pasteValue: "   abc   ,   def   " },
     85                 { setValue: "   abc      def   ", pasteValue: "abcdef" }],
     86    },
     87    { value: "   abc   \n   def   ",
     88      expected: [{ setValue: "   abc      def   ", pasteValue: "   abc   \n   def   " },
     89                 { setValue: "   abc      def   ", pasteValue: "   abc   " },
     90                 { setValue: "   abc      def   ", pasteValue: "   abc       def   " },
     91                 { setValue: "   abc      def   ", pasteValue: "   abc      def   " },
     92                 { setValue: "   abc      def   ", pasteValue: "   abc   ,   def   " },
     93                 { setValue: "   abc      def   ", pasteValue: "   abcdef   " }],
     94    },
     95  ];
     96 
     97  let container = document.getElementById("container");
     98  for (let i = 0; i <= 5; i++) {
     99    await SpecialPowers.pushPrefEnv({"set": [["editor.singleLine.pasteNewlines", i]]});
    100    container.innerHTML = `<input id="input${i}" type="text">`;
    101    let input = document.getElementById(`input${i}`);
    102    input.focus();
    103    let editor = SpecialPowers.wrap(input).editor;
    104    for (const kLineBreaker of ["\n", "\r", "\r\n"]) {
    105      for (let kTest of kTests) {
    106        let value = kTest.value.replace(/\n/g, kLineBreaker);
    107        input.value = value;
    108        is(editor.rootElement.firstChild.wholeText, kTest.expected[i].setValue,
    109           `Setting value to "${value.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/ /g, "\u00A0")}" when pref is ${i}`);
    110        input.value = "";
    111 
    112        await copyPlaintext(value);
    113        input.focus();
    114        synthesizeKey("v", {accelKey: true});
    115        is(editor.rootElement.firstChild.wholeText, kTest.expected[i].pasteValue,
    116           `Pasting "${value.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/ /g, "\u00A0")}" when pref is ${i}`);
    117        input.value = "";
    118      }
    119    }
    120  }
    121 
    122  SimpleTest.finish();
    123 }
    124 
    125 SimpleTest.waitForFocus(doTests);
    126 </script>
    127 </pre>
    128 </body>
    129 </html>