Range-adopt-test.html (2551B)
1 <!DOCTYPE html> 2 <script src='/resources/testharness.js'></script> 3 <script src='/resources/testharnessreport.js'></script> 4 <script> 5 function createRangeWithUnparentedContainerOfSingleElement() { 6 const range = document.createRange(); 7 const container = document.createElement("container"); 8 const element = document.createElement("element"); 9 container.appendChild(element); 10 range.selectNode(element); 11 return range; 12 } 13 function nestRangeInOuterContainer(range) { 14 range.startContainer.ownerDocument.createElement("outer").appendChild(range.startContainer); 15 } 16 function moveNodeToNewlyCreatedDocumentWithAppendChild(node) { 17 document.implementation.createDocument(null, null).appendChild(node); 18 } 19 20 //Tests removing only element 21 test(()=>{ 22 const range = createRangeWithUnparentedContainerOfSingleElement(); 23 range.startContainer.removeChild(range.startContainer.firstChild); 24 assert_equals(range.endOffset, 0); 25 }, "Range in document: Removing the only element in the range must collapse the range"); 26 27 28 //Tests removing only element after parented container moved to another document 29 test(()=>{ 30 const range = createRangeWithUnparentedContainerOfSingleElement(); 31 nestRangeInOuterContainer(range); 32 moveNodeToNewlyCreatedDocumentWithAppendChild(range.startContainer); 33 assert_equals(range.endOffset, 0); 34 }, "Parented range container moved to another document with appendChild: Moving the element to the other document must collapse the range"); 35 36 //Tests removing only element after parentless container moved oo another document 37 test(()=>{ 38 const range = createRangeWithUnparentedContainerOfSingleElement(); 39 moveNodeToNewlyCreatedDocumentWithAppendChild(range.startContainer); 40 range.startContainer.removeChild(range.startContainer.firstChild); 41 assert_equals(range.endOffset, 0); 42 }, "Parentless range container moved to another document with appendChild: Removing the only element in the range must collapse the range"); 43 44 //Tests removing only element after parentless container of container moved to another document 45 test(()=>{ 46 const range = createRangeWithUnparentedContainerOfSingleElement(); 47 nestRangeInOuterContainer(range); 48 moveNodeToNewlyCreatedDocumentWithAppendChild(range.startContainer.parentNode); 49 range.startContainer.removeChild(range.startContainer.firstChild); 50 assert_equals(range.endOffset, 0); 51 }, "Range container's parentless container moved to another document with appendChild: Removing the only element in the range must collapse the range"); 52 </script>