X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=inline;f=src%2Fmanipulation.js;h=596a457368fed08cce5294f904854cc3f564d795;hb=ea5ce8c2a916ad44af21411d6e2b21ab9f6863f0;hp=eea377b7eb6c5f20e5fa05883c30220d42ecd45f;hpb=bbffc99f7c60d42d4286786dfd6a43aa31d62ae2;p=jquery.git
diff --git a/src/manipulation.js b/src/manipulation.js
index eea377b..596a457 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 = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
+ rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,
rtagName = /<([\w:]+)/,
rtbody = /
" + tag + ">";
- },
+ 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, "" ],
@@ -17,6 +16,7 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
tr: [ 2, "" ],
td: [ 3, "" ],
col: [ 2, "" ],
+ area: [ 1, "" ],
_default: [ 0, "", "" ]
};
@@ -31,40 +31,37 @@ if ( !jQuery.support.htmlSerialize ) {
jQuery.fn.extend({
text: function( text ) {
- if ( typeof text !== "object" && text !== undefined )
- return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
+ if ( jQuery.isFunction(text) ) {
+ return this.each(function(i) {
+ var self = jQuery( this );
- var ret = "";
-
- jQuery.each( text || this, function(){
- jQuery.each( this.childNodes, function(){
- if ( this.nodeType !== 8 ) {
- ret += this.nodeType !== 1 ?
- this.nodeValue :
- jQuery.fn.text( [ this ] );
- }
+ self.text( text.call(this, i, self.text()) );
});
- });
+ }
- return ret;
+ if ( typeof text !== "object" && text !== undefined ) {
+ return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
+ }
+
+ 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) );
});
}
if ( this[0] ) {
// The elements to wrap the target around
- var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
+ var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
if ( this[0].parentNode ) {
wrap.insertBefore( this[0] );
}
- wrap.map(function(){
+ wrap.map(function() {
var elem = this;
while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
@@ -79,27 +76,41 @@ 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 );
}
}).end();
},
-
+
append: function() {
- return this.domManip(arguments, true, function(elem){
+ return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 ) {
this.appendChild( elem );
}
@@ -107,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 );
}
@@ -116,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 ) {
@@ -128,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 ) {
@@ -138,49 +149,73 @@ jQuery.fn.extend({
}
},
- 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;
+ // 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 ] );
}
- return jQuery.clean([html.replace(rinlinejQuery, "")
- .replace(rleadingWhitespace, "")], ownerDocument)[0];
- } else {
- return this.cloneNode(true);
+ if ( elem.parentNode ) {
+ elem.parentNode.removeChild( elem );
+ }
}
- });
+ }
- // Copy the events from the original to the clone
- if ( events === true ) {
- var orig = this.find("*").andSelf(), i = 0;
+ return this;
+ },
- ret.find("*").andSelf().each(function(){
- if ( this.nodeName !== orig[i].nodeName ) { return; }
+ 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("*") );
+ }
- var events = jQuery.data( orig[i], "events" );
+ // Remove any remaining nodes
+ while ( elem.firstChild ) {
+ elem.removeChild( elem.firstChild );
+ }
+ }
- for ( var type in events ) {
- for ( var handler in events[ type ] ) {
- jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
- }
+ 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.
+
+ // 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] );
}
- i++;
- });
+ cloneFixAttributes( this, clone );
+ }
+
+ return clone;
+ });
+
+ // Copy the events from the original to the clone
+ if ( events === true ) {
+ cloneCopyEvent( this, ret );
+ cloneCopyEvent( this.find("*"), ret.find("*") );
}
// Return the cloned set
@@ -189,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" && !/