OwnPropertyKeys.html (3006B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Legacy platform objects [[OwnPropertyKeys]] method</title> 4 <link rel="help" href="https://webidl.spec.whatwg.org/#legacy-platform-object-ownpropertykeys"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 function custom_assert_array_equals(actual, expected, msg) { 9 function replacer(k, v) { 10 if (typeof v === "symbol") { 11 return v.toString(); 12 } 13 return v; 14 } 15 assert_array_equals(actual, expected, " actual " + JSON.stringify(actual, replacer) + " expected " + JSON.stringify(expected, replacer)); 16 } 17 18 test(function() { 19 var element = document.createElement("div"); 20 element.appendChild(document.createElement("div")); 21 element.appendChild(document.createElement("div")); 22 element.appendChild(document.createElement("div")); 23 custom_assert_array_equals(Reflect.ownKeys(element.childNodes), ["0", "1", "2"]); 24 }, "must enumerate property indices in ascending numerical order"); 25 26 test(function() { 27 var element = document.createElement("div"); 28 element.setAttribute("data-foo", "foo content"); 29 element.setAttribute("data-bar", "bar content"); 30 element.setAttribute("data-baz", "baz content"); 31 custom_assert_array_equals(Reflect.ownKeys(element.dataset), ["foo", "bar", "baz"]); 32 }, "must enumerate property names in list order"); 33 34 35 test(function() { 36 var element = document.createElement("div"); 37 element.setAttribute("id", "foo"); 38 element.setAttribute("class", "bar"); 39 custom_assert_array_equals(Reflect.ownKeys(element.attributes), ["0", "1", "id", "class"]); 40 }, "must first enumerate property indices in ascending numerical order, then named properties in list order"); 41 42 43 test(function() { 44 var element = document.createElement("div"); 45 element.attributes.foo = "some value"; 46 element.attributes.bar = "and another"; 47 element.setAttribute("id", "foo"); 48 element.setAttribute("class", "bar"); 49 custom_assert_array_equals(Reflect.ownKeys(element.attributes), ["0", "1", "id", "class", "foo", "bar"]); 50 }, "must enumerate own properties after indexed and named properties even when they're added first"); 51 52 test(function() { 53 var symb1 = Symbol(); 54 var symb2 = Symbol(); 55 var element = document.createElement("div"); 56 element.attributes.foo = "some value"; 57 element.attributes[symb1] = "symb1"; 58 element.attributes[symb2] = "symb2"; 59 element.attributes.bar = "and another"; 60 element.setAttribute("id", "foo"); 61 element.setAttribute("class", "bar"); 62 custom_assert_array_equals(Reflect.ownKeys(element.attributes), 63 ["0", "1", "id", "class", "foo", "bar", symb1, symb2]); 64 }, "must enumerate symbols after strings, regardless of which ones got added first"); 65 </script>