test_nsIEditor_outputToString.html (6055B)
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; 13 14 /** 15 * TODO: Add "text/html" cases and other `nsIDocumentEncoder.*` options. 16 */ 17 (function test_with_text_editor() { 18 for (const test of [ 19 { 20 tag: "input", 21 innerHTML: "<input>", 22 }, 23 { 24 tag: "textarea", 25 innerHTML: "<textarea></textarea>", 26 }, 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)` 35 ); 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)` 41 ); 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` 47 ); 48 if (editor.flags & Ci.nsIEditor.eEditorSingleLineMask) { 49 continue; 50 } 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` 56 ); 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` 62 ); 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` 68 ); 69 } 70 })(); 71 72 function getHTMLEditor() { 73 const editingSession = SpecialPowers.wrap(window).docShell.editingSession; 74 if (!editingSession) { 75 return null; 76 } 77 return editingSession.getEditorForWindow(window); 78 } 79 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)` 88 ); 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)` 94 ); 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` 102 ); 103 104 document.body.removeAttribute("contenteditable"); 105 document.body.innerHTML = "<div contenteditable></div>"; 106 is( 107 getHTMLEditor().outputToString("text/plain", Ci.nsIDocumentEncoder.OutputRaw).replace(/\r/g, ""), 108 "", 109 `outputToString("text/plain", OutputRaw) for empty <div contenteditable> should return empty string (before focused)` 110 ); 111 document.body.querySelector("div[contenteditable]").focus(); 112 is( 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)` 116 ); 117 document.body.querySelector("div[contenteditable]").innerHTML = sourceHasParagraphsAndDivs; 118 is( 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` 122 ); 123 })(); 124 125 document.body.innerHTML = originalBody; 126 SimpleTest.finish(); 127 }); 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>