From b7af8a745b6ee6a75769383277a5e8c92a98d268 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Thu, 17 Jul 2008 15:31:28 -0500 Subject: [PATCH] Improve NodeList detection for Safari's $A function. [#187 state:resolved] --- CHANGELOG | 2 ++ src/array.js | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index dcf3fca..a78e3dc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Improve NodeList detection for Safari's $A function. [Garrett Smith, jddalton] + * Use different tactic to sniff for Opera in order to avoid false positives in IE. [Tobie Langel, jddalton] * Rename variable in Form.Element.Serializers.select. (jddalton) diff --git a/src/array.js b/src/array.js index 3b84c97..8c0cc11 100644 --- a/src/array.js +++ b/src/array.js @@ -8,9 +8,13 @@ function $A(iterable) { if (Prototype.Browser.WebKit) { $A = function(iterable) { - if (!iterable) return []; - if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && - iterable.toArray) return iterable.toArray(); + if (!iterable) return []; + // In Safari, only use the `toArray` method if it's not a NodeList. + // A NodeList is a function, has an function `item` property, and a numeric + // `length` property. Adapted from Google Doctype. + if (!(typeof iterable === 'function' && typeof iterable.length === + 'number' && typeof iterable.item === 'function') && iterable.toArray) + return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); while (length--) results[length] = iterable[length]; return results;