Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE>
<html>
<head>
<title>Test for nsIEditorMailSupport.insertTextWithQuotations()</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div contenteditable></div>
<iframe srcdoc="<body contenteditable></body>"></iframe>
<script>
"use strict";
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
const iframe = document.querySelector("iframe");
await new Promise(resolve => {
if (iframe.contentDocument?.readyState == "complete") {
resolve();
return;
}
iframe.addEventListener("load", resolve, {once: true});
});
function testInDiv() {
const inPlaintextMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
const inReadonlyMode = getEditor(window).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`;
const editor = document.querySelector("div[contenteditable]");
editor.innerHTML = "";
editor.focus();
getEditorMailSupport(window).insertTextWithQuotations(
"This is Text\n\n> This is a quote."
);
is(
editor.innerHTML,
'This is Text<br><br><span style="white-space: pre-wrap;">&gt; This is a quote.</span><br>',
`The <div contenteditable> should have the expected innerHTML ${editorDescription}`
);
is(
editor.querySelector("span")?.getAttribute("_moz_quote"),
"true",
`The <span> element in the <div contenteditable> should have _moz_quote="true" ${editorDescription}`
);
}
function testInBody() {
const inPlaintextMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
const inReadonlyMode = getEditor(iframe.contentWindow).flags & SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
const editorDescription = `(readonly=${!!inReadonlyMode}, plaintext=${!!inPlaintextMode})`;
const editor = iframe.contentDocument.body;
editor.innerHTML = "";
iframe.contentWindow.getSelection().collapse(document.body, 0);
getEditorMailSupport(iframe.contentWindow).insertTextWithQuotations(
"This is Text\n\n> This is a quote."
);
is(
editor.innerHTML,
'This is Text<br><br><span style="white-space: pre-wrap; display: block; width: 98vw;">&gt; This is a quote.</span><br>',
`The <body> should have the expected innerHTML ${editorDescription}`
);
is(
editor.querySelector("span")?.getAttribute("_moz_quote"),
"true",
`The <span> element in the <body> should have _moz_quote="true" ${editorDescription}`
);
}
for (const testReadOnly of [false, true]) {
// Even if the HTMLEditor is readonly, XPCOM API should keep working.
if (testReadOnly) {
getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
} else {
getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorReadonlyMask;
}
getEditor(window).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
getEditor(iframe.contentWindow).flags &= ~SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
testInDiv();
testInBody();
getEditor(window).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
getEditor(iframe.contentWindow).flags |= SpecialPowers.Ci.nsIEditor.eEditorPlaintextMask;
testInDiv();
testInBody();
}
SimpleTest.finish();
});
function getEditor(aWindow) {
const editingSession = SpecialPowers.wrap(aWindow).docShell.editingSession;
return editingSession.getEditorForWindow(aWindow);
}
function getEditorMailSupport(aWindow) {
return getEditor(aWindow).QueryInterface(SpecialPowers.Ci.nsIEditorMailSupport);
}
</script>
</body>
</html>