You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

488 lines
12 KiB

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