From: Dave Methvin Date: Fri, 5 Apr 2013 21:30:48 +0000 (-0400) Subject: Fix #13741. Make wrap/unwrap methods optional. Close gh-1222. X-Git-Tag: 2.0.0-beta3~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5031c9db4bf22cc04472346eeee8f25a61c5ee68;p=jquery.git Fix #13741. Make wrap/unwrap methods optional. Close gh-1222. --- diff --git a/Gruntfile.js b/Gruntfile.js index 809ea3552..5e2917373 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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" }, diff --git a/README.md b/README.md index 573c68502..89a57e6d1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/manipulation.js b/src/manipulation.js index a5a1a3975..5cfc853be 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -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 ) { diff --git a/src/wrap.js b/src/wrap.js new file mode 100644 index 000000000..5968adbc6 --- /dev/null +++ b/src/wrap.js @@ -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(); + } +}); diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index c9e6c8f9a..012a0efc9 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -103,221 +103,233 @@ test( "text(Function) with incoming value", function() { equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." ); }); -var testWrap = function( val ) { +if ( jQuery.fn.wrap ) { - expect( 19 ); + var testWrap = function( val ) { - var defaultText, result, j, i, cacheLength; + expect( 19 ); - defaultText = "Try them out:", - result = jQuery("#first").wrap( val("
") ).text(); + var defaultText, result, j, i, cacheLength; - equal( defaultText, result, "Check for wrapping of on-the-fly html" ); - ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); + defaultText = "Try them out:", + result = jQuery("#first").wrap( val("
") ).text(); - QUnit.reset(); - result = jQuery("#first").wrap( val(document.getElementById("empty")) ).parent(); - ok( result.is("ol"), "Check for element wrapping" ); - equal( result.text(), defaultText, "Check for element wrapping" ); + equal( defaultText, result, "Check for wrapping of on-the-fly html" ); + ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); - QUnit.reset(); - jQuery("#check1").on( "click", function() { - var checkbox = this; + QUnit.reset(); + result = jQuery("#first").wrap( val(document.getElementById("empty")) ).parent(); + ok( result.is("ol"), "Check for element wrapping" ); + equal( result.text(), defaultText, "Check for element wrapping" ); - ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); - jQuery( checkbox ).wrap( val("") ); - ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); - }).prop( "checked", false )[ 0 ].click(); + QUnit.reset(); + jQuery("#check1").on( "click", function() { + var checkbox = this; - // using contents will get comments regular, text, and comment nodes - j = jQuery("#nonnodes").contents(); - j.wrap( val("") ); + ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); + jQuery( checkbox ).wrap( val("") ); + ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); + }).prop( "checked", false )[ 0 ].click(); - // Blackberry 4.6 doesn't maintain comments in the DOM - equal( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[ 0 ].childNodes.length, "Check node,textnode,comment wraps ok" ); - equal( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" ); + // using contents will get comments regular, text, and comment nodes + j = jQuery("#nonnodes").contents(); + j.wrap( val("") ); - // Try wrapping a disconnected node - cacheLength = 0; - for ( i in jQuery.cache ) { - cacheLength++; - } + // Blackberry 4.6 doesn't maintain comments in the DOM + equal( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[ 0 ].childNodes.length, "Check node,textnode,comment wraps ok" ); + equal( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" ); - j = jQuery("