blob: dec21ea0b46482442e3c6021987ab1f8c434fddd [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001define( [
2 "./core",
3 "./core/isAttached",
Renovate botf591dcf2023-12-30 14:13:54 +00004 "./var/flat",
Copybara botbe50d492023-11-30 00:16:42 +01005 "./var/isFunction",
6 "./var/push",
7 "./var/rcheckableType",
8 "./core/access",
9 "./manipulation/var/rtagName",
10 "./manipulation/var/rscriptType",
11 "./manipulation/wrapMap",
12 "./manipulation/getAll",
13 "./manipulation/setGlobalEval",
14 "./manipulation/buildFragment",
15 "./manipulation/support",
16
17 "./data/var/dataPriv",
18 "./data/var/dataUser",
19 "./data/var/acceptData",
20 "./core/DOMEval",
21 "./core/nodeName",
22
23 "./core/init",
24 "./traversing",
25 "./selector",
26 "./event"
Renovate botf591dcf2023-12-30 14:13:54 +000027], function( jQuery, isAttached, flat, isFunction, push, rcheckableType,
Copybara botbe50d492023-11-30 00:16:42 +010028 access, rtagName, rscriptType,
29 wrapMap, getAll, setGlobalEval, buildFragment, support,
30 dataPriv, dataUser, acceptData, DOMEval, nodeName ) {
31
32"use strict";
33
34var
35
Copybara botbe50d492023-11-30 00:16:42 +010036 // Support: IE <=10 - 11, Edge 12 - 13 only
37 // In IE/Edge using regex groups here causes severe slowdowns.
38 // See https://connect.microsoft.com/IE/feedback/details/1736512/
39 rnoInnerhtml = /<script|<style|<link/i,
40
41 // checked="checked" or checked
42 rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
43 rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
44
45// Prefer a tbody over its parent table for containing new rows
46function manipulationTarget( elem, content ) {
47 if ( nodeName( elem, "table" ) &&
48 nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
49
50 return jQuery( elem ).children( "tbody" )[ 0 ] || elem;
51 }
52
53 return elem;
54}
55
56// Replace/restore the type attribute of script elements for safe DOM manipulation
57function disableScript( elem ) {
58 elem.type = ( elem.getAttribute( "type" ) !== null ) + "/" + elem.type;
59 return elem;
60}
61function restoreScript( elem ) {
62 if ( ( elem.type || "" ).slice( 0, 5 ) === "true/" ) {
63 elem.type = elem.type.slice( 5 );
64 } else {
65 elem.removeAttribute( "type" );
66 }
67
68 return elem;
69}
70
71function cloneCopyEvent( src, dest ) {
Renovate botf591dcf2023-12-30 14:13:54 +000072 var i, l, type, pdataOld, udataOld, udataCur, events;
Copybara botbe50d492023-11-30 00:16:42 +010073
74 if ( dest.nodeType !== 1 ) {
75 return;
76 }
77
78 // 1. Copy private data: events, handlers, etc.
79 if ( dataPriv.hasData( src ) ) {
Renovate botf591dcf2023-12-30 14:13:54 +000080 pdataOld = dataPriv.get( src );
Copybara botbe50d492023-11-30 00:16:42 +010081 events = pdataOld.events;
82
83 if ( events ) {
Renovate botf591dcf2023-12-30 14:13:54 +000084 dataPriv.remove( dest, "handle events" );
Copybara botbe50d492023-11-30 00:16:42 +010085
86 for ( type in events ) {
87 for ( i = 0, l = events[ type ].length; i < l; i++ ) {
88 jQuery.event.add( dest, type, events[ type ][ i ] );
89 }
90 }
91 }
92 }
93
94 // 2. Copy user data
95 if ( dataUser.hasData( src ) ) {
96 udataOld = dataUser.access( src );
97 udataCur = jQuery.extend( {}, udataOld );
98
99 dataUser.set( dest, udataCur );
100 }
101}
102
103// Fix IE bugs, see support tests
104function fixInput( src, dest ) {
105 var nodeName = dest.nodeName.toLowerCase();
106
107 // Fails to persist the checked state of a cloned checkbox or radio button.
108 if ( nodeName === "input" && rcheckableType.test( src.type ) ) {
109 dest.checked = src.checked;
110
111 // Fails to return the selected option to the default selected state when cloning options
112 } else if ( nodeName === "input" || nodeName === "textarea" ) {
113 dest.defaultValue = src.defaultValue;
114 }
115}
116
117function domManip( collection, args, callback, ignored ) {
118
119 // Flatten any nested arrays
Renovate botf591dcf2023-12-30 14:13:54 +0000120 args = flat( args );
Copybara botbe50d492023-11-30 00:16:42 +0100121
122 var fragment, first, scripts, hasScripts, node, doc,
123 i = 0,
124 l = collection.length,
125 iNoClone = l - 1,
126 value = args[ 0 ],
127 valueIsFunction = isFunction( value );
128
129 // We can't cloneNode fragments that contain checked, in WebKit
130 if ( valueIsFunction ||
131 ( l > 1 && typeof value === "string" &&
132 !support.checkClone && rchecked.test( value ) ) ) {
133 return collection.each( function( index ) {
134 var self = collection.eq( index );
135 if ( valueIsFunction ) {
136 args[ 0 ] = value.call( this, index, self.html() );
137 }
138 domManip( self, args, callback, ignored );
139 } );
140 }
141
142 if ( l ) {
143 fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
144 first = fragment.firstChild;
145
146 if ( fragment.childNodes.length === 1 ) {
147 fragment = first;
148 }
149
150 // Require either new content or an interest in ignored elements to invoke the callback
151 if ( first || ignored ) {
152 scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
153 hasScripts = scripts.length;
154
155 // Use the original fragment for the last item
156 // instead of the first because it can end up
157 // being emptied incorrectly in certain situations (#8070).
158 for ( ; i < l; i++ ) {
159 node = fragment;
160
161 if ( i !== iNoClone ) {
162 node = jQuery.clone( node, true, true );
163
164 // Keep references to cloned scripts for later restoration
165 if ( hasScripts ) {
166
167 // Support: Android <=4.0 only, PhantomJS 1 only
168 // push.apply(_, arraylike) throws on ancient WebKit
169 jQuery.merge( scripts, getAll( node, "script" ) );
170 }
171 }
172
173 callback.call( collection[ i ], node, i );
174 }
175
176 if ( hasScripts ) {
177 doc = scripts[ scripts.length - 1 ].ownerDocument;
178
179 // Reenable scripts
180 jQuery.map( scripts, restoreScript );
181
182 // Evaluate executable scripts on first document insertion
183 for ( i = 0; i < hasScripts; i++ ) {
184 node = scripts[ i ];
185 if ( rscriptType.test( node.type || "" ) &&
186 !dataPriv.access( node, "globalEval" ) &&
187 jQuery.contains( doc, node ) ) {
188
189 if ( node.src && ( node.type || "" ).toLowerCase() !== "module" ) {
190
191 // Optional AJAX dependency, but won't run scripts if not present
192 if ( jQuery._evalUrl && !node.noModule ) {
193 jQuery._evalUrl( node.src, {
194 nonce: node.nonce || node.getAttribute( "nonce" )
Renovate botf591dcf2023-12-30 14:13:54 +0000195 }, doc );
Copybara botbe50d492023-11-30 00:16:42 +0100196 }
197 } else {
198 DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
199 }
200 }
201 }
202 }
203 }
204 }
205
206 return collection;
207}
208
209function remove( elem, selector, keepData ) {
210 var node,
211 nodes = selector ? jQuery.filter( selector, elem ) : elem,
212 i = 0;
213
214 for ( ; ( node = nodes[ i ] ) != null; i++ ) {
215 if ( !keepData && node.nodeType === 1 ) {
216 jQuery.cleanData( getAll( node ) );
217 }
218
219 if ( node.parentNode ) {
220 if ( keepData && isAttached( node ) ) {
221 setGlobalEval( getAll( node, "script" ) );
222 }
223 node.parentNode.removeChild( node );
224 }
225 }
226
227 return elem;
228}
229
230jQuery.extend( {
231 htmlPrefilter: function( html ) {
Renovate botf591dcf2023-12-30 14:13:54 +0000232 return html;
Copybara botbe50d492023-11-30 00:16:42 +0100233 },
234
235 clone: function( elem, dataAndEvents, deepDataAndEvents ) {
236 var i, l, srcElements, destElements,
237 clone = elem.cloneNode( true ),
238 inPage = isAttached( elem );
239
240 // Fix IE cloning issues
241 if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
242 !jQuery.isXMLDoc( elem ) ) {
243
244 // We eschew Sizzle here for performance reasons: https://jsperf.com/getall-vs-sizzle/2
245 destElements = getAll( clone );
246 srcElements = getAll( elem );
247
248 for ( i = 0, l = srcElements.length; i < l; i++ ) {
249 fixInput( srcElements[ i ], destElements[ i ] );
250 }
251 }
252
253 // Copy the events from the original to the clone
254 if ( dataAndEvents ) {
255 if ( deepDataAndEvents ) {
256 srcElements = srcElements || getAll( elem );
257 destElements = destElements || getAll( clone );
258
259 for ( i = 0, l = srcElements.length; i < l; i++ ) {
260 cloneCopyEvent( srcElements[ i ], destElements[ i ] );
261 }
262 } else {
263 cloneCopyEvent( elem, clone );
264 }
265 }
266
267 // Preserve script evaluation history
268 destElements = getAll( clone, "script" );
269 if ( destElements.length > 0 ) {
270 setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
271 }
272
273 // Return the cloned set
274 return clone;
275 },
276
277 cleanData: function( elems ) {
278 var data, elem, type,
279 special = jQuery.event.special,
280 i = 0;
281
282 for ( ; ( elem = elems[ i ] ) !== undefined; i++ ) {
283 if ( acceptData( elem ) ) {
284 if ( ( data = elem[ dataPriv.expando ] ) ) {
285 if ( data.events ) {
286 for ( type in data.events ) {
287 if ( special[ type ] ) {
288 jQuery.event.remove( elem, type );
289
290 // This is a shortcut to avoid jQuery.event.remove's overhead
291 } else {
292 jQuery.removeEvent( elem, type, data.handle );
293 }
294 }
295 }
296
297 // Support: Chrome <=35 - 45+
298 // Assign undefined instead of using delete, see Data#remove
299 elem[ dataPriv.expando ] = undefined;
300 }
301 if ( elem[ dataUser.expando ] ) {
302
303 // Support: Chrome <=35 - 45+
304 // Assign undefined instead of using delete, see Data#remove
305 elem[ dataUser.expando ] = undefined;
306 }
307 }
308 }
309 }
310} );
311
312jQuery.fn.extend( {
313 detach: function( selector ) {
314 return remove( this, selector, true );
315 },
316
317 remove: function( selector ) {
318 return remove( this, selector );
319 },
320
321 text: function( value ) {
322 return access( this, function( value ) {
323 return value === undefined ?
324 jQuery.text( this ) :
325 this.empty().each( function() {
326 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
327 this.textContent = value;
328 }
329 } );
330 }, null, value, arguments.length );
331 },
332
333 append: function() {
334 return domManip( this, arguments, function( elem ) {
335 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
336 var target = manipulationTarget( this, elem );
337 target.appendChild( elem );
338 }
339 } );
340 },
341
342 prepend: function() {
343 return domManip( this, arguments, function( elem ) {
344 if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
345 var target = manipulationTarget( this, elem );
346 target.insertBefore( elem, target.firstChild );
347 }
348 } );
349 },
350
351 before: function() {
352 return domManip( this, arguments, function( elem ) {
353 if ( this.parentNode ) {
354 this.parentNode.insertBefore( elem, this );
355 }
356 } );
357 },
358
359 after: function() {
360 return domManip( this, arguments, function( elem ) {
361 if ( this.parentNode ) {
362 this.parentNode.insertBefore( elem, this.nextSibling );
363 }
364 } );
365 },
366
367 empty: function() {
368 var elem,
369 i = 0;
370
371 for ( ; ( elem = this[ i ] ) != null; i++ ) {
372 if ( elem.nodeType === 1 ) {
373
374 // Prevent memory leaks
375 jQuery.cleanData( getAll( elem, false ) );
376
377 // Remove any remaining nodes
378 elem.textContent = "";
379 }
380 }
381
382 return this;
383 },
384
385 clone: function( dataAndEvents, deepDataAndEvents ) {
386 dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
387 deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
388
389 return this.map( function() {
390 return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
391 } );
392 },
393
394 html: function( value ) {
395 return access( this, function( value ) {
396 var elem = this[ 0 ] || {},
397 i = 0,
398 l = this.length;
399
400 if ( value === undefined && elem.nodeType === 1 ) {
401 return elem.innerHTML;
402 }
403
404 // See if we can take a shortcut and just use innerHTML
405 if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
406 !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
407
408 value = jQuery.htmlPrefilter( value );
409
410 try {
411 for ( ; i < l; i++ ) {
412 elem = this[ i ] || {};
413
414 // Remove element nodes and prevent memory leaks
415 if ( elem.nodeType === 1 ) {
416 jQuery.cleanData( getAll( elem, false ) );
417 elem.innerHTML = value;
418 }
419 }
420
421 elem = 0;
422
423 // If using innerHTML throws an exception, use the fallback method
424 } catch ( e ) {}
425 }
426
427 if ( elem ) {
428 this.empty().append( value );
429 }
430 }, null, value, arguments.length );
431 },
432
433 replaceWith: function() {
434 var ignored = [];
435
436 // Make the changes, replacing each non-ignored context element with the new content
437 return domManip( this, arguments, function( elem ) {
438 var parent = this.parentNode;
439
440 if ( jQuery.inArray( this, ignored ) < 0 ) {
441 jQuery.cleanData( getAll( this ) );
442 if ( parent ) {
443 parent.replaceChild( elem, this );
444 }
445 }
446
447 // Force callback invocation
448 }, ignored );
449 }
450} );
451
452jQuery.each( {
453 appendTo: "append",
454 prependTo: "prepend",
455 insertBefore: "before",
456 insertAfter: "after",
457 replaceAll: "replaceWith"
458}, function( name, original ) {
459 jQuery.fn[ name ] = function( selector ) {
460 var elems,
461 ret = [],
462 insert = jQuery( selector ),
463 last = insert.length - 1,
464 i = 0;
465
466 for ( ; i <= last; i++ ) {
467 elems = i === last ? this : this.clone( true );
468 jQuery( insert[ i ] )[ original ]( elems );
469
470 // Support: Android <=4.0 only, PhantomJS 1 only
471 // .get() because push.apply(_, arraylike) throws on ancient WebKit
472 push.apply( ret, elems.get() );
473 }
474
475 return this.pushStack( ret );
476 };
477} );
478
479return jQuery;
480} );