summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIEditorMailSupport_insertTextWithQuotations.html
blob: 921e137fa8e5be28f3458c75d2e21f2aa2460e3d (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
96
97
98
99
100
101
102
103
104
<!DOCTYPE>
<html>
<head>
  <title>Test for nsIEditorMailSupport.insertTextWithQuotations()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div contenteditable></div>
<iframe srcdoc="<body contenteditable></body>"></iframe>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const iframe = document.querySelector("iframe");
  await new Promise(resolve => {
    if (iframe.contentDocument?.readyState == "complete") {
      resolve();
      return;
    }
    iframe.addEventListener("load", resolve, {once: true});
  });

  function testInDiv() {
    const inPlaintextMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    const inReadonlyMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
    const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`;
    const editor = document.querySelector("div[contenteditable]");
    editor.innerHTML = "";
    editor.focus();
    getEditorMailSupport(window).insertTextWithQuotations(
      "This is Text\n\n> This is a quote."
    );
    is(
      editor.innerHTML,
      'This is Text<br><br><span style="white-space: pre-wrap;">&gt; This is a quote.</span><br>',
      `The <div contenteditable> should have the expected innerHTML ${editorDescription}`
    );
    is(
      editor.querySelector("span")?.getAttribute("_moz_quote"),
      "true",
      `The <span> element in the <div contenteditable> should have _moz_quote="true" ${editorDescription}`
    );
  }

  function testInBody() {
    const inPlaintextMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    const inReadonlyMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
    const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`;
    const editor = iframe.contentDocument.body;
    editor.innerHTML = "";
    iframe.contentWindow.getSelection().collapse(document.body, 0);
    getEditorMailSupport(iframe.contentWindow).insertTextWithQuotations(
      "This is Text\n\n> This is a quote."
    );
    is(
      editor.innerHTML,
      'This is Text<br><br><span style="white-space: pre-wrap; display: block; width: 98vw;">&gt; This is a quote.</span><br>',
      `The <body> should have the expected innerHTML ${editorDescription}`
    );
    is(
      editor.querySelector("span")?.getAttribute("_moz_quote"),
      "true",
      `The <span> element in the <body> should have _moz_quote="true" ${editorDescription}`
    );
  }

  for (const testReadOnly of [false, true]) {
    // Even if the HTMLEditor is readonly, XPCOM API should keep working.
    if (testReadOnly) {
      getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
      getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
    } else {
      getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
      getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
    }

    getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    testInDiv();
    testInBody();

    getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
    testInDiv();
    testInBody();
  }

  SimpleTest.finish();
});

function getEditor(aWindow) {
  const editingSession = SpecialPowers.wrap(aWindow).docShell.editingSession;
  return editingSession.getEditorForWindow(aWindow);
}

function getEditorMailSupport(aWindow) {
  return getEditor(aWindow).QueryInterface(SpecialPowers.Ci.nsIEditorMailSupport);
}
</script>
</body>

</html>