5 <title>Tests of nsIEditor#outputToString()
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css"/>
9 SimpleTest
.waitForExplicitFinish();
10 SimpleTest
.waitForFocus(() => {
11 const originalBody
= document
.body
.innerHTML
;
12 const Ci
= SpecialPowers
.Ci
;
15 * TODO: Add "text/html" cases and other `nsIDocumentEncoder.*` options.
17 (function test_with_text_editor() {
25 innerHTML
: "<textarea></textarea>",
28 document
.body
.innerHTML
= test
.innerHTML
;
29 const textControl
= document
.body
.querySelector(test
.tag
);
30 const editor
= SpecialPowers
.wrap(textControl
).editor
;
32 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
),
34 `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (before focused)`
38 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
),
40 `outputToString("text/plain", OutputRaw) for <${test.tag}> should return empty string (after focused)`
42 textControl
.value
= "abc";
44 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
),
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
) {
51 textControl
.value
= "abc\ndef";
53 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
).replace(/\r/g
, ""),
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";
59 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
).replace(/\r/g
, ""),
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";
65 editor
.outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
).replace(/\r/g
, ""),
67 `outputToString("text/plain", OutputRaw) for <${test.tag}> whose value is "abc\ndef\n\n" should return the value as-is`
72 function getHTMLEditor() {
73 const editingSession
= SpecialPowers
.wrap(window
).docShell
.editingSession
;
74 if (!editingSession
) {
77 return editingSession
.getEditorForWindow(window
);
80 (function test_with_contenteditable() {
81 document
.body
.setAttribute("contenteditable", "");
83 document
.body
.innerHTML
= "";
85 getHTMLEditor().outputToString("text/plain", Ci
.nsIDocumentEncoder
.OutputRaw
).replace(/\r/g
, ""),
87 `outputToString("text/plain", OutputRaw) for empty <body contenteditable> should return empty string (before focused)`
89 document
.body
.focus();
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.
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`
125 document
.body
.innerHTML
= originalBody
;
132 <div id=
"content" style=
"display: none"></div>
133 <pre id=
"test"></pre>