3 if ( !jQuery.fn.wrap ) { // no wrap module
7 QUnit.module( "wrap", {
8 afterEach: moduleTeardown
11 // See test/unit/manipulation.js for explanation about these 2 functions
12 function manipulationBareObj( value ) {
16 function manipulationFunctionReturningObj( value ) {
22 function testWrap( val, assert ) {
26 var defaultText, result, j, i, cacheLength;
28 defaultText = "Try them out:";
29 result = jQuery( "#first" ).wrap( val( "<div class='red'><span></span></div>" ) ).text();
32 defaultText, result, "Check for wrapping of on-the-fly html"
35 jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
38 result = jQuery( "#first" ).wrap( val( document.getElementById( "empty" ) ) ).parent();
40 result.is( "ol" ), "Check for element wrapping"
43 result.text(), defaultText, "Check for element wrapping"
46 jQuery( "#check1" ).on( "click", function() {
50 checkbox.checked, "Checkbox's state is erased after wrap() action, see #769"
52 jQuery( checkbox ).wrap( val( "<div id='c1' style='display:none;'></div>" ) );
54 checkbox.checked, "Checkbox's state is erased after wrap() action, see #769"
56 } ).prop( "checked", false )[ 0 ].click();
58 // using contents will get comments regular, text, and comment nodes
59 j = jQuery( "#nonnodes" ).contents();
60 j.wrap( val( "<i></i>" ) );
63 jQuery( "#nonnodes > i" ).length, 3,
64 "Check node,textnode,comment wraps ok"
67 jQuery( "#nonnodes > i" ).text(), j.text(),
68 "Check node,textnode,comment wraps doesn't hurt text"
71 // Try wrapping a disconnected node
73 for ( i in jQuery.cache ) {
77 j = jQuery( "<label/>" ).wrap( val( "<li/>" ) );
79 j[ 0 ] .nodeName.toUpperCase(), "LABEL", "Element is a label"
82 j[ 0 ].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped"
85 for ( i in jQuery.cache ) {
89 cacheLength, 0, "No memory leak in jQuery.cache (bug #7165)"
92 // Wrap an element containing a text node
93 j = jQuery( "<span/>" ).wrap( "<div>test</div>" );
95 j[ 0 ].previousSibling.nodeType, 3, "Make sure the previous node is a text element"
98 j[ 0 ].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element."
101 // Try to wrap an element with multiple elements (should fail)
102 j = jQuery( "<div><span></span></div>" ).children().wrap( "<p></p><div></div>" );
104 j[ 0 ].parentNode.parentNode.childNodes.length, 1,
105 "There should only be one element wrapping."
108 j.length, 1, "There should only be one element (no cloning)."
111 j[ 0 ].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph."
114 // Wrap an element with a jQuery set
115 j = jQuery( "<span/>" ).wrap( jQuery( "<div></div>" ) );
117 j[ 0 ].parentNode.nodeName.toLowerCase(), "div", "Wrapping works."
120 // Wrap an element with a jQuery set and event
121 result = jQuery( "<div></div>" ).on( "click", function() {
123 true, "Event triggered."
126 // Remove handlers on detached elements
128 jQuery( this ).off();
131 j = jQuery( "<span/>" ).wrap( result );
133 j[ 0 ].parentNode.nodeName.toLowerCase(), "div", "Wrapping works."
136 j.parent().trigger( "click" );
139 QUnit.test( "wrap(String|Element)", function( assert ) {
140 testWrap( manipulationBareObj, assert );
143 QUnit.test( "wrap(Function)", function( assert ) {
144 testWrap( manipulationFunctionReturningObj, assert );
147 QUnit.test( "wrap(Function) with index (#10177)", function( assert ) {
148 var expectedIndex = 0,
149 targets = jQuery( "#qunit-fixture p" );
151 assert.expect( targets.length );
152 targets.wrap( function( i ) {
155 "Check if the provided index (" + i + ") is as expected (" + expectedIndex + ")"
159 return "<div id='wrap_index_'" + i + "'></div>";
163 QUnit.test( "wrap(String) consecutive elements (#10177)", function( assert ) {
164 var targets = jQuery( "#qunit-fixture p" );
166 assert.expect( targets.length * 2 );
167 targets.wrap( "<div class='wrapper'></div>" );
169 targets.each( function() {
170 var $this = jQuery( this );
173 $this.parent().is( ".wrapper" ), "Check each elements parent is correct (.wrapper)"
176 $this.siblings().length, 0, "Each element should be wrapped individually"
181 QUnit.test( "wrapAll(String)", function( assert ) {
187 prev = jQuery( "#firstp" )[ 0 ].previousSibling;
188 p = jQuery( "#firstp,#first" )[ 0 ].parentNode;
189 result = jQuery( "#firstp,#first" ).wrapAll( "<div class='red'><div class='tmp'></div></div>" );
192 result.parent().length, 1, "Check for wrapping of on-the-fly html"
195 jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
198 jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
201 jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
204 jQuery( "#first" ).parent().parent()[ 0 ].parentNode, p, "Correct Parent"
209 QUnit.test( "wrapAll(Function)", function( assert ) {
212 var prev = jQuery( "#firstp" )[ 0 ].previousSibling,
213 p = jQuery( "#firstp,#first" )[ 0 ].parentNode,
214 result = jQuery( "#firstp,#first" ).wrapAll( function() {
215 return "<div class='red'><div class='tmp'></div></div>";
219 result.parent().length, 1, "Check for wrapping of on-the-fly html"
222 jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
225 jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
228 jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent"
231 jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
235 QUnit.test( "wrapAll(Function) check execution characteristics", function( assert ) {
240 jQuery( "non-existent" ).wrapAll( function() {
246 !i, "should not execute function argument if target element does not exist"
249 jQuery( "#firstp" ).wrapAll( function( index ) {
251 this, jQuery( "#firstp" )[ 0 ], "context must be the first found element"
254 index, undefined, "index argument should not be included in function execution"
259 QUnit.test( "wrapAll(Element)", function( assert ) {
264 prev = jQuery( "#firstp" )[ 0 ].previousSibling;
265 p = jQuery( "#first" )[ 0 ].parentNode;
266 jQuery( "#firstp,#first" ).wrapAll( document.getElementById( "empty" ) );
269 jQuery( "#first" ).parent()[ 0 ], jQuery( "#firstp" ).parent()[ 0 ], "Same Parent"
272 jQuery( "#first" ).parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
275 jQuery( "#first" ).parent()[ 0 ].parentNode, p, "Correct Parent"
279 QUnit.test( "wrapInner(String)", function( assert ) {
285 num = jQuery( "#first" ).children().length;
286 jQuery( "#first" ).wrapInner( "<div class='red'><div id='tmp'></div></div>" );
289 jQuery( "#first" ).children().length, 1, "Only one child"
292 jQuery( "#first" ).children().is( ".red" ), "Verify Right Element"
295 jQuery( "#first" ).children().children().children().length, num, "Verify Elements Intact"
298 num = jQuery( "#first" ).html( "foo<div>test</div><div>test2</div>" ).children().length;
299 jQuery( "#first" ).wrapInner( "<div class='red'><div id='tmp'></div></div>" );
301 jQuery( "#first" ).children().length, 1, "Only one child"
304 jQuery( "#first" ).children().is( ".red" ), "Verify Right Element"
307 jQuery( "#first" ).children().children().children().length, num, "Verify Elements Intact"
311 QUnit.test( "wrapInner(Element)", function( assert ) {
316 div = jQuery( "<div/>" );
318 num = jQuery( "#first" ).children().length;
319 jQuery( "#first" ).wrapInner( document.getElementById( "empty" ) );
321 jQuery( "#first" ).children().length, 1, "Only one child"
324 jQuery( "#first" ).children().is( "#empty" ), "Verify Right Element"
327 jQuery( "#first" ).children().children().length, num, "Verify Elements Intact"
330 div.wrapInner( "<span></span>" );
332 div.children().length, 1, "The contents were wrapped."
335 div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted."
339 QUnit.test( "wrapInner(Function) returns String", function( assert ) {
344 val = manipulationFunctionReturningObj;
346 num = jQuery( "#first" ).children().length;
347 jQuery( "#first" ).wrapInner( val( "<div class='red'><div id='tmp'></div></div>" ) );
350 jQuery( "#first" ).children().length, 1, "Only one child"
353 jQuery( "#first" ).children().is( ".red" ), "Verify Right Element"
356 jQuery( "#first" ).children().children().children().length, num, "Verify Elements Intact"
359 num = jQuery( "#first" ).html( "foo<div>test</div><div>test2</div>" ).children().length;
360 jQuery( "#first" ).wrapInner( val( "<div class='red'><div id='tmp'></div></div>" ) );
362 jQuery( "#first" ).children().length, 1, "Only one child"
365 jQuery( "#first" ).children().is( ".red" ), "Verify Right Element"
368 jQuery( "#first" ).children().children().children().length, num, "Verify Elements Intact"
372 QUnit.test( "wrapInner(Function) returns Element", function( assert ) {
377 val = manipulationFunctionReturningObj,
378 div = jQuery( "<div/>" );
380 num = jQuery( "#first" ).children().length;
381 jQuery( "#first" ).wrapInner( val( document.getElementById( "empty" ) ) );
383 jQuery( "#first" ).children().length, 1, "Only one child"
386 jQuery( "#first" ).children().is( "#empty" ), "Verify Right Element"
389 jQuery( "#first" ).children().children().length, num, "Verify Elements Intact"
392 div.wrapInner( val( "<span></span>" ) );
394 div.children().length, 1, "The contents were wrapped."
397 div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted."
401 QUnit.test( "unwrap()", function( assert ) {
405 jQuery( "body" ).append(
406 " <div id='unwrap' style='display: none;'> <div id='unwrap1'>" +
407 " <span class='unwrap'>a</span> <span class='unwrap'>b</span> </div> <div id='unwrap2'>" +
408 " <span class='unwrap'>c</span> <span class='unwrap'>d</span> </div> <div id='unwrap3'>" +
409 " <b><span class='unwrap unwrap3'>e</span></b>" +
410 " <b><span class='unwrap unwrap3'>f</span></b> </div> </div>"
413 var abcd = jQuery( "#unwrap1 > span, #unwrap2 > span" ).get(),
414 abcdef = jQuery( "#unwrap span" ).get();
417 jQuery( "#unwrap1 span" ).add( "#unwrap2 span:first-child" ).unwrap().length, 3,
418 "make #unwrap1 and #unwrap2 go away"
421 jQuery( "#unwrap > span" ).get(), abcd, "all four spans should still exist"
425 jQuery( "#unwrap3 span" ).unwrap().get(), jQuery( "#unwrap3 > span" ).get(),
426 "make all b in #unwrap3 go away"
430 jQuery( "#unwrap3 span" ).unwrap().get(), jQuery( "#unwrap > span.unwrap3" ).get(),
431 "make #unwrap3 go away"
435 jQuery( "#unwrap" ).children().get(), abcdef, "#unwrap only contains 6 child spans"
439 jQuery( "#unwrap > span" ).unwrap().get(), jQuery( "body > span.unwrap" ).get(),
440 "make the 6 spans become children of body"
444 jQuery( "body > span.unwrap" ).unwrap().get(), jQuery( "body > span.unwrap" ).get(),
445 "can't unwrap children of body"
448 jQuery( "body > span.unwrap" ).unwrap().get(), abcdef, "can't unwrap children of body"
452 jQuery( "body > span.unwrap" ).get(), abcdef, "body contains 6 .unwrap child spans"
455 jQuery( "body > span.unwrap" ).remove();
458 QUnit.test( "unwrap( selector )", function( assert ) {
462 jQuery( "body" ).append(
463 " <div id='unwrap' style='display: none;'> <div id='unwrap1'>" +
464 "<span class='unwrap'>a</span> <span class='unwrap'>b</span> </div>" +
465 " <div id='unwrap2'> <span class='unwrap'>c</span> <span class='unwrap'>d</span>" +
469 // Shouldn't unwrap, no match
470 jQuery( "#unwrap1 span" ) .unwrap( "#unwrap2" );
472 jQuery( "#unwrap1" ).length, 1, "still wrapped"
475 // Shouldn't unwrap, no match
476 jQuery( "#unwrap1 span" ) .unwrap( "span" );
478 jQuery( "#unwrap1" ).length, 1, "still wrapped"
482 jQuery( "#unwrap1 span" ) .unwrap( "#unwrap1" );
484 jQuery( "#unwrap1" ).length, 0, "unwrapped match"
487 // Check return values
489 jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "quote" ).get(),
490 "return on unmatched unwrap"
493 jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "#unwrap2" ).get(),
494 "return on matched unwrap"
497 jQuery( "body > span.unwrap" ).remove();
500 QUnit.test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function( assert ) {
504 var $wraptarget = jQuery( "<div id='wrap-target'>Target</div>" ).appendTo( "#qunit-fixture" ),
505 $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
507 $wraptarget.wrapAll( "<aside style='background-color:green'></aside>" );
510 $wraptarget.parent( "aside" ).get( 0 ).style.backgroundColor, "transparent",
511 "HTML5 elements created with wrapAll inherit styles"
514 $section.get( 0 ).style.backgroundColor, "transparent",
515 "HTML5 elements create with jQuery( string ) inherit styles"
519 QUnit.test( "wrapping scripts (#10470)", function( assert ) {
523 var script = document.createElement( "script" );
524 script.text = script.textContent =
525 "QUnit.assert.ok( !document.eval10470, 'script evaluated once' ); document.eval10470 = true;";
527 document.eval10470 = false;
528 jQuery( "#qunit-fixture" ).empty()[ 0 ].appendChild( script );
529 jQuery( "#qunit-fixture script" ).wrap( "<b></b>" );
531 script.parentNode, jQuery( "#qunit-fixture > b" )[ 0 ], "correctly wrapped"
533 jQuery( script ).remove();