summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIEditor_outputToString.html
blob: 9d2e83245bcfb9695fe586c7e711ac368382f942 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests of nsIEditor#outputToString()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();
    SimpleTest.waitForFocus(() => {
      const originalBody = document.body.innerHTML;
      const Ci = SpecialPowers.Ci;

      /**
       * TODO: Add "text/html" cases and other `nsIDocumentEncoder.*` options.
       */
      (function test_with_text_editor() {
        for (const test of [
            {
              tag: "input",
              innerHTML: "<input>",
            },
            {
              tag: "textarea",
              innerHTML: "<textarea></textarea>",
            },
          ]) {
            document.body.innerHTML = test.innerHTML;
            const textControl = document.body.querySelector(test.tag);
            const editor = SpecialPowers.wrap(textControl).editor;
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
              "",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (before focused)`
            );
            textControl.focus();
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
              "",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (after focused)`
            );
            textControl.value = "abc";
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
              "abc",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc" should return the value as-is`
            );
            if (editor.flags & Ci.nsIEditor.eEditorSingleLineMask) {
              continue;
            }
            textControl.value = "abc\ndef";
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
              "abc\ndef",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef" should return the value as-is`
            );
            textControl.value = "abc\ndef\n";
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
              "abc\ndef\n",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef\n" should return the value as-is`
            );
            textControl.value = "abc\ndef\n\n";
            is(
              editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
              "abc\ndef\n\n",
              `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef\n\n" should return the value as-is`
            );
          }
      })();

      function getHTMLEditor() {
        const editingSession = SpecialPowers.wrap(window).docShell.editingSession;
        if (!editingSession) {
          return null;
        }
        return editingSession.getEditorForWindow(window);
      }

      (function test_with_contenteditable() {
        document.body.setAttribute("contenteditable", "");
        document.body.blur();
        document.body.innerHTML = "";
        is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          "",
          `outputToString("text/plain", OutputRaw) for empty <body contenteditable> should return empty string (before focused)`
        );
        document.body.focus();
        is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          "", // Ignore the padding <br> element for empty editor.
          `outputToString("text/plain", OutputRaw) for empty <body contenteditable> should return empty string (after focused)`
        );
        const sourceHasParagraphsAndDivs = "<p>abc</p><p>def<br></p><div>ghi</div><div>jkl<br>mno<br></div>";
        document.body.innerHTML = sourceHasParagraphsAndDivs;
        // XXX Oddly, an ASCII white-space is inserted at the head of the result.
        todo_is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          sourceHasParagraphsAndDivs.replace(/<br>/gi, "\n").replace(/<[^>]+>/g, ""),
          `outputToString("text/plain", OutputRaw) for <body contenteditable> should return the expected string`
        );

        document.body.removeAttribute("contenteditable");
        document.body.innerHTML = "<div contenteditable></div>";
        is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          "",
          `outputToString("text/plain", OutputRaw) for empty <div contenteditable> should return empty string (before focused)`
        );
        document.body.querySelector("div[contenteditable]").focus();
        is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          "", // Ignore the padding <br> element for empty editor.
          `outputToString("text/plain", OutputRaw) for empty <div contenteditable> should return empty string (after focused)`
        );
        document.body.querySelector("div[contenteditable]").innerHTML = sourceHasParagraphsAndDivs;
        is(
          getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
          sourceHasParagraphsAndDivs.replace(/<br>/gi, "\n").replace(/<[^>]+>/g, ""),
          `outputToString("text/plain", OutputRaw) for <div contenteditable> should return the expected string`
        );
      })();

      document.body.innerHTML = originalBody;
      SimpleTest.finish();
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>