Offset: allow offset setter to throw for disconnected elements
[jquery.git] / src / wrap.js
blob0d9bf0f2ed646c1c2379afea502bcd6289e9a65a
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( selector ) {
67                 this.parent( selector ).not( "body" ).each(function() {
68                         jQuery( this ).replaceWith( this.childNodes );
69                 });
70                 return this;
71         }
72 });
74 return jQuery;
75 });