From 508b1e243224f18dd429f3087d7d6460e3a4eeb5 Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Fri, 25 Apr 2008 03:48:07 +0000 Subject: [PATCH] jquery core: fixed makeArray to recognize the window (has length) test runner: updated the tests for makeArray --- src/core.js | 10 ++++++---- test/unit/core.js | 14 +++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/core.js b/src/core.js index 66efc182c..c5fb865ed 100644 --- a/src/core.js +++ b/src/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; }, diff --git a/test/unit/core.js b/test/unit/core.js index b79b8cb21..8cd1aee9a 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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" ); }); \ No newline at end of file -- 2.39.5