From fe5fdb31c0495d492745e6221b1b5be785fd1de4 Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Fri, 18 Feb 2011 18:40:35 +0000 Subject: [PATCH] Fix `d3_array` for older/more esoteric browsers. This code is adapted from `makeArray` in Sizzle.js. --- src/core/array.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/core/array.js b/src/core/array.js index 0fdbfc87..267e3029 100644 --- a/src/core/array.js +++ b/src/core/array.js @@ -1,3 +1,34 @@ function d3_array(psuedoarray) { return Array.prototype.slice.call(psuedoarray); } + +// Adapted from Sizzle.js: +// +// Perform a simple check to determine if the browser is capable of +// converting a NodeList to an array using builtin methods. +// Also verifies that the returned array holds DOM nodes +// (which is not the case in the Blackberry browser) +try { + Array.prototype.slice.call(document.documentElement.childNodes, 0)[0].nodeType; +// Provide a fallback method if it does not work +} catch(e) { + d3_array = function(array) { + var i = 0, + ret = []; + + if (toString.call(array) === "[object Array]") { + Array.prototype.push.apply(ret, array); + } else { + if (typeof array.length === "number") { + for (var l = array.length; i < l; i++) { + ret.push(array[i]); + } + } else { + for (; array[i]; i++) { + ret.push(array[i]); + } + } + } + return ret; + }; +}