insertparagraph-in-child-of-head.tentative.html (15425B)
1 <!doctype html> 2 <html> 3 <head> 4 <meta chareset="utf-8"> 5 <meta name="timeout" content="long"> 6 <meta name="variant" content="?designMode=off&white-space=normal"> 7 <meta name="variant" content="?designMode=off&white-space=pre"> 8 <meta name="variant" content="?designMode=off&white-space=pre-line"> 9 <meta name="variant" content="?designMode=off&white-space=pre-wrap"> 10 <meta name="variant" content="?designMode=on&white-space=normal"> 11 <meta name="variant" content="?designMode=on&white-space=pre"> 12 <meta name="variant" content="?designMode=on&white-space=pre-line"> 13 <meta name="variant" content="?designMode=on&white-space=pre-wrap"> 14 <title>Insert paragraph in a block element in the head element</title> 15 <script src="/resources/testharness.js"></script> 16 <script src="/resources/testharnessreport.js"></script> 17 <script src="/resources/testdriver.js"></script> 18 <script src="/resources/testdriver-vendor.js"></script> 19 <script src="/resources/testdriver-actions.js"></script> 20 <script src="../include/editor-test-utils.js"></script> 21 </head> 22 <body> 23 <iframe srcdoc=""></iframe> 24 <script> 25 "use strict"; 26 27 const searchParams = new URLSearchParams(document.location.search); 28 const whiteSpace = searchParams.get("white-space"); 29 const testingDesignMode = searchParams.get("designMode") == "on"; 30 31 const isPreformatted = 32 whiteSpace == "pre" || whiteSpace == "pre-line" || whiteSpace == "pre-wrap"; 33 34 const iframe = document.querySelector("iframe"); 35 const minimumSrcDoc = 36 "<html>" + 37 "<head style='display:block'>" + 38 "<title>iframe</title>" + 39 "<script src='/resources/testdriver.js'></" + "script>" + 40 "<script src='/resources/testdriver-vendor.js'></" + "script>" + 41 "<script src='/resources/testdriver-actions.js'></" + "script>" + 42 "</head>" + 43 "<body><br></body>" + 44 "</html>"; 45 46 async function initializeAndWaitForLoad(iframeElement, srcDocValue) { 47 const waitForLoad = 48 new Promise( 49 resolve => iframeElement.addEventListener("load", resolve, {once: true}) 50 ); 51 iframeElement.srcdoc = srcDocValue; 52 await waitForLoad; 53 if (testingDesignMode) { 54 iframeElement.contentDocument.designMode = "on"; 55 } else { 56 iframeElement.contentDocument.documentElement.setAttribute("contenteditable", ""); 57 } 58 iframeElement.contentWindow.focus(); 59 iframeElement.contentDocument.execCommand("defaultParagraphSeparator", false, "div"); 60 } 61 62 function removeResourceScriptElements(node) { 63 node.querySelectorAll("script").forEach( 64 element => { 65 if (element.getAttribute("src")?.startsWith("/resources")) { 66 element.remove() 67 } 68 } 69 ); 70 } 71 72 // DO NOT USE multi-line comment in this file, then, you can comment out 73 // unnecessary tests when you need to attach the browser with a debugger. 74 75 // <title>, <style> and <script> cannot have <br> element. Therefore, it's 76 // hard to think what is the best if linefeeds are not preformatted. 77 promise_test(async () => { 78 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 79 const childDoc = iframe.contentDocument; 80 const title = childDoc.querySelector("title"); 81 title.setAttribute("style", `display:block;white-space:${whiteSpace}`); 82 const utils = new EditorTestUtils(title); 83 utils.setupEditingHost("{}"); 84 await utils.sendEnterKey(); 85 removeResourceScriptElements(childDoc); 86 title.removeAttribute("style"); 87 childDoc.head.removeAttribute("style"); 88 89 if (!isPreformatted) { 90 assert_in_array( 91 childDoc.documentElement.innerHTML, 92 [ 93 "<head><title></title></head><body><br></body>", // noop 94 "<head><title>\n</title></head><body><br></body>", // (collapse white-space) 95 "<head><title>\n\n</title></head><body><br></body>", // (collapse white-spaces) 96 ], 97 "0-2 collapsible linefeed(s) should be inserted" 98 ); 99 } else { 100 // The second linefeed is required to make the last line visible 101 assert_equals( 102 childDoc.documentElement.innerHTML, 103 "<head><title>\n\n</title></head><body><br></body>", 104 "2 preformatted linefeeds should be inserted" 105 ); 106 } 107 }, `insertParagraph in empty <title style="display:block;white-space:${whiteSpace}"> should not split it`); 108 109 promise_test(async () => { 110 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 111 const childDoc = iframe.contentDocument; 112 const title = childDoc.querySelector("title"); 113 title.setAttribute("style", `display:block;white-space:${whiteSpace}`); 114 const utils = new EditorTestUtils(title); 115 utils.setupEditingHost("ab[]cd"); 116 await utils.sendEnterKey(); 117 removeResourceScriptElements(childDoc); 118 title.removeAttribute("style"); 119 childDoc.head.removeAttribute("style"); 120 121 if (!isPreformatted) { 122 assert_in_array( 123 childDoc.documentElement.innerHTML, 124 [ 125 "<head><title>abcd</title></head><body><br></body>", // noop 126 "<head><title>ab\ncd</title></head><body><br></body>", // (collapsible white-space) 127 ], 128 "0-1 collapsible linefeed should be inserted" 129 ); 130 } else { 131 assert_equals( 132 childDoc.documentElement.innerHTML, 133 "<head><title>ab\ncd</title></head><body><br></body>", 134 "1 preformatted linefeed should be inserted" 135 ); 136 } 137 }, `insertParagraph in <title style="display:block;white-space:${whiteSpace}"> containing text should not split it`); 138 139 promise_test(async () => { 140 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 141 const childDoc = iframe.contentDocument; 142 const style = childDoc.createElement("style"); 143 style.setAttribute("style", `display:block;white-space:${whiteSpace}`); 144 childDoc.head.appendChild(style); 145 const utils = new EditorTestUtils(style); 146 utils.setupEditingHost("{}"); 147 await utils.sendEnterKey(); 148 removeResourceScriptElements(childDoc); 149 style.removeAttribute("style"); 150 childDoc.head.removeAttribute("style"); 151 152 if (!isPreformatted) { 153 assert_in_array( 154 childDoc.documentElement.innerHTML, 155 [ 156 "<head><title>iframe</title><style></style></head><body><br></body>", // noop 157 "<head><title>iframe</title><style>\n</style></head><body><br></body>", // (collapsible white-space) 158 "<head><title>iframe</title><style>\n\n</style></head><body><br></body>", // (collapsible white-spaces) 159 ], 160 "0-2 collapsible linefeeds should be inserted" 161 ); 162 } else { 163 // The second linefeed is required to make the last line visible 164 assert_equals( 165 childDoc.documentElement.innerHTML, 166 "<head><title>iframe</title><style>\n\n</style></head><body><br></body>", 167 "2 preformatted linefeeds should be inserted" 168 ); 169 } 170 }, `insertParagraph in empty <style style="display:block;white-space:${whiteSpace}"> should not split it`); 171 172 promise_test(async () => { 173 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 174 const childDoc = iframe.contentDocument; 175 const style = childDoc.createElement("style"); 176 style.setAttribute("style", `display:block;white-space:${whiteSpace}`); 177 childDoc.head.appendChild(style); 178 const utils = new EditorTestUtils(style); 179 utils.setupEditingHost("ab[]cd"); 180 await utils.sendEnterKey(); 181 removeResourceScriptElements(childDoc); 182 style.removeAttribute("style"); 183 childDoc.head.removeAttribute("style"); 184 185 if (!isPreformatted) { 186 assert_in_array( 187 childDoc.documentElement.innerHTML, 188 [ 189 "<head><title>iframe</title><style>abcd</style></head><body><br></body>", // noop 190 "<head><title>iframe</title><style>ab\ncd</style></head><body><br></body>", // (collapsible white-space) 191 ], 192 "0-1 collapsible linefeed should be inserted" 193 ); 194 } else { 195 assert_equals( 196 childDoc.documentElement.innerHTML, 197 "<head><title>iframe</title><style>ab\ncd</style></head><body><br></body>", 198 "1 preformatted linefeed should be inserted" 199 ); 200 } 201 }, `insertParagraph in <style style="display:block;white-space:${whiteSpace}"> containing text should not split it`); 202 203 promise_test(async () => { 204 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 205 const childDoc = iframe.contentDocument; 206 const script = childDoc.createElement("script"); 207 script.setAttribute("style", `display:block;white-space:${whiteSpace}`); 208 childDoc.head.appendChild(script); 209 // Setting <script>.innerHTML causes throwing exception because it runs 210 // setting script, so we cannot use EditorTestUtils.setupEditingHost. 211 childDoc.getSelection().collapse(script, 0); 212 const utils = new EditorTestUtils(childDoc.documentElement); 213 await utils.sendEnterKey(); 214 removeResourceScriptElements(childDoc); 215 script.removeAttribute("style"); 216 childDoc.head.removeAttribute("style"); 217 218 if (!isPreformatted) { 219 assert_in_array( 220 childDoc.documentElement.innerHTML, 221 [ 222 "<head><title>iframe</title><script></" + "script></head><body><br></body>", // noop 223 "<head><title>iframe</title><script>\n</" + "script></head><body><br></body>", // (collapsible white-space) 224 "<head><title>iframe</title><script>\n\n</" + "script></head><body><br></body>", // (collapsible white-spaces) 225 ], 226 "0-2 collapsible linefeeds should be inserted" 227 ); 228 } else { 229 // The second linefeed is required to make the last line visible 230 assert_equals( 231 childDoc.documentElement.innerHTML, 232 "<head><title>iframe</title><script>\n\n</" + "script></head><body><br></body>", 233 "2 preformatted linefeeds should be inserted" 234 ); 235 } 236 }, `insertParagraph in empty <script style="display:block;white-space:${whiteSpace}"> should not split it`); 237 238 promise_test(async () => { 239 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 240 const childDoc = iframe.contentDocument; 241 const script = childDoc.createElement("script"); 242 script.setAttribute("style", `display:block;white-space:${whiteSpace}`); 243 childDoc.head.appendChild(script); 244 // Setting <script>.innerHTML causes throwing exception because it runs 245 // setting script, so we cannot use EditorTestUtils.setupEditingHost. 246 script.innerText = "// ab// cd"; 247 childDoc.getSelection().collapse(script.firstChild, "// ab".length); 248 const utils = new EditorTestUtils(childDoc.documentElement); 249 await utils.sendEnterKey(); 250 removeResourceScriptElements(childDoc); 251 script.removeAttribute("style"); 252 childDoc.head.removeAttribute("style"); 253 254 if (!isPreformatted) { 255 assert_in_array( 256 childDoc.documentElement.innerHTML, 257 [ 258 "<head><title>iframe</title><script>// ab// cd</" + "script></head><body><br></body>", // noop 259 "<head><title>iframe</title><script>// ab\n// cd</" + "script></head><body><br></body>", // (collapsible white-space) 260 ], 261 "0-1 linefeed should be inserted" 262 ); 263 } else { 264 assert_equals( 265 childDoc.documentElement.innerHTML, 266 "<head><title>iframe</title><script>// ab\n// cd</" + "script></head><body><br></body>", 267 "1 preformatted linefeed should be inserted" 268 ); 269 } 270 }, `insertParagraph in <script style="display:block;white-space:${whiteSpace}"> containing text should not split it`); 271 272 // <div> element in the <head> should be same behavior as the following result. 273 // See insertparagraph-in-child-of-html.tentative.html for the detail. 274 promise_test(async () => { 275 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 276 const childDoc = iframe.contentDocument; 277 const div = childDoc.createElement("div"); 278 div.setAttribute("style", `white-space:${whiteSpace}`); 279 childDoc.head.appendChild(div); 280 const utils = new EditorTestUtils(div); 281 utils.setupEditingHost("{}"); 282 await utils.sendEnterKey(); 283 removeResourceScriptElements(childDoc); 284 childDoc.head.removeAttribute("style"); 285 286 if (!isPreformatted) { 287 assert_equals( 288 childDoc.documentElement.innerHTML, 289 `<head><title>iframe</title><div style="white-space:${whiteSpace}"><br></div><div style="white-space:${whiteSpace}"><br></div></head><body><br></body>`, 290 "The <div> should be split" 291 ); 292 } else { 293 assert_in_array( 294 childDoc.documentElement.innerHTML, 295 [ 296 `<head><title>iframe</title><div style="white-space:${whiteSpace}">\n</div><div style="white-space:${whiteSpace}">\n</div></head><body><br></body>`, 297 `<head><title>iframe</title><div style="white-space:${whiteSpace}"><br></div><div style="white-space:${whiteSpace}"><br></div></head><body><br></body>`, 298 ], 299 "The <div> should be split" 300 ); 301 } 302 }, `insertParagraph in empty <div style="white-space:${ 303 whiteSpace 304 }"> in the <head> should split the <div>`); 305 306 promise_test(async () => { 307 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 308 const childDoc = iframe.contentDocument; 309 const div = childDoc.createElement("div"); 310 div.setAttribute("style", `white-space:${whiteSpace}`); 311 childDoc.head.appendChild(div); 312 const utils = new EditorTestUtils(div); 313 utils.setupEditingHost("{}<br>"); 314 await utils.sendEnterKey(); 315 removeResourceScriptElements(childDoc); 316 childDoc.head.removeAttribute("style"); 317 318 if (!isPreformatted) { 319 assert_equals( 320 childDoc.documentElement.innerHTML, 321 `<head><title>iframe</title><div style="white-space:${whiteSpace}"><br></div><div style="white-space:${whiteSpace}"><br></div></head><body><br></body>`, 322 "The <div> should be split" 323 ); 324 } else { 325 assert_in_array( 326 childDoc.documentElement.innerHTML, 327 [ 328 `<head><title>iframe</title><div style="white-space:${whiteSpace}">\n</div><div style="white-space:${whiteSpace}">\n</div></head><body><br></body>`, 329 `<head><title>iframe</title><div style="white-space:${whiteSpace}"><br></div><div style="white-space:${whiteSpace}">\n</div></head><body><br></body>`, 330 `<head><title>iframe</title><div style="white-space:${whiteSpace}">\n</div><div style="white-space:${whiteSpace}"><br></div></head><body><br></body>`, 331 `<head><title>iframe</title><div style="white-space:${whiteSpace}"><br></div><div style="white-space:${whiteSpace}"><br></div></head><body><br></body>`, 332 ], 333 "The <div> should be split" 334 ); 335 } 336 }, `insertParagraph in <div style="white-space:${ 337 whiteSpace 338 }"> (containing only a <br>) in the <head> should split the <div> element`); 339 340 promise_test(async () => { 341 await initializeAndWaitForLoad(iframe, minimumSrcDoc); 342 const childDoc = iframe.contentDocument; 343 const div = childDoc.createElement("div"); 344 div.setAttribute("style", `white-space:${whiteSpace}`); 345 childDoc.head.appendChild(div); 346 const utils = new EditorTestUtils(div); 347 utils.setupEditingHost("ab[]cd"); 348 await utils.sendEnterKey(); 349 removeResourceScriptElements(childDoc); 350 childDoc.head.removeAttribute("style"); 351 352 assert_in_array( 353 childDoc.documentElement.innerHTML, 354 [ 355 `<head><title>iframe</title><div style="white-space:${whiteSpace}">ab</div><div style="white-space:${whiteSpace}">cd</div></head><body><br></body>`, 356 `<head><title>iframe</title><div style="white-space:${whiteSpace}">ab</div><div style="white-space:${whiteSpace}">cd<br></div></head><body><br></body>`, 357 `<head><title>iframe</title><div style="white-space:${whiteSpace}">ab<br></div><div style="white-space:${whiteSpace}">cd</div></head><body><br></body>`, 358 `<head><title>iframe</title><div style="white-space:${whiteSpace}">ab<br></div><div style="white-space:${whiteSpace}">cd<br></div></head><body><br></body>`, 359 ], 360 "The <div> should be split" 361 ); 362 }, `insertParagraph in <div style="white-space:${ 363 whiteSpace 364 }"> (containing text) in the <head> should split the <div> element`); 365 </script> 366 </body> 367 </html>