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

@@ -77,8 +77,8 @@
/** Used to assign default `context` object properties */
var contextProps = [
'Array', 'Boolean', 'Date', 'Error', 'Function', 'Math', 'Number', 'Object',
'RegExp', 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN',
'parseInt', 'setTimeout'
'RegExp', 'String', '_', 'clearTimeout', 'document', 'isFinite', 'isNaN',
'parseInt', 'setTimeout', 'TypeError', 'window', 'WinRTError'
];
/** Used to fix the JScript [[DontEnum]] bug */
@@ -499,6 +499,10 @@
objectProto = Object.prototype,
stringProto = String.prototype;
/** Used to detect DOM support */
var window = context.window,
document = window && window.document;
/** Used to restore the original `_` reference in `noConflict` */
var oldDash = context._;
@@ -700,6 +704,14 @@
*/
support.argsObject = arguments.constructor == Object && !(arguments instanceof Array);
/**
* Detect if the DOM is supported.
*
* @memberOf _.support
* @type boolean
*/
support.dom = !!document && typeof document == 'object' && reNative.test(clearTimeout) && reNative.test(setTimeout);
/**
* Detect if `name` or `message` properties of `Error.prototype` are
* enumerable by default. (IE < 9, Safari < 5.1)
@@ -1822,7 +1834,7 @@
* @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
*/
function isNative(value) {
return typeof value == 'function' && reNative.test(value);
return typeof value == 'function' && reNative.test(fnToString.call(value));
}
/**
@@ -2630,7 +2642,15 @@
* // => true
*/
function isElement(value) {
return value && value.nodeType === 1 || false;
return value && typeof value == 'object' && value.nodeType === 1 &&
(support.nodeClass ? toString.call(value).indexOf('Element') > -1 : isNode(value)) || false;
}
// fallback for environments without DOM support
if (!support.dom) {
isElement = function(value) {
return value && typeof value == 'object' && value.nodeType === 1 &&
!isPlainObject(value) || false;
};
}
/**