X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fmanipulation.js;h=96caa02d0b2948a14b91343b44b0d8900383c890;hb=69497c3fd7ff560be0e47b4c65076915cca756bc;hp=7500730f9d699b27d63f9f22e0a5462b99b48e5f;hpb=61e37d41490aed319a176c6ca7feb95e5505059c;p=jquery.git diff --git a/src/manipulation.js b/src/manipulation.js index 7500730..96caa02 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -1,15 +1,14 @@ +(function( jQuery ) { + var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, rleadingWhitespace = /^\s+/, - rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g, - rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i, + rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig, rtagName = /<([\w:]+)/, rtbody = /"; - }, + rhtml = /<|&#?\w+;/, + rnocache = /<(?:script|object|embed|option|style)/i, + // checked="checked" or checked (html5) + rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, wrapMap = { option: [ 1, "" ], legend: [ 1, "
", "
" ], @@ -33,8 +32,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) ); + return this.each(function(i) { + var self = jQuery( this ); + + self.text( text.call(this, i, self.text()) ); }); } @@ -42,13 +43,13 @@ jQuery.fn.extend({ return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) ); } - return jQuery.getText( this ); + return jQuery.text( this ); }, 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) ); }); } @@ -75,8 +76,22 @@ jQuery.fn.extend({ }, wrapInner: function( html ) { + if ( jQuery.isFunction( html ) ) { + return this.each(function(i) { + jQuery(this).wrapInner( html.call(this, i) ); + }); + } + return this.each(function() { - jQuery( this ).contents().wrapAll( html ); + var self = jQuery( this ), + contents = self.contents(); + + if ( contents.length ) { + contents.wrapAll( html ); + + } else { + self.append( html ); + } }); }, @@ -134,30 +149,67 @@ jQuery.fn.extend({ } }, + // 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() { - if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) { - // IE copies events bound via attachEvent when - // using cloneNode. Calling detachEvent on the - // clone will also remove the events from the orignal - // In order to get around this, we use innerHTML. - // Unfortunately, this means some modifications to - // attributes in IE that are actually only stored - // as properties will not be copied (such as the - // the name attribute on an input). - var html = this.outerHTML, ownerDocument = this.ownerDocument; - if ( !html ) { - var div = ownerDocument.createElement("div"); - div.appendChild( this.cloneNode(true) ); - html = div.innerHTML; + 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. + + // Using Sizzle here is crazy slow, so we use getElementsByTagName + // instead + var srcElements = this.getElementsByTagName("*"), + destElements = clone.getElementsByTagName("*"); + + // Weird iteration because IE will replace the length property + // with an element if you are cloning the body and one of the + // elements on the page has a name or id of "length" + for ( var i = 0; srcElements[i]; ++i ) { + cloneFixAttributes( srcElements[i], destElements[i] ); } - return jQuery.clean([html.replace(rinlinejQuery, "") - .replace(rleadingWhitespace, "")], ownerDocument)[0]; - } else { - return this.cloneNode(true); + cloneFixAttributes( this, clone ); } + + return clone; }); // Copy the events from the original to the clone @@ -172,20 +224,22 @@ 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; // See if we can take a shortcut and just use innerHTML - } else if ( typeof value === "string" && !/