GetOwnProperty.html (3903B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Legacy platform objects [[GetOwnProperty]] method</title> 4 <link rel="help" href="https://webidl.spec.whatwg.org/#legacy-platform-object-getownproperty"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="./helper.js"></script> 8 <script> 9 10 test(function() { 11 // DOMTokenList has an indexed property getter, no indexed property setter 12 // and no named property handlers. 13 let div = document.createElement("div"); 14 div.classList.add("baz", "quux"); 15 const domTokenList = div.classList; 16 assert_prop_desc_equals( 17 domTokenList, "1", 18 {value: "quux", writable: false, enumerable: true, configurable: true}, 19 "[[GetOwnProperty]] for indexed properties returns the right descriptor"); 20 assert_prop_desc_equals( 21 domTokenList, "42", undefined, 22 "[[GetOwnProperty]] with invalid index returns undefined"); 23 assert_array_equals(Object.keys(domTokenList), ["0", "1"]); 24 assert_array_equals(Object.values(domTokenList), ["baz", "quux"]); 25 26 // getElementsByTagName() returns an HTMLCollection. 27 // HTMLCollection has indexed and named property getters, no setters. Its IDL 28 // interface declaration has [LegacyUnenumerableNamedProperties] so its named 29 // properties are not enumerable. 30 let span1 = document.createElement("span"); 31 span1.id = "foo"; 32 let span2 = document.createElement("span"); 33 span2.id = "bar"; 34 document.head.appendChild(span1); 35 document.head.appendChild(span2); 36 const elementList = document.getElementsByTagName("span"); 37 assert_prop_desc_equals( 38 elementList, "foo", 39 {value: span1, writable: false, enumerable: false, configurable: true}, 40 "[[GetOwnProperty]] for named properties returns the right descriptor"); 41 assert_prop_desc_equals( 42 elementList, "1", 43 {value: span2, writable: false, enumerable: true, configurable: true}, 44 "[[GetOwnProperty]] for indexed properties returns the right descriptor"); 45 assert_prop_desc_equals( 46 elementList, "unknown", undefined, 47 "[[GetOwnProperty]] with invalid property name returns undefined"); 48 assert_array_equals(Object.keys(elementList), ["0", "1"]); 49 assert_array_equals(Object.values(elementList), [span1, span2]); 50 }, "[[GetOwnProperty]] with getters and no setters"); 51 52 test(function() { 53 // DOMStringMap supports named property getters and setters, but not indexed 54 // properties. 55 let span = document.createElement("span"); 56 span.dataset.foo = "bar"; 57 assert_prop_desc_equals( 58 span.dataset, "foo", 59 {value: "bar", writable: true, enumerable: true, configurable: true}, 60 "[[GetOwnProperty]] for named properties returns the right descriptor"); 61 assert_prop_desc_equals( 62 span.dataset, "unknown", undefined, 63 "[[GetOwnProperty]] with invalid property name returns undefined"); 64 assert_array_equals(Object.keys(span.dataset), ["foo"]); 65 assert_array_equals(Object.values(span.dataset), ["bar"]); 66 }, "[[GetOwnProperty]] with named property getters and setters"); 67 68 test(function() { 69 // HTMLSelectElement has indexed property getters and setters, but no support 70 // for named properties. 71 let selectElement = document.createElement("select"); 72 assert_prop_desc_equals( 73 selectElement, "0", undefined, 74 "[[GetOwnProperty]] with invalid property index returns undefined"); 75 let optionElement = document.createElement("option"); 76 selectElement.appendChild(optionElement); 77 assert_prop_desc_equals( 78 selectElement, "0", 79 {value: optionElement, writable: true, enumerable: true, configurable: true}, 80 "[[GetOwnProperty]] for indexed properties returns the right descriptor"); 81 assert_array_equals(Object.keys(selectElement), ["0"]); 82 assert_array_equals(Object.values(selectElement), [optionElement]); 83 }, "[[GetOwnProperty]] with indexed property getters and setters"); 84 </script>