jquery/src/manipulation.js

664 lines
17 KiB
JavaScript
Raw Normal View History

2012-12-19 16:27:42 +00:00
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,
rtagName = /<([\w:]+)/,
rhtml = /<|&#?\w+;/,
rnoInnerhtml = /<(?:script|style|link)/i,
manipulation_rcheckableType = /^(?:checkbox|radio)$/i,
// checked="checked" or checked
rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
rscriptType = /^$|\/(?:java|ecma)script/i,
rscriptTypeMasked = /^true\/(.*)/,
rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g,
wrapMap = {
option: [ 1, "<select multiple='multiple'>" ],
legend: [ 1, "<fieldset>" ],
area: [ 1, "<map>" ],
param: [ 1, "<object>" ],
thead: [ 1, "<table>" ],
tr: [ 2, "<table><tbody>" ],
col: [ 2, "<table><tbody></tbody><colgroup>", "</table>" ],
td: [ 3, "<table><tbody><tr>" ],
2012-12-19 16:26:49 +00:00
_default: [ 0, "" ]
};
wrapMap.optgroup = wrapMap.option;
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
wrapMap.th = wrapMap.td;
jQuery.fn.extend({
text: function( value ) {
return jQuery.access( this, function( value ) {
return value === undefined ?
jQuery.text( this ) :
this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) );
}, null, value, arguments.length );
},
wrapAll: function( html ) {
if ( jQuery.isFunction( html ) ) {
return this.each(function(i) {
jQuery(this).wrapAll( html.call(this, i) );
});
}
2009-07-21 09:17:33 +00:00
if ( this[0] ) {
// The elements to wrap the target around
var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
if ( this[0].parentNode ) {
wrap.insertBefore( this[0] );
}
wrap.map(function() {
var elem = this;
while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
elem = elem.firstChild;
}
return elem;
}).append( this );
}
return this;
},
wrapInner: function( html ) {
if ( jQuery.isFunction( html ) ) {
2012-12-19 18:33:55 +00:00
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 ) {
var isFunction = jQuery.isFunction( html );
2012-12-19 18:33:55 +00:00
return this.each(function( i ) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
});
},
unwrap: 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 ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.appendChild( elem );
}
});
},
prepend: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.insertBefore( elem, this.firstChild );
}
});
},
before: function() {
return this.domManip( arguments, false, function( elem ) {
if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this );
}
});
},
after: function() {
return this.domManip( arguments, false, function( elem ) {
if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this.nextSibling );
}
});
},
// keepData is for internal use only--do not document
remove: function( selector, keepData ) {
2012-12-19 18:33:55 +00:00
var elem, i = 0;
2012-12-19 18:33:55 +00:00
for ( ; ( elem = this[ i ] ) != null; i++ ) {
if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) {
if ( !keepData && elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem ) );
}
if ( elem.parentNode ) {
if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
setGlobalEval( getAll( elem, "script" ) );
}
elem.parentNode.removeChild( elem );
}
}
}
return this;
},
empty: function() {
var elem,
i = 0;
2012-12-19 18:42:07 +00:00
for ( ; ( elem = this[ i ] ) != null; i++ ) {
if ( elem.nodeType === 1 ) {
2012-12-19 18:42:07 +00:00
// Remove element nodes and prevent memory leaks
jQuery.cleanData( getAll( elem, false ) );
2012-12-19 18:42:07 +00:00
// Remove any remaining nodes
elem.textContent = "";
}
}
return this;
},
2011-01-20 20:25:04 +00:00
clone: function( dataAndEvents, deepDataAndEvents ) {
dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
2011-01-20 20:25:04 +00:00
deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
2011-01-20 19:25:56 +00:00
return this.map( function () {
2011-01-20 20:25:04 +00:00
return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
});
},
html: function( value ) {
return jQuery.access( this, function( value ) {
var elem = this[0] || {},
i = 0,
l = this.length;
if ( value === undefined ) {
2012-12-19 16:26:49 +00:00
return elem.nodeType === 1 ? elem.innerHTML: undefined;
}
// See if we can take a shortcut and just use innerHTML
if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
2012-12-19 18:59:16 +00:00
!wrapMap[ ( rtagName.exec( value ) || ["", ""] )[ 1 ].toLowerCase() ] ) {
value = value.replace( rxhtmlTag, "<$1></$2>" );
try {
for (; i < l; i++ ) {
2012-12-19 18:59:16 +00:00
elem = this[ i ] || {};
if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) );
elem.innerHTML = value;
}
}
elem = 0;
// If using innerHTML throws an exception, use the fallback method
} catch( e ) {}
}
if ( elem ) {
this.empty().append( value );
}
}, null, value, arguments.length );
},
replaceWith: function( value ) {
var isFunction = jQuery.isFunction( value );
// 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 ( !isFunction && typeof value !== "string" ) {
value = jQuery( value ).not( this ).detach();
}
return this.domManip( [ value ], true, function( elem ) {
var next = this.nextSibling,
parent = this.parentNode;
if ( parent && this.nodeType === 1 || this.nodeType === 11 ) {
jQuery( this ).remove();
if ( next ) {
next.parentNode.insertBefore( elem, next );
} else {
parent.appendChild( elem );
}
}
});
},
detach: function( selector ) {
return this.remove( selector, true );
},
domManip: function( args, table, callback ) {
// Flatten any nested arrays
args = core_concat.apply( [], args );
2012-12-13 17:40:55 +00:00
var fragment, first, scripts, hasScripts, node, doc,
i = 0,
l = this.length,
set = this,
2012-12-13 17:40:55 +00:00
iNoClone = l - 1,
value = args[0],
isFunction = jQuery.isFunction( value );
// We can't cloneNode fragments that contain checked, in WebKit
if ( isFunction || !( l <= 1 || typeof value !== "string" || jQuery.support.checkClone || !rchecked.test( value ) ) ) {
return this.each(function( index ) {
var self = set.eq( index );
if ( isFunction ) {
args[0] = value.call( this, index, table ? self.html() : undefined );
}
self.domManip( args, table, callback );
});
}
if ( this[0] ) {
doc = this[0].ownerDocument;
fragment = doc.createDocumentFragment();
jQuery.clean( args, doc, fragment, undefined, this );
first = fragment.firstChild;
if ( fragment.childNodes.length === 1 ) {
fragment = first;
}
if ( first ) {
table = table && jQuery.nodeName( first, "tr" );
scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
hasScripts = scripts.length;
2009-03-23 01:55:17 +00:00
// Use the original fragment for the last item instead of the first because it can end up
// being emptied incorrectly in certain situations (#8070).
2012-12-13 17:40:55 +00:00
for ( ; i < l; i++ ) {
node = fragment;
if ( i !== iNoClone ) {
node = jQuery.clone( node, true, true );
// Keep references to cloned scripts for later restoration
if ( hasScripts ) {
jQuery.merge( scripts, getAll( node, "script" ) );
}
}
callback.call(
2012-12-19 18:59:16 +00:00
table && jQuery.nodeName( this[ i ], "table" ) ?
findOrAppend( this[ i ], "tbody" ) :
this[ i ],
node,
i
);
}
if ( hasScripts ) {
doc = scripts[ scripts.length - 1 ].ownerDocument;
// Reenable scripts
jQuery.map( scripts, restoreScript );
// Evaluate executable scripts on first document insertion
for ( i = 0; i < hasScripts; i++ ) {
node = scripts[ i ];
if ( rscriptType.test( node.type || "" ) &&
!jQuery._data( node, "globalEval" ) && jQuery.contains( doc, node ) ) {
if ( node.src ) {
// Hope ajax is available...
jQuery.ajax({
url: node.src,
dataType: "script",
async: false,
global: false,
"throws": true
});
} else {
jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) );
}
}
}
}
}
}
return this;
}
});
function findOrAppend( elem, tag ) {
return elem.getElementsByTagName( tag )[0] || elem.appendChild( elem.ownerDocument.createElement( tag ) );
}
// Replace/restore the type attribute of script elements for safe DOM manipulation
function disableScript( elem ) {
var attr = elem.getAttributeNode("type");
elem.type = ( attr && attr.specified ) + "/" + elem.type;
return elem;
}
function restoreScript( elem ) {
var match = rscriptTypeMasked.exec( elem.type );
if ( match ) {
2012-12-19 18:59:16 +00:00
elem.type = match[ 1 ];
} else {
elem.removeAttribute("type");
}
return elem;
}
// Mark scripts as having already been evaluated
function setGlobalEval( elems, refElements ) {
var elem,
i = 0;
2012-12-19 18:59:16 +00:00
for ( ; (elem = elems[ i ]) != null; i++ ) {
jQuery._data( elem, "globalEval", !refElements || jQuery._data( refElements[ i ], "globalEval" ) );
}
}
2011-01-20 19:25:56 +00:00
function cloneCopyEvent( src, dest ) {
2011-01-20 19:25:56 +00:00
if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
return;
}
var type, i, l,
oldData = jQuery._data( src ),
curData = jQuery._data( dest, oldData ),
events = oldData.events;
if ( events ) {
delete curData.handle;
curData.events = {};
for ( type in events ) {
for ( i = 0, l = events[ type ].length; i < l; i++ ) {
jQuery.event.add( dest, type, events[ type ][ i ] );
}
}
2011-01-20 19:25:56 +00:00
}
// make the cloned public data object a copy from the original
if ( curData.data ) {
curData.data = jQuery.extend( {}, curData.data );
}
}
function fixCloneNodeIssues( src, dest ) {
2012-12-19 20:52:39 +00:00
var nodeName;
// We do not need to do anything for non-Elements
if ( dest.nodeType !== 1 ) {
return;
}
nodeName = dest.nodeName.toLowerCase();
2012-12-19 20:52:39 +00:00
if ( nodeName === "object" ) {
if ( dest.parentNode ) {
dest.outerHTML = src.outerHTML;
}
// This path appears unavoidable for IE9. When cloning an object
// element in IE9, the outerHTML strategy above is not sufficient.
// If the src has innerHTML and the destination does not,
// copy the src.innerHTML into the dest.innerHTML. #10324
2012-12-19 20:52:39 +00:00
if ( src.innerHTML && !jQuery.trim( dest.innerHTML ) ) {
dest.innerHTML = src.innerHTML;
}
2012-12-19 20:52:39 +00:00
// IE9-10 fails to persist the checked state of a cloned checkbox or radio button.
} else if ( nodeName === "input" && manipulation_rcheckableType.test( src.type ) ) {
2012-12-19 20:52:39 +00:00
dest.checked = src.checked;
2012-12-19 20:52:39 +00:00
// IE9-10 fails to return the selected option to the default selected
// state when cloning options
} else if ( nodeName === "input" || nodeName === "textarea" ) {
dest.defaultValue = src.defaultValue;
}
}
jQuery.each({
appendTo: "append",
prependTo: "prepend",
insertBefore: "before",
insertAfter: "after",
replaceAll: "replaceWith"
}, function( name, original ) {
jQuery.fn[ name ] = function( selector ) {
var elems,
i = 0,
ret = [],
insert = jQuery( selector ),
last = insert.length - 1;
2012-12-13 17:40:55 +00:00
for ( ; i <= last; i++ ) {
2012-12-19 21:08:24 +00:00
elems = i === last ? this : this.clone( true );
2012-12-19 18:59:16 +00:00
jQuery( insert[ i ] )[ original ]( elems );
2012-12-13 17:40:55 +00:00
2012-12-19 21:08:24 +00:00
core_push.apply( ret, elems );
2012-12-13 17:40:55 +00:00
}
2012-12-13 17:40:55 +00:00
return this.pushStack( ret );
};
});
function getAll( context, tag ) {
var elems, elem,
i = 0,
2012-12-19 21:08:24 +00:00
ret = typeof context.getElementsByTagName !== "undefined" ? context.getElementsByTagName( tag || "*" ) :
typeof context.querySelectorAll !== "undefined" ? context.querySelectorAll( tag || "*" ) :
undefined;
2012-12-19 21:08:24 +00:00
if ( !ret ) {
for ( ret = [], elems = context.childNodes || context; (elem = elems[ i ]) != null; i++ ) {
2012-12-19 21:10:20 +00:00
core_push.apply( ret, !tag || jQuery.nodeName( elem, tag ) ?
2012-12-19 21:08:24 +00:00
getAll( elem, tag ) :
elems );
}
}
return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
2012-12-19 21:08:24 +00:00
jQuery.merge( [ context ], ret ) :
ret;
}
jQuery.extend({
2011-01-20 20:25:04 +00:00
clone: function( elem, dataAndEvents, deepDataAndEvents ) {
2012-12-19 16:26:49 +00:00
var destElements, srcElements, node, i,
inPage = jQuery.contains( elem.ownerDocument, elem ),
clone = elem.cloneNode( true );
// IE<=8 does not properly clone detached, unknown element nodes
2012-12-19 20:52:39 +00:00
if ( !jQuery.support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) && !jQuery.isXMLDoc( elem ) ) {
2011-01-20 19:25:56 +00:00
// We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
destElements = getAll( clone );
srcElements = getAll( elem );
2011-01-20 19:25:56 +00:00
2012-12-19 20:52:39 +00:00
// Fix IE cloning issues
2012-12-19 18:59:16 +00:00
for ( i = 0; (node = srcElements[ i ]) != null; ++i ) {
// Ensure that the destination node is not null; Fixes #9587
2012-12-19 18:59:16 +00:00
if ( destElements[ i ] ) {
fixCloneNodeIssues( node, destElements[ i ] );
}
2011-01-20 19:25:56 +00:00
}
}
// Copy the events from the original to the clone
2011-01-20 20:25:04 +00:00
if ( dataAndEvents ) {
if ( deepDataAndEvents ) {
srcElements = srcElements || getAll( elem );
destElements = destElements || getAll( clone );
2011-01-20 19:25:56 +00:00
2012-12-19 18:59:16 +00:00
for ( i = 0; (node = srcElements[ i ]) != null; i++ ) {
cloneCopyEvent( node, destElements[ i ] );
2011-01-20 19:25:56 +00:00
}
} else {
cloneCopyEvent( elem, clone );
2011-01-20 19:25:56 +00:00
}
}
// Preserve script evaluation history
destElements = getAll( clone, "script" );
if ( destElements.length > 0 ) {
setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
}
2011-01-20 19:25:56 +00:00
// Return the cloned set
return clone;
},
clean: function( elems, context, fragment, scripts, selection ) {
2012-12-19 16:34:08 +00:00
var elem, i, j, tmp, tag, wrap,
ret = [],
2012-12-19 16:26:49 +00:00
container = context === document && fragment;
// Ensure that context is a document
if ( !context || typeof context.createDocumentFragment === "undefined" ) {
context = document;
}
2012-12-19 18:59:16 +00:00
for ( i = 0; (elem = elems[ i ]) != null; i++ ) {
if ( elem || elem === 0 ) {
// Add nodes directly
2012-11-19 22:17:48 +00:00
if ( jQuery.type( elem ) === "object" ) {
jQuery.merge( ret, elem.nodeType ? [ elem ] : elem );
// Convert non-html into a text node
} else if ( !rhtml.test( elem ) ) {
ret.push( context.createTextNode( elem ) );
// Convert html into DOM nodes
} else {
// Ensure a safe container
2012-12-19 16:26:49 +00:00
container = container || context.createDocumentFragment();
tmp = tmp || container.appendChild( context.createElement("div") );
// Deserialize a standard representation
2012-12-19 18:59:16 +00:00
tag = ( rtagName.exec( elem ) || ["", ""] )[ 1 ].toLowerCase();
wrap = wrapMap[ tag ] || wrapMap._default;
2012-12-19 18:59:16 +00:00
tmp.innerHTML = wrap[ 1 ] + elem.replace( rxhtmlTag, "<$1></$2>" ) + ( wrap[ 2 ] || "" );
// Descend through wrappers to the right content
j = wrap[0];
while ( j-- ) {
tmp = tmp.lastChild;
}
jQuery.merge( ret, tmp.childNodes );
// Fix #12392 for WebKit and IE > 9
tmp.textContent = "";
// Remember the top-level container for proper cleanup
2012-12-19 16:26:49 +00:00
tmp = container.lastChild;
}
}
}
2012-12-19 16:26:49 +00:00
// Fix #11356: Clear elements from fragment
if ( tmp ) {
2012-12-19 16:26:49 +00:00
container.removeChild( tmp );
}
2009-03-23 01:55:17 +00:00
if ( fragment ) {
2012-12-19 18:59:16 +00:00
for ( i = 0; (elem = ret[ i ]) != null; i++ ) {
2012-12-19 16:26:49 +00:00
container = jQuery.contains( elem.ownerDocument, elem );
// Append to fragment
// #4087 - If origin and destination elements are the same, and this is
// that element, do not append to fragment
2012-12-13 17:40:55 +00:00
if ( !selection || jQuery.inArray( elem, selection ) === -1 ) {
fragment.appendChild( elem );
}
tmp = getAll( elem, "script" );
// Preserve script evaluation history
2012-12-19 16:26:49 +00:00
if ( container ) {
setGlobalEval( tmp );
}
// Capture executables
if ( scripts ) {
2012-12-19 18:59:16 +00:00
for ( j = 0; (elem = tmp[ j ]) != null; j++ ) {
if ( rscriptType.test( elem.type || "" ) ) {
scripts.push( elem );
}
}
}
}
}
return ret;
},
cleanData: function( elems, /* internal */ acceptData ) {
var data, id, elem, type,
i = 0,
internalKey = jQuery.expando,
cache = jQuery.cache,
deleteExpando = jQuery.support.deleteExpando,
special = jQuery.event.special;
2012-12-19 18:59:16 +00:00
for ( ; (elem = elems[ i ]) != null; i++ ) {
if ( acceptData || jQuery.acceptData( elem ) ) {
id = elem[ internalKey ];
data = id && cache[ id ];
if ( data ) {
if ( data.events ) {
for ( type in data.events ) {
if ( special[ type ] ) {
jQuery.event.remove( elem, type );
// This is a shortcut to avoid jQuery.event.remove's overhead
} else {
jQuery.removeEvent( elem, type, data.handle );
}
}
}
// Remove cache only if it was not already removed by jQuery.event.remove
if ( cache[ id ] ) {
delete cache[ id ];
// IE does not allow us to delete expando properties from nodes,
// nor does it have a removeAttribute function on Document nodes;
// we must handle all of these cases
if ( deleteExpando ) {
delete elem[ internalKey ];
} else if ( elem.removeAttribute ) {
elem.removeAttribute( internalKey );
} else {
elem[ internalKey ] = null;
}
core_deletedIds.push( id );
}
}
}
}
}
});