Skip to content

Commit fe5fdb3

Browse files
committed
Fix d3_array for older/more esoteric browsers.
This code is adapted from `makeArray` in Sizzle.js.
1 parent 8da80dc commit fe5fdb3

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/core/array.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
11
function d3_array(psuedoarray) {
22
return Array.prototype.slice.call(psuedoarray);
33
}
4+
5+
// Adapted from Sizzle.js:
6+
//
7+
// Perform a simple check to determine if the browser is capable of
8+
// converting a NodeList to an array using builtin methods.
9+
// Also verifies that the returned array holds DOM nodes
10+
// (which is not the case in the Blackberry browser)
11+
try {
12+
Array.prototype.slice.call(document.documentElement.childNodes, 0)[0].nodeType;
13+
// Provide a fallback method if it does not work
14+
} catch(e) {
15+
d3_array = function(array) {
16+
var i = 0,
17+
ret = [];
18+
19+
if (toString.call(array) === "[object Array]") {
20+
Array.prototype.push.apply(ret, array);
21+
} else {
22+
if (typeof array.length === "number") {
23+
for (var l = array.length; i < l; i++) {
24+
ret.push(array[i]);
25+
}
26+
} else {
27+
for (; array[i]; i++) {
28+
ret.push(array[i]);
29+
}
30+
}
31+
}
32+
return ret;
33+
};
34+
}

0 commit comments

Comments
 (0)