manipulation.js 12 KB

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