rootNode.html (4902B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset=utf-8> 5 <title>Node.prototype.getRootNode()</title> 6 <link rel="help" href="https://dom.spec.whatwg.org/#dom-node-getrootnode"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 </head> 10 <body> 11 <script> 12 13 test(function () { 14 var shadowHost = document.createElement('div'); 15 document.body.appendChild(shadowHost); 16 17 var shadowRoot = shadowHost.attachShadow({mode: 'open'}); 18 shadowRoot.innerHTML = '<div class="shadowChild">content</div>'; 19 20 var shadowChild = shadowRoot.querySelector('.shadowChild'); 21 assert_equals(shadowChild.getRootNode({composed: true}), document, "getRootNode() must return context object's shadow-including root if options's composed is true"); 22 assert_equals(shadowChild.getRootNode({composed: false}), shadowRoot, "getRootNode() must return context object's root if options's composed is false"); 23 assert_equals(shadowChild.getRootNode(), shadowRoot, "getRootNode() must return context object's root if options's composed is default false"); 24 25 }, "getRootNode() must return context object's shadow-including root if options's composed is true, and context object's root otherwise"); 26 27 test(function () { 28 var element = document.createElement('div'); 29 assert_equals(element.getRootNode(), element, 'getRootNode() on an element without a parent must return the element itself'); 30 31 var text = document.createTextNode(''); 32 assert_equals(text.getRootNode(), text, 'getRootNode() on a text node without a parent must return the text node itself'); 33 34 var processingInstruction = document.createProcessingInstruction('target', 'data'); 35 assert_equals(processingInstruction.getRootNode(), processingInstruction, 'getRootNode() on a processing instruction node without a parent must return the processing instruction node itself'); 36 37 assert_equals(document.getRootNode(), document, 'getRootNode() on a document node must return the document itself'); 38 39 }, 'getRootNode() must return the context object when it does not have any parent'); 40 41 test(function () { 42 var parent = document.createElement('div'); 43 44 var element = document.createElement('div'); 45 parent.appendChild(element); 46 assert_equals(element.getRootNode(), parent, 'getRootNode() on an element with a single ancestor must return the parent node'); 47 48 var text = document.createTextNode(''); 49 parent.appendChild(text); 50 assert_equals(text.getRootNode(), parent, 'getRootNode() on a text node with a single ancestor must return the parent node'); 51 52 var processingInstruction = document.createProcessingInstruction('target', 'data'); 53 parent.appendChild(processingInstruction) 54 assert_equals(processingInstruction.getRootNode(), parent, 'getRootNode() on a processing instruction node with a single ancestor must return the parent node'); 55 56 }, 'getRootNode() must return the parent node of the context object when the context object has a single ancestor not in a document'); 57 58 test(function () { 59 var parent = document.createElement('div'); 60 document.body.appendChild(parent); 61 62 var element = document.createElement('div'); 63 parent.appendChild(element); 64 assert_equals(element.getRootNode(), document, 'getRootNode() on an element inside a document must return the document'); 65 66 var text = document.createTextNode(''); 67 parent.appendChild(text); 68 assert_equals(text.getRootNode(), document, 'getRootNode() on a text node inside a document must return the document'); 69 70 var processingInstruction = document.createProcessingInstruction('target', 'data'); 71 parent.appendChild(processingInstruction) 72 assert_equals(processingInstruction.getRootNode(), document, 'getRootNode() on a processing instruction node inside a document must return the document'); 73 }, 'getRootNode() must return the document when a node is in document'); 74 75 test(function () { 76 var fragment = document.createDocumentFragment(); 77 var parent = document.createElement('div'); 78 fragment.appendChild(parent); 79 80 var element = document.createElement('div'); 81 parent.appendChild(element); 82 assert_equals(element.getRootNode(), fragment, 'getRootNode() on an element inside a document fragment must return the fragment'); 83 84 var text = document.createTextNode(''); 85 parent.appendChild(text); 86 assert_equals(text.getRootNode(), fragment, 'getRootNode() on a text node inside a document fragment must return the fragment'); 87 88 var processingInstruction = document.createProcessingInstruction('target', 'data'); 89 parent.appendChild(processingInstruction) 90 assert_equals(processingInstruction.getRootNode(), fragment, 91 'getRootNode() on a processing instruction node inside a document fragment must return the fragment'); 92 }, 'getRootNode() must return a document fragment when a node is in the fragment'); 93 94 </script> 95 </body> 96 </html>