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

40
dist/lodash.compat.js vendored
View File

@@ -78,8 +78,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 */
@@ -500,6 +500,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._;
@@ -701,6 +705,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)
@@ -1805,7 +1817,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));
}
/**
@@ -2613,7 +2625,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;
};
}
/**
@@ -4142,7 +4162,7 @@
}
/**
* The opposite of `_.filter` this method returns the elements of a
* The opposite of `_.filter`; this method returns the elements of a
* collection that the callback does **not** return truey for.
*
* If a property name is provided for `callback` the created "_.pluck" style
@@ -5168,7 +5188,7 @@
}
/**
* The opposite of `_.initial` this method gets all but the first element or
* The opposite of `_.initial`; this method gets all but the first element or
* first `n` elements of an array. If a callback function is provided elements
* at the beginning of the array are excluded from the result as long as the
* callback returns truey. The callback is bound to `thisArg` and invoked
@@ -6225,6 +6245,9 @@
* Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
* corresponding HTML entities.
*
* Note: No other characters are escaped. To escape additional characters
* use a third-party library like [_he_](http://mths.be/he).
*
* @static
* @memberOf _
* @category Utilities
@@ -6747,10 +6770,13 @@
}
/**
* The inverse of `_.escape` this method converts the HTML entities
* The inverse of `_.escape`; this method converts the HTML entities
* `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` in `string` to their
* corresponding characters.
*
* Note: No other HTML entities are unescaped. To unescape additional HTML
* entities use a third-party library like [_he_](http://mths.be/he).
*
* @static
* @memberOf _
* @category Utilities