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