No Description

manipulation.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. define( [
  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. "./data/var/dataPriv",
  17. "./data/var/dataUser",
  18. "./data/var/acceptData",
  19. "./core/DOMEval",
  20. "./core/nodeName",
  21. "./core/init",
  22. "./traversing",
  23. "./selector",
  24. "./event"
  25. ], function( jQuery, isAttached, concat, isFunction, push, rcheckableType,
  26. access, rtagName, rscriptType,
  27. wrapMap, getAll, setGlobalEval, buildFragment, support,
  28. dataPriv, dataUser, acceptData, DOMEval, nodeName ) {
  29. "use strict";
  30. var
  31. /* eslint-disable max-len */
  32. // See https://github.com/eslint/eslint/issues/3229
  33. rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi,
  34. /* eslint-enable */
  35. // Support: IE <=10 - 11, Edge 12 - 13 only
  36. // In IE/Edge using regex groups here causes severe slowdowns.
  37. // See https://connect.microsoft.com/IE/feedback/details/1736512/
  38. rnoInnerhtml = /<script|<style|<link/i,
  39. // checked="checked" or checked
  40. rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
  41. rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
  42. // Prefer a tbody over its parent table for containing new rows
  43. function manipulationTarget( elem, content ) {
  44. if ( nodeName( elem, "table" ) &&
  45. nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
  46. return jQuery( elem ).children( "tbody" )[ 0 ] || elem;
  47. }
  48. return elem;
  49. }
  50. // Replace/restore the type attribute of script elements for safe DOM manipulation
  51. function disableScript( elem ) {
  52. elem.type = ( elem.getAttribute( "type" ) !== null ) + "/" + elem.type;
  53. return elem;
  54. }
  55. function restoreScript( elem ) {
  56. if ( ( elem.type || "" ).slice( 0, 5 ) === "true/" ) {
  57. elem.type = elem.type.slice( 5 );
  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. valueIsFunction = isFunction( value );
  110. // We can't cloneNode fragments that contain checked, in WebKit
  111. if ( valueIsFunction ||
  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 ( valueIsFunction ) {
  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 && ( node.type || "" ).toLowerCase() !== "module" ) {
  159. // Optional AJAX dependency, but won't run scripts if not present
  160. if ( jQuery._evalUrl && !node.noModule ) {
  161. jQuery._evalUrl( node.src, {
  162. nonce: node.nonce || node.getAttribute( "nonce" )
  163. } );
  164. }
  165. } else {
  166. DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. return collection;
  174. }
  175. function remove( elem, selector, keepData ) {
  176. var node,
  177. nodes = selector ? jQuery.filter( selector, elem ) : elem,
  178. i = 0;
  179. for ( ; ( node = nodes[ i ] ) != null; i++ ) {
  180. if ( !keepData && node.nodeType === 1 ) {
  181. jQuery.cleanData( getAll( node ) );
  182. }
  183. if ( node.parentNode ) {
  184. if ( keepData && isAttached( node ) ) {
  185. setGlobalEval( getAll( node, "script" ) );
  186. }
  187. node.parentNode.removeChild( node );
  188. }
  189. }
  190. return elem;
  191. }
  192. jQuery.extend( {
  193. htmlPrefilter: function( html ) {
  194. return html.replace( rxhtmlTag, "<$1></$2>" );
  195. },
  196. clone: function( elem, dataAndEvents, deepDataAndEvents ) {
  197. var i, l, srcElements, destElements,
  198. clone = elem.cloneNode( true ),
  199. inPage = isAttached( elem );
  200. // Fix IE cloning issues
  201. if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
  202. !jQuery.isXMLDoc( elem ) ) {
  203. // We eschew Sizzle here for performance reasons: https://jsperf.com/getall-vs-sizzle/2
  204. destElements = getAll( clone );
  205. srcElements = getAll( elem );
  206. for ( i = 0, l = srcElements.length; i < l; i++ ) {
  207. fixInput( srcElements[ i ], destElements[ i ] );
  208. }
  209. }
  210. // Copy the events from the original to the clone
  211. if ( dataAndEvents ) {
  212. if ( deepDataAndEvents ) {
  213. srcElements = srcElements || getAll( elem );
  214. destElements = destElements || getAll( clone );
  215. for ( i = 0, l = srcElements.length; i < l; i++ ) {
  216. cloneCopyEvent( srcElements[ i ], destElements[ i ] );
  217. }
  218. } else {
  219. cloneCopyEvent( elem, clone );
  220. }
  221. }
  222. // Preserve script evaluation history
  223. destElements = getAll( clone, "script" );
  224. if ( destElements.length > 0 ) {
  225. setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
  226. }
  227. // Return the cloned set
  228. return clone;
  229. },
  230. cleanData: function( elems ) {
  231. var data, elem, type,
  232. special = jQuery.event.special,
  233. i = 0;
  234. for ( ; ( elem = elems[ i ] ) !== undefined; i++ ) {
  235. if ( acceptData( elem ) ) {
  236. if ( ( data = elem[ dataPriv.expando ] ) ) {
  237. if ( data.events ) {
  238. for ( type in data.events ) {
  239. if ( special[ type ] ) {
  240. jQuery.event.remove( elem, type );
  241. // This is a shortcut to avoid jQuery.event.remove's overhead
  242. } else {
  243. jQuery.removeEvent( elem, type, data.handle );
  244. }
  245. }
  246. }
  247. // Support: Chrome <=35 - 45+
  248. // Assign undefined instead of using delete, see Data#remove
  249. elem[ dataPriv.expando ] = undefined;
  250. }
  251. if ( elem[ dataUser.expando ] ) {
  252. // Support: Chrome <=35 - 45+
  253. // Assign undefined instead of using delete, see Data#remove
  254. elem[ dataUser.expando ] = undefined;
  255. }
  256. }
  257. }
  258. }
  259. } );
  260. jQuery.fn.extend( {
  261. detach: function( selector ) {
  262. return remove( this, selector, true );
  263. },
  264. remove: function( selector ) {
  265. return remove( this, selector );
  266. },
  267. text: function( value ) {
  268. return access( this, function( value ) {
  269. return value === undefined ?
  270. jQuery.text( this ) :
  271. this.empty().each( function() {
  272. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  273. this.textContent = value;
  274. }
  275. } );
  276. }, null, value, arguments.length );
  277. },
  278. append: function() {
  279. return domManip( this, arguments, function( elem ) {
  280. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  281. var target = manipulationTarget( this, elem );
  282. target.appendChild( elem );
  283. }
  284. } );
  285. },
  286. prepend: function() {
  287. return domManip( this, arguments, function( elem ) {
  288. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  289. var target = manipulationTarget( this, elem );
  290. target.insertBefore( elem, target.firstChild );
  291. }
  292. } );
  293. },
  294. before: function() {
  295. return domManip( this, arguments, function( elem ) {
  296. if ( this.parentNode ) {
  297. this.parentNode.insertBefore( elem, this );
  298. }
  299. } );
  300. },
  301. after: function() {
  302. return domManip( this, arguments, function( elem ) {
  303. if ( this.parentNode ) {
  304. this.parentNode.insertBefore( elem, this.nextSibling );
  305. }
  306. } );
  307. },
  308. empty: function() {
  309. var elem,
  310. i = 0;
  311. for ( ; ( elem = this[ i ] ) != null; i++ ) {
  312. if ( elem.nodeType === 1 ) {
  313. // Prevent memory leaks
  314. jQuery.cleanData( getAll( elem, false ) );
  315. // Remove any remaining nodes
  316. elem.textContent = "";
  317. }
  318. }
  319. return this;
  320. },
  321. clone: function( dataAndEvents, deepDataAndEvents ) {
  322. dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
  323. deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
  324. return this.map( function() {
  325. return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
  326. } );
  327. },
  328. html: function( value ) {
  329. return access( this, function( value ) {
  330. var elem = this[ 0 ] || {},
  331. i = 0,
  332. l = this.length;
  333. if ( value === undefined && elem.nodeType === 1 ) {
  334. return elem.innerHTML;
  335. }
  336. // See if we can take a shortcut and just use innerHTML
  337. if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
  338. !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
  339. value = jQuery.htmlPrefilter( value );
  340. try {
  341. for ( ; i < l; i++ ) {
  342. elem = this[ i ] || {};
  343. // Remove element nodes and prevent memory leaks
  344. if ( elem.nodeType === 1 ) {
  345. jQuery.cleanData( getAll( elem, false ) );
  346. elem.innerHTML = value;
  347. }
  348. }
  349. elem = 0;
  350. // If using innerHTML throws an exception, use the fallback method
  351. } catch ( e ) {}
  352. }
  353. if ( elem ) {
  354. this.empty().append( value );
  355. }
  356. }, null, value, arguments.length );
  357. },
  358. replaceWith: function() {
  359. var ignored = [];
  360. // Make the changes, replacing each non-ignored context element with the new content
  361. return domManip( this, arguments, function( elem ) {
  362. var parent = this.parentNode;
  363. if ( jQuery.inArray( this, ignored ) < 0 ) {
  364. jQuery.cleanData( getAll( this ) );
  365. if ( parent ) {
  366. parent.replaceChild( elem, this );
  367. }
  368. }
  369. // Force callback invocation
  370. }, ignored );
  371. }
  372. } );
  373. jQuery.each( {
  374. appendTo: "append",
  375. prependTo: "prepend",
  376. insertBefore: "before",
  377. insertAfter: "after",
  378. replaceAll: "replaceWith"
  379. }, function( name, original ) {
  380. jQuery.fn[ name ] = function( selector ) {
  381. var elems,
  382. ret = [],
  383. insert = jQuery( selector ),
  384. last = insert.length - 1,
  385. i = 0;
  386. for ( ; i <= last; i++ ) {
  387. elems = i === last ? this : this.clone( true );
  388. jQuery( insert[ i ] )[ original ]( elems );
  389. // Support: Android <=4.0 only, PhantomJS 1 only
  390. // .get() because push.apply(_, arraylike) throws on ancient WebKit
  391. push.apply( ret, elems.get() );
  392. }
  393. return this.pushStack( ret );
  394. };
  395. } );
  396. return jQuery;
  397. } );