active.window.js (4156B)
1 function assertOpenIsEffective(doc, initialNodeCount) { 2 assert_equals(doc.childNodes.length, initialNodeCount); 3 4 // Test direct document.open() call. 5 assert_equals(doc.open(), doc); 6 assert_equals(doc.childNodes.length, 0, "after open: no nodes in document"); 7 doc.write("<!DOCTYPE html>"); 8 assert_equals(doc.childNodes.length, 1, "after write: doctype node in document"); 9 doc.close(); 10 assert_equals(doc.childNodes.length, 2, "after parser close: doctype node and an html element in document"); 11 12 // Test implicit document.open() call through write(). Since we called 13 // doc.close() above, which sets the insertion point of the parser to 14 // undefined, document.write() will run the document open steps. 15 doc.write(); 16 assert_equals(doc.childNodes.length, 0, "after implicit open: no nodes in document"); 17 doc.write("<!DOCTYPE html>"); 18 assert_equals(doc.childNodes.length, 1, "after write: doctype node in document"); 19 doc.close(); 20 assert_equals(doc.childNodes.length, 2, "after parser close: doctype node and an html element in document"); 21 } 22 23 test(t => { 24 const frame = document.body.appendChild(document.createElement("iframe")); 25 t.add_cleanup(() => frame.remove()); 26 assertOpenIsEffective(frame.contentDocument, 1); 27 }, "document.open() removes the document's children (fully active document)"); 28 29 async_test(t => { 30 const frame = document.body.appendChild(document.createElement("iframe")); 31 t.add_cleanup(() => frame.remove()); 32 frame.onload = t.step_func(() => { 33 const childFrame = frame.contentDocument.querySelector("iframe"); 34 const childDoc = childFrame.contentDocument; 35 const childWin = childFrame.contentWindow; 36 37 // Right now childDoc is still fully active. 38 39 frame.onload = t.step_func_done(() => { 40 // Now childDoc is still active but no longer fully active. 41 assertOpenIsEffective(childDoc, 1); 42 }); 43 frame.src = "/common/blank.html"; 44 }); 45 frame.src = "resources/page-with-frame.html"; 46 }, "document.open() removes the document's children (active but not fully active document)"); 47 48 test(t => { 49 const frame = document.body.appendChild(document.createElement("iframe")); 50 const doc = frame.contentDocument; 51 52 // Right now the frame is connected and it has an active document. 53 frame.remove(); 54 55 // Now the frame is no longer connected. Its document is no longer active. 56 assertOpenIsEffective(doc, 1); 57 }, "document.open() removes the document's children (non-active document with an associated Window object; frame is removed)"); 58 59 async_test(t => { 60 const frame = document.body.appendChild(document.createElement("iframe")); 61 t.add_cleanup(() => frame.remove()); 62 frame.src = "resources/dummy.html"; 63 64 frame.onload = t.step_func(() => { 65 const firstDocument = frame.contentDocument; 66 // Right now the frame is connected and it has an active document. 67 68 frame.onload = t.step_func_done(() => { 69 // Now even though the frame is still connected, its document is no 70 // longer active. 71 assert_not_equals(frame.contentDocument, firstDocument); 72 assertOpenIsEffective(firstDocument, 2); 73 }); 74 75 frame.src = "/common/blank.html"; 76 }); 77 }, "document.open() removes the document's children (non-active document with an associated Window object; navigated away)"); 78 79 test(t => { 80 const doc = document.implementation.createHTMLDocument(); 81 assertOpenIsEffective(doc, 2); 82 }, "document.open() removes the document's children (non-active document without an associated Window object; createHTMLDocument)"); 83 84 test(t => { 85 const doc = new DOMParser().parseFromString("", "text/html"); 86 assertOpenIsEffective(doc, 1); 87 }, "document.open() removes the document's children (non-active document without an associated Window object; DOMParser)"); 88 89 async_test(t => { 90 const xhr = new XMLHttpRequest(); 91 xhr.onload = t.step_func_done(() => { 92 assert_equals(xhr.status, 200); 93 assertOpenIsEffective(xhr.responseXML, 2); 94 }); 95 xhr.responseType = "document"; 96 xhr.open("GET", "resources/dummy.html"); 97 xhr.send(); 98 }, "document.open() removes the document's children (non-active document without an associated Window object; XMLHttpRequest)");