From 0db70aa1fa084ccf1ef90d9b78382df95bdf9c85 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski?= Date: Mon, 8 Apr 2013 23:40:08 +0200 Subject: [PATCH] Fix #13741. Make wrap/unwrap methods optional; close gh-1236. move size() test to unit/deprecated; don't use size() in other tests; make 2 unit tests actually fire; code cleanup --- Gruntfile.js | 1 + README.md | 1 + src/manipulation.js | 65 ---------- src/wrap.js | 66 ++++++++++ test/index.html | 1 + test/unit/core.js | 5 - test/unit/css.js | 2 +- test/unit/deprecated.js | 9 +- test/unit/manipulation.js | 249 +---------------------------------- test/unit/traversing.js | 30 ++--- test/unit/wrap.js | 265 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 362 insertions(+), 332 deletions(-) create mode 100644 src/wrap.js create mode 100644 test/unit/wrap.js diff --git a/Gruntfile.js b/Gruntfile.js index 24e30d2d3..27786474e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -56,6 +56,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 4e30b7932..8f299d384 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,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. 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 938fb0c3a..585d261e5 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -60,71 +60,6 @@ jQuery.fn.extend({ }, null, value, arguments.length ); }, - wrapAll: function( html ) { - 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 - var 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.firstChild && elem.firstChild.nodeType === 1 ) { - elem = elem.firstChild; - } - - 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, 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..6ff84c8b6 --- /dev/null +++ b/src/wrap.js @@ -0,0 +1,66 @@ +jQuery.fn.extend({ + wrapAll: function( html ) { + 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 + var 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.firstChild && elem.firstChild.nodeType === 1 ) { + elem = elem.firstChild; + } + + 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/index.html b/test/index.html index 8633120e2..282c2e6ff 100644 --- a/test/index.html +++ b/test/index.html @@ -57,6 +57,7 @@ + diff --git a/test/unit/core.js b/test/unit/core.js index 415318ecb..0969c933e 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -680,11 +680,6 @@ test("length", function() { equal( jQuery("#qunit-fixture p").length, 6, "Get Number of Elements Found" ); }); -test("size()", function() { - expect(1); - equal( jQuery("#qunit-fixture p").size(), 6, "Get Number of Elements Found" ); -}); - test("get()", function() { expect(1); deepEqual( jQuery("#qunit-fixture p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" ); diff --git a/test/unit/css.js b/test/unit/css.js index b71fd0962..f59c5e724 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -579,7 +579,7 @@ test( "show() resolves correct default display for detached nodes", function(){ equal( tr[ 0 ].style.display, trDisplay, "For detached tr elements, display should always be like for attached trs" ); tr.remove(); - span = span = jQuery("").hide().show(); + span = jQuery("").hide().show(); equal( span[ 0 ].style.display, "inline", "For detached span elements, display should always be inline" ); span.remove(); }); diff --git a/test/unit/deprecated.js b/test/unit/deprecated.js index 330bda200..5839ceec3 100644 --- a/test/unit/deprecated.js +++ b/test/unit/deprecated.js @@ -1 +1,8 @@ -module("deprecated"); +module("deprecated", { teardown: moduleTeardown }); + +if ( jQuery.fn.size ) { + test("size()", function() { + expect(1); + equal( jQuery("#qunit-fixture p").size(), 6, "Get Number of Elements Found" ); + }); +} diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index daa822ca7..8311ee134 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -103,222 +103,6 @@ test( "text(Function) with incoming value", function() { equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." ); }); -function testWrap( val ) { - - expect( 19 ); - - var defaultText, result, j, i, cacheLength; - - defaultText = "Try them out:", - result = jQuery("#first").wrap( val("
") ).text(); - - 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(); - 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" ); - - QUnit.reset(); - jQuery("#check1").on( "click", function() { - var checkbox = this; - - 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(); - - // using contents will get comments regular, text, and comment nodes - j = jQuery("#nonnodes").contents(); - j.wrap( val("") ); - - // 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" ); - - // Try wrapping a disconnected node - cacheLength = 0; - for ( i in jQuery.cache ) { - cacheLength++; - } - - j = jQuery("