Skip to content

Commit

Permalink
Test and fix an Object.keys IE 8 bug:
Browse files Browse the repository at this point in the history
Some global variables, like `localStorage`, would throw when `o.constructor.prototype` was compared with `===` to `o` - weird.

Fixes #275.
  • Loading branch information
ljharb committed Jul 3, 2015
1 parent 9d3d9cf commit 95730e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,25 @@ defineProperties(ArrayPrototype, {
var hasDontEnumBug = !({ 'toString': null }).propertyIsEnumerable('toString');
var hasProtoEnumBug = function () {}.propertyIsEnumerable('prototype');
var hasStringEnumBug = !owns('x', '0');
var equalsConstructorPrototype = function (o) {
var ctor = o.constructor;
return ctor && ctor.prototype === o;
};
var blacklistedKeys = ['window', 'console', 'parent', 'self', 'frames'];
var hasAutomationEqualityBug = (function () {
/* globals window */
if (typeof window === 'undefined') { return false; }
for (var k in window) {
if (blacklistedKeys.indexOf(k) === -1 && owns(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
equalsConstructorPrototype(window[k]);
} catch (e) {
return true;
}
}
}
return false;
}());
var dontEnums = [
'toString',
'toLocaleString',
Expand Down Expand Up @@ -888,8 +907,7 @@ defineProperties($Object, {
}

if (hasDontEnumBug) {
var ctor = object.constructor;
var skipConstructor = ctor && ctor.prototype === object;
var skipConstructor = hasAutomationEqualityBug || equalsConstructorPrototype(object);
for (var j = 0; j < dontEnumsLength; j++) {
var dontEnum = dontEnums[j];
if (!(skipConstructor && dontEnum === 'constructor') && owns(object, dontEnum)) {
Expand Down
24 changes: 22 additions & 2 deletions tests/spec/s-object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* global describe, it, xit, expect, beforeEach, jasmine */
/* global describe, it, xit, expect, beforeEach, jasmine, window */

var ifWindowIt = typeof window === 'undefined' ? xit : it;

describe('Object', function () {
'use strict';

describe('Object.keys', function () {
describe('.keys()', function () {
var obj = {
str: 'boz',
obj: { },
Expand Down Expand Up @@ -102,6 +104,24 @@ describe('Object', function () {
expected.sort();
expect(actual).toEqual(expected);
});

ifWindowIt('can serialize all objects on the `window`', function () {
var has = Object.prototype.hasOwnProperty;
var keys, exception;
var blacklistedKeys = ['window', 'console', 'parent', 'self', 'frames'];
for (var k in window) {
keys = exception = void 0;
if (blacklistedKeys.indexOf(k) === -1 && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
keys = Object.keys(window[k]);
} catch (e) {
exception = e;
}
expect(Array.isArray(keys)).toEqual(true);
expect(exception).toBeUndefined();
}
}
});
});

describe('Object.isExtensible', function () {
Expand Down

0 comments on commit 95730e9

Please sign in to comment.