manipulation.js 12 KB

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