UNPKG

22.8 kBJavaScriptView Raw
1/*!
2 * https://github.com/es-shims/es5-shim
3 * @license es5-shim Copyright 2009-2020 by contributors, MIT License
4 * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
5 */
6
7// vim: ts=4 sts=4 sw=4 expandtab
8
9// Add semicolon to prevent IIFE from being passed as argument to concatenated code.
10; // eslint-disable-line no-extra-semi
11
12// UMD (Universal Module Definition)
13// see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
14(function (root, factory) {
15 'use strict';
16
17 /* global define */
18 if (typeof define === 'function' && define.amd) {
19 // AMD. Register as an anonymous module.
20 define(factory);
21 } else if (typeof exports === 'object') {
22 // Node. Does not work with strict CommonJS, but
23 // only CommonJS-like enviroments that support module.exports,
24 // like Node.
25 module.exports = factory();
26 } else {
27 // Browser globals (root is window)
28 root.returnExports = factory(); // eslint-disable-line no-param-reassign
29 }
30}(this, function () {
31
32 var call = Function.call;
33 var prototypeOfObject = Object.prototype;
34 var owns = call.bind(prototypeOfObject.hasOwnProperty);
35 var isEnumerable = call.bind(prototypeOfObject.propertyIsEnumerable);
36 var toStr = call.bind(prototypeOfObject.toString);
37
38 // If JS engine supports accessors creating shortcuts.
39 var defineGetter;
40 var defineSetter;
41 var lookupGetter;
42 var lookupSetter;
43 var supportsAccessors = owns(prototypeOfObject, '__defineGetter__');
44 if (supportsAccessors) {
45 /* eslint-disable no-underscore-dangle, no-restricted-properties */
46 defineGetter = call.bind(prototypeOfObject.__defineGetter__);
47 defineSetter = call.bind(prototypeOfObject.__defineSetter__);
48 lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
49 lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
50 /* eslint-enable no-underscore-dangle, no-restricted-properties */
51 }
52
53 var isPrimitive = function isPrimitive(o) {
54 return o == null || (typeof o !== 'object' && typeof o !== 'function');
55 };
56
57 // ES5 15.2.3.2
58 // http://es5.github.com/#x15.2.3.2
59 if (!Object.getPrototypeOf) {
60 // https://github.com/es-shims/es5-shim/issues#issue/2
61 // http://ejohn.org/blog/objectgetprototypeof/
62 // recommended by fschaefer on github
63 //
64 // sure, and webreflection says ^_^
65 // ... this will nerever possibly return null
66 // ... Opera Mini breaks here with infinite loops
67 Object.getPrototypeOf = function getPrototypeOf(object) {
68 // eslint-disable-next-line no-proto
69 var proto = object.__proto__;
70 if (proto || proto == null) { // `undefined` is for pre-proto browsers
71 return proto;
72 } else if (toStr(object.constructor) === '[object Function]') {
73 return object.constructor.prototype;
74 } else if (object instanceof Object) {
75 return prototypeOfObject;
76 }
77 // Correctly return null for Objects created with `Object.create(null)`
78 // (shammed or native) or `{ __proto__: null}`. Also returns null for
79 // cross-realm objects on browsers that lack `__proto__` support (like
80 // IE <11), but that's the best we can do.
81 return null;
82
83 };
84 }
85
86 // ES5 15.2.3.3
87 // http://es5.github.com/#x15.2.3.3
88
89 // check whether getOwnPropertyDescriptor works if it's given. Otherwise, shim partially.
90 if (Object.defineProperty) {
91 var doesGetOwnPropertyDescriptorWork = function doesGetOwnPropertyDescriptorWork(object) {
92 try {
93 object.sentinel = 0; // eslint-disable-line no-param-reassign
94 return Object.getOwnPropertyDescriptor(object, 'sentinel').value === 0;
95 } catch (exception) {
96 return false;
97 }
98 };
99 var getOwnPropertyDescriptorWorksOnObject = doesGetOwnPropertyDescriptorWork({});
100 var getOwnPropertyDescriptorWorksOnDom = typeof document === 'undefined'
101 || doesGetOwnPropertyDescriptorWork(document.createElement('div'));
102 if (!getOwnPropertyDescriptorWorksOnDom || !getOwnPropertyDescriptorWorksOnObject) {
103 var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor;
104 }
105 }
106
107 if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
108 var ERR_NON_OBJECT = 'Object.getOwnPropertyDescriptor called on a non-object: ';
109
110 /* eslint-disable no-proto */
111 Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
112 if (isPrimitive(object)) {
113 throw new TypeError(ERR_NON_OBJECT + object);
114 }
115
116 // make a valiant attempt to use the real getOwnPropertyDescriptor
117 // for I8's DOM elements.
118 if (getOwnPropertyDescriptorFallback) {
119 try {
120 return getOwnPropertyDescriptorFallback.call(Object, object, property);
121 } catch (exception) {
122 // try the shim if the real one doesn't work
123 }
124 }
125
126 var descriptor;
127
128 // If object does not owns property return undefined immediately.
129 if (!owns(object, property)) {
130 return descriptor;
131 }
132
133 // If object has a property then it's for sure `configurable`, and
134 // probably `enumerable`. Detect enumerability though.
135 descriptor = {
136 enumerable: isEnumerable(object, property),
137 configurable: true
138 };
139
140 // If JS engine supports accessor properties then property may be a
141 // getter or setter.
142 if (supportsAccessors) {
143 // Unfortunately `__lookupGetter__` will return a getter even
144 // if object has own non getter property along with a same named
145 // inherited getter. To avoid misbehavior we temporary remove
146 // `__proto__` so that `__lookupGetter__` will return getter only
147 // if it's owned by an object.
148 var prototype = object.__proto__;
149 var notPrototypeOfObject = object !== prototypeOfObject;
150 // avoid recursion problem, breaking in Opera Mini when
151 // Object.getOwnPropertyDescriptor(Object.prototype, 'toString')
152 // or any other Object.prototype accessor
153 if (notPrototypeOfObject) {
154 object.__proto__ = prototypeOfObject; // eslint-disable-line no-param-reassign
155 }
156
157 var getter = lookupGetter(object, property);
158 var setter = lookupSetter(object, property);
159
160 if (notPrototypeOfObject) {
161 // Once we have getter and setter we can put values back.
162 object.__proto__ = prototype; // eslint-disable-line no-param-reassign
163 }
164
165 if (getter || setter) {
166 if (getter) {
167 descriptor.get = getter;
168 }
169 if (setter) {
170 descriptor.set = setter;
171 }
172 // If it was accessor property we're done and return here
173 // in order to avoid adding `value` to the descriptor.
174 return descriptor;
175 }
176 }
177
178 // If we got this far we know that object has an own property that is
179 // not an accessor so we set it as a value and return descriptor.
180 descriptor.value = object[property];
181 descriptor.writable = true;
182 return descriptor;
183 };
184 /* eslint-enable no-proto */
185 }
186
187 // ES5 15.2.3.4
188 // http://es5.github.com/#x15.2.3.4
189 if (!Object.getOwnPropertyNames) {
190 Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
191 return Object.keys(object);
192 };
193 }
194
195 // ES5 15.2.3.5
196 // http://es5.github.com/#x15.2.3.5
197 if (!Object.create) {
198
199 // Contributed by Brandon Benvie, October, 2012
200 var createEmpty;
201 var supportsProto = !({ __proto__: null } instanceof Object);
202 // the following produces false positives
203 // in Opera Mini => not a reliable check
204 // Object.prototype.__proto__ === null
205
206 // Check for document.domain and active x support
207 // No need to use active x approach when document.domain is not set
208 // see https://github.com/es-shims/es5-shim/issues/150
209 // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
210 /* global ActiveXObject */
211 var shouldUseActiveX = function shouldUseActiveX() {
212 // return early if document.domain not set
213 if (!document.domain) {
214 return false;
215 }
216
217 try {
218 return !!new ActiveXObject('htmlfile');
219 } catch (exception) {
220 return false;
221 }
222 };
223
224 // This supports IE8 when document.domain is used
225 // see https://github.com/es-shims/es5-shim/issues/150
226 // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
227 var getEmptyViaActiveX = function getEmptyViaActiveX() {
228 var empty;
229 var xDoc;
230
231 xDoc = new ActiveXObject('htmlfile');
232
233 var script = 'script';
234 xDoc.write('<' + script + '></' + script + '>');
235 xDoc.close();
236
237 empty = xDoc.parentWindow.Object.prototype;
238 xDoc = null;
239
240 return empty;
241 };
242
243 // The original implementation using an iframe
244 // before the activex approach was added
245 // see https://github.com/es-shims/es5-shim/issues/150
246 var getEmptyViaIFrame = function getEmptyViaIFrame() {
247 var iframe = document.createElement('iframe');
248 var parent = document.body || document.documentElement;
249 var empty;
250
251 iframe.style.display = 'none';
252 parent.appendChild(iframe);
253 // eslint-disable-next-line no-script-url
254 iframe.src = 'javascript:';
255
256 empty = iframe.contentWindow.Object.prototype;
257 parent.removeChild(iframe);
258 iframe = null;
259
260 return empty;
261 };
262
263 /* global document */
264 if (supportsProto || typeof document === 'undefined') {
265 createEmpty = function () {
266 return { __proto__: null };
267 };
268 } else {
269 // In old IE __proto__ can't be used to manually set `null`, nor does
270 // any other method exist to make an object that inherits from nothing,
271 // aside from Object.prototype itself. Instead, create a new global
272 // object and *steal* its Object.prototype and strip it bare. This is
273 // used as the prototype to create nullary objects.
274 createEmpty = function () {
275 // Determine which approach to use
276 // see https://github.com/es-shims/es5-shim/issues/150
277 var empty = shouldUseActiveX() ? getEmptyViaActiveX() : getEmptyViaIFrame();
278
279 delete empty.constructor;
280 delete empty.hasOwnProperty;
281 delete empty.propertyIsEnumerable;
282 delete empty.isPrototypeOf;
283 delete empty.toLocaleString;
284 delete empty.toString;
285 delete empty.valueOf;
286
287 var Empty = function Empty() {};
288 Empty.prototype = empty;
289 // short-circuit future calls
290 createEmpty = function () {
291 return new Empty();
292 };
293 return new Empty();
294 };
295 }
296
297 Object.create = function create(prototype, properties) {
298
299 var object;
300 var Type = function Type() {}; // An empty constructor.
301
302 if (prototype === null) {
303 object = createEmpty();
304 } else if (isPrimitive(prototype)) {
305 // In the native implementation `parent` can be `null`
306 // OR *any* `instanceof Object` (Object|Function|Array|RegExp|etc)
307 // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
308 // like they are in modern browsers. Using `Object.create` on DOM elements
309 // is...err...probably inappropriate, but the native version allows for it.
310 throw new TypeError('Object prototype may only be an Object or null'); // same msg as Chrome
311 } else {
312 Type.prototype = prototype;
313 object = new Type();
314 // IE has no built-in implementation of `Object.getPrototypeOf`
315 // neither `__proto__`, but this manually setting `__proto__` will
316 // guarantee that `Object.getPrototypeOf` will work as expected with
317 // objects created using `Object.create`
318 // eslint-disable-next-line no-proto
319 object.__proto__ = prototype;
320 }
321
322 if (properties !== void 0) {
323 Object.defineProperties(object, properties);
324 }
325
326 return object;
327 };
328 }
329
330 // ES5 15.2.3.6
331 // http://es5.github.com/#x15.2.3.6
332
333 // Patch for WebKit and IE8 standard mode
334 // Designed by hax <hax.github.com>
335 // related issue: https://github.com/es-shims/es5-shim/issues#issue/5
336 // IE8 Reference:
337 // http://msdn.microsoft.com/en-us/library/dd282900.aspx
338 // http://msdn.microsoft.com/en-us/library/dd229916.aspx
339 // WebKit Bugs:
340 // https://bugs.webkit.org/show_bug.cgi?id=36423
341
342 var doesDefinePropertyWork = function doesDefinePropertyWork(object) {
343 try {
344 Object.defineProperty(object, 'sentinel', {});
345 return 'sentinel' in object;
346 } catch (exception) {
347 return false;
348 }
349 };
350
351 // check whether defineProperty works if it's given. Otherwise,
352 // shim partially.
353 if (Object.defineProperty) {
354 var definePropertyWorksOnObject = doesDefinePropertyWork({});
355 var definePropertyWorksOnDom = typeof document === 'undefined'
356 || doesDefinePropertyWork(document.createElement('div'));
357 if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
358 var definePropertyFallback = Object.defineProperty,
359 definePropertiesFallback = Object.defineProperties;
360 }
361 }
362
363 if (!Object.defineProperty || definePropertyFallback) {
364 var ERR_NON_OBJECT_DESCRIPTOR = 'Property description must be an object: ';
365 var ERR_NON_OBJECT_TARGET = 'Object.defineProperty called on non-object: ';
366 var ERR_ACCESSORS_NOT_SUPPORTED = 'getters & setters can not be defined on this javascript engine';
367
368 Object.defineProperty = function defineProperty(object, property, descriptor) {
369 if (isPrimitive(object)) {
370 throw new TypeError(ERR_NON_OBJECT_TARGET + object);
371 }
372 if (isPrimitive(descriptor)) {
373 throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
374 }
375 // make a valiant attempt to use the real defineProperty
376 // for I8's DOM elements.
377 if (definePropertyFallback) {
378 try {
379 return definePropertyFallback.call(Object, object, property, descriptor);
380 } catch (exception) {
381 // try the shim if the real one doesn't work
382 }
383 }
384
385 // If it's a data property.
386 if ('value' in descriptor) {
387 // fail silently if 'writable', 'enumerable', or 'configurable'
388 // are requested but not supported
389 /*
390 // alternate approach:
391 if ( // can't implement these features; allow false but not true
392 ('writable' in descriptor && !descriptor.writable) ||
393 ('enumerable' in descriptor && !descriptor.enumerable) ||
394 ('configurable' in descriptor && !descriptor.configurable)
395 ))
396 throw new RangeError(
397 'This implementation of Object.defineProperty does not support configurable, enumerable, or writable.'
398 );
399 */
400
401 if (supportsAccessors && (lookupGetter(object, property) || lookupSetter(object, property))) {
402 // As accessors are supported only on engines implementing
403 // `__proto__` we can safely override `__proto__` while defining
404 // a property to make sure that we don't hit an inherited
405 // accessor.
406 /* eslint-disable no-proto, no-param-reassign */
407 var prototype = object.__proto__;
408 object.__proto__ = prototypeOfObject;
409 // Deleting a property anyway since getter / setter may be
410 // defined on object itself.
411 delete object[property];
412 object[property] = descriptor.value;
413 // Setting original `__proto__` back now.
414 object.__proto__ = prototype;
415 /* eslint-enable no-proto, no-param-reassign */
416 } else {
417 object[property] = descriptor.value; // eslint-disable-line no-param-reassign
418 }
419 } else {
420 var hasGetter = 'get' in descriptor;
421 var hasSetter = 'set' in descriptor;
422 if (!supportsAccessors && (hasGetter || hasSetter)) {
423 throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
424 }
425 // If we got that far then getters and setters can be defined !!
426 if (hasGetter) {
427 defineGetter(object, property, descriptor.get);
428 }
429 if (hasSetter) {
430 defineSetter(object, property, descriptor.set);
431 }
432 }
433 return object;
434 };
435 }
436
437 // ES5 15.2.3.7
438 // http://es5.github.com/#x15.2.3.7
439 if (!Object.defineProperties || definePropertiesFallback) {
440 Object.defineProperties = function defineProperties(object, properties) {
441 // make a valiant attempt to use the real defineProperties
442 if (definePropertiesFallback) {
443 try {
444 return definePropertiesFallback.call(Object, object, properties);
445 } catch (exception) {
446 // try the shim if the real one doesn't work
447 }
448 }
449
450 Object.keys(properties).forEach(function (property) {
451 if (property !== '__proto__') {
452 Object.defineProperty(object, property, properties[property]);
453 }
454 });
455 return object;
456 };
457 }
458
459 // ES5 15.2.3.8
460 // http://es5.github.com/#x15.2.3.8
461 if (!Object.seal) {
462 Object.seal = function seal(object) {
463 if (Object(object) !== object) {
464 throw new TypeError('Object.seal can only be called on Objects.');
465 }
466 // this is misleading and breaks feature-detection, but
467 // allows "securable" code to "gracefully" degrade to working
468 // but insecure code.
469 return object;
470 };
471 }
472
473 // ES5 15.2.3.9
474 // http://es5.github.com/#x15.2.3.9
475 if (!Object.freeze) {
476 Object.freeze = function freeze(object) {
477 if (Object(object) !== object) {
478 throw new TypeError('Object.freeze can only be called on Objects.');
479 }
480 // this is misleading and breaks feature-detection, but
481 // allows "securable" code to "gracefully" degrade to working
482 // but insecure code.
483 return object;
484 };
485 }
486
487 // detect a Rhino bug and patch it
488 try {
489 Object.freeze(function () {});
490 } catch (exception) {
491 Object.freeze = (function (freezeObject) {
492 return function freeze(object) {
493 if (typeof object === 'function') {
494 return object;
495 }
496 return freezeObject(object);
497
498 };
499 }(Object.freeze));
500 }
501
502 // ES5 15.2.3.10
503 // http://es5.github.com/#x15.2.3.10
504 if (!Object.preventExtensions) {
505 Object.preventExtensions = function preventExtensions(object) {
506 if (Object(object) !== object) {
507 throw new TypeError('Object.preventExtensions can only be called on Objects.');
508 }
509 // this is misleading and breaks feature-detection, but
510 // allows "securable" code to "gracefully" degrade to working
511 // but insecure code.
512 return object;
513 };
514 }
515
516 // ES5 15.2.3.11
517 // http://es5.github.com/#x15.2.3.11
518 if (!Object.isSealed) {
519 Object.isSealed = function isSealed(object) {
520 if (Object(object) !== object) {
521 throw new TypeError('Object.isSealed can only be called on Objects.');
522 }
523 return false;
524 };
525 }
526
527 // ES5 15.2.3.12
528 // http://es5.github.com/#x15.2.3.12
529 if (!Object.isFrozen) {
530 Object.isFrozen = function isFrozen(object) {
531 if (Object(object) !== object) {
532 throw new TypeError('Object.isFrozen can only be called on Objects.');
533 }
534 return false;
535 };
536 }
537
538 // ES5 15.2.3.13
539 // http://es5.github.com/#x15.2.3.13
540 if (!Object.isExtensible) {
541 Object.isExtensible = function isExtensible(object) {
542 // 1. If Type(O) is not Object throw a TypeError exception.
543 if (Object(object) !== object) {
544 throw new TypeError('Object.isExtensible can only be called on Objects.');
545 }
546 // 2. Return the Boolean value of the [[Extensible]] internal property of O.
547 var name = '';
548 while (owns(object, name)) {
549 name += '?';
550 }
551 object[name] = true; // eslint-disable-line no-param-reassign
552 var returnValue = owns(object, name);
553 delete object[name]; // eslint-disable-line no-param-reassign
554 return returnValue;
555 };
556 }
557
558}));