manipulation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 (trac-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. // Unwrap a CDATA section containing script contents. This shouldn't be
  161. // needed as in XML documents they're already not visible when
  162. // inspecting element contents and in HTML documents they have no
  163. // meaning but we're preserving that logic for backwards compatibility.
  164. // This will be removed completely in 4.0. See gh-4904.
  165. DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return collection;
  173. }
  174. function remove( elem, selector, keepData ) {
  175. var node,
  176. nodes = selector ? jQuery.filter( selector, elem ) : elem,
  177. i = 0;
  178. for ( ; ( node = nodes[ i ] ) != null; i++ ) {
  179. if ( !keepData && node.nodeType === 1 ) {
  180. jQuery.cleanData( getAll( node ) );
  181. }
  182. if ( node.parentNode ) {
  183. if ( keepData && isAttached( node ) ) {
  184. setGlobalEval( getAll( node, "script" ) );
  185. }
  186. node.parentNode.removeChild( node );
  187. }
  188. }
  189. return elem;
  190. }
  191. jQuery.extend( {
  192. htmlPrefilter: function( html ) {
  193. return html;
  194. },
  195. clone: function( elem, dataAndEvents, deepDataAndEvents ) {
  196. var i, l, srcElements, destElements,
  197. clone = elem.cloneNode( true ),
  198. inPage = isAttached( elem );
  199. // Fix IE cloning issues
  200. if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
  201. !jQuery.isXMLDoc( elem ) ) {
  202. // We eschew jQuery#find here for performance reasons:
  203. // 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. } );