diff options
author | Timo Tijhof <krinklemail@gmail.com> | 2013-12-14 02:10:46 +0100 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2013-12-30 12:36:31 -0500 |
commit | 1828c9856e54b7d5ead7b374b247f8432f96c278 (patch) | |
tree | 93c28c1dac3d20218a98a3236ae34ac339cc0bab /test/unit/traversing.js | |
parent | 7468461569fa2908aa84f162b3ad7cfda42b46bf (diff) | |
download | jquery-1828c9856e54b7d5ead7b374b247f8432f96c278.tar.gz jquery-1828c9856e54b7d5ead7b374b247f8432f96c278.zip |
traversing: Optimise .add by not using makeArray
(cherry picked from commit b39ee05b69336d5a6230287ab8bd48c48e5b206f)
Closes gh-1430
Diffstat (limited to 'test/unit/traversing.js')
-rw-r--r-- | test/unit/traversing.js | 138 |
1 files changed, 111 insertions, 27 deletions
diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 292cb41b9..77479ff19 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -699,52 +699,136 @@ test("sort direction", function() { }); }); -test("add(String|Element|Array|undefined)", function() { - expect( 15 ); - - var divs, tmp, x, notDefined; +test("add(String selector)", function() { + expect( 2 ); - deepEqual( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" ); - deepEqual( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" ); + var divs; - // We no longer support .add(form.elements), unfortunately. - // There is no way, in browsers, to reliably determine the difference - // between form.elements and form - and doing .add(form) and having it - // add the form elements is way to unexpected, so this gets the boot. - // ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" ); - - // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE - // 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()" ); + deepEqual( + 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/>"); - 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("<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" ); +}); - 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" ); +test("add(Array elements)", function() { + expect( 1 ); - 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" ); + 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 ); - equal( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" ); + // 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. + // There is no way, in browsers, to reliably determine the difference + // between form.elements and form - and doing .add(form) and having it + // add the form elements is way to unexpected, so this gets the boot. + //ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" ); + + // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE + // 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()" ); }); test("add(String, Context)", function() { |