X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fmanipulation.js;h=841447fa2efcd801daca97eb4762d851f939e04e;hb=823eecab9073b43a283a2500e9e43c3a06cc2495;hp=9b7cfefb43fd85046b88d12980ddc951a1fd6fda;hpb=7481a3645af63cef1406687190fd62bdfb1bf254;p=jquery.git diff --git a/src/manipulation.js b/src/manipulation.js index 9b7cfef..841447f 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -7,7 +7,7 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, rtbody = /
", "" ], @@ -183,51 +183,13 @@ jQuery.fn.extend({ return this; }, - clone: function( events ) { - // Do the clone - var ret = this.map(function() { - var clone = this.cloneNode(true); - if ( !jQuery.support.noCloneEvent && (this.nodeType === 1 || this.nodeType === 11) && !jQuery.isXMLDoc(this) ) { - // IE copies events bound via attachEvent when using cloneNode. - // Calling detachEvent on the clone will also remove the events - // from the original. In order to get around this, we use some - // proprietary methods to clear the events. Thanks to MooTools - // guys for this hotness. - var srcElements = jQuery(this).find('*').andSelf(); - jQuery(clone).find('*').andSelf().each(function (i, clone) { - // We do not need to do anything for non-Elements - if (this.nodeType !== 1) { - return; - } - - // clearAttributes removes the attributes, but also - // removes the attachEvent events - clone.clearAttributes(); - - // mergeAttributes only merges back on the original attributes, - // not the events - clone.mergeAttributes(srcElements[i]); - - // IE6-8 fail to clone children inside object elements that use - // the proprietary classid attribute value (rather than the type - // attribute) to identify the type of content to display - if (clone.nodeName.toLowerCase() === 'object') { - clone.outerHTML = srcElements[i].outerHTML; - } - }); - } + clone: function( dataAndEvents, deepDataAndEvents ) { + dataAndEvents = dataAndEvents == null ? false : dataAndEvents; + deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents; - return clone; + return this.map( function () { + return jQuery.clone( this, dataAndEvents, deepDataAndEvents ); }); - - // Copy the events from the original to the clone - if ( events === true ) { - cloneCopyEvent( this, ret ); - cloneCopyEvent( this.find("*"), ret.find("*") ); - } - - // Return the cloned set - return ret; }, html: function( value ) { @@ -349,13 +311,20 @@ jQuery.fn.extend({ if ( first ) { table = table && jQuery.nodeName( first, "tr" ); - for ( var i = 0, l = this.length; i < l; i++ ) { + for ( var i = 0, l = this.length, lastIndex = l - 1; i < l; i++ ) { callback.call( table ? root(this[i], first) : this[i], - i > 0 || results.cacheable || this.length > 1 ? - jQuery(fragment).clone(true)[0] : + // Make sure that we do not leak memory by inadvertently discarding + // the original fragment (which might have attached data) instead of + // using it; in addition, use the original fragment object for the last + // item instead of first because it can end up being emptied incorrectly + // in certain situations (Bug #8070). + // Fragments from the fragment cache must always be cloned and never used + // in place. + results.cacheable || (l > 1 && i < lastIndex) ? + jQuery.clone( fragment, true, true ) : fragment ); } @@ -377,41 +346,97 @@ function root( elem, cur ) { elem; } -function cloneCopyEvent(orig, ret) { - var i = 0; +function cloneCopyEvent( src, dest ) { - ret.each(function() { - if ( this.nodeType !== 1 || this.nodeName !== (orig[i] && orig[i].nodeName) ) { - return; - } + if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) { + return; + } + + var internalKey = jQuery.expando, + oldData = jQuery.data( src ), + curData = jQuery.data( dest, oldData ); - var oldData = jQuery.data( orig[i++] ), - curData = jQuery.data( this, oldData ), - events = oldData && oldData.events; + // Switch to use the internal data object, if it exists, for the next + // stage of data copying + if ( (oldData = oldData[ internalKey ]) ) { + var events = oldData.events; + curData = curData[ internalKey ] = jQuery.extend({}, oldData); 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 ); + for ( var i = 0, l = events[ type ].length; i < l; i++ ) { + jQuery.event.add( dest, type, events[ type ][ i ], events[ type ][ i ].data ); } } } - }); + } +} + +function cloneFixAttributes(src, dest) { + // We do not need to do anything for non-Elements + if ( dest.nodeType !== 1 ) { + return; + } + + var nodeName = dest.nodeName.toLowerCase(); + + // clearAttributes removes the attributes, which we don't want, + // but also removes the attachEvent events, which we *do* want + dest.clearAttributes(); + + // mergeAttributes, in contrast, only merges back on the + // original attributes, not the events + dest.mergeAttributes(src); + + // IE6-8 fail to clone children inside object elements that use + // the proprietary classid attribute value (rather than the type + // attribute) to identify the type of content to display + if ( nodeName === "object" ) { + dest.outerHTML = src.outerHTML; + + } else if ( nodeName === "input" && (src.type === "checkbox" || src.type === "radio") ) { + // IE6-8 fails to persist the checked state of a cloned checkbox + // or radio button. Worse, IE6-7 fail to give the cloned element + // a checked appearance if the defaultChecked value isn't also set + if ( src.checked ) { + dest.defaultChecked = dest.checked = src.checked; + } + + // IE6-7 get confused and end up setting the value of a cloned + // checkbox/radio button to an empty string instead of "on" + if ( dest.value !== src.value ) { + dest.value = src.value; + } + + // IE6-8 fails to return the selected option to the default selected + // state when cloning options + } else if ( nodeName === "option" ) { + dest.selected = src.defaultSelected; + + // IE6-8 fails to set the defaultValue to the correct value when + // cloning other types of input fields + } else if ( nodeName === "input" || nodeName === "textarea" ) { + dest.defaultValue = src.defaultValue; + } + + // Event data gets referenced instead of copied if the expando + // gets copied too + dest.removeAttribute( jQuery.expando ); } jQuery.buildFragment = function( args, nodes, scripts ) { var fragment, cacheable, cacheresults, doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document); - // Only cache "small" (1/2 KB) strings that are associated with the main document + // Only cache "small" (1/2 KB) HTML strings that are associated with the main document // Cloning options loses the selected state, so don't cache them // IE 6 doesn't like it when you put