Release: push dist to same remote as project
[jquery.git] / src / wrap.js
blobee02aca758d6c7ad21f7f22f338ab3f77e7f4690
1 define([
2         "./core",
3         "./core/init",
4         "./manipulation", // clone
5         "./traversing" // parent, contents
6 ], function( jQuery ) {
8 jQuery.fn.extend({
9         wrapAll: function( html ) {
10                 var wrap;
12                 if ( this[ 0 ] ) {
13                         if ( jQuery.isFunction( html ) ) {
14                                 html = html.call( this[ 0 ] );
15                         }
17                         // The elements to wrap the target around
18                         wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
20                         if ( this[ 0 ].parentNode ) {
21                                 wrap.insertBefore( this[ 0 ] );
22                         }
24                         wrap.map(function() {
25                                 var elem = this;
27                                 while ( elem.firstElementChild ) {
28                                         elem = elem.firstElementChild;
29                                 }
31                                 return elem;
32                         }).append( this );
33                 }
35                 return this;
36         },
38         wrapInner: function( html ) {
39                 if ( jQuery.isFunction( html ) ) {
40                         return this.each(function( i ) {
41                                 jQuery( this ).wrapInner( html.call(this, i) );
42                         });
43                 }
45                 return this.each(function() {
46                         var self = jQuery( this ),
47                                 contents = self.contents();
49                         if ( contents.length ) {
50                                 contents.wrapAll( html );
52                         } else {
53                                 self.append( html );
54                         }
55                 });
56         },
58         wrap: function( html ) {
59                 var isFunction = jQuery.isFunction( html );
61                 return this.each(function( i ) {
62                         jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
63                 });
64         },
66         unwrap: function() {
67                 return this.parent().each(function() {
68                         if ( !jQuery.nodeName( this, "body" ) ) {
69                                 jQuery( this ).replaceWith( this.childNodes );
70                         }
71                 }).end();
72         }
73 });
75 return jQuery;
76 });