From b4df18c1758160586dde20f4cfd072379edc339d Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 28 Jul 2014 20:17:21 +0100 Subject: [PATCH] es5-shim: Add polyfill for Object.create Follows-up fff9a814f2327eb. Per the OOjs readme, it requires: > (..) parser compatibility with ES3 engines. > To support ES3 engines, ensure an appropriate polyfill is > loaded for these methods: > * Array.isArray > * Object.create (basic use only) > * Object.keys > * JSON.stringify The es5-shim and oojs modules already provide JSON, and most Array and Object methods. Except for Object.create because there are a few features in it that can't be emulated, and hence es5-shim refuses to ship the polyfill for it. Bug: 67590 Change-Id: I4838d78f77a3a71f80e6750adb914213b5ca4df5 --- resources/Resources.php | 1 + resources/src/polyfill-object-create.js | 62 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 resources/src/polyfill-object-create.js diff --git a/resources/Resources.php b/resources/Resources.php index 99e1e4fd39..29c74ee015 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1500,6 +1500,7 @@ return array( 'es5-shim' => array( 'scripts' => array( 'resources/lib/es5-shim/es5-shim.js', + 'resources/src/polyfill-object-create.js', ), 'targets' => array( 'desktop', 'mobile' ), 'skipFunction' => 'resources/src/es5-skip.js', diff --git a/resources/src/polyfill-object-create.js b/resources/src/polyfill-object-create.js new file mode 100644 index 0000000000..607faf649f --- /dev/null +++ b/resources/src/polyfill-object-create.js @@ -0,0 +1,62 @@ +/** + * Simplified version of es5-sham#Object-create that also works around a bug + * in the actual es5-sham: https://github.com/es-shims/es5-shim/issues/252 + * + * Does not: + * - Support empty inheritance via `Object.create(null)`. + * - Support getter and setter accessors via `Object.create( .., properties )`. + * - Support custom property descriptor (e.g. writable, configurtable, enumerable). + * - Leave behind an enumerable "__proto__" all over the place. + * + * @author Timo Tijhof, 2014 + */ + +// ES5 15.2.3.5 +// http://es5.github.com/#x15.2.3.5 +if ( !Object.create ) { + ( function () { + var hasOwn = Object.hasOwnProperty, + // https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug + // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation + isEnumBug = !{ valueOf: 0 }.propertyIsEnumerable( 'valueOf' ); + + // Reusable constructor function for Object.create + function Empty() {} + + function defineProperty( object, key, property ) { + if ( hasOwn.call( property, 'value' ) ) { + object[ key ] = property.value; + } else { + object[ key ] = property; + } + } + + Object.create = function create( prototype, properties ) { + var object, key; + + if ( prototype !== Object( prototype ) ) { + throw new TypeError( 'Called on non-object' ); + } + + Empty.prototype = prototype; + object = new Empty(); + + if ( properties !== undefined ) { + if ( !isEnumBug ) { + for ( key in properties ) { + if ( hasOwn.call( properties, key ) ) { + defineProperty( object, key, properties[ key ] ); + } + } + } else { + Object.keys( properties ).forEach( function ( key ) { + defineProperty( object, key, properties[ key ] ); + } ); + } + } + + return object; + }; + + }() ); +} -- 2.20.1