insertlinebreak.tentative.html (6836B)
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset=utf-8> 5 <meta name="flags" content="may"> 6 <title>Testing normalizing white-space sequence after execCommand("insertlinebreak", false, "foo")</title> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 </head> 10 <body> 11 <script> 12 "use strict"; 13 14 setup({explicit_done: true}); 15 16 function runTests() { 17 // README: 18 // These tests based on the behavior of Chrome 83. This test does NOT define 19 // nor suggest any standard behavior (actually, some expected results might 20 // look odd), but this test must help you to understand how other browsers 21 // use different logic to normalize white-space sequence. 22 23 document.body.innerHTML = "<div contenteditable></div>"; 24 let editor = document.querySelector("div[contenteditable]"); 25 editor.focus(); 26 let selection = document.getSelection(); 27 28 function escape(str) { 29 return typeof(str) === "string" ? str.replace(/\u00A0/ig, " ") : ""; 30 } 31 32 function generateWhiteSpaces(num, lastIsAlwaysNBSP) { 33 let str = ""; 34 for (let i = 0; i < num - 1; i++) { 35 str += i % 2 ? " " : "\u00A0"; 36 } 37 str += lastIsAlwaysNBSP || num % 2 ? "\u00A0" : " "; 38 return escape(str); 39 } 40 function getDescriptionForTextNode(textNode) { 41 return selection.focusNode === textNode ? 42 `${escape(textNode.data.slice(0, selection.focusOffset))}[]${escape(textNode.data.slice(selection.focusOffset))}` : 43 escape(textNode); 44 } 45 46 editor.innerHTML = "a b"; 47 selection.collapse(editor.firstChild, 0); 48 test(function () { 49 document.execCommand("insertlinebreak", false, ""); 50 assert_equals(editor.innerHTML, 51 `<br>a b`, 52 "Modified text is wrong"); 53 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 54 55 editor.innerHTML = "a b"; 56 selection.collapse(editor.firstChild, 1); 57 test(function () { 58 document.execCommand("insertlinebreak", false, ""); 59 assert_equals(editor.innerHTML, 60 `a<br>${escape(generateWhiteSpaces(9, false))}b`, 61 "Modified text is wrong"); 62 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 63 64 editor.innerHTML = "a b"; 65 selection.collapse(editor.firstChild, 2); 66 test(function () { 67 document.execCommand("insertlinebreak", false, ""); 68 assert_equals(editor.innerHTML, 69 `a <br>${escape(generateWhiteSpaces(8, false))}b`, 70 "Modified text is wrong"); 71 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 72 73 editor.innerHTML = "a b"; 74 selection.collapse(editor.firstChild, 3); 75 test(function () { 76 document.execCommand("insertlinebreak", false, ""); 77 assert_equals(editor.innerHTML, 78 `a <br>${escape(generateWhiteSpaces(7, false))}b`, 79 "Modified text is wrong"); 80 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 81 82 editor.innerHTML = "a b"; 83 selection.collapse(editor.firstChild, 4); 84 test(function () { 85 document.execCommand("insertlinebreak", false, ""); 86 assert_equals(editor.innerHTML, 87 `a <br>${escape(generateWhiteSpaces(6, false))}b`, 88 "Modified text is wrong"); 89 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 90 91 editor.innerHTML = "a b"; 92 selection.collapse(editor.firstChild, 5); 93 test(function () { 94 document.execCommand("insertlinebreak", false, ""); 95 assert_equals(editor.innerHTML, 96 `a <br>${escape(generateWhiteSpaces(5, false))}b`, 97 "Modified text is wrong"); 98 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 99 100 editor.innerHTML = "a b"; 101 selection.collapse(editor.firstChild, 6); 102 test(function () { 103 document.execCommand("insertlinebreak", false, ""); 104 assert_equals(editor.innerHTML, 105 `a <br>${escape(generateWhiteSpaces(4, false))}b`, 106 "Modified text is wrong"); 107 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 108 109 editor.innerHTML = "a b"; 110 selection.collapse(editor.firstChild, 7); 111 test(function () { 112 document.execCommand("insertlinebreak", false, ""); 113 assert_equals(editor.innerHTML, 114 `a <br>${escape(generateWhiteSpaces(3, false))}b`, 115 "Modified text is wrong"); 116 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 117 118 editor.innerHTML = "a b"; 119 selection.collapse(editor.firstChild, 8); 120 test(function () { 121 document.execCommand("insertlinebreak", false, ""); 122 assert_equals(editor.innerHTML, 123 `a <br>${escape(generateWhiteSpaces(2, false))}b`, 124 "Modified text is wrong"); 125 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 126 127 editor.innerHTML = "a b"; 128 selection.collapse(editor.firstChild, 9); 129 test(function () { 130 document.execCommand("insertlinebreak", false, ""); 131 assert_equals(editor.innerHTML, 132 `a <br>${escape(generateWhiteSpaces(1, false))}b`, 133 "Modified text is wrong"); 134 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 135 136 editor.innerHTML = "a b"; 137 selection.collapse(editor.firstChild, 10); 138 test(function () { 139 document.execCommand("insertlinebreak", false, ""); 140 assert_equals(editor.innerHTML, 141 `a <br>b`, 142 "Modified text is wrong"); 143 }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`); 144 145 done(); 146 } 147 148 window.addEventListener("load", runTests, {once: true}); 149 </script> 150 </body> 151 </html>