Skip to content
Permalink
Browse files

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

test runner: updated the tests for makeArray
  • Loading branch information
flesler committed Apr 25, 2008
1 parent 25f9974 commit 508b1e243224f18dd429f3087d7d6460e3a4eeb5
Showing with 15 additions and 9 deletions.
  1. +6 −4 src/core.js
  2. +9 −5 test/unit/core.js
@@ -1114,13 +1114,15 @@ jQuery.extend({
makeArray: function( array ) {
var ret = [];

if( array != undefined )
//strings and functions also have 'length'
if( array.length != undefined && !array.split && !array.call )
for( var i = array.length; i; )
if( array != undefined ){
var i = array.length;
//the window, strings and functions also have 'length'
if( i != undefined && typeof array == 'object' && array != window )
while( i )
ret[--i] = array[i];
else
ret[0] = array;
}

return ret;
},
@@ -1563,7 +1563,7 @@ test("contents()", function() {
});

test("$.makeArray", function(){
expect(11);
expect(13);

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( 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( {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)
equals( $.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
//function, is tricky as it has length
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" );
});

0 comments on commit 508b1e2

Please sign in to comment.
You can’t perform that action at this time.