Event: Fix handling of multiple async focus events
[jquery.git] / src / wrap.js
blob41b716f9f78a00a1d5d917b3d1fc84757ffedb47
1 define( [
2         "./core",
3         "./var/isFunction",
4         "./core/init",
5         "./manipulation", // clone
6         "./traversing" // parent, contents
7 ], function( jQuery, isFunction ) {
9 "use strict";
11 jQuery.fn.extend( {
12         wrapAll: function( html ) {
13                 var wrap;
15                 if ( this[ 0 ] ) {
16                         if ( isFunction( html ) ) {
17                                 html = html.call( this[ 0 ] );
18                         }
20                         // The elements to wrap the target around
21                         wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
23                         if ( this[ 0 ].parentNode ) {
24                                 wrap.insertBefore( this[ 0 ] );
25                         }
27                         wrap.map( function() {
28                                 var elem = this;
30                                 while ( elem.firstElementChild ) {
31                                         elem = elem.firstElementChild;
32                                 }
34                                 return elem;
35                         } ).append( this );
36                 }
38                 return this;
39         },
41         wrapInner: function( html ) {
42                 if ( isFunction( html ) ) {
43                         return this.each( function( i ) {
44                                 jQuery( this ).wrapInner( html.call( this, i ) );
45                         } );
46                 }
48                 return this.each( function() {
49                         var self = jQuery( this ),
50                                 contents = self.contents();
52                         if ( contents.length ) {
53                                 contents.wrapAll( html );
55                         } else {
56                                 self.append( html );
57                         }
58                 } );
59         },
61         wrap: function( html ) {
62                 var htmlIsFunction = isFunction( html );
64                 return this.each( function( i ) {
65                         jQuery( this ).wrapAll( htmlIsFunction ? html.call( this, i ) : html );
66                 } );
67         },
69         unwrap: function( selector ) {
70                 this.parent( selector ).not( "body" ).each( function() {
71                         jQuery( this ).replaceWith( this.childNodes );
72                 } );
73                 return this;
74         }
75 } );
77 return jQuery;
78 } );