vendor/assets/javascripts/es5-shim/shims/es5-sham.js in es5-shim-rails-2.0.5.1 vs vendor/assets/javascripts/es5-shim/shims/es5-sham.js in es5-shim-rails-4.0.1

- old
+ new

@@ -1,46 +1,116 @@ -// Copyright 2009-2012 by contributors, MIT License +/*! + * https://github.com/es-shims/es5-shim + * @license es5-shim Copyright 2009-2014 by contributors, MIT License + * see https://github.com/es-shims/es5-shim/blob/master/LICENSE + */ + // vim: ts=4 sts=4 sw=4 expandtab -// Module systems magic dance -(function (definition) { - // RequireJS - if (typeof define == "function") { - define(definition); - // YUI3 - } else if (typeof YUI == "function") { - YUI.add("es5-sham", definition); - // CommonJS and <script> +//Add semicolon to prevent IIFE from being passed as argument to concated code. +; + +// UMD (Universal Module Definition) +// see https://github.com/umdjs/umd/blob/master/returnExports.js +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(factory); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like enviroments that support module.exports, + // like Node. + module.exports = factory(); } else { - definition(); - } -})(function () { + // Browser globals (root is window) + root.returnExports = factory(); + } +}(this, function () { +var call = Function.prototype.call; +var prototypeOfObject = Object.prototype; +var owns = call.bind(prototypeOfObject.hasOwnProperty); + +// If JS engine supports accessors creating shortcuts. +var defineGetter; +var defineSetter; +var lookupGetter; +var lookupSetter; +var supportsAccessors = owns(prototypeOfObject, "__defineGetter__"); +if (supportsAccessors) { + defineGetter = call.bind(prototypeOfObject.__defineGetter__); + defineSetter = call.bind(prototypeOfObject.__defineSetter__); + lookupGetter = call.bind(prototypeOfObject.__lookupGetter__); + lookupSetter = call.bind(prototypeOfObject.__lookupSetter__); +} + // ES5 15.2.3.2 // http://es5.github.com/#x15.2.3.2 if (!Object.getPrototypeOf) { - // https://github.com/kriskowal/es5-shim/issues#issue/2 + // https://github.com/es-shims/es5-shim/issues#issue/2 // http://ejohn.org/blog/objectgetprototypeof/ // recommended by fschaefer on github + // + // sure, and webreflection says ^_^ + // ... this will nerever possibly return null + // ... Opera Mini breaks here with infinite loops Object.getPrototypeOf = function getPrototypeOf(object) { - return object.__proto__ || ( - object.constructor - ? object.constructor.prototype - : prototypeOfObject - ); + var proto = object.__proto__; + if (proto || proto === null) { + return proto; + } else if (object.constructor) { + return object.constructor.prototype; + } else { + return prototypeOfObject; + } }; } -// ES5 15.2.3.3 -// http://es5.github.com/#x15.2.3.3 -if (!Object.getOwnPropertyDescriptor) { +//ES5 15.2.3.3 +//http://es5.github.com/#x15.2.3.3 + +function doesGetOwnPropertyDescriptorWork(object) { + try { + object.sentinel = 0; + return Object.getOwnPropertyDescriptor( + object, + "sentinel" + ).value === 0; + } catch (exception) { + // returns falsy + } +} + +//check whether getOwnPropertyDescriptor works if it's given. Otherwise, +//shim partially. +if (Object.defineProperty) { + var getOwnPropertyDescriptorWorksOnObject = doesGetOwnPropertyDescriptorWork({}); + var getOwnPropertyDescriptorWorksOnDom = typeof document === "undefined" || + doesGetOwnPropertyDescriptorWork(document.createElement("div")); + if (!getOwnPropertyDescriptorWorksOnDom || !getOwnPropertyDescriptorWorksOnObject) { + var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor; + } +} + +if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) { var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a non-object: "; Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) { - if ((typeof object != "object" && typeof object != "function") || object === null) { + if ((typeof object !== "object" && typeof object !== "function") || object === null) { throw new TypeError(ERR_NON_OBJECT + object); } + + // make a valiant attempt to use the real getOwnPropertyDescriptor + // for I8's DOM elements. + if (getOwnPropertyDescriptorFallback) { + try { + return getOwnPropertyDescriptorFallback.call(Object, object, property); + } catch (exception) { + // try the shim if the real one doesn't work + } + } + // If object does not owns property return undefined immediately. if (!owns(object, property)) { return; } @@ -55,17 +125,25 @@ // if object has own non getter property along with a same named // inherited getter. To avoid misbehavior we temporary remove // `__proto__` so that `__lookupGetter__` will return getter only // if it's owned by an object. var prototype = object.__proto__; - object.__proto__ = prototypeOfObject; + var notPrototypeOfObject = object !== prototypeOfObject; + // avoid recursion problem, breaking in Opera Mini when + // Object.getOwnPropertyDescriptor(Object.prototype, 'toString') + // or any other Object.prototype accessor + if (notPrototypeOfObject) { + object.__proto__ = prototypeOfObject; + } var getter = lookupGetter(object, property); var setter = lookupSetter(object, property); - // Once we have getter and setter we can put values back. - object.__proto__ = prototype; + if (notPrototypeOfObject) { + // Once we have getter and setter we can put values back. + object.__proto__ = prototype; + } if (getter || setter) { if (getter) { descriptor.get = getter; } @@ -79,10 +157,11 @@ } // If we got this far we know that object has an own property that is // not an accessor so we set it as a value and return descriptor. descriptor.value = object[property]; + descriptor.writable = true; return descriptor; }; } // ES5 15.2.3.4 @@ -97,22 +176,25 @@ // http://es5.github.com/#x15.2.3.5 if (!Object.create) { // Contributed by Brandon Benvie, October, 2012 var createEmpty; - var supportsProto = Object.prototype.__proto__ === null; - if (supportsProto || typeof document == 'undefined') { + var supportsProto = !({__proto__:null} instanceof Object); + // the following produces false positives + // in Opera Mini => not a reliable check + // Object.prototype.__proto__ === null + if (supportsProto || typeof document === 'undefined') { createEmpty = function () { return { "__proto__": null }; }; } else { // In old IE __proto__ can't be used to manually set `null`, nor does // any other method exist to make an object that inherits from nothing, // aside from Object.prototype itself. Instead, create a new global // object and *steal* its Object.prototype and strip it bare. This is // used as the prototype to create nullary objects. - createEmpty = (function () { + createEmpty = function () { var iframe = document.createElement('iframe'); var parent = document.body || document.documentElement; iframe.style.display = 'none'; parent.appendChild(iframe); iframe.src = 'javascript:'; @@ -128,15 +210,16 @@ delete empty.valueOf; empty.__proto__ = null; function Empty() {} Empty.prototype = empty; - - return function () { + // short-circuit future calls + createEmpty = function () { return new Empty(); }; - })(); + return new Empty(); + }; } Object.create = function create(prototype, properties) { var object; @@ -173,11 +256,11 @@ // ES5 15.2.3.6 // http://es5.github.com/#x15.2.3.6 // Patch for WebKit and IE8 standard mode // Designed by hax <hax.github.com> -// related issue: https://github.com/kriskowal/es5-shim/issues#issue/5 +// related issue: https://github.com/es-shims/es5-shim/issues#issue/5 // IE8 Reference: // http://msdn.microsoft.com/en-us/library/dd282900.aspx // http://msdn.microsoft.com/en-us/library/dd229916.aspx // WebKit Bugs: // https://bugs.webkit.org/show_bug.cgi?id=36423 @@ -193,11 +276,11 @@ // check whether defineProperty works if it's given. Otherwise, // shim partially. if (Object.defineProperty) { var definePropertyWorksOnObject = doesDefinePropertyWork({}); - var definePropertyWorksOnDom = typeof document == "undefined" || + var definePropertyWorksOnDom = typeof document === "undefined" || doesDefinePropertyWork(document.createElement("div")); if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) { var definePropertyFallback = Object.defineProperty, definePropertiesFallback = Object.defineProperties; } @@ -208,14 +291,14 @@ var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: " var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " + "on this javascript engine"; Object.defineProperty = function defineProperty(object, property, descriptor) { - if ((typeof object != "object" && typeof object != "function") || object === null) { + if ((typeof object !== "object" && typeof object !== "function") || object === null) { throw new TypeError(ERR_NON_OBJECT_TARGET + object); } - if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null) { + if ((typeof descriptor !== "object" && typeof descriptor !== "function") || descriptor === null) { throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor); } // make a valiant attempt to use the real defineProperty // for I8's DOM elements. if (definePropertyFallback) { @@ -287,13 +370,13 @@ return definePropertiesFallback.call(Object, object, properties); } catch (exception) { // try the shim if the real one doesn't work } } - + for (var property in properties) { - if (owns(properties, property) && property != "__proto__") { + if (owns(properties, property) && property !== "__proto__") { Object.defineProperty(object, property, properties[property]); } } return object; }; @@ -325,11 +408,11 @@ try { Object.freeze(function () {}); } catch (exception) { Object.freeze = (function freeze(freezeObject) { return function freeze(object) { - if (typeof object == "function") { + if (typeof object === "function") { return object; } else { return freezeObject(object); } }; @@ -381,6 +464,7 @@ delete object[name]; return returnValue; }; } -}); +})); +