basics.html (2366B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Named access on the window object</title> 4 <link rel="author" title="Matthew Phillips" href="mailto:matthew@matthewphillips.info"> 5 <link rel="help" href="https://html.spec.whatwg.org/#named-access-on-the-window-object"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 9 <body> 10 <script> 11 "use strict"; 12 13 test(() => { 14 const img = document.createElement("img"); 15 img.setAttribute("name", "namedimage"); 16 document.body.appendChild(img); 17 18 assert_equals(img, window.namedimage); 19 }, "A named property must return an element as-is if its name attribute matches"); 20 21 test(() => { 22 const img = document.createElement("img"); 23 img.setAttribute("name", "foo"); 24 25 assert_false("foo" in window); 26 }, "A named property must not be set if the element is not reachable from the Document"); 27 28 test(() => { 29 const img = document.createElement("img"); 30 img.setAttribute("name", "enumerable-foo"); 31 document.body.appendChild(img); 32 33 let found = false; 34 for (const key in window) { 35 if (key === "enumerable-foo") { 36 found = true; 37 } 38 } 39 40 assert_false(found); 41 }, "A named property must not be enumerable"); 42 43 test(() => { 44 const img = document.createElement("img"); 45 img.setAttribute("id", "foo-id"); 46 document.body.appendChild(img); 47 48 assert_equals(img, window["foo-id"]); 49 }, "A named property must return any element for which the id attribute matches"); 50 51 test(() => { 52 document.body.appendChild(document.createTextNode("foo")); 53 54 const img = document.createElement("img"); 55 img.setAttribute("id", "with-non-element"); 56 document.body.appendChild(img); 57 58 assert_equals(window["with-non-element"], img); 59 }, "A non element node in the document should not cause errors"); 60 61 test(() => { 62 const img = document.createElement("img"); 63 document.body.appendChild(img); 64 img.setAttribute("name", "dupe"); 65 img.setAttribute("id", "dupe"); 66 assert_equals(window.dupe, img); 67 68 const img2 = document.createElement("img"); 69 document.body.appendChild(img2); 70 img2.setAttribute("name", "dupe"); 71 img2.setAttribute("id", "dupe"); 72 73 assert_equals(window.dupe.length, 2); 74 assert_equals(window.dupe[0], img); 75 assert_equals(window.dupe[1], img2); 76 }, "An element with identical name and id attributes should occur in the HTMLCollection once, not twice"); 77 </script>