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>" ],
37 // Some of the following wrappers are not fully defined, because
38 // their parent elements (except for "table" element) could be omitted
39 // since browser parsers are smart enough to auto-insert them
41 // Support: Android 2.3
42 // Android browser doesn't auto-insert colgroup
43 col: [ 2, "<table><colgroup>", "</colgroup></table>" ],
45 // Auto-insert "tbody" element
46 tr: [ 2, "<table>", "</table>" ],
48 // Auto-insert "tbody" and "tr" elements
49 td: [ 3, "<table>", "</table>" ],
51 _default: [ 0, "", "" ]
55 wrapMap.optgroup = wrapMap.option;
57 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
58 wrapMap.th = wrapMap.td;
60 function manipulationTarget( elem, content ) {
61 if ( jQuery.nodeName( elem, "table" ) &&
62 jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
64 return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
70 // Replace/restore the type attribute of script elements for safe DOM manipulation
71 function disableScript( elem ) {
72 elem.type = (elem.getAttribute("type") !== null) + "/" + elem.type;
75 function restoreScript( elem ) {
76 var match = rscriptTypeMasked.exec( elem.type );
79 elem.type = match[ 1 ];
81 elem.removeAttribute("type");
87 // Mark scripts as having already been evaluated
88 function setGlobalEval( elems, refElements ) {
92 for ( ; i < l; i++ ) {
94 elems[ i ], "globalEval", !refElements || dataPriv.get( refElements[ i ], "globalEval" )
99 function cloneCopyEvent( src, dest ) {
100 var i, l, type, pdataOld, pdataCur, udataOld, udataCur, events;
102 if ( dest.nodeType !== 1 ) {
106 // 1. Copy private data: events, handlers, etc.
107 if ( dataPriv.hasData( src ) ) {
108 pdataOld = dataPriv.access( src );
109 pdataCur = dataPriv.set( dest, pdataOld );
110 events = pdataOld.events;
113 delete pdataCur.handle;
114 pdataCur.events = {};
116 for ( type in events ) {
117 for ( i = 0, l = events[ type ].length; i < l; i++ ) {
118 jQuery.event.add( dest, type, events[ type ][ i ] );
125 if ( dataUser.hasData( src ) ) {
126 udataOld = dataUser.access( src );
127 udataCur = jQuery.extend( {}, udataOld );
129 dataUser.set( dest, udataCur );
133 function getAll( context, tag ) {
135 // Use typeof to avoid zero-argument method invocation on host objects (#15151)
136 var ret = typeof context.getElementsByTagName !== "undefined" ?
137 context.getElementsByTagName( tag || "*" ) :
138 typeof context.querySelectorAll !== "undefined" ?
139 context.querySelectorAll( tag || "*" ) :
142 return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
143 jQuery.merge( [ context ], ret ) :
147 // Fix IE bugs, see support tests
148 function fixInput( src, dest ) {
149 var nodeName = dest.nodeName.toLowerCase();
151 // Fails to persist the checked state of a cloned checkbox or radio button.
152 if ( nodeName === "input" && rcheckableType.test( src.type ) ) {
153 dest.checked = src.checked;
155 // Fails to return the selected option to the default selected state when cloning options
156 } else if ( nodeName === "input" || nodeName === "textarea" ) {
157 dest.defaultValue = src.defaultValue;
162 clone: function( elem, dataAndEvents, deepDataAndEvents ) {
163 var i, l, srcElements, destElements,
164 clone = elem.cloneNode( true ),
165 inPage = jQuery.contains( elem.ownerDocument, elem );
167 // Fix IE cloning issues
168 if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
169 !jQuery.isXMLDoc( elem ) ) {
171 // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
172 destElements = getAll( clone );
173 srcElements = getAll( elem );
175 for ( i = 0, l = srcElements.length; i < l; i++ ) {
176 fixInput( srcElements[ i ], destElements[ i ] );
180 // Copy the events from the original to the clone
181 if ( dataAndEvents ) {
182 if ( deepDataAndEvents ) {
183 srcElements = srcElements || getAll( elem );
184 destElements = destElements || getAll( clone );
186 for ( i = 0, l = srcElements.length; i < l; i++ ) {
187 cloneCopyEvent( srcElements[ i ], destElements[ i ] );
190 cloneCopyEvent( elem, clone );
194 // Preserve script evaluation history
195 destElements = getAll( clone, "script" );
196 if ( destElements.length > 0 ) {
197 setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
200 // Return the cloned set
204 buildFragment: function( elems, context, scripts, selection ) {
205 var elem, tmp, tag, wrap, contains, j,
206 fragment = context.createDocumentFragment(),
211 for ( ; i < l; i++ ) {
214 if ( elem || elem === 0 ) {
216 // Add nodes directly
217 if ( jQuery.type( elem ) === "object" ) {
218 // Support: Android<4.1, PhantomJS<2
219 // push.apply(_, arraylike) throws on ancient WebKit
220 jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
222 // Convert non-html into a text node
223 } else if ( !rhtml.test( elem ) ) {
224 nodes.push( context.createTextNode( elem ) );
226 // Convert html into DOM nodes
228 tmp = tmp || fragment.appendChild( context.createElement("div") );
230 // Deserialize a standard representation
231 tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
232 wrap = wrapMap[ tag ] || wrapMap._default;
233 tmp.innerHTML = wrap[ 1 ] + elem.replace( rxhtmlTag, "<$1></$2>" ) + wrap[ 2 ];
235 // Descend through wrappers to the right content
241 // Support: Android<4.1, PhantomJS<2
242 // push.apply(_, arraylike) throws on ancient WebKit
243 jQuery.merge( nodes, tmp.childNodes );
245 // Remember the top-level container
246 tmp = fragment.firstChild;
248 // Ensure the created nodes are orphaned (#12392)
249 tmp.textContent = "";
254 // Remove wrapper from fragment
255 fragment.textContent = "";
258 while ( (elem = nodes[ i++ ]) ) {
260 // #4087 - If origin and destination elements are the same, and this is
261 // that element, do not do anything
262 if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
266 contains = jQuery.contains( elem.ownerDocument, elem );
268 // Append to fragment
269 tmp = getAll( fragment.appendChild( elem ), "script" );
271 // Preserve script evaluation history
273 setGlobalEval( tmp );
276 // Capture executables
279 while ( (elem = tmp[ j++ ]) ) {
280 if ( rscriptType.test( elem.type || "" ) ) {
281 scripts.push( elem );
290 cleanData: function( elems ) {
291 var data, elem, type,
292 special = jQuery.event.special,
295 for ( ; (elem = elems[ i ]) !== undefined; i++ ) {
296 if ( jQuery.acceptData( elem ) && (data = elem[ dataPriv.expando ])) {
298 for ( type in data.events ) {
299 if ( special[ type ] ) {
300 jQuery.event.remove( elem, type );
302 // This is a shortcut to avoid jQuery.event.remove's overhead
304 jQuery.removeEvent( elem, type, data.handle );
315 text: function( value ) {
316 return access( this, function( value ) {
317 return value === undefined ?
318 jQuery.text( this ) :
319 this.empty().each(function() {
320 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
321 this.textContent = value;
324 }, null, value, arguments.length );
328 return this.domManip( arguments, function( elem ) {
329 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
330 var target = manipulationTarget( this, elem );
331 target.appendChild( elem );
336 prepend: function() {
337 return this.domManip( arguments, function( elem ) {
338 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
339 var target = manipulationTarget( this, elem );
340 target.insertBefore( elem, target.firstChild );
346 return this.domManip( arguments, function( elem ) {
347 if ( this.parentNode ) {
348 this.parentNode.insertBefore( elem, this );
354 return this.domManip( arguments, function( elem ) {
355 if ( this.parentNode ) {
356 this.parentNode.insertBefore( elem, this.nextSibling );
361 remove: function( selector, keepData /* Internal Use Only */ ) {
363 elems = selector ? jQuery.filter( selector, this ) : this,
366 for ( ; (elem = elems[i]) != null; i++ ) {
367 if ( !keepData && elem.nodeType === 1 ) {
368 jQuery.cleanData( getAll( elem ) );
371 if ( elem.parentNode ) {
372 if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
373 setGlobalEval( getAll( elem, "script" ) );
375 elem.parentNode.removeChild( elem );
386 for ( ; (elem = this[i]) != null; i++ ) {
387 if ( elem.nodeType === 1 ) {
389 // Prevent memory leaks
390 jQuery.cleanData( getAll( elem, false ) );
392 // Remove any remaining nodes
393 elem.textContent = "";
400 clone: function( dataAndEvents, deepDataAndEvents ) {
401 dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
402 deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
404 return this.map(function() {
405 return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
409 html: function( value ) {
410 return access( this, function( value ) {
411 var elem = this[ 0 ] || {},
415 if ( value === undefined && elem.nodeType === 1 ) {
416 return elem.innerHTML;
419 // See if we can take a shortcut and just use innerHTML
420 if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
421 !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
423 value = value.replace( rxhtmlTag, "<$1></$2>" );
426 for ( ; i < l; i++ ) {
427 elem = this[ i ] || {};
429 // Remove element nodes and prevent memory leaks
430 if ( elem.nodeType === 1 ) {
431 jQuery.cleanData( getAll( elem, false ) );
432 elem.innerHTML = value;
438 // If using innerHTML throws an exception, use the fallback method
443 this.empty().append( value );
445 }, null, value, arguments.length );
448 replaceWith: function() {
449 var arg = arguments[ 0 ];
451 // Make the changes, replacing each context element with the new content
452 this.domManip( arguments, function( elem ) {
453 arg = this.parentNode;
455 jQuery.cleanData( getAll( this ) );
458 arg.replaceChild( elem, this );
462 // Force removal if there was no new content (e.g., from empty arguments)
463 return arg && (arg.length || arg.nodeType) ? this : this.remove();
466 detach: function( selector ) {
467 return this.remove( selector, true );
470 domManip: function( args, callback ) {
472 // Flatten any nested arrays
473 args = concat.apply( [], args );
475 var fragment, first, scripts, hasScripts, node, doc,
481 isFunction = jQuery.isFunction( value );
483 // We can't cloneNode fragments that contain checked, in WebKit
485 ( l > 1 && typeof value === "string" &&
486 !support.checkClone && rchecked.test( value ) ) ) {
487 return this.each(function( index ) {
488 var self = set.eq( index );
490 args[ 0 ] = value.call( this, index, self.html() );
492 self.domManip( args, callback );
497 fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
498 first = fragment.firstChild;
500 if ( fragment.childNodes.length === 1 ) {
505 scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
506 hasScripts = scripts.length;
508 // Use the original fragment for the last item
509 // instead of the first because it can end up
510 // being emptied incorrectly in certain situations (#8070).
511 for ( ; i < l; i++ ) {
514 if ( i !== iNoClone ) {
515 node = jQuery.clone( node, true, true );
517 // Keep references to cloned scripts for later restoration
519 // Support: Android<4.1, PhantomJS<2
520 // push.apply(_, arraylike) throws on ancient WebKit
521 jQuery.merge( scripts, getAll( node, "script" ) );
525 callback.call( this[ i ], node, i );
529 doc = scripts[ scripts.length - 1 ].ownerDocument;
532 jQuery.map( scripts, restoreScript );
534 // Evaluate executable scripts on first document insertion
535 for ( i = 0; i < hasScripts; i++ ) {
537 if ( rscriptType.test( node.type || "" ) &&
538 !dataPriv.access( node, "globalEval" ) &&
539 jQuery.contains( doc, node ) ) {
542 // Optional AJAX dependency, but won't run scripts if not present
543 if ( jQuery._evalUrl ) {
544 jQuery._evalUrl( node.src );
547 jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
561 prependTo: "prepend",
562 insertBefore: "before",
563 insertAfter: "after",
564 replaceAll: "replaceWith"
565 }, function( name, original ) {
566 jQuery.fn[ name ] = function( selector ) {
569 insert = jQuery( selector ),
570 last = insert.length - 1,
573 for ( ; i <= last; i++ ) {
574 elems = i === last ? this : this.clone( true );
575 jQuery( insert[ i ] )[ original ]( elems );
577 // Support: Android<4.1, PhantomJS<2
578 // .get() because push.apply(_, arraylike) throws on ancient WebKit
579 push.apply( ret, elems.get() );
582 return this.pushStack( ret );