6 "./manipulation/var/rcheckableType",
7 "./manipulation/support",
16 ], function( jQuery, concat, push, access, rcheckableType, support, dataPriv, dataUser ) {
19 rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:-]+)[^>]*)\/>/gi,
20 rtagName = /<([\w:-]+)/,
22 rnoInnerhtml = /<(?:script|style|link)/i,
23 // checked="checked" or checked
24 rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
25 rscriptType = /^$|\/(?:java|ecma)script/i,
26 rscriptTypeMasked = /^true\/(.*)/,
27 rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g,
29 // We have to close these tags to support XHTML (#13200)
33 option: [ 1, "<select multiple='multiple'>", "</select>" ],
35 thead: [ 1, "<table>", "</table>" ],
36 col: [ 2, "<table><colgroup>", "</colgroup></table>" ],
37 tr: [ 2, "<table><tbody>", "</tbody></table>" ],
38 td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
40 _default: [ 0, "", "" ]
44 wrapMap.optgroup = wrapMap.option;
46 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
47 wrapMap.th = wrapMap.td;
49 // Manipulating tables requires a tbody
50 function manipulationTarget( elem, content ) {
51 return jQuery.nodeName( elem, "table" ) &&
52 jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ?
54 elem.getElementsByTagName("tbody")[0] ||
55 elem.appendChild( elem.ownerDocument.createElement("tbody") ) :
59 // Replace/restore the type attribute of script elements for safe DOM manipulation
60 function disableScript( elem ) {
61 elem.type = (elem.getAttribute("type") !== null) + "/" + elem.type;
64 function restoreScript( elem ) {
65 var match = rscriptTypeMasked.exec( elem.type );
68 elem.type = match[ 1 ];
70 elem.removeAttribute("type");
76 // Mark scripts as having already been evaluated
77 function setGlobalEval( elems, refElements ) {
81 for ( ; i < l; i++ ) {
83 elems[ i ], "globalEval", !refElements || dataPriv.get( refElements[ i ], "globalEval" )
88 function cloneCopyEvent( src, dest ) {
89 var i, l, type, pdataOld, pdataCur, udataOld, udataCur, events;
91 if ( dest.nodeType !== 1 ) {
95 // 1. Copy private data: events, handlers, etc.
96 if ( dataPriv.hasData( src ) ) {
97 pdataOld = dataPriv.access( src );
98 pdataCur = dataPriv.set( dest, pdataOld );
99 events = pdataOld.events;
102 delete pdataCur.handle;
103 pdataCur.events = {};
105 for ( type in events ) {
106 for ( i = 0, l = events[ type ].length; i < l; i++ ) {
107 jQuery.event.add( dest, type, events[ type ][ i ] );
114 if ( dataUser.hasData( src ) ) {
115 udataOld = dataUser.access( src );
116 udataCur = jQuery.extend( {}, udataOld );
118 dataUser.set( dest, udataCur );
122 function getAll( context, tag ) {
124 // Use typeof to avoid zero-argument method invocation on host objects (#15151)
125 var ret = typeof context.getElementsByTagName !== "undefined" ?
126 context.getElementsByTagName( tag || "*" ) :
127 typeof context.querySelectorAll !== "undefined" ?
128 context.querySelectorAll( tag || "*" ) :
131 return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
132 jQuery.merge( [ context ], ret ) :
136 // Fix IE bugs, see support tests
137 function fixInput( src, dest ) {
138 var nodeName = dest.nodeName.toLowerCase();
140 // Fails to persist the checked state of a cloned checkbox or radio button.
141 if ( nodeName === "input" && rcheckableType.test( src.type ) ) {
142 dest.checked = src.checked;
144 // Fails to return the selected option to the default selected state when cloning options
145 } else if ( nodeName === "input" || nodeName === "textarea" ) {
146 dest.defaultValue = src.defaultValue;
151 clone: function( elem, dataAndEvents, deepDataAndEvents ) {
152 var i, l, srcElements, destElements,
153 clone = elem.cloneNode( true ),
154 inPage = jQuery.contains( elem.ownerDocument, elem );
156 // Fix IE cloning issues
157 if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
158 !jQuery.isXMLDoc( elem ) ) {
160 // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
161 destElements = getAll( clone );
162 srcElements = getAll( elem );
164 for ( i = 0, l = srcElements.length; i < l; i++ ) {
165 fixInput( srcElements[ i ], destElements[ i ] );
169 // Copy the events from the original to the clone
170 if ( dataAndEvents ) {
171 if ( deepDataAndEvents ) {
172 srcElements = srcElements || getAll( elem );
173 destElements = destElements || getAll( clone );
175 for ( i = 0, l = srcElements.length; i < l; i++ ) {
176 cloneCopyEvent( srcElements[ i ], destElements[ i ] );
179 cloneCopyEvent( elem, clone );
183 // Preserve script evaluation history
184 destElements = getAll( clone, "script" );
185 if ( destElements.length > 0 ) {
186 setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
189 // Return the cloned set
193 buildFragment: function( elems, context, scripts, selection ) {
194 var elem, tmp, tag, wrap, contains, j,
195 fragment = context.createDocumentFragment(),
200 for ( ; i < l; i++ ) {
203 if ( elem || elem === 0 ) {
205 // Add nodes directly
206 if ( jQuery.type( elem ) === "object" ) {
207 // Support: Android<4.1, PhantomJS<2
208 // push.apply(_, arraylike) throws on ancient WebKit
209 jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
211 // Convert non-html into a text node
212 } else if ( !rhtml.test( elem ) ) {
213 nodes.push( context.createTextNode( elem ) );
215 // Convert html into DOM nodes
217 tmp = tmp || fragment.appendChild( context.createElement("div") );
219 // Deserialize a standard representation
220 tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
221 wrap = wrapMap[ tag ] || wrapMap._default;
222 tmp.innerHTML = wrap[ 1 ] + elem.replace( rxhtmlTag, "<$1></$2>" ) + wrap[ 2 ];
224 // Descend through wrappers to the right content
230 // Support: Android<4.1, PhantomJS<2
231 // push.apply(_, arraylike) throws on ancient WebKit
232 jQuery.merge( nodes, tmp.childNodes );
234 // Remember the top-level container
235 tmp = fragment.firstChild;
237 // Ensure the created nodes are orphaned (#12392)
238 tmp.textContent = "";
243 // Remove wrapper from fragment
244 fragment.textContent = "";
247 while ( (elem = nodes[ i++ ]) ) {
249 // #4087 - If origin and destination elements are the same, and this is
250 // that element, do not do anything
251 if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
255 contains = jQuery.contains( elem.ownerDocument, elem );
257 // Append to fragment
258 tmp = getAll( fragment.appendChild( elem ), "script" );
260 // Preserve script evaluation history
262 setGlobalEval( tmp );
265 // Capture executables
268 while ( (elem = tmp[ j++ ]) ) {
269 if ( rscriptType.test( elem.type || "" ) ) {
270 scripts.push( elem );
279 cleanData: function( elems ) {
280 var data, elem, type, key,
281 special = jQuery.event.special,
284 for ( ; (elem = elems[ i ]) !== undefined; i++ ) {
285 if ( jQuery.acceptData( elem ) ) {
286 key = elem[ dataPriv.expando ];
288 if ( key && (data = dataPriv.cache[ key ]) ) {
290 for ( type in data.events ) {
291 if ( special[ type ] ) {
292 jQuery.event.remove( elem, type );
294 // This is a shortcut to avoid jQuery.event.remove's overhead
296 jQuery.removeEvent( elem, type, data.handle );
300 if ( dataPriv.cache[ key ] ) {
301 // Discard any remaining `private` data
302 delete dataPriv.cache[ key ];
306 // Discard any remaining `user` data
307 delete dataUser.cache[ elem[ dataUser.expando ] ];
313 text: function( value ) {
314 return access( this, function( value ) {
315 return value === undefined ?
316 jQuery.text( this ) :
317 this.empty().each(function() {
318 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
319 this.textContent = value;
322 }, null, value, arguments.length );
326 return this.domManip( arguments, function( elem ) {
327 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
328 var target = manipulationTarget( this, elem );
329 target.appendChild( elem );
334 prepend: function() {
335 return this.domManip( arguments, function( elem ) {
336 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
337 var target = manipulationTarget( this, elem );
338 target.insertBefore( elem, target.firstChild );
344 return this.domManip( arguments, function( elem ) {
345 if ( this.parentNode ) {
346 this.parentNode.insertBefore( elem, this );
352 return this.domManip( arguments, function( elem ) {
353 if ( this.parentNode ) {
354 this.parentNode.insertBefore( elem, this.nextSibling );
359 remove: function( selector, keepData /* Internal Use Only */ ) {
361 elems = selector ? jQuery.filter( selector, this ) : this,
364 for ( ; (elem = elems[i]) != null; i++ ) {
365 if ( !keepData && elem.nodeType === 1 ) {
366 jQuery.cleanData( getAll( elem ) );
369 if ( elem.parentNode ) {
370 if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
371 setGlobalEval( getAll( elem, "script" ) );
373 elem.parentNode.removeChild( elem );
384 for ( ; (elem = this[i]) != null; i++ ) {
385 if ( elem.nodeType === 1 ) {
387 // Prevent memory leaks
388 jQuery.cleanData( getAll( elem, false ) );
390 // Remove any remaining nodes
391 elem.textContent = "";
398 clone: function( dataAndEvents, deepDataAndEvents ) {
399 dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
400 deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
402 return this.map(function() {
403 return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
407 html: function( value ) {
408 return access( this, function( value ) {
409 var elem = this[ 0 ] || {},
413 if ( value === undefined && elem.nodeType === 1 ) {
414 return elem.innerHTML;
417 // See if we can take a shortcut and just use innerHTML
418 if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
419 !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
421 value = value.replace( rxhtmlTag, "<$1></$2>" );
424 for ( ; i < l; i++ ) {
425 elem = this[ i ] || {};
427 // Remove element nodes and prevent memory leaks
428 if ( elem.nodeType === 1 ) {
429 jQuery.cleanData( getAll( elem, false ) );
430 elem.innerHTML = value;
436 // If using innerHTML throws an exception, use the fallback method
441 this.empty().append( value );
443 }, null, value, arguments.length );
446 replaceWith: function() {
447 var arg = arguments[ 0 ];
449 // Make the changes, replacing each context element with the new content
450 this.domManip( arguments, function( elem ) {
451 arg = this.parentNode;
453 jQuery.cleanData( getAll( this ) );
456 arg.replaceChild( elem, this );
460 // Force removal if there was no new content (e.g., from empty arguments)
461 return arg && (arg.length || arg.nodeType) ? this : this.remove();
464 detach: function( selector ) {
465 return this.remove( selector, true );
468 domManip: function( args, callback ) {
470 // Flatten any nested arrays
471 args = concat.apply( [], args );
473 var fragment, first, scripts, hasScripts, node, doc,
479 isFunction = jQuery.isFunction( value );
481 // We can't cloneNode fragments that contain checked, in WebKit
483 ( l > 1 && typeof value === "string" &&
484 !support.checkClone && rchecked.test( value ) ) ) {
485 return this.each(function( index ) {
486 var self = set.eq( index );
488 args[ 0 ] = value.call( this, index, self.html() );
490 self.domManip( args, callback );
495 fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
496 first = fragment.firstChild;
498 if ( fragment.childNodes.length === 1 ) {
503 scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
504 hasScripts = scripts.length;
506 // Use the original fragment for the last item
507 // instead of the first because it can end up
508 // being emptied incorrectly in certain situations (#8070).
509 for ( ; i < l; i++ ) {
512 if ( i !== iNoClone ) {
513 node = jQuery.clone( node, true, true );
515 // Keep references to cloned scripts for later restoration
517 // Support: Android<4.1, PhantomJS<2
518 // push.apply(_, arraylike) throws on ancient WebKit
519 jQuery.merge( scripts, getAll( node, "script" ) );
523 callback.call( this[ i ], node, i );
527 doc = scripts[ scripts.length - 1 ].ownerDocument;
530 jQuery.map( scripts, restoreScript );
532 // Evaluate executable scripts on first document insertion
533 for ( i = 0; i < hasScripts; i++ ) {
535 if ( rscriptType.test( node.type || "" ) &&
536 !dataPriv.access( node, "globalEval" ) &&
537 jQuery.contains( doc, node ) ) {
540 // Optional AJAX dependency, but won't run scripts if not present
541 if ( jQuery._evalUrl ) {
542 jQuery._evalUrl( node.src );
545 jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
559 prependTo: "prepend",
560 insertBefore: "before",
561 insertAfter: "after",
562 replaceAll: "replaceWith"
563 }, function( name, original ) {
564 jQuery.fn[ name ] = function( selector ) {
567 insert = jQuery( selector ),
568 last = insert.length - 1,
571 for ( ; i <= last; i++ ) {
572 elems = i === last ? this : this.clone( true );
573 jQuery( insert[ i ] )[ original ]( elems );
575 // Support: Android<4.1, PhantomJS<2
576 // .get() because push.apply(_, arraylike) throws on ancient WebKit
577 push.apply( ret, elems.get() );
580 return this.pushStack( ret );