insertparagraph.tentative.html (2744B)
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("insertparagraph", 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 = "<div>a b</div>"; 47 selection.collapse(editor.firstChild.firstChild, 0); 48 test(function () { 49 document.execCommand("insertparagraph", false, ""); 50 assert_equals(editor.innerHTML, 51 `<div><br></div><div>a b</div>`, 52 "Modified text is wrong"); 53 }, `execCommand("insertparagraph", false, "") at "<div>${getDescriptionForTextNode(editor.firstChild.firstChild)}</div>"`); 54 55 for (let i = 1; i <= 10; i++) { 56 editor.innerHTML = "<div>a b</div>"; 57 selection.collapse(editor.firstChild.firstChild, i); 58 test(function () { 59 let text = editor.firstChild.firstChild.data; 60 document.execCommand("insertparagraph", false, ""); 61 assert_equals(editor.innerHTML, 62 `<div>${escape(text.slice(0, i))}</div><div>${escape(text.slice(i))}</div>`, 63 "Modified text is wrong"); 64 }, `execCommand("insertparagraph", false, "") at "<div>${getDescriptionForTextNode(editor.firstChild.firstChild)}</div>"`); 65 } 66 67 done(); 68 } 69 70 window.addEventListener("load", runTests, {once: true}); 71 </script> 72 </body> 73 </html>