Skip to content

Commit

Permalink
simulate object.create via ActiveXObject under IE8 zloirock#727
Browse files Browse the repository at this point in the history
  • Loading branch information
aleen42 committed Dec 14, 2019
1 parent 6dcf223 commit 5efd505
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions packages/core-js/internals/object-create.js
Expand Up @@ -8,16 +8,33 @@ var sharedKey = require('../internals/shared-key');

var IE_PROTO = sharedKey('IE_PROTO');
var PROTOTYPE = 'prototype';
var SCRIPT = 'script';
var EmptyConstructor = function () { /* empty */ };
var scriptTag = function (content) {
var GT = '>';
var LT = '<';
return LT + SCRIPT + GT + content + LT + '/' + SCRIPT + GT;
};

var NullProtoObject = function (ProtoObject) {
var length = enumBugKeys.length;
while (length--) delete ProtoObject[PROTOTYPE][enumBugKeys[length]];
return ProtoObject;
};

// Create object with fake `null` prototype: use ActiveX Object with cleared prototype
var NullProtoObjectViaActiveX = function (activeXDocument) {
activeXDocument.write(scriptTag(''));
activeXDocument.close();
var temp = activeXDocument.parentWindow.Object;
activeXDocument = null; // avoid memory leak
return NullProtoObject(temp);
};

// Create object with fake `null` prototype: use iframe Object with cleared prototype
var NullProtoObject = function () {
var NullProtoObjectViaIFrame = function () {
// Thrash, waste and sodomy: IE GC bug
var iframe = documentCreateElement('iframe');
var length = enumBugKeys.length;
var GT = '>';
var LT = '<';
var SCRIPT = 'script';
var JS = 'java' + SCRIPT + ':';
var iframeDocument;
iframe.style.display = 'none';
Expand All @@ -26,11 +43,9 @@ var NullProtoObject = function () {
iframe.src = String(JS);
iframeDocument = iframe.contentWindow.document;
iframeDocument.open();
iframeDocument.write(LT + SCRIPT + GT + 'document.F=Object' + LT + '/' + SCRIPT + GT);
iframeDocument.write(scriptTag('document.F=Object'));
iframeDocument.close();
NullProtoObject = iframeDocument.F;
while (length--) delete NullProtoObject[PROTOTYPE][enumBugKeys[length]];
return NullProtoObject();
return NullProtoObject(iframeDocument.F);
};

hiddenKeys[IE_PROTO] = true;
Expand All @@ -45,6 +60,18 @@ module.exports = Object.create || function create(O, Properties) {
EmptyConstructor[PROTOTYPE] = null;
// add "__proto__" for Object.getPrototypeOf polyfill
result[IE_PROTO] = O;
} else result = NullProtoObject();
} else {
// Check for document.domain and active x support
// No need to use active x approach when document.domain is not set
// see https://github.com/es-shims/es5-shim/issues/150
// variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
var activeXDocument;
try {
/* global ActiveXObject */
activeXDocument = document.domain && new ActiveXObject('htmlfile');
} catch (error) { /* ignore */ }

result = activeXDocument ? NullProtoObjectViaActiveX(activeXDocument) : NullProtoObjectViaIFrame();
}
return Properties === undefined ? result : defineProperties(result, Properties);
};

0 comments on commit 5efd505

Please sign in to comment.