test_nsIEditorMailSupport_insertTextWithQuotations.html (3981B)
1 <!DOCTYPE> 2 <html> 3 <head> 4 <title>Test for nsIEditorMailSupport.insertTextWithQuotations()</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> 7 </head> 8 <body> 9 <div contenteditable></div> 10 <iframe srcdoc="<body contenteditable></body>"></iframe> 11 <script> 12 "use strict"; 13 14 SimpleTest.waitForExplicitFinish(); 15 SimpleTest.waitForFocus(async () => { 16 const iframe = document.querySelector("iframe"); 17 await new Promise(resolve => { 18 if (iframe.contentDocument?.readyState == "complete") { 19 resolve(); 20 return; 21 } 22 iframe.addEventListener("load", resolve, {once: true}); 23 }); 24 25 function testInDiv() { 26 const inPlaintextMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 27 const inReadonlyMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 28 const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`; 29 const editor = document.querySelector("div[contenteditable]"); 30 editor.innerHTML = ""; 31 editor.focus(); 32 getEditorMailSupport(window).insertTextWithQuotations( 33 "This is Text\n\n> This is a quote." 34 ); 35 is( 36 editor.innerHTML, 37 'This is Text<br><br><span style="white-space: pre-wrap;">> This is a quote.</span>', 38 `The <div contenteditable> should have the expected innerHTML ${editorDescription}` 39 ); 40 is( 41 editor.querySelector("span")?.getAttribute("_moz_quote"), 42 "true", 43 `The <span> element in the <div contenteditable> should have _moz_quote="true" ${editorDescription}` 44 ); 45 } 46 47 function testInBody() { 48 const inPlaintextMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 49 const inReadonlyMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 50 const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`; 51 const editor = iframe.contentDocument.body; 52 editor.innerHTML = ""; 53 iframe.contentWindow.getSelection().collapse(document.body, 0); 54 getEditorMailSupport(iframe.contentWindow).insertTextWithQuotations( 55 "This is Text\n\n> This is a quote." 56 ); 57 is( 58 editor.innerHTML, 59 'This is Text<br><br><span style="white-space: pre-wrap; display: block; width: 98vw;">> This is a quote.</span><br>', 60 `The <body> should have the expected innerHTML ${editorDescription}` 61 ); 62 is( 63 editor.querySelector("span")?.getAttribute("_moz_quote"), 64 "true", 65 `The <span> element in the <body> should have _moz_quote="true" ${editorDescription}` 66 ); 67 } 68 69 for (const testReadOnly of [false, true]) { 70 // Even if the HTMLEditor is readonly, XPCOM API should keep working. 71 if (testReadOnly) { 72 getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 73 getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 74 } else { 75 getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 76 getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask; 77 } 78 79 getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 80 getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 81 testInDiv(); 82 testInBody(); 83 84 getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 85 getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask; 86 testInDiv(); 87 testInBody(); 88 } 89 90 SimpleTest.finish(); 91 }); 92 93 function getEditor(aWindow) { 94 const editingSession = SpecialPowers.wrap(aWindow).docShell.editingSession; 95 return editingSession.getEditorForWindow(aWindow); 96 } 97 98 function getEditorMailSupport(aWindow) { 99 return getEditor(aWindow).QueryInterface(SpecialPowers.Ci.nsIEditorMailSupport); 100 } 101 </script> 102 </body> 103 104 </html>