Add _.support.hostObject and rename isNode to isHostObject.

This commit is contained in:
John-David Dalton
2014-08-20 22:32:57 -07:00
parent bde931f2f5
commit c3126e24ce
2 changed files with 32 additions and 26 deletions

View File

@@ -544,19 +544,6 @@
return '\\' + stringEscapes[chr]; return '\\' + stringEscapes[chr];
} }
/**
* Checks if `value` is a DOM node in IE < 9.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a DOM node, else `false`.
*/
function isNode(value) {
// IE < 9 presents DOM nodes as `Object` objects except they have `toString`
// methods that are `typeof` "string" and still can coerce nodes to strings
return typeof value.toString != 'function' && typeof (value + '') == 'string';
}
/** /**
* Used by `_.trimmedLeftIndex` and `_.trimmedRightIndex` to determine if a * Used by `_.trimmedLeftIndex` and `_.trimmedRightIndex` to determine if a
* character code is whitespace. * character code is whitespace.
@@ -1004,19 +991,25 @@
} }
/** /**
* Detect if the `[[Class]]` of DOM nodes is resolvable (all but IE < 9) * Detect if the host objects are detectable (IE < 9).
* and that the JS engine errors when attempting to coerce an object to a
* string without a `toString` function.
* *
* @memberOf _.support * @memberOf _.support
* @type boolean * @type boolean
*/ */
try { try {
support.nodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + '')); support.hostObject = !({ 'toString': 0 } + '');
} catch(e) { } catch(e) {
support.nodeClass = true; support.hostObject = false;
} }
/**
* Detect if the `[[Class]]` of DOM nodes is resolvable (all but IE < 9).
*
* @memberOf _.support
* @type boolean
*/
support.nodeClass = !(toString.call(document) == objectClass && support.hostObject);
/** /**
* Detect if `arguments` object indexes are non-enumerable. * Detect if `arguments` object indexes are non-enumerable.
* *
@@ -1882,7 +1875,7 @@
return false; return false;
} }
} }
else if (isErr || (valClass == objectClass && (support.nodeClass || !(isNode(value) || isNode(other))))) { else if (isErr || (valClass == objectClass && (support.nodeClass || !(isHostObject(value) || isHostObject(other))))) {
// unwrap any `lodash` wrapped values // unwrap any `lodash` wrapped values
var valWrapped = hasOwnProperty.call(value, '__wrapped__'), var valWrapped = hasOwnProperty.call(value, '__wrapped__'),
othWrapped = hasOwnProperty.call(other, '__wrapped__'); othWrapped = hasOwnProperty.call(other, '__wrapped__');
@@ -2885,7 +2878,7 @@
*/ */
function initObjectClone(object, isDeep) { function initObjectClone(object, isDeep) {
var className = toString.call(object); var className = toString.call(object);
if (!cloneableClasses[className] || (!support.nodeClass && isNode(object))) { if (!cloneableClasses[className] || (!support.nodeClass && isHostObject(object))) {
return object; return object;
} }
var Ctor = object.constructor, var Ctor = object.constructor,
@@ -2943,6 +2936,19 @@
arrayLikeClasses[toString.call(value)]) || false; arrayLikeClasses[toString.call(value)]) || false;
} }
/**
* Checks if `value` is a host object in IE < 9.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a host object, else `false`.
*/
var isHostObject = support.hostObject ? constant(false) : function(value) {
// IE < 9 presents many host objects as `Object` objects that can coerce to
// strings despite having improperly defined `toString` methods
return typeof value.toString != 'function' && typeof (value + '') == 'string';
};
/** /**
* Checks if `value` is suitable for strict equality comparisons, i.e. `===`. * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
* *
@@ -3072,7 +3078,7 @@
(!hasOwnProperty.call(value, 'constructor') && (!hasOwnProperty.call(value, 'constructor') &&
(Ctor = value.constructor, isFunction(Ctor) && !(Ctor instanceof Ctor))) || (Ctor = value.constructor, isFunction(Ctor) && !(Ctor instanceof Ctor))) ||
(!support.argsClass && isArguments(value)) || (!support.argsClass && isArguments(value)) ||
(!support.nodeClass && isNode(value))) { (!support.nodeClass && isHostObject(value))) {
return false; return false;
} }
// IE < 9 iterates inherited properties before own properties. If the first // IE < 9 iterates inherited properties before own properties. If the first
@@ -7112,7 +7118,7 @@
*/ */
function isElement(value) { function isElement(value) {
return (value && typeof value == 'object' && value.nodeType === 1 && return (value && typeof value == 'object' && value.nodeType === 1 &&
(support.nodeClass ? toString.call(value).indexOf('Element') > -1 : isNode(value))) || false; (support.nodeClass ? toString.call(value).indexOf('Element') > -1 : isHostObject(value))) || false;
} }
// fallback for environments without DOM support // fallback for environments without DOM support
if (!support.dom) { if (!support.dom) {
@@ -7361,12 +7367,11 @@
* @returns {boolean} Returns `true` if `value` is a native function, else `false`. * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
*/ */
function isNative(value) { function isNative(value) {
var type = typeof value; if (isFunction(value)) {
if (type == 'function') {
return reNative.test(fnToString.call(value)); return reNative.test(fnToString.call(value));
} }
if (value && type == 'object') { if (value && typeof value == 'object') {
return !('constructor' in value && 'toString' in value) return !('constructor' in value) && isHostObject(value)
? reNative.test(value) ? reNative.test(value)
: reHostCtor.test(toString.call(value)); : reHostCtor.test(toString.call(value));
} }

View File

@@ -9923,6 +9923,7 @@
'fastBind', 'fastBind',
'funcDecomp', 'funcDecomp',
'funcNames', 'funcNames',
'hostObject',
'nodeClass', 'nodeClass',
'nonEnumArgs', 'nonEnumArgs',
'nonEnumShadows', 'nonEnumShadows',