forked from bazaarvoice/bv-ui-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @fileOverview
* This file exposes a utility function to get a reset version of a primitive
* constructor, in the event that a site has overridden or polyfilled
* prototype methods in their own primitives, or used a utility like
* Prototype.js, which does that on its own.
*/
var waitForBody = require('../waitForBody');
var constructors = {};
// eslint-disable-next-line no-useless-escape
var constructorNameRegExp = /function\s+([^\(\s]+)/;
var iframe;
var getOriginalConstructor = function getOriginalConstructor (constructor) {
return new Promise(function (resolve) {
waitForBody(function () {
var constructorName = constructor.name;
// IE11 doesn't have a constructor.name property, so just in case any other
// browsers also don't, use a simple regex to pull the constructor's name
// out of the .toString()'d function declaration, e.g. "function Array()"
if (!constructorName) {
constructorName = constructorNameRegExp.exec(constructor.toString())[1];
}
if (!iframe) {
iframe = document.createElement('iframe');
iframe.src = 'about:blank';
iframe.style.display = 'none';
iframe.height = '0';
iframe.width = '0';
iframe.tabIndex = '-1';
iframe.title = 'empty';
iframe.className = 'hidden';
document.body.appendChild(iframe);
}
if (!constructors[constructorName]) {
constructors[constructorName] = iframe.contentWindow[constructorName];
}
resolve(constructors[constructorName]);
});
});
};
module.exports = getOriginalConstructor;