summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_textarea_value_not_include_cr.html
blob: f687792f8bcf18ae8d490af4615215f49470aec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
  <title>Test for HTMLTextAreaElement.value not returning value including CR</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none;">

</div>

<textarea></textarea>

<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  /**
   * This test should check only the cases with emulating complicated
   * key events and using XPCOM methods.  If you need to add simple textcases,
   * use testing/web-platform/tests/html/semantics/forms/the-textarea-element/value-defaultValue-textContent.html
   * instead
   */
  let textarea = document.querySelector("textarea");
  textarea.focus();
  // This shouldn't occur because widget handles control characters if they
  // receive native key event, but for backward compatibility as XUL platform,
  // let's check this.
  synthesizeKey("\r");
  is(textarea.value, "\n", "Inputting \\r from keyboard event should be converted to \\n");

  textarea.value = "";
  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      "ab\ncd\nef",
      () => { SpecialPowers.clipboardCopyString("ab\r\ncd\ref"); },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });
  synthesizeKey("v", {accelKey: true});
  is(textarea.value, "ab\ncd\nef", "Pasting \\r from clipboard should be converted to \\n");

  textarea.value = "";
  SpecialPowers.wrap(textarea).editor.insertText("ab\r\ncd\ref");
  is(textarea.value, "ab\ncd\nef", "Inserting \\r with nsIEditor.insertText() should be converted to \\n");

  textarea.value = "";
  synthesizeCompositionChange(
    { "composition":
      { "string": "ab\r\ncd\ref",
        "clauses":
        [
          { "length": 9, "attr": COMPOSITION_ATTR_RAW_CLAUSE }
        ]
      },
      "caret": { "start": 9, "length": 0 },
    });
  is(textarea.value, "ab\ncd\nef", "Inserting \\r with composition should be converted to \\n");

  synthesizeComposition({type: "compositioncommitasis"});
  is(textarea.value, "ab\ncd\nef", "Inserting \\r with committing composition should be converted to \\n");

  // We don't need to test spellchecker features on Android because of unsupported.
  if (!navigator.appVersion.includes("Android")) {
    ok(true, "Waiting to run spellchecker...");
    let inlineSpellchecker = SpecialPowers.wrap(textarea).editor.getInlineSpellChecker(true);
    textarea.value = "abx ";
    await new Promise(resolve => {
      const { onSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
        "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
      );
      onSpellCheck(textarea, () => {
        SimpleTest.executeSoon(resolve);
      });
    });
    let anonymousDivElement = SpecialPowers.wrap(textarea).editor.rootElement;
    let misspelledWord = inlineSpellchecker.getMisspelledWord(anonymousDivElement.firstChild, 0);
    is(misspelledWord.startOffset, 0, "Misspelled word start should be 0");
    is(misspelledWord.endOffset, 3, "Misspelled word end should be 3");
    inlineSpellchecker.replaceWord(anonymousDivElement.firstChild, 0, "ab\r\ncd\ref");
    is(textarea.value, "ab\ncd\nef ", "Inserting \\r from spellchecker should be converted to \\n");
  }

  SimpleTest.finish();
});
</script>
</pre>
</body>
</html>