( function() {
if ( !jQuery.fn.wrap ) { // no wrap module
return;
}
QUnit.module( "wrap", {
teardown: moduleTeardown
} );
// See test/unit/manipulation.js for explanation about these 2 functions
function manipulationBareObj( value ) {
return value;
}
function manipulationFunctionReturningObj( value ) {
return function() {
return value;
};
}
function testWrap( val, assert ) {
assert.expect( 19 );
var defaultText, result, j, i, cacheLength;
defaultText = "Try them out:";
result = jQuery( "#first" ).wrap( val( "
" ) ).text();
assert.equal(
defaultText, result, "Check for wrapping of on-the-fly html"
);
assert.ok(
jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
);
result = jQuery( "#first" ).wrap( val( document.getElementById( "empty" ) ) ).parent();
assert.ok(
result.is( "ol" ), "Check for element wrapping"
);
assert.equal(
result.text(), defaultText, "Check for element wrapping"
);
jQuery( "#check1" ).on( "click", function() {
var checkbox = this;
assert.ok(
checkbox.checked, "Checkbox's state is erased after wrap() action, see #769"
);
jQuery( checkbox ).wrap( val( "" ) );
assert.ok(
checkbox.checked, "Checkbox's state is erased after wrap() action, see #769"
);
} ).prop( "checked", false )[ 0 ].click();
// using contents will get comments regular, text, and comment nodes
j = jQuery( "#nonnodes" ).contents();
j.wrap( val( "" ) );
assert.equal(
jQuery( "#nonnodes > i" ).length, 3, "Check node,textnode,comment wraps ok"
);
assert.equal(
jQuery( "#nonnodes > i" ).text(),
j.text(),
"Check node,textnode,comment wraps doesn't hurt text"
);
// Try wrapping a disconnected node
cacheLength = 0;
for ( i in jQuery.cache ) {
cacheLength++;
}
j = jQuery( "" ).wrap( val( "" ) );
assert.equal(
j[ 0 ] .nodeName.toUpperCase(), "LABEL", "Element is a label"
);
assert.equal(
j[ 0 ].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped"
);
for ( i in jQuery.cache ) {
cacheLength--;
}
assert.equal(
cacheLength, 0, "No memory leak in jQuery.cache (bug #7165)"
);
// Wrap an element containing a text node
j = jQuery( "" ).wrap( "
test
" );
assert.equal(
j[ 0 ].previousSibling.nodeType, 3, "Make sure the previous node is a text element"
);
assert.equal(
j[ 0 ].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element."
);
// Try to wrap an element with multiple elements (should fail)
j = jQuery( "
" ).children().wrap( "" );
assert.equal(
j[ 0 ].parentNode.parentNode.childNodes.length,
1,
"There should only be one element wrapping."
);
assert.equal(
j.length, 1, "There should only be one element (no cloning)."
);
assert.equal(
j[ 0 ].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph."
);
// Wrap an element with a jQuery set
j = jQuery( "" ).wrap( jQuery( "" ) );
assert.equal(
j[ 0 ].parentNode.nodeName.toLowerCase(), "div", "Wrapping works."
);
// Wrap an element with a jQuery set and event
result = jQuery( "" ).on( "click", function() {
assert.ok(
true, "Event triggered."
);
// Remove handlers on detached elements
result.off();
jQuery( this ).off();
} );
j = jQuery( "" ).wrap( result );
assert.equal(
j[ 0 ].parentNode.nodeName.toLowerCase(), "div", "Wrapping works."
);
j.parent().trigger( "click" );
}
QUnit.test( "wrap(String|Element)", function( assert ) {
testWrap( manipulationBareObj, assert );
} );
QUnit.test( "wrap(Function)", function( assert ) {
testWrap( manipulationFunctionReturningObj, assert );
} );
QUnit.test( "wrap(Function) with index (#10177)", function( assert ) {
var expectedIndex = 0,
targets = jQuery( "#qunit-fixture p" );
assert.expect( targets.length );
targets.wrap( function( i ) {
assert.equal(
i,
expectedIndex,
"Check if the provided index (" + i + ") is as expected (" + expectedIndex + ")"
);
expectedIndex++;
return "";
} );
} );
QUnit.test( "wrap(String) consecutive elements (#10177)", function( assert ) {
var targets = jQuery( "#qunit-fixture p" );
assert.expect( targets.length * 2 );
targets.wrap( "" );
targets.each( function() {
var $this = jQuery( this );
assert.ok(
$this.parent().is( ".wrapper" ),
"Check each elements parent is correct (.wrapper)"
);
assert.equal(
$this.siblings().length, 0, "Each element should be wrapped individually"
);
} );
} );
QUnit.test( "wrapAll(String)", function( assert ) {
assert.expect( 5 );
var prev, p, result;
prev = jQuery( "#firstp" )[ 0 ].previousSibling;
p = jQuery( "#firstp,#first" )[ 0 ].parentNode;
result = jQuery( "#firstp,#first" ).wrapAll( "
" );
assert.equal(
result.parent().length, 1, "Check for wrapping of on-the-fly html"
);
assert.ok(
jQuery( "#first" ).parent().parent().is( ".red" ),
"Check if wrapper has class 'red'"
);
assert.ok(
jQuery( "#firstp" ).parent().parent().is( ".red" ),
"Check if wrapper has class 'red'"
);
assert.equal(
jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
);
assert.equal(
jQuery( "#first" ).parent().parent()[ 0 ].parentNode, p, "Correct Parent"
);
} );
QUnit.test( "wrapAll(Function)", function( assert ) {
assert.expect( 5 );
var prev = jQuery( "#firstp" )[ 0 ].previousSibling,
p = jQuery( "#firstp,#first" )[ 0 ].parentNode,
result = jQuery( "#firstp,#first" ).wrapAll( function() {
return "
";
} );
assert.equal(
result.parent().length, 1, "Check for wrapping of on-the-fly html"
);
assert.ok(
jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
);
assert.ok(
jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
);
assert.ok(
jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent"
);
assert.strictEqual(
jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
);
} );
QUnit.test( "wrapAll(Function) check execution characteristics", function( assert ) {
assert.expect( 3 );
var i = 0;
jQuery( "non-existent" ).wrapAll( function() {
i++;
return "";
} );
assert.ok(
!i, "should not execute function argument if target element does not exist"
);
jQuery( "#firstp" ).wrapAll( function( index ) {
assert.strictEqual(
this, jQuery( "#firstp" )[ 0 ], "context must be the first found element"
);
assert.strictEqual(
index, undefined, "index argument should not be included in function execution"
);
} );
} );
QUnit.test( "wrapAll(Function)", function( assert ) {
assert.expect( 5 );
var prev = jQuery( "#firstp" )[ 0 ].previousSibling,
p = jQuery( "#firstp,#first" )[ 0 ].parentNode,
result = jQuery( "#firstp,#first" ).wrapAll( function() {
return "
";
} );
assert.equal(
result.parent().length, 1, "Check for wrapping of on-the-fly html"
);
assert.ok(
jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
);
assert.ok(
jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'"
);
assert.ok(
jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent"
);
assert.strictEqual(
jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
);
} );
QUnit.test( "wrapAll(Function) check execution characteristics", function( assert ) {
assert.expect( 3 );
var i = 0;
jQuery( "non-existent" ).wrapAll( function() {
i++;
return "";
} );
assert.ok(
!i, "should not execute function argument if target element does not exist"
);
jQuery( "#firstp" ).wrapAll( function( index ) {
assert.strictEqual(
this, jQuery( "#firstp" )[ 0 ], "context must be the first found element"
);
assert.strictEqual(
index, undefined, "index argument should not be included in function execution"
);
} );
} );
QUnit.test( "wrapAll(Element)", function( assert ) {
assert.expect( 3 );
var prev, p;
prev = jQuery( "#firstp" )[ 0 ].previousSibling;
p = jQuery( "#first" )[ 0 ].parentNode;
jQuery( "#firstp,#first" ).wrapAll( document.getElementById( "empty" ) );
assert.equal(
jQuery( "#first" ).parent()[ 0 ], jQuery( "#firstp" ).parent()[ 0 ], "Same Parent"
);
assert.equal(
jQuery( "#first" ).parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling"
);
assert.equal(
jQuery( "#first" ).parent()[ 0 ].parentNode, p, "Correct Parent"
);
} );
QUnit.test( "wrapInner(String)", function( assert ) {
assert.expect( 6 );
var num;
num = jQuery( "#first" ).children().length;
jQuery( "#first" ).wrapInner( "
" );
assert.equal(
jQuery( "#first" ).children().length, 1, "Only one child"
);
assert.ok(
jQuery( "#first" ).children().is( ".red" ), "Verify Right Element"
);
assert.equal(
jQuery( "#first" ).children().children().children().length, num, "Verify Elements Intact"
);
num = jQuery( "#first" ).html( "foo