traversing: Optimise .add by not using makeArray

(cherry picked from commit b39ee05b69)

Closes gh-1430
This commit is contained in:
Timo Tijhof 2013-12-14 02:10:46 +01:00 committed by Richard Gibson
parent 7468461569
commit 1828c9856e
2 changed files with 124 additions and 41 deletions

View File

@ -106,12 +106,11 @@ jQuery.fn.extend({
}, },
add: function( selector, context ) { add: function( selector, context ) {
var set = typeof selector === "string" ? return this.pushStack(
jQuery( selector, context ) : jQuery.unique(
jQuery.makeArray( selector && selector.nodeType ? [ selector ] : selector ), jQuery.merge( this.get(), jQuery( selector, context ) )
all = jQuery.merge( this.get(), set ); )
);
return this.pushStack( jQuery.unique(all) );
}, },
addBack: function( selector ) { addBack: function( selector ) {

View File

@ -699,13 +699,126 @@ test("sort direction", function() {
}); });
}); });
test("add(String|Element|Array|undefined)", function() { test("add(String selector)", function() {
expect( 15 ); expect( 2 );
var divs, tmp, x, notDefined; var divs;
deepEqual( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" ); deepEqual(
deepEqual( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" ); jQuery("#sndp").add("#en").add("#sap").toArray(),
q("sndp", "en", "sap"),
"Check elements from document"
);
divs = jQuery("<div/>").add("#sndp");
ok( divs[0].parentNode, "Sort with the disconnected node last (started with disconnected first)." );
});
test("add(String selector, String context)", function() {
expect( 1 );
deepEqual(
jQuery([]).add("div", "#nothiddendiv").toArray(),
q("nothiddendivchild"),
"Check elements from document"
);
});
test("add(String html)", function() {
expect( 3 );
var x,
divs = jQuery("#sndp").add("<div/>");
ok( !divs[1].parentNode, "Sort with the disconnected node last." );
x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
equal( x[0].id, "x1", "Check detached element1" );
equal( x[1].id, "x2", "Check detached element2" );
});
test("add(jQuery)", function() {
expect( 4 );
var x,
tmp = jQuery("<div/>");
x = jQuery([])
.add(
jQuery("<p id='x1'>xxx</p>").appendTo(tmp)
)
.add(
jQuery("<p id='x2'>xxx</p>").appendTo(tmp)
);
equal( x[0].id, "x1", "Check element1 in detached parent" );
equal( x[1].id, "x2", "Check element2 in detached parent" );
x = jQuery([])
.add(
jQuery("<p id='x1'>xxx</p>")
)
.add(
jQuery("<p id='x2'>xxx</p>")
);
equal( x[0].id, "x1", "Check detached element1" );
equal( x[1].id, "x2", "Check detached element2" );
});
test("add(Element)", function() {
expect( 2 );
var x,
tmp = jQuery("<div/>");
x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
equal( x[0].id, "x1", "Check on-the-fly element1" );
equal( x[1].id, "x2", "Check on-the-fly element2" );
});
test("add(Array elements)", function() {
expect( 1 );
deepEqual(
jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).toArray(),
q("sndp", "en", "sap"),
"Check elements from document"
);
});
test("add(Window)", function() {
expect( 1 );
var frame1 = document.createElement( "iframe" ),
frame2 = document.createElement( "iframe" );
// This increases window.length and sets window[i] available
document.body.appendChild( frame1 );
document.body.appendChild( frame2 );
// Window is tricky because it is a lot like an array, even Array#slice will
// turn it into a multi-item array.
equal( jQuery([]).add( window ).length, 1, "Add a window" );
document.body.removeChild( frame1 );
document.body.removeChild( frame2 );
});
test("add(NodeList|undefined|HTMLFormElement|HTMLSelectElement)", function() {
expect( 4 );
var ps, notDefined;
ps = document.getElementsByTagName("p");
equal( jQuery([]).add(ps).length, ps.length, "Add a NodeList" );
equal( jQuery([]).add(notDefined).length, 0, "Adding undefined adds nothing" );
equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" );
equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" );
// We no longer support .add(form.elements), unfortunately. // We no longer support .add(form.elements), unfortunately.
// There is no way, in browsers, to reliably determine the difference // There is no way, in browsers, to reliably determine the difference
@ -716,35 +829,6 @@ test("add(String|Element|Array|undefined)", function() {
// For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
// use jQuery([]).add(form.elements) instead. // use jQuery([]).add(form.elements) instead.
//equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" ); //equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
divs = jQuery("<div/>").add("#sndp");
ok( divs[0].parentNode, "Sort with the disconnected node last (started with disconnected first)." );
divs = jQuery("#sndp").add("<div/>");
ok( !divs[1].parentNode, "Sort with the disconnected node last." );
tmp = jQuery("<div/>");
x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
equal( x[0].id, "x1", "Check on-the-fly element1" );
equal( x[1].id, "x2", "Check on-the-fly element2" );
x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
equal( x[0].id, "x1", "Check on-the-fly element1" );
equal( x[1].id, "x2", "Check on-the-fly element2" );
x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
equal( x[0].id, "x1", "Check on-the-fly element1" );
equal( x[1].id, "x2", "Check on-the-fly element2" );
x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
equal( x[0].id, "x1", "Check on-the-fly element1" );
equal( x[1].id, "x2", "Check on-the-fly element2" );
equal( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" );
equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" );
}); });
test("add(String, Context)", function() { test("add(String, Context)", function() {