manipulation.js 12 KB

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