PageSourceSearch

https://my.dundeecity.gov.uk/libraries/js/es5-sham.js

js dundeecity.gov.uk collected 2026-10-01 10:32:27 UTC 16,056 bytes, 446 lines download raw bytes

1// Copyright 2009-2012 by contributors, MIT License
2// vim: ts=4 sts=4 sw=4 expandtab
3
4//Add semicolon to prevent IIFE from being passed as argument to concated code.
5;
6// Module systems magic dance
7(function (definition) {
8    // RequireJS
9    if (typeof define == "function") {
10        define(definition);
11    // YUI3
12    } else if (typeof YUI == "function") {
13        YUI.add("es5-sham", definition);
14    // CommonJS and <script>
15    } else {
16        definition();
17    }
18})(function () {
19
20
21var call = Function.prototype.call;
22var prototypeOfObject = Object.prototype;
23var owns = call.bind(prototypeOfObject.hasOwnProperty);
24
25// If JS engine supports accessors creating shortcuts.
26var defineGetter;
27var defineSetter;
28var lookupGetter;
29var lookupSetter;
30var supportsAccessors;
31if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) {
32    defineGetter = call.bind(prototypeOfObject.__defineGetter__);
33    defineSetter = call.bind(prototypeOfObject.__defineSetter__);
34    lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
35    lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
36}
37
38// ES5 15.2.3.2
39// http://es5.github.com/#x15.2.3.2
40if (!Object.getPrototypeOf) {
41    // https://github.com/kriskowal/es5-shim/issues#issue/2
42    // http://ejohn.org/blog/objectgetprototypeof/
43    // recommended by fschaefer on github
44    Object.getPrototypeOf = function getPrototypeOf(object) {
45        return object.__proto__ || (
46            object.constructor
47                ? object.constructor.prototype
48                : prototypeOfObject
49        );
50    };
51}
52
53//ES5 15.2.3.3
54//http://es5.github.com/#x15.2.3.3
55
56function doesGetOwnPropertyDescriptorWork(object) {
57    try {
58        object.sentinel = 0;
59        return Object.getOwnPropertyDescriptor(
60                object,
61                "sentinel"
62        ).value === 0;
63    } catch (exception) {
64        // returns falsy
65    }
66}
67
68//check whether getOwnPropertyDescriptor works if it's given. Otherwise,
69//shim partially.
70if (Object.defineProperty) {
71    var getOwnPropertyDescriptorWorksOnObject = 
72        doesGetOwnPropertyDescriptorWork({});
73    var getOwnPropertyDescriptorWorksOnDom = typeof document == "undefined" ||
74    doesGetOwnPropertyDescriptorWork(document.createElement("div"));
75    if (!getOwnPropertyDescriptorWorksOnDom || 
76            !getOwnPropertyDescriptorWorksOnObject
77    ) {
78        var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor;
79    }
80}
81
82if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
83    var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a non-object: ";
84
85    Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
86        if ((typeof object != "object" && typeof object != "function") || object === null) {
87            throw new TypeError(ERR_NON_OBJECT + object);
88        }
89
90        // make a valiant attempt to use the real getOwnPropertyDescriptor
91        // for I8's DOM elements.
92        if (getOwnPropertyDescriptorFallback) {
93            try {
94                return getOwnPropertyDescriptorFallback.call(Object, object, property);
95            } catch (exception) {
96                // try the shim if the real one doesn't work
97            }
98        }
99
100        // If object does not owns property return undefined immediately.
101        if (!owns(object, property)) {
102            return;
103        }
104
105        // If object has a property then it's for sure both `enumerable` and
106        // `configurable`.
107        var descriptor =  { enumerable: true, configurable: true };
108
109        // If JS engine supports accessor properties then property may be a
110        // getter or setter.
111        if (supportsAccessors) {
112            // Unfortunately `__lookupGetter__` will return a getter even
113            // if object has own non getter property along with a same named
114            // inherited getter. To avoid misbehavior we temporary remove
115            // `__proto__` so that `__lookupGetter__` will return getter only
116            // if it's owned by an object.
117            var prototype = object.__proto__;
118            object.__proto__ = prototypeOfObject;
119
120            var getter = lookupGetter(object, property);
121            var setter = lookupSetter(object, property);
122
123            // Once we have getter and setter we can put values back.
124            object.__proto__ = prototype;
125
126            if (getter || setter) {
127                if (getter) {
128                    descriptor.get = getter;
129                }
130                if (setter) {
131                    descriptor.set = setter;
132                }
133                // If it was accessor property we're done and return here
134                // in order to avoid adding `value` to the descriptor.
135                return descriptor;
136            }
137        }
138
139        // If we got this far we know that object has an own property that is
140        // not an accessor so we set it as a value and return descriptor.
141        descriptor.value = object[property];
142        descriptor.writable = true;
143        return descriptor;
144    };
145}
146
147// ES5 15.2.3.4
148// http://es5.github.com/#x15.2.3.4
149if (!Object.getOwnPropertyNames) {
150    Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
151        return Object.keys(object);
152    };
153}
154
155// ES5 15.2.3.5
156// http://es5.github.com/#x15.2.3.5
157if (!Object.create) {
158
159    // Contributed by Brandon Benvie, October, 2012
160    var createEmpty;
161    var supportsProto = Object.prototype.__proto__ === null;
162    if (supportsProto || typeof document == 'undefined') {
163        createEmpty = function () {
164            return { "__proto__": null };
165        };
166    } else {
167        // In old IE __proto__ can't be used to manually set `null`, nor does
168        // any other method exist to make an object that inherits from nothing,
169        // aside from Object.prototype itself. Instead, create a new global
170        // object and *steal* its Object.prototype and strip it bare. This is
171        // used as the prototype to create nullary objects.
172        createEmpty = function () {
173            var iframe = document.createElement('iframe');
174            var parent = document.body || document.documentElement;
175            iframe.style.display = 'none';
176            parent.appendChild(iframe);
177            iframe.src = 'javascript:';
178            var empty = iframe.contentWindow.Object.prototype;
179            parent.removeChild(iframe);
180            iframe = null;
181            delete empty.constructor;
182            delete empty.hasOwnProperty;
183            delete empty.propertyIsEnumerable;
184            delete empty.isPrototypeOf;
185            delete empty.toLocaleString;
186            delete empty.toString;
187            delete empty.valueOf;
188            empty.__proto__ = null;
189
190            function Empty() {}
191            Empty.prototype = empty;
192            // short-circuit future calls
193            createEmpty = function () {
194                return new Empty();
195            };
196            return new Empty();
197        };
198    }
199
200    Object.create = function create(prototype, properties) {
201
202        var object;
203        function Type() {}  // An empty constructor.
204
205        if (prototype === null) {
206            object = createEmpty();
207        } else {
208            if (typeof prototype !== "object" && typeof prototype !== "function") {
209                // In the native implementation `parent` can be `null`
210                // OR *any* `instanceof Object`  (Object|Function|Array|RegExp|etc)
211                // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
212                // like they are in modern browsers. Using `Object.create` on DOM elements
213                // is...err...probably inappropriate, but the native version allows for it.
214                throw new TypeError("Object prototype may only be an Object or null"); // same msg as Chrome
215            }
216            Type.prototype = prototype;
217            object = new Type();
218            // IE has no built-in implementation of `Object.getPrototypeOf`
219            // neither `__proto__`, but this manually setting `__proto__` will
220            // guarantee that `Object.getPrototypeOf` will work as expected with
221            // objects created using `Object.create`
222            object.__proto__ = prototype;
223        }
224
225        if (properties !== void 0) {
226            Object.defineProperties(object, properties);
227        }
228
229        return object;
230    };
231}
232
233// ES5 15.2.3.6
234// http://es5.github.com/#x15.2.3.6
235
236// Patch for WebKit and IE8 standard mode
237// Designed by hax <hax.github.com>
238// related issue: https://github.com/kriskowal/es5-shim/issues#issue/5
239// IE8 Reference:
240//     http://msdn.microsoft.com/en-us/library/dd282900.aspx
241//     http://msdn.microsoft.com/en-us/library/dd229916.aspx
242// WebKit Bugs:
243//     https://bugs.webkit.org/show_bug.cgi?id=36423
244
245function doesDefinePropertyWork(object) {
246    try {
247        Object.defineProperty(object, "sentinel", {});
248        return "sentinel" in object;
249    } catch (exception) {
250        // returns falsy
251    }
252}
253
254// check whether defineProperty works if it's given. Otherwise,
255// shim partially.
256if (Object.defineProperty) {
257    var definePropertyWorksOnObject = doesDefinePropertyWork({});
258    var definePropertyWorksOnDom = typeof document == "undefined" ||
259        doesDefinePropertyWork(document.createElement("div"));
260    if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
261        var definePropertyFallback = Object.defineProperty,
262            definePropertiesFallback = Object.defineProperties;
263    }
264}
265
266if (!Object.defineProperty || definePropertyFallback) {
267    var ERR_NON_OBJECT_DESCRIPTOR = "Property description must be an object: ";
268    var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: "
269    var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " +
270                                      "on this javascript engine";
271
272    Object.defineProperty = function defineProperty(object, property, descriptor) {
273        if ((typeof object != "object" && typeof object != "function") || object === null) {
274            throw new TypeError(ERR_NON_OBJECT_TARGET + object);
275        }
276        if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null) {
277            throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
278        }
279        // make a valiant attempt to use the real defineProperty
280        // for I8's DOM elements.
281        if (definePropertyFallback) {
282            try {
283                return definePropertyFallback.call(Object, object, property, descriptor);
284            } catch (exception) {
285                // try the shim if the real one doesn't work
286            }
287        }
288
289        // If it's a data property.
290        if (owns(descriptor, "value")) {
291            // fail silently if "writable", "enumerable", or "configurable"
292            // are requested but not supported
293            /*
294            // alternate approach:
295            if ( // can't implement these features; allow false but not true
296                !(owns(descriptor, "writable") ? descriptor.writable : true) ||
297                !(owns(descriptor, "enumerable") ? descriptor.enumerable : true) ||
298                !(owns(descriptor, "configurable") ? descriptor.configurable : true)
299            )
300                throw new RangeError(
301                    "This implementation of Object.defineProperty does not " +
302                    "support configurable, enumerable, or writable."
303                );
304            */
305
306            if (supportsAccessors && (lookupGetter(object, property) ||
307                                      lookupSetter(object, property)))
308            {
309                // As accessors are supported only on engines implementing
310                // `__proto__` we can safely override `__proto__` while defining
311                // a property to make sure that we don't hit an inherited
312                // accessor.
313                var prototype = object.__proto__;
314                object.__proto__ = prototypeOfObject;
315                // Deleting a property anyway since getter / setter may be
316                // defined on object itself.
317                delete object[property];
318                object[property] = descriptor.value;
319                // Setting original `__proto__` back now.
320                object.__proto__ = prototype;
321            } else {
322                object[property] = descriptor.value;
323            }
324        } else {
325            if (!supportsAccessors) {
326                throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
327            }
328            // If we got that far then getters and setters can be defined !!
329            if (owns(descriptor, "get")) {
330                defineGetter(object, property, descriptor.get);
331            }
332            if (owns(descriptor, "set")) {
333                defineSetter(object, property, descriptor.set);
334            }
335        }
336        return object;
337    };
338}
339
340// ES5 15.2.3.7
341// http://es5.github.com/#x15.2.3.7
342if (!Object.defineProperties || definePropertiesFallback) {
343    Object.defineProperties = function defineProperties(object, properties) {
344        // make a valiant attempt to use the real defineProperties
345        if (definePropertiesFallback) {
346            try {
347                return definePropertiesFallback.call(Object, object, properties);
348            } catch (exception) {
349                // try the shim if the real one doesn't work
350            }
351        }
352
353        for (var property in properties) {
354            if (owns(properties, property) && property != "__proto__") {
355                Object.defineProperty(object, property, properties[property]);
356            }
357        }
358        return object;
359    };
360}
361
362// ES5 15.2.3.8
363// http://es5.github.com/#x15.2.3.8
364if (!Object.seal) {
365    Object.seal = function seal(object) {
366        // this is misleading and breaks feature-detection, but
367        // allows "securable" code to "gracefully" degrade to working
368        // but insecure code.
369        return object;
370    };
371}
372
373// ES5 15.2.3.9
374// http://es5.github.com/#x15.2.3.9
375if (!Object.freeze) {
376    Object.freeze = function freeze(object) {
377        // this is misleading and breaks feature-detection, but
378        // allows "securable" code to "gracefully" degrade to working
379        // but insecure code.
380        return object;
381    };
382}
383
384// detect a Rhino bug and patch it
385try {
386    Object.freeze(function () {});
387} catch (exception) {
388    Object.freeze = (function freeze(freezeObject) {
389        return function freeze(object) {
390            if (typeof object == "function") {
391                return object;
392            } else {
393                return freezeObject(object);
394            }
395        };
396    })(Object.freeze);
397}
398
399// ES5 15.2.3.10
400// http://es5.github.com/#x15.2.3.10
401if (!Object.preventExtensions) {
402    Object.preventExtensions = function preventExtensions(object) {
403        // this is misleading and breaks feature-detection, but
404        // allows "securable" code to "gracefully" degrade to working
405        // but insecure code.
406        return object;
407    };
408}
409
410// ES5 15.2.3.11
411// http://es5.github.com/#x15.2.3.11
412if (!Object.isSealed) {
413    Object.isSealed = function isSealed(object) {
414        return false;
415    };
416}
417
418// ES5 15.2.3.12
419// http://es5.github.com/#x15.2.3.12
420if (!Object.isFrozen) {
421    Object.isFrozen = function isFrozen(object) {
422        return false;
423    };
424}
425
426// ES5 15.2.3.13
427// http://es5.github.com/#x15.2.3.13
428if (!Object.isExtensible) {
429    Object.isExtensible = function isExtensible(object) {
430        // 1. If Type(O) is not Object throw a TypeError exception.
431        if (Object(object) !== object) {
432            throw new TypeError(); // TODO message
433        }
434        // 2. Return the Boolean value of the [[Extensible]] internal property of O.
435        var name = '';
436        while (owns(object, name)) {
437            name += '?';
438        }
439        object[name] = true;
440        var returnValue = owns(object, name);
441        delete object[name];
442        return returnValue;
443    };
444}
445
446});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.