Commit 5031c9db authored by Dave Methvin's avatar Dave Methvin

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

parent 1114611f
...@@ -51,6 +51,7 @@ module.exports = function( grunt ) { ...@@ -51,6 +51,7 @@ module.exports = function( grunt ) {
"src/event.js", "src/event.js",
"src/traversing.js", "src/traversing.js",
"src/manipulation.js", "src/manipulation.js",
{ flag: "wrap", src: "src/wrap.js" },
{ flag: "css", src: "src/css.js" }, { flag: "css", src: "src/css.js" },
"src/serialize.js", "src/serialize.js",
{ flag: "event-alias", src: "src/event-alias.js" }, { flag: "event-alias", src: "src/event-alias.js" },
......
...@@ -86,6 +86,7 @@ For example, an app that only used JSONP for `$.ajax()` and did not need to calc ...@@ -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")`. - **effects**: The `.animate()` method and its shorthands such as `.slideUp()` or `.hide("slow")`.
- **event-alias**: All event attaching/triggering shorthands like `.click()` or `.mouseover()`. - **event-alias**: All event attaching/triggering shorthands like `.click()` or `.mouseover()`.
- **offset**: The `.offset()`, `.position()`, `.offsetParent()`, `.scrollLeft()`, and `.scrollTop()` methods. - **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. - **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. 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.
......
...@@ -37,74 +37,6 @@ jQuery.fn.extend({ ...@@ -37,74 +37,6 @@ jQuery.fn.extend({
}, null, value, arguments.length ); }, 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() { append: function() {
return this.domManip(arguments, true, function( elem ) { return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
......
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();
}
});
...@@ -103,7 +103,9 @@ test( "text(Function) with incoming value", function() { ...@@ -103,7 +103,9 @@ test( "text(Function) with incoming value", function() {
equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." ); equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
}); });
var testWrap = function( val ) { if ( jQuery.fn.wrap ) {
var testWrap = function( val ) {
expect( 19 ); expect( 19 );
...@@ -183,17 +185,17 @@ var testWrap = function( val ) { ...@@ -183,17 +185,17 @@ var testWrap = function( val ) {
// clean up attached elements // clean up attached elements
QUnit.reset(); QUnit.reset();
}; };
test( "wrap(String|Element)", function() { test( "wrap(String|Element)", function() {
testWrap( manipulationBareObj ); testWrap( manipulationBareObj );
}); });
test( "wrap(Function)", function() { test( "wrap(Function)", function() {
testWrap( manipulationFunctionReturningObj ); testWrap( manipulationFunctionReturningObj );
}); });
test( "wrap(Function) with index (#10177)", function() { test( "wrap(Function) with index (#10177)", function() {
var expectedIndex = 0, var expectedIndex = 0,
targets = jQuery("#qunit-fixture p"); targets = jQuery("#qunit-fixture p");
...@@ -204,9 +206,9 @@ test( "wrap(Function) with index (#10177)", function() { ...@@ -204,9 +206,9 @@ test( "wrap(Function) with index (#10177)", function() {
return "<div id='wrap_index_'" + i + "'></div>"; return "<div id='wrap_index_'" + i + "'></div>";
}); });
}); });
test( "wrap(String) consecutive elements (#10177)", function() { test( "wrap(String) consecutive elements (#10177)", function() {
var targets = jQuery("#qunit-fixture p"); var targets = jQuery("#qunit-fixture p");
expect( targets.length * 2 ); expect( targets.length * 2 );
...@@ -218,9 +220,12 @@ test( "wrap(String) consecutive elements (#10177)", function() { ...@@ -218,9 +220,12 @@ test( "wrap(String) consecutive elements (#10177)", function() {
ok( $this.parent().is(".wrapper"), "Check each elements parent is correct (.wrapper)" ); ok( $this.parent().is(".wrapper"), "Check each elements parent is correct (.wrapper)" );
equal( $this.siblings().length, 0, "Each element should be wrapped individually" ); equal( $this.siblings().length, 0, "Each element should be wrapped individually" );
}); });
}); });
}
if ( jQuery.fn.wrapAll ) {
var testWrapAll = function( val ) { var testWrapAll = function( val ) {
expect( 8 ); expect( 8 );
...@@ -244,13 +249,16 @@ var testWrapAll = function( val ) { ...@@ -244,13 +249,16 @@ var testWrapAll = function( val ) {
equal( jQuery("#first").parent()[ 0 ], jQuery("#firstp").parent()[ 0 ], "Same Parent" ); equal( jQuery("#first").parent()[ 0 ], jQuery("#firstp").parent()[ 0 ], "Same Parent" );
equal( jQuery("#first").parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); equal( jQuery("#first").parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" );
equal( jQuery("#first").parent()[ 0 ].parentNode, p, "Correct Parent" ); equal( jQuery("#first").parent()[ 0 ].parentNode, p, "Correct Parent" );
}; };
test( "wrapAll(String|Element)", function() { test( "wrapAll(String|Element)", function() {
testWrapAll( manipulationBareObj ); testWrapAll( manipulationBareObj );
}); });
}
if ( jQuery.fn.wrapInner ) {
var testWrapInner = function( val ) { var testWrapInner = function( val ) {
expect( 11 ); expect( 11 );
...@@ -281,17 +289,20 @@ var testWrapInner = function( val ) { ...@@ -281,17 +289,20 @@ var testWrapInner = function( val ) {
div.wrapInner( val("<span></span>") ); div.wrapInner( val("<span></span>") );
equal( div.children().length, 1, "The contents were wrapped." ); equal( div.children().length, 1, "The contents were wrapped." );
equal( div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted." ); equal( div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted." );
}; };
test( "wrapInner(String|Element)", function() { test( "wrapInner(String|Element)", function() {
testWrapInner( manipulationBareObj ); testWrapInner( manipulationBareObj );
}); });
test( "wrapInner(Function)", function() { test( "wrapInner(Function)", function() {
testWrapInner( manipulationFunctionReturningObj ); testWrapInner( manipulationFunctionReturningObj );
}); });
}
test( "unwrap()", function() { if ( jQuery.fn.unwrap ) {
test( "unwrap()", function() {
expect( 9 ); expect( 9 );
...@@ -317,7 +328,8 @@ test( "unwrap()", function() { ...@@ -317,7 +328,8 @@ test( "unwrap()", function() {
deepEqual( jQuery("body > span.unwrap").get(), abcdef, "body contains 6 .unwrap child spans" ); deepEqual( jQuery("body > span.unwrap").get(), abcdef, "body contains 6 .unwrap child spans" );
jQuery("body > span.unwrap").remove(); jQuery("body > span.unwrap").remove();
}); });
}
var testAppendForObject = function( valueObj, isFragment ) { var testAppendForObject = function( valueObj, isFragment ) {
var $base, var $base,
...@@ -495,7 +507,9 @@ var testAppend = function( valueObj ) { ...@@ -495,7 +507,9 @@ var testAppend = function( valueObj ) {
$radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent ); $radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
$radioChecked.trigger("click"); $radioChecked.trigger("click");
$radioUnchecked[ 0 ].checked = false; $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( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" ); equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
...@@ -1926,7 +1940,9 @@ test( "jQuery.clone - no exceptions for object elements #9587", function() { ...@@ -1926,7 +1940,9 @@ test( "jQuery.clone - no exceptions for object elements #9587", function() {
} }
}); });
test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() { if ( jQuery.fn.wrapAll ) {
test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() {
expect( 2 ); expect( 2 );
...@@ -1937,7 +1953,8 @@ test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", functio ...@@ -1937,7 +1953,8 @@ 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( $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" ); notEqual( $section.get( 0 ).style.backgroundColor, "transparent", "HTML5 elements create with jQuery( string ) inherit styles" );
}); });
}
test( "Cloned, detached HTML5 elems (#10667,10670)", function() { test( "Cloned, detached HTML5 elems (#10667,10670)", function() {
...@@ -2135,7 +2152,9 @@ test( "script evaluation (#11795)", function() { ...@@ -2135,7 +2152,9 @@ test( "script evaluation (#11795)", function() {
objGlobal.ok = isOk; objGlobal.ok = isOk;
}); });
test( "wrapping scripts (#10470)", function() { if ( jQuery.fn.wrap ) {
test( "wrapping scripts (#10470)", function() {
expect( 2 ); expect( 2 );
...@@ -2147,7 +2166,9 @@ test( "wrapping scripts (#10470)", function() { ...@@ -2147,7 +2166,9 @@ test( "wrapping scripts (#10470)", function() {
jQuery("#qunit-fixture script").wrap("<b></b>"); jQuery("#qunit-fixture script").wrap("<b></b>");
strictEqual( script.parentNode, jQuery("#qunit-fixture > b")[ 0 ], "correctly wrapped" ); strictEqual( script.parentNode, jQuery("#qunit-fixture > b")[ 0 ], "correctly wrapped" );
jQuery( script ).remove(); jQuery( script ).remove();
}); });
}
test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() { test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment