Skip to content

Commit

Permalink
part 2: Make HTMLEditor handle insertHTML as inserting plaintext …
Browse files Browse the repository at this point in the history
…converted from given source

Differential Revision: https://phabricator.services.mozilla.com/D223909

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1920646
gecko-commit: 39f1a092f31fc2950ea974b4df5e8710f9bf500d
gecko-reviewers: m_kato
  • Loading branch information
masayuki-nakano authored and moz-wptsync-bot committed Oct 3, 2024
1 parent e0e8311 commit 947f66f
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions editing/plaintext-only/insertHTML.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="timeout" content="long">
<meta name="variant" content="?white-space=normal">
<meta name="variant" content="?white-space=pre">
<meta name="variant" content="?white-space=pre-line">
<meta name="variant" content="?white-space=pre-wrap">
<title>Pasting rich text into contenteditable=plaintext-only</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../include/editor-test-utils.js"></script>
<script>
"use strict";

const searchParams = new URLSearchParams(document.location.search);
const whiteSpace = searchParams.get("white-space");
const useBR = whiteSpace == "normal";
const collapseWhiteSpaces = whiteSpace == "normal" || whiteSpace == "pre-line";

addEventListener("load", () => {
const editingHost = document.createElement("div");
editingHost.style.whiteSpace = whiteSpace;
editingHost.contentEditable = "plaintext-only";
document.body.appendChild(editingHost);
editingHost.focus();
editingHost.getBoundingClientRect();
const utils = new EditorTestUtils(editingHost);

for (const data of [
{
insertHTML: "plaintext",
expected: "plaintext",
},
{
// line breaks should not be preformatted
insertHTML: "1st line\n2nd line",
expected: "1st line 2nd line",
},
{
// preformatted line breaks should appear as-is
insertHTML: "<pre>1st line\n2nd line</pre>",
expected: useBR
? "1st line<br>2nd line"
: ["1st line<br>2nd line", "1st line\n2nd line"],
},
{
// text should be inserted into the <b>
initialInnerHTML: "<b>{}</b>",
insertHTML: "plaintext",
expected: "<b>plaintext</b>",
},
{
// text should be inserted into the <b>
initialInnerHTML: "<b>{}<br></b>",
insertHTML: "plaintext",
expected: ["<b>plaintext</b>", "<b>plaintext<br></b>"],
},
{
// text should be inserted into the <b>
initialInnerHTML: "<b>A[]B</b>",
insertHTML: "plaintext",
expected: "<b>AplaintextB</b>",
},
{
// text should be inserted into the <span> even if it's meaningless
initialInnerHTML: "<span>A[]B</span>",
insertHTML: "plaintext",
expected: "<span>AplaintextB</span>",
},
{
// inserting one paragraph should cause inserting only its contents.
// (but it's okay other serialized text.)
insertHTML: "<div>abc</div>",
expected: "abc",
},
{
// inserting one paragraph should cause inserting only its contents.
// (but it's okay other serialized text.)
insertHTML: "<div>abc<br>def</div>",
expected: useBR ? "abc<br>def" : ["abc<br>def", "abc\ndef"],
},
{
// inserting 2 or more paragraphs should be handled as multiple lines
insertHTML: "<div>abc</div><div>def</div>",
expected: useBR ? "abc<br>def" : ["abc<br>def", "abc\ndef"],
},
{
// inserting 2 or more paragraphs should be handled as multiple lines
insertHTML: "<div>abc<br>def</div><div>ghi<br>jkl</div>",
expected: useBR
? "abc<br>def<br>ghi<br>jkl"
: ["abc<br>def<br>ghi<br>jkl",
"abc\ndef\nghi\njkl"],
},
{
// <noscript> content should not be inserted
insertHTML: "<noscript>no script</noscript>",
expected: "",
},
{
// <noframes> content should not be inserted
insertHTML: "<noframes>no frames</noframes>",
expected: "",
},
{
// <script> content should not be inserted
insertHTML: `<script>script</${"script"}>`,
expected: "",
},
{
// <style> content should not be inserted
insertHTML: "<style>style</style>",
expected: "",
},
{
// <head> content should not be inserted
insertHTML: "<html><head><title>title</title></head><body>body</body></html>",
expected: "body",
},
{
// white-spaces should be collapsed
insertHTML: "plain text",
expected: "plain text",
},
{
// white-spaces should be collapsed
insertHTML: "<span>plain text</span>",
expected: "plain text",
},
{
// preformatted white-spaces should not be collapsed
insertHTML: "<pre>plain text</pre>",
expected: !collapseWhiteSpaces
? "plain text"
: ["plain &nbsp;text", "plain&nbsp; text", "plain&nbsp;&nbsp;text",
"plain \u00A0text", "plain\u00A0 text", "plain\u00A0\u00A0text"],
},
{
// even if inserting HTML is empty, selected text should be deleted
initialInnerHTML: "A[B]C",
insertHTML: "",
expected: "AC",
},
]) {
test(() => {
utils.setupEditingHost(data.initialInnerHTML ? data.initialInnerHTML : "");
document.execCommand("insertHTML", false, data.insertHTML);
if (Array.isArray(data.expected)) {
assert_in_array(editingHost.innerHTML, data.expected);
} else {
assert_equals(editingHost.innerHTML, data.expected);
}
}, `execCommand("insertHTML", false, "${data.insertHTML}") when "${
data.initialInnerHTML ? data.initialInnerHTML : ""
}"`);
}
}, {once: true});
</script>
</head>
<body></body>
</html>

0 comments on commit 947f66f

Please sign in to comment.