jquery core: fixed makeArray to recognize the window (has length)

test runner: updated the tests for makeArray
This commit is contained in:
Ariel Flesler 2008-04-25 03:48:07 +00:00
parent 25f9974cee
commit 508b1e2432
2 changed files with 15 additions and 9 deletions

View file

@ -1114,13 +1114,15 @@ jQuery.extend({
makeArray: function( array ) { makeArray: function( array ) {
var ret = []; var ret = [];
if( array != undefined ) if( array != undefined ){
//strings and functions also have 'length' var i = array.length;
if( array.length != undefined && !array.split && !array.call ) //the window, strings and functions also have 'length'
for( var i = array.length; i; ) if( i != undefined && typeof array == 'object' && array != window )
while( i )
ret[--i] = array[i]; ret[--i] = array[i];
else else
ret[0] = array; ret[0] = array;
}
return ret; return ret;
}, },

View file

@ -1563,7 +1563,7 @@ test("contents()", function() {
}); });
test("$.makeArray", function(){ test("$.makeArray", function(){
expect(11); expect(13);
equals( $.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" ); equals( $.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
@ -1577,14 +1577,18 @@ test("$.makeArray", function(){
equals( $.makeArray( "foo" )[0], "foo", "Pass makeArray a string" ); equals( $.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
equals( typeof $.makeArray( true )[0], "boolean", "Pass makeArray a boolean" ); equals( $.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
equals( $.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" ); equals( $.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" );
equals( $.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" ); equals( $.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
equals( $.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "HEAD", "Pass makeArray a childNodeson array" ); equals( $.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "HEAD", "Pass makeArray a childNodes array" );
//function (tricky, they have length) //function, is tricky as it has length
equals( $.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" ); equals( $.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
//window, also has length
equals( $.makeArray(window)[0], window, "Pass makeArray the window" );
equals( $.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
}); });