manipulation.js 12 KB

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