Bug 1874684 - Part 17: Fix uninitialised variable warnings from clang-tidy. r=allstarschh
[gecko.git] / editor / libeditor / tests / test_nsIEditor_outputToString.html
blob9d2e83245bcfb9695fe586c7e711ac368382f942
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Tests of nsIEditor#outputToString()</title>
6 <script src="/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
8 <script>
9 SimpleTest.waitForExplicitFinish();
10 SimpleTest.waitForFocus(() => {
11 const originalBody = document.body.innerHTML;
12 const Ci = SpecialPowers.Ci;
14 /**
15 * TODO: Add "text/html" cases and other `nsIDocumentEncoder.*` options.
17 (function test_with_text_editor() {
18 for (const test of [
20 tag: "input",
21 innerHTML: "<input>",
24 tag: "textarea",
25 innerHTML: "<textarea></textarea>",
27 ]) {
28 document.body.innerHTML = test.innerHTML;
29 const textControl = document.body.querySelector(test.tag);
30 const editor = SpecialPowers.wrap(textControl).editor;
31 is(
32 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
33 "",
34 `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (before focused)`
36 textControl.focus();
37 is(
38 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
39 "",
40 `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (after focused)`
42 textControl.value = "abc";
43 is(
44 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw),
45 "abc",
46 `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc" should return the value as-is`
48 if (editor.flags & Ci.nsIEditor.eEditorSingleLineMask) {
49 continue;
51 textControl.value = "abc\ndef";
52 is(
53 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
54 "abc\ndef",
55 `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef" should return the value as-is`
57 textControl.value = "abc\ndef\n";
58 is(
59 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
60 "abc\ndef\n",
61 `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef\n" should return the value as-is`
63 textControl.value = "abc\ndef\n\n";
64 is(
65 editor.outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
66 "abc\ndef\n\n",
67 `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef\n\n" should return the value as-is`
70 })();
72 function getHTMLEditor() {
73 const editingSession = SpecialPowers.wrap(window).docShell.editingSession;
74 if (!editingSession) {
75 return null;
77 return editingSession.getEditorForWindow(window);
80 (function test_with_contenteditable() {
81 document.body.setAttribute("contenteditable", "");
82 document.body.blur();
83 document.body.innerHTML = "";
84 is(
85 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
86 "",
87 `outputToString("text/plain", OutputRaw) for empty <body contenteditable> should return empty string (before focused)`
89 document.body.focus();
90 is(
91 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
92 "", // Ignore the padding <br> element for empty editor.
93 `outputToString("text/plain", OutputRaw) for empty <body contenteditable> should return empty string (after focused)`
95 const sourceHasParagraphsAndDivs = "<p>abc</p><p>def<br></p><div>ghi</div><div>jkl<br>mno<br></div>";
96 document.body.innerHTML = sourceHasParagraphsAndDivs;
97 // XXX Oddly, an ASCII white-space is inserted at the head of the result.
98 todo_is(
99 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
100 sourceHasParagraphsAndDivs.replace(/<br>/gi, "\n").replace(/<[^>]+>/g, ""),
101 `outputToString("text/plain", OutputRaw) for <body contenteditable> should return the expected string`
104 document.body.removeAttribute("contenteditable");
105 document.body.innerHTML = "<div contenteditable></div>";
107 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
109 `outputToString("text/plain", OutputRaw) for empty <div contenteditable> should return empty string (before focused)`
111 document.body.querySelector("div[contenteditable]").focus();
113 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
114 "", // Ignore the padding <br> element for empty editor.
115 `outputToString("text/plain", OutputRaw) for empty <div contenteditable> should return empty string (after focused)`
117 document.body.querySelector("div[contenteditable]").innerHTML = sourceHasParagraphsAndDivs;
119 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""),
120 sourceHasParagraphsAndDivs.replace(/<br>/gi, "\n").replace(/<[^>]+>/g, ""),
121 `outputToString("text/plain", OutputRaw) for <div contenteditable> should return the expected string`
123 })();
125 document.body.innerHTML = originalBody;
126 SimpleTest.finish();
128 </script>
129 </head>
130 <body>
131 <p id="display"></p>
132 <div id="content" style="display: none"></div>
133 <pre id="test"></pre>
134 </body>
135 </html>