Fix #13741. Make wrap/unwrap methods optional. Close gh-1222.

This commit is contained in:
Dave Methvin 2013-04-05 17:30:48 -04:00
parent 1114611f77
commit 5031c9db4b
5 changed files with 272 additions and 248 deletions

View File

@ -51,6 +51,7 @@ module.exports = function( grunt ) {
"src/event.js",
"src/traversing.js",
"src/manipulation.js",
{ flag: "wrap", src: "src/wrap.js" },
{ flag: "css", src: "src/css.js" },
"src/serialize.js",
{ flag: "event-alias", src: "src/event-alias.js" },

View File

@ -86,6 +86,7 @@ For example, an app that only used JSONP for `$.ajax()` and did not need to calc
- **effects**: The `.animate()` method and its shorthands such as `.slideUp()` or `.hide("slow")`.
- **event-alias**: All event attaching/triggering shorthands like `.click()` or `.mouseover()`.
- **offset**: The `.offset()`, `.position()`, `.offsetParent()`, `.scrollLeft()`, and `.scrollTop()` methods.
- **wrap**: The `.wrap()`, `.wrapAll()`, `.wrapInner()`, and `.unwrap()` methods.
- **sizzle**: The Sizzle selector engine. When this module is excluded, it is replaced by a rudimentary selector engine based on the browser's `querySelectorAll` method that does not support jQuery selector extensions or enhanced semantics. See the selector-native.js file for details.
The grunt build process is aware of dependencies across modules. If you explicitly remove a module, its dependent modules will be removed as well. For example, excluding the css module also excludes effects, since the effects module uses `.css()` to animate CSS properties. These dependencies are listed in Gruntfile.js and the build process shows a message for each dependent module it excludes.

View File

@ -37,74 +37,6 @@ jQuery.fn.extend({
}, null, value, arguments.length );
},
wrapAll: function( html ) {
var wrap;
if ( jQuery.isFunction( html ) ) {
return this.each(function( i ) {
jQuery( this ).wrapAll( html.call(this, i) );
});
}
if ( this[ 0 ] ) {
// The elements to wrap the target around
wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
if ( this[ 0 ].parentNode ) {
wrap.insertBefore( this[ 0 ] );
}
wrap.map(function() {
var elem = this;
while ( elem.firstElementChild ) {
elem = elem.firstElementChild;
}
return elem;
}).append( this );
}
return this;
},
wrapInner: function( html ) {
if ( jQuery.isFunction( html ) ) {
return this.each(function( i ) {
jQuery( this ).wrapInner( html.call(this, i) );
});
}
return this.each(function() {
var self = jQuery( this ),
contents = self.contents();
if ( contents.length ) {
contents.wrapAll( html );
} else {
self.append( html );
}
});
},
wrap: function( html ) {
var isFunction = jQuery.isFunction( html );
return this.each(function( i ) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
});
},
unwrap: function() {
return this.parent().each(function() {
if ( !jQuery.nodeName( this, "body" ) ) {
jQuery( this ).replaceWith( this.childNodes );
}
}).end();
},
append: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {

69
src/wrap.js Normal file
View File

@ -0,0 +1,69 @@
jQuery.fn.extend({
wrapAll: function( html ) {
var wrap;
if ( jQuery.isFunction( html ) ) {
return this.each(function( i ) {
jQuery( this ).wrapAll( html.call(this, i) );
});
}
if ( this[ 0 ] ) {
// The elements to wrap the target around
wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
if ( this[ 0 ].parentNode ) {
wrap.insertBefore( this[ 0 ] );
}
wrap.map(function() {
var elem = this;
while ( elem.firstElementChild ) {
elem = elem.firstElementChild;
}
return elem;
}).append( this );
}
return this;
},
wrapInner: function( html ) {
if ( jQuery.isFunction( html ) ) {
return this.each(function( i ) {
jQuery( this ).wrapInner( html.call(this, i) );
});
}
return this.each(function() {
var self = jQuery( this ),
contents = self.contents();
if ( contents.length ) {
contents.wrapAll( html );
} else {
self.append( html );
}
});
},
wrap: function( html ) {
var isFunction = jQuery.isFunction( html );
return this.each(function( i ) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
});
},
unwrap: function() {
return this.parent().each(function() {
if ( !jQuery.nodeName( this, "body" ) ) {
jQuery( this ).replaceWith( this.childNodes );
}
}).end();
}
});

View File

@ -103,6 +103,8 @@ test( "text(Function) with incoming value", function() {
equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
});
if ( jQuery.fn.wrap ) {
var testWrap = function( val ) {
expect( 19 );
@ -219,6 +221,9 @@ test( "wrap(String) consecutive elements (#10177)", function() {
equal( $this.siblings().length, 0, "Each element should be wrapped individually" );
});
});
}
if ( jQuery.fn.wrapAll ) {
var testWrapAll = function( val ) {
@ -249,6 +254,9 @@ var testWrapAll = function( val ) {
test( "wrapAll(String|Element)", function() {
testWrapAll( manipulationBareObj );
});
}
if ( jQuery.fn.wrapInner ) {
var testWrapInner = function( val ) {
@ -290,6 +298,9 @@ test( "wrapInner(String|Element)", function() {
test( "wrapInner(Function)", function() {
testWrapInner( manipulationFunctionReturningObj );
});
}
if ( jQuery.fn.unwrap ) {
test( "unwrap()", function() {
@ -318,6 +329,7 @@ test( "unwrap()", function() {
jQuery("body > span.unwrap").remove();
});
}
var testAppendForObject = function( valueObj, isFragment ) {
var $base,
@ -495,7 +507,9 @@ var testAppend = function( valueObj ) {
$radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
$radioChecked.trigger("click");
$radioUnchecked[ 0 ].checked = false;
$radioParent.wrap("<div></div>");
jQuery("<div/>").insertBefore($radioParent).append($radioParent);
equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
@ -1926,6 +1940,8 @@ test( "jQuery.clone - no exceptions for object elements #9587", function() {
}
});
if ( jQuery.fn.wrapAll ) {
test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() {
expect( 2 );
@ -1938,6 +1954,7 @@ test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", functio
notEqual( $wraptarget.parent("aside").get( 0 ).style.backgroundColor, "transparent", "HTML5 elements created with wrapAll inherit styles" );
notEqual( $section.get( 0 ).style.backgroundColor, "transparent", "HTML5 elements create with jQuery( string ) inherit styles" );
});
}
test( "Cloned, detached HTML5 elems (#10667,10670)", function() {
@ -2135,6 +2152,8 @@ test( "script evaluation (#11795)", function() {
objGlobal.ok = isOk;
});
if ( jQuery.fn.wrap ) {
test( "wrapping scripts (#10470)", function() {
expect( 2 );
@ -2149,6 +2168,8 @@ test( "wrapping scripts (#10470)", function() {
jQuery( script ).remove();
});
}
test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
expect( 10 );