constructors.html (5660B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Realm for constructed objects</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id=log></div> 7 <script> 8 function object_realm(dp) { 9 // Note that browsers use the URL of the relevant global object's associated 10 // Document. 11 // https://github.com/w3c/DOM-Parsing/issues/46 12 var url = DOMParser.prototype.parseFromString.call(dp, "x", "text/html").documentURI; 13 var file = url.slice(url.lastIndexOf("/") + 1); 14 switch (file) { 15 case "constructors.html": 16 return "parent window"; 17 case "constructors-support.html": 18 return "child window"; 19 default: 20 return "???"; 21 } 22 } 23 24 async_test(function() { 25 var iframe = document.createElement("iframe"); 26 iframe.onload = this.step_func_done(function() { 27 var child = iframe.contentWindow; 28 test(function() { 29 var dp = new DOMParser(); 30 assert_equals(Object.getPrototypeOf(dp), DOMParser.prototype); 31 assert_equals(object_realm(dp), "parent window"); 32 }, "Normal constructor in parent window"); 33 34 test(function() { 35 var dp = new child.DOMParser(); 36 assert_equals(Object.getPrototypeOf(dp), child.DOMParser.prototype); 37 assert_equals(object_realm(dp), "child window"); 38 }, "Normal constructor in child window"); 39 40 test(function() { 41 var dp = Reflect.construct(child.DOMParser, [], DOMParser); 42 assert_equals(Object.getPrototypeOf(dp), DOMParser.prototype); 43 assert_equals(object_realm(dp), "child window"); 44 }, "Constructor in child window with normal NewTarget from parent window"); 45 46 test(function() { 47 var dp = Reflect.construct(DOMParser, [], child.DOMParser); 48 assert_equals(Object.getPrototypeOf(dp), child.DOMParser.prototype); 49 assert_equals(object_realm(dp), "parent window"); 50 }, "Constructor in parent window with normal NewTarget from child window"); 51 52 test(function() { 53 class DOMParserSubclass extends DOMParser {} 54 var dp = new DOMParserSubclass(); 55 assert_equals(Object.getPrototypeOf(dp), DOMParserSubclass.prototype); 56 assert_equals(object_realm(dp), "parent window"); 57 }, "Subclass constructor in parent window"); 58 59 test(function() { 60 var dp = new child.DOMParserSubclass(); 61 assert_equals(Object.getPrototypeOf(dp), child.DOMParserSubclass.prototype); 62 assert_equals(object_realm(dp), "child window"); 63 }, "Subclass constructor in child window"); 64 65 test(function() { 66 class ForeignDOMParserSubclass extends child.DOMParser {} 67 var dp = new ForeignDOMParserSubclass(); 68 assert_equals(Object.getPrototypeOf(dp), ForeignDOMParserSubclass.prototype); 69 assert_equals(object_realm(dp), "child window"); 70 }, "Subclass constructor in parent window with parent class in child window"); 71 72 test(function() { 73 var dp = new child.ForeignDOMParserSubclass(); 74 assert_equals(Object.getPrototypeOf(dp), child.ForeignDOMParserSubclass.prototype); 75 assert_equals(object_realm(dp), "parent window"); 76 }, "Subclass constructor in child window with parent class in parent window"); 77 78 test(function() { 79 var badNewTarget = function() {}; 80 badNewTarget.prototype = 7; 81 82 var dp = Reflect.construct(child.DOMParser, [], badNewTarget); 83 assert_equals(Object.getPrototypeOf(dp), DOMParser.prototype); 84 assert_equals(object_realm(dp), "child window"); 85 }, "Constructor in child window with bad NewTarget from parent window"); 86 87 test(function() { 88 var dp = Reflect.construct(DOMParser, [], child.badNewTarget); 89 assert_equals(Object.getPrototypeOf(dp), child.DOMParser.prototype); 90 assert_equals(object_realm(dp), "parent window"); 91 }, "Constructor in parent window with bad NewTarget from child window"); 92 93 test(function() { 94 var badNewTarget = Function.prototype.bind.call(new child.Function()); 95 badNewTarget.prototype = 7; 96 97 var dp = Reflect.construct(DOMParser, [], badNewTarget); 98 assert_equals(Object.getPrototypeOf(dp), child.DOMParser.prototype); 99 assert_equals(object_realm(dp), "parent window"); 100 }, "Constructor in parent window with bad NewTarget from parent window that's a bound child window function"); 101 102 test(function() { 103 var badNewTarget = child.Function.prototype.bind.call(new Function()); 104 badNewTarget.prototype = 7; 105 106 var dp = Reflect.construct(child.DOMParser, [], badNewTarget); 107 assert_equals(Object.getPrototypeOf(dp), DOMParser.prototype); 108 assert_equals(object_realm(dp), "child window"); 109 }, "Constructor in child window with bad NewTarget from child window that's a bound parent window function"); 110 111 test(function() { 112 var badNewTarget = new Proxy(new child.Function(), {}); 113 badNewTarget.prototype = 7; 114 115 var dp = Reflect.construct(DOMParser, [], badNewTarget); 116 assert_equals(Object.getPrototypeOf(dp), child.DOMParser.prototype); 117 assert_equals(object_realm(dp), "parent window"); 118 }, "Constructor in parent window with bad NewTarget from parent window that's a proxy for a child window function"); 119 120 test(function() { 121 var badNewTarget = new child.Proxy(new Function(), {}); 122 badNewTarget.prototype = 7; 123 124 var dp = Reflect.construct(child.DOMParser, [], badNewTarget); 125 assert_equals(Object.getPrototypeOf(dp), DOMParser.prototype); 126 assert_equals(object_realm(dp), "child window"); 127 }, "Constructor in child window with bad NewTarget from child window that's a proxy for a parent window function"); 128 }); 129 iframe.src = "support/constructors-support.html"; 130 document.body.appendChild(iframe); 131 }); 132 </script>