X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fmanipulation.js;h=ed049de7cba1e8339e4789acba8950842c1f1121;hb=98c7248518f9a2082ccf50240b5ab44bf98d7b5e;hp=17cfa842dc6d9f48ac389cdd5701233e728aa214;hpb=1a4d1904ae8631f94b7400d99af24d3fe2f33ecd;p=jquery.git diff --git a/src/manipulation.js b/src/manipulation.js index 17cfa84..ed049de 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -1,12 +1,13 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, rleadingWhitespace = /^\s+/, rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g, - rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i, + rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i, rtagName = /<([\w:]+)/, rtbody = /"; }, @@ -32,9 +33,10 @@ if ( !jQuery.support.htmlSerialize ) { jQuery.fn.extend({ text: function( text ) { - if(jQuery.isFunction(text)) { - return this.each(function() { - return jQuery(this).text( text.call(this) ); + if ( jQuery.isFunction(text) ) { + return this.each(function(i) { + var self = jQuery(this); + self.text( text.call(this, i, self.text()) ); }); } @@ -47,8 +49,8 @@ jQuery.fn.extend({ wrapAll: function( html ) { if ( jQuery.isFunction( html ) ) { - return this.each(function() { - jQuery(this).wrapAll( html.apply(this, arguments) ); + return this.each(function(i) { + jQuery(this).wrapAll( html.call(this, i) ); }); } @@ -60,7 +62,7 @@ jQuery.fn.extend({ wrap.insertBefore( this[0] ); } - wrap.map(function(){ + wrap.map(function() { var elem = this; while ( elem.firstChild && elem.firstChild.nodeType === 1 ) { @@ -75,19 +77,32 @@ jQuery.fn.extend({ }, wrapInner: function( html ) { - return this.each(function(){ - jQuery( this ).contents().wrapAll( html ); + if ( jQuery.isFunction( html ) ) { + return this.each(function(i) { + jQuery(this).wrapInner( html.call(this, i) ); + }); + } + + return this.each(function() { + var self = jQuery( this ), contents = self.contents(); + + if ( contents.length ) { + contents.wrapAll( html ); + + } else { + self.append( html ); + } }); }, wrap: function( html ) { - return this.each(function(){ + return this.each(function() { jQuery( this ).wrapAll( html ); }); }, unwrap: function() { - return this.parent().each(function(){ + return this.parent().each(function() { if ( !jQuery.nodeName( this, "body" ) ) { jQuery( this ).replaceWith( this.childNodes ); } @@ -95,7 +110,7 @@ jQuery.fn.extend({ }, append: function() { - return this.domManip(arguments, true, function(elem){ + return this.domManip(arguments, true, function( elem ) { if ( this.nodeType === 1 ) { this.appendChild( elem ); } @@ -103,7 +118,7 @@ jQuery.fn.extend({ }, prepend: function() { - return this.domManip(arguments, true, function(elem){ + return this.domManip(arguments, true, function( elem ) { if ( this.nodeType === 1 ) { this.insertBefore( elem, this.firstChild ); } @@ -112,7 +127,7 @@ jQuery.fn.extend({ before: function() { if ( this[0] && this[0].parentNode ) { - return this.domManip(arguments, false, function(elem){ + return this.domManip(arguments, false, function( elem ) { this.parentNode.insertBefore( elem, this ); }); } else if ( arguments.length ) { @@ -124,7 +139,7 @@ jQuery.fn.extend({ after: function() { if ( this[0] && this[0].parentNode ) { - return this.domManip(arguments, false, function(elem){ + return this.domManip(arguments, false, function( elem ) { this.parentNode.insertBefore( elem, this.nextSibling ); }); } else if ( arguments.length ) { @@ -133,10 +148,44 @@ jQuery.fn.extend({ return set; } }, + + // keepData is for internal use only--do not document + remove: function( selector, keepData ) { + for ( var i = 0, elem; (elem = this[i]) != null; i++ ) { + if ( !selector || jQuery.filter( selector, [ elem ] ).length ) { + if ( !keepData && elem.nodeType === 1 ) { + jQuery.cleanData( elem.getElementsByTagName("*") ); + jQuery.cleanData( [ elem ] ); + } + + if ( elem.parentNode ) { + elem.parentNode.removeChild( elem ); + } + } + } + + return this; + }, + + empty: function() { + for ( var i = 0, elem; (elem = this[i]) != null; i++ ) { + // Remove element nodes and prevent memory leaks + if ( elem.nodeType === 1 ) { + jQuery.cleanData( elem.getElementsByTagName("*") ); + } + + // Remove any remaining nodes + while ( elem.firstChild ) { + elem.removeChild( elem.firstChild ); + } + } + + return this; + }, clone: function( events ) { // Do the clone - var ret = this.map(function(){ + var ret = this.map(function() { if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) { // IE copies events bound via attachEvent when // using cloneNode. Calling detachEvent on the @@ -172,7 +221,7 @@ jQuery.fn.extend({ html: function( value ) { if ( value === undefined ) { - return this[0] ? + return this[0] && this[0].nodeType === 1 ? this[0].innerHTML.replace(rinlinejQuery, "") : null; @@ -181,11 +230,13 @@ jQuery.fn.extend({ (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) && !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) { + value = value.replace(rxhtmlTag, fcloseTag); + try { for ( var i = 0, l = this.length; i < l; i++ ) { // Remove element nodes and prevent memory leaks if ( this[i].nodeType === 1 ) { - cleanData( this[i].getElementsByTagName("*") ); + jQuery.cleanData( this[i].getElementsByTagName("*") ); this[i].innerHTML = value; } } @@ -195,6 +246,14 @@ jQuery.fn.extend({ this.empty().append( value ); } + } else if ( jQuery.isFunction( value ) ) { + this.each(function(i){ + var self = jQuery(this), old = self.html(); + self.empty().append(function(){ + return value.call( this, i, old ); + }); + }); + } else { this.empty().append( value ); } @@ -204,7 +263,19 @@ jQuery.fn.extend({ replaceWith: function( value ) { if ( this[0] && this[0].parentNode ) { - return this.each(function(){ + // Make sure that the elements are removed from the DOM before they are inserted + // this can help fix replacing a parent with child elements + if ( !jQuery.isFunction( value ) ) { + value = jQuery( value ).detach(); + + } else { + return this.each(function(i) { + var self = jQuery(this), old = self.html(); + self.replaceWith( value.call( this, i, old ) ); + }); + } + + return this.each(function() { var next = this.nextSibling, parent = this.parentNode; jQuery(this).remove(); @@ -227,10 +298,18 @@ jQuery.fn.extend({ domManip: function( args, table, callback ) { var results, first, value = args[0], scripts = []; - if ( jQuery.isFunction(value) ) { + // We can't cloneNode fragments that contain checked, in WebKit + if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) { return this.each(function() { - args[0] = value.call(this); - return jQuery(this).domManip( args, table, callback ); + jQuery(this).domManip( args, table, callback, true ); + }); + } + + if ( jQuery.isFunction(value) ) { + return this.each(function(i) { + var self = jQuery(this); + args[0] = value.call(this, i, table ? self.html() : undefined); + self.domManip( args, table, callback ); }); } @@ -278,26 +357,37 @@ jQuery.fn.extend({ function cloneCopyEvent(orig, ret) { var i = 0; - ret.each(function(){ - if ( this.nodeName !== orig[i].nodeName ) { + ret.each(function() { + if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) { return; } - jQuery.data( this, jQuery.data( orig[i++] ) ); + var oldData = jQuery.data( orig[i++] ), curData = jQuery.data( this, oldData ), events = oldData && oldData.events; + + if ( events ) { + delete curData.handle; + curData.events = {}; + + for ( var type in events ) { + for ( var handler in events[ type ] ) { + jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data ); + } + } + } }); } -function buildFragment(args, nodes, scripts){ - var fragment, cacheable, cached, cacheresults, doc; +function buildFragment( args, nodes, scripts ) { + var fragment, cacheable, cacheresults, doc; - if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf(" or - wrap[1] == "" && !hasBody ? + wrap[1] === "
" && !hasBody ? div.childNodes : []; @@ -458,13 +518,27 @@ jQuery.extend({ } return ret; - } -}); - -function cleanData( elems ) { - for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) { - if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) { - delete jQuery.cache[ id ]; + }, + + cleanData: function( elems ) { + var data, id, cache = jQuery.cache; + + for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { + id = elem[ jQuery.expando ]; + + if ( id ) { + data = cache[ id ]; + + if ( data.events ) { + for ( var event in data.events ) { + removeEvent( elem, event, data.handle ); + } + } + + removeExpando( elem ); + + delete cache[ id ]; + } } } -} +});