wrap.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. define( [
  2. "./core",
  3. "./core/init",
  4. "./manipulation", // clone
  5. "./traversing" // parent, contents
  6. ], function( jQuery ) {
  7. "use strict";
  8. jQuery.fn.extend( {
  9. wrapAll: function( html ) {
  10. var wrap;
  11. if ( this[ 0 ] ) {
  12. if ( jQuery.isFunction( html ) ) {
  13. html = html.call( this[ 0 ] );
  14. }
  15. // The elements to wrap the target around
  16. wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );
  17. if ( this[ 0 ].parentNode ) {
  18. wrap.insertBefore( this[ 0 ] );
  19. }
  20. wrap.map( function() {
  21. var elem = this;
  22. while ( elem.firstElementChild ) {
  23. elem = elem.firstElementChild;
  24. }
  25. return elem;
  26. } ).append( this );
  27. }
  28. return this;
  29. },
  30. wrapInner: function( html ) {
  31. if ( jQuery.isFunction( html ) ) {
  32. return this.each( function( i ) {
  33. jQuery( this ).wrapInner( html.call( this, i ) );
  34. } );
  35. }
  36. return this.each( function() {
  37. var self = jQuery( this ),
  38. contents = self.contents();
  39. if ( contents.length ) {
  40. contents.wrapAll( html );
  41. } else {
  42. self.append( html );
  43. }
  44. } );
  45. },
  46. wrap: function( html ) {
  47. var isFunction = jQuery.isFunction( html );
  48. return this.each( function( i ) {
  49. jQuery( this ).wrapAll( isFunction ? html.call( this, i ) : html );
  50. } );
  51. },
  52. unwrap: function( selector ) {
  53. this.parent( selector ).not( "body" ).each( function() {
  54. jQuery( this ).replaceWith( this.childNodes );
  55. } );
  56. return this;
  57. }
  58. } );
  59. return jQuery;
  60. } );