Fix `d3_array` for older/more esoteric browsers.

This code is adapted from `makeArray` in Sizzle.js.
This commit is contained in:
Jason Davies 2011-02-18 18:40:35 +00:00
Родитель 8da80dcc99
Коммит fe5fdb31c0
1 изменённых файлов: 31 добавлений и 0 удалений

Просмотреть файл

@ -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;
};
}