Manipulation: Support $el.html(selfRemovingScript) (#5378)
[jquery.git] / src / wrap.js
blob4a37002a721b3f8f528038597091373323a1ad8f
1 import { jQuery } from "./core.js";
3 import "./core/init.js";
4 import "./manipulation.js"; // clone
5 import "./traversing.js"; // parent, contents
7 jQuery.fn.extend( {
8         wrapAll: function( html ) {
9                 var wrap;
11                 if ( this[ 0 ] ) {
12                         if ( typeof html === "function" ) {
13                                 html = html.call( this[ 0 ] );
14                         }
16                         // The elements to wrap the target around
17                         wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
19                         if ( this[ 0 ].parentNode ) {
20                                 wrap.insertBefore( this[ 0 ] );
21                         }
23                         wrap.map( function() {
24                                 var elem = this;
26                                 while ( elem.firstElementChild ) {
27                                         elem = elem.firstElementChild;
28                                 }
30                                 return elem;
31                         } ).append( this );
32                 }
34                 return this;
35         },
37         wrapInner: function( html ) {
38                 if ( typeof html === "function" ) {
39                         return this.each( function( i ) {
40                                 jQuery( this ).wrapInner( html.call( this, i ) );
41                         } );
42                 }
44                 return this.each( function() {
45                         var self = jQuery( this ),
46                                 contents = self.contents();
48                         if ( contents.length ) {
49                                 contents.wrapAll( html );
51                         } else {
52                                 self.append( html );
53                         }
54                 } );
55         },
57         wrap: function( html ) {
58                 var htmlIsFunction = typeof html === "function";
60                 return this.each( function( i ) {
61                         jQuery( this ).wrapAll( htmlIsFunction ? html.call( this, i ) : html );
62                 } );
63         },
65         unwrap: function( selector ) {
66                 this.parent( selector ).not( "body" ).each( function() {
67                         jQuery( this ).replaceWith( this.childNodes );
68                 } );
69                 return this;
70         }
71 } );
73 export { jQuery, jQuery as $ };