Fix #4262: faster .eq(), closes gh-1000.

This commit is contained in:
Richard Gibson 2012-10-19 17:08:50 -04:00 committed by Dave Methvin
parent 32051e97c1
commit b5084b4bf2
2 changed files with 16 additions and 48 deletions

View File

@ -234,11 +234,9 @@ jQuery.fn = jQuery.prototype = {
return this;
},
eq: function( i ) {
i = +i;
return i === -1 ?
this.slice( i ) :
this.slice( i, i + 1 );
slice: function() {
return this.pushStack( core_slice.apply( this, arguments ),
"slice", core_slice.call(arguments).join(",") );
},
first: function() {
@ -249,9 +247,10 @@ jQuery.fn = jQuery.prototype = {
return this.eq( -1 );
},
slice: function() {
return this.pushStack( core_slice.apply( this, arguments ),
"slice", core_slice.call(arguments).join(",") );
eq: function( i ) {
var len = this.length,
j = +i + ( i < 0 ? len : 0 );
return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] );
},
map: function( callback ) {

View File

@ -150,20 +150,20 @@ test("jQuery()", function() {
equal( jQuery(" a<div>" + lng + "</div>b ").length, 1, "Make sure whitespace and other characters are trimmed on long strings." );
});
test("selector state", function() {
expect(31);
test( "selector state", function() {
expect( 18 );
var test;
test = jQuery(undefined);
test = jQuery( undefined );
equal( test.selector, "", "Empty jQuery Selector" );
equal( test.context, undefined, "Empty jQuery Context" );
test = jQuery(document);
test = jQuery( document );
equal( test.selector, "", "Document Selector" );
equal( test.context, document, "Document Context" );
test = jQuery(document.body);
test = jQuery( document.body );
equal( test.selector, "", "Body Selector" );
equal( test.context, document.body, "Body Context" );
@ -175,53 +175,22 @@ test("selector state", function() {
equal( test.selector, "#notfoundnono", "#notfoundnono Selector" );
equal( test.context, document, "#notfoundnono Context" );
test = jQuery("#qunit-fixture", document);
test = jQuery( "#qunit-fixture", document );
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
equal( test.context, document, "#qunit-fixture Context" );
test = jQuery("#qunit-fixture", document.body);
test = jQuery( "#qunit-fixture", document.body );
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
equal( test.context, document.body, "#qunit-fixture Context" );
// Test cloning
test = jQuery(test);
test = jQuery( test );
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
equal( test.context, document.body, "#qunit-fixture Context" );
test = jQuery(document.body).find("#qunit-fixture");
test = jQuery( document.body ).find("#qunit-fixture");
equal( test.selector, "#qunit-fixture", "#qunit-fixture find Selector" );
equal( test.context, document.body, "#qunit-fixture find Context" );
test = jQuery("#qunit-fixture").filter("div");
equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter Selector" );
equal( test.context, document, "#qunit-fixture filter Context" );
test = jQuery("#qunit-fixture").not("div");
equal( test.selector, "#qunit-fixture.not(div)", "#qunit-fixture not Selector" );
equal( test.context, document, "#qunit-fixture not Context" );
test = jQuery("#qunit-fixture").filter("div").not("div");
equal( test.selector, "#qunit-fixture.filter(div).not(div)", "#qunit-fixture filter, not Selector" );
equal( test.context, document, "#qunit-fixture filter, not Context" );
test = jQuery("#qunit-fixture").filter("div").not("div").end();
equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter, not, end Selector" );
equal( test.context, document, "#qunit-fixture filter, not, end Context" );
test = jQuery("#qunit-fixture").parent("body");
equal( test.selector, "#qunit-fixture.parent(body)", "#qunit-fixture parent Selector" );
equal( test.context, document, "#qunit-fixture parent Context" );
test = jQuery("#qunit-fixture").eq(0);
equal( test.selector, "#qunit-fixture.slice(0,1)", "#qunit-fixture eq Selector" );
equal( test.context, document, "#qunit-fixture eq Context" );
var d = "<div />";
equal(
jQuery(d).appendTo(jQuery(d)).selector,
jQuery(d).appendTo(d).selector,
"manipulation methods make same selector for jQuery objects"
);
});
test( "globalEval", function() {