1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
<!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.setAttribute("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\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\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\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\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 text", "plain text", "plain 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>
|