( 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, jQuery( "#nonnodes" )[ 0 ].childNodes.length,
"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( "