Make _.isElement more robust. [closes #427]

This commit is contained in:
John-David Dalton
2013-12-12 22:22:47 -08:00
parent ad2683b610
commit 40421f4321
9 changed files with 294 additions and 179 deletions

View File

@@ -43,6 +43,12 @@
Object._keys = Object.keys;
Object.keys = function() {};
window._clearTimeout = clearTimeout;
window.clearTimeout = function() {};
window._setTimeout = setTimeout;
window.setTimeout = function() {};
}
function removeBizarroMethods() {
@@ -81,6 +87,12 @@
} else {
delete Object.keys;
}
window.clearTimeout = _clearTimeout;
window._clearTimeout = undefined;
window.setTimeout = _setTimeout;
window._setTimeout = undefined;
delete Array._isArray;
delete Date._now;
delete Function.prototype._bind;

View File

@@ -9,7 +9,7 @@
amd = root.define && define.amd,
argv = root.process && process.argv,
document = !phantom && root.document,
body = document && document.body,
body = root.document && root.document.body,
create = Object.create,
freeze = Object.freeze,
noop = function() {},
@@ -227,51 +227,59 @@
'})'
].join('\n')));
// fake dom
var window = global.window = {};
window.document = {};
window.window = window;
// add extensions
Function.prototype._method = function() {};
// set bad shims
Array._isArray = Array.isArray;
var _isArray = Array.isArray;
Array.isArray = function() {};
Date._now = Date.now;
var _now = Date.now;
Date.now = function() {};
Function.prototype._bind = Function.prototype.bind;
var _bind = Function.prototype.bind;
Function.prototype.bind = function() { return function() {}; };
Object._create = Object.create;
var _create = Object.create;
Object.create = function() {};
Object._defineProperty = Object.defineProperty;
var _defineProperty = Object.defineProperty;
Object.defineProperty = function() {};
Object._getPrototypeOf = Object.getPrototypeOf;
var _getPrototypeOf = Object.getPrototypeOf;
Object.getPrototypeOf = function() {};
Object._keys = Object.keys;
var _keys = Object.keys;
Object.keys = function() {};
var _clearTimeout = global.clearTimeout;
global.clearTimeout = function() {};
var _setTimeout = global.setTimeout;
global.setTimeout = function() {};
// load Lo-Dash and expose it to the bad extensions/shims
lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro;
// restore native methods
Array.isArray = Array._isArray;
Date.now = Date._now;
Function.prototype.bind = Function.prototype._bind;
Object.create = Object._create;
Object.defineProperty = Object._defineProperty;
Object.getPrototypeOf = Object._getPrototypeOf;
Object.keys = Object._keys;
Array.isArray = _isArray;
Date.now = _now;
Function.prototype.bind = _bind;
Object.create = _create;
Object.defineProperty = _defineProperty;
Object.getPrototypeOf = _getPrototypeOf;
Object.keys = _keys;
delete Array._isArray;
delete Date._now;
delete Function.prototype._bind;
global.clearTimeout = _clearTimeout;
global.setTimeout = _setTimeout;
delete global.window;
delete Function.prototype._method;
delete Object._create;
delete Object._defineProperty;
delete Object._getPrototypeOf;
delete Object._keys;
} catch(e) { }
}
if (!_._object && document) {
@@ -3525,10 +3533,15 @@
QUnit.module('lodash.isElement');
(function() {
test('should use strict equality in its duck type check', 6, function() {
var element = body || { 'nodeType': 1 };
function Element() {
this.nodeType = 1;
}
test('should use robust check', 7, function() {
var element = body || new Element;
strictEqual(_.isElement(element), true);
strictEqual(_.isElement({ 'nodeType': 1 }), false);
strictEqual(_.isElement({ 'nodeType': new Number(1) }), false);
strictEqual(_.isElement({ 'nodeType': true }), false);
strictEqual(_.isElement({ 'nodeType': [1] }), false);
@@ -3536,6 +3549,16 @@
strictEqual(_.isElement({ 'nodeType': '001' }), false);
});
test('should use a stronger check in browsers', 1, function() {
var lodash = document ? _ : lodashBizarro;
if (lodash) {
strictEqual(lodash.isElement(new Element), false);
}
else {
skipTest();
}
});
test('should work with elements from another realm', 1, function() {
if (_._element) {
strictEqual(_.isElement(_._element), true);
@@ -6927,6 +6950,7 @@
var props = [
'argsClass',
'argsObject',
'dom',
'enumErrorProps',
'enumPrototypes',
'fastBind',