manipulation.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. define( [
  2. "./core",
  3. "./var/document",
  4. "./var/concat",
  5. "./var/push",
  6. "./var/deletedIds",
  7. "./core/access",
  8. "./manipulation/var/rcheckableType",
  9. "./manipulation/var/rtagName",
  10. "./manipulation/var/rscriptType",
  11. "./manipulation/var/rleadingWhitespace",
  12. "./manipulation/var/nodeNames",
  13. "./manipulation/createSafeFragment",
  14. "./manipulation/wrapMap",
  15. "./manipulation/getAll",
  16. "./manipulation/setGlobalEval",
  17. "./manipulation/buildFragment",
  18. "./manipulation/support",
  19. "./data/var/acceptData",
  20. "./core/init",
  21. "./traversing",
  22. "./selector",
  23. "./event"
  24. ], function( jQuery, document, concat, push, deletedIds, access,
  25. rcheckableType, rtagName, rscriptType, rleadingWhitespace, nodeNames,
  26. createSafeFragment, wrapMap, getAll, setGlobalEval,
  27. buildFragment, support, acceptData ) {
  28. var rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g,
  29. rnoshimcache = new RegExp( "<(?:" + nodeNames + ")[\\s/>]", "i" ),
  30. rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:-]+)[^>]*)\/>/gi,
  31. // Support: IE 10-11, Edge 10240+
  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. rscriptTypeMasked = /^true\/(.*)/,
  38. rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g,
  39. safeFragment = createSafeFragment( document ),
  40. fragmentDiv = safeFragment.appendChild( document.createElement( "div" ) );
  41. // Support: IE<8
  42. // Manipulating tables requires a tbody
  43. function manipulationTarget( elem, content ) {
  44. return jQuery.nodeName( elem, "table" ) &&
  45. jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ?
  46. elem.getElementsByTagName( "tbody" )[ 0 ] ||
  47. elem.appendChild( elem.ownerDocument.createElement( "tbody" ) ) :
  48. elem;
  49. }
  50. // Replace/restore the type attribute of script elements for safe DOM manipulation
  51. function disableScript( elem ) {
  52. elem.type = ( jQuery.find.attr( elem, "type" ) !== null ) + "/" + elem.type;
  53. return elem;
  54. }
  55. function restoreScript( elem ) {
  56. var match = rscriptTypeMasked.exec( elem.type );
  57. if ( match ) {
  58. elem.type = match[ 1 ];
  59. } else {
  60. elem.removeAttribute( "type" );
  61. }
  62. return elem;
  63. }
  64. function cloneCopyEvent( src, dest ) {
  65. if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
  66. return;
  67. }
  68. var type, i, l,
  69. oldData = jQuery._data( src ),
  70. curData = jQuery._data( dest, oldData ),
  71. events = oldData.events;
  72. if ( events ) {
  73. delete curData.handle;
  74. curData.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. // make the cloned public data object a copy from the original
  82. if ( curData.data ) {
  83. curData.data = jQuery.extend( {}, curData.data );
  84. }
  85. }
  86. function fixCloneNodeIssues( src, dest ) {
  87. var nodeName, e, data;
  88. // We do not need to do anything for non-Elements
  89. if ( dest.nodeType !== 1 ) {
  90. return;
  91. }
  92. nodeName = dest.nodeName.toLowerCase();
  93. // IE6-8 copies events bound via attachEvent when using cloneNode.
  94. if ( !support.noCloneEvent && dest[ jQuery.expando ] ) {
  95. data = jQuery._data( dest );
  96. for ( e in data.events ) {
  97. jQuery.removeEvent( dest, e, data.handle );
  98. }
  99. // Event data gets referenced instead of copied if the expando gets copied too
  100. dest.removeAttribute( jQuery.expando );
  101. }
  102. // IE blanks contents when cloning scripts, and tries to evaluate newly-set text
  103. if ( nodeName === "script" && dest.text !== src.text ) {
  104. disableScript( dest ).text = src.text;
  105. restoreScript( dest );
  106. // IE6-10 improperly clones children of object elements using classid.
  107. // IE10 throws NoModificationAllowedError if parent is null, #12132.
  108. } else if ( nodeName === "object" ) {
  109. if ( dest.parentNode ) {
  110. dest.outerHTML = src.outerHTML;
  111. }
  112. // This path appears unavoidable for IE9. When cloning an object
  113. // element in IE9, the outerHTML strategy above is not sufficient.
  114. // If the src has innerHTML and the destination does not,
  115. // copy the src.innerHTML into the dest.innerHTML. #10324
  116. if ( support.html5Clone && ( src.innerHTML && !jQuery.trim( dest.innerHTML ) ) ) {
  117. dest.innerHTML = src.innerHTML;
  118. }
  119. } else if ( nodeName === "input" && rcheckableType.test( src.type ) ) {
  120. // IE6-8 fails to persist the checked state of a cloned checkbox
  121. // or radio button. Worse, IE6-7 fail to give the cloned element
  122. // a checked appearance if the defaultChecked value isn't also set
  123. dest.defaultChecked = dest.checked = src.checked;
  124. // IE6-7 get confused and end up setting the value of a cloned
  125. // checkbox/radio button to an empty string instead of "on"
  126. if ( dest.value !== src.value ) {
  127. dest.value = src.value;
  128. }
  129. // IE6-8 fails to return the selected option to the default selected
  130. // state when cloning options
  131. } else if ( nodeName === "option" ) {
  132. dest.defaultSelected = dest.selected = src.defaultSelected;
  133. // IE6-8 fails to set the defaultValue to the correct value when
  134. // cloning other types of input fields
  135. } else if ( nodeName === "input" || nodeName === "textarea" ) {
  136. dest.defaultValue = src.defaultValue;
  137. }
  138. }
  139. function domManip( collection, args, callback, ignored ) {
  140. // Flatten any nested arrays
  141. args = concat.apply( [], args );
  142. var first, node, hasScripts,
  143. scripts, doc, fragment,
  144. i = 0,
  145. l = collection.length,
  146. iNoClone = l - 1,
  147. value = args[ 0 ],
  148. isFunction = jQuery.isFunction( value );
  149. // We can't cloneNode fragments that contain checked, in WebKit
  150. if ( isFunction ||
  151. ( l > 1 && typeof value === "string" &&
  152. !support.checkClone && rchecked.test( value ) ) ) {
  153. return collection.each( function( index ) {
  154. var self = collection.eq( index );
  155. if ( isFunction ) {
  156. args[ 0 ] = value.call( this, index, self.html() );
  157. }
  158. domManip( self, args, callback, ignored );
  159. } );
  160. }
  161. if ( l ) {
  162. fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
  163. first = fragment.firstChild;
  164. if ( fragment.childNodes.length === 1 ) {
  165. fragment = first;
  166. }
  167. // Require either new content or an interest in ignored elements to invoke the callback
  168. if ( first || ignored ) {
  169. scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
  170. hasScripts = scripts.length;
  171. // Use the original fragment for the last item
  172. // instead of the first because it can end up
  173. // being emptied incorrectly in certain situations (#8070).
  174. for ( ; i < l; i++ ) {
  175. node = fragment;
  176. if ( i !== iNoClone ) {
  177. node = jQuery.clone( node, true, true );
  178. // Keep references to cloned scripts for later restoration
  179. if ( hasScripts ) {
  180. // Support: Android<4.1, PhantomJS<2
  181. // push.apply(_, arraylike) throws on ancient WebKit
  182. jQuery.merge( scripts, getAll( node, "script" ) );
  183. }
  184. }
  185. callback.call( collection[ i ], node, i );
  186. }
  187. if ( hasScripts ) {
  188. doc = scripts[ scripts.length - 1 ].ownerDocument;
  189. // Reenable scripts
  190. jQuery.map( scripts, restoreScript );
  191. // Evaluate executable scripts on first document insertion
  192. for ( i = 0; i < hasScripts; i++ ) {
  193. node = scripts[ i ];
  194. if ( rscriptType.test( node.type || "" ) &&
  195. !jQuery._data( node, "globalEval" ) &&
  196. jQuery.contains( doc, node ) ) {
  197. if ( node.src ) {
  198. // Optional AJAX dependency, but won't run scripts if not present
  199. if ( jQuery._evalUrl ) {
  200. jQuery._evalUrl( node.src );
  201. }
  202. } else {
  203. jQuery.globalEval(
  204. ( node.text || node.textContent || node.innerHTML || "" )
  205. .replace( rcleanScript, "" )
  206. );
  207. }
  208. }
  209. }
  210. }
  211. // Fix #11809: Avoid leaking memory
  212. fragment = first = null;
  213. }
  214. }
  215. return collection;
  216. }
  217. function remove( elem, selector, keepData ) {
  218. var node,
  219. elems = selector ? jQuery.filter( selector, elem ) : elem,
  220. i = 0;
  221. for ( ; ( node = elems[ i ] ) != null; i++ ) {
  222. if ( !keepData && node.nodeType === 1 ) {
  223. jQuery.cleanData( getAll( node ) );
  224. }
  225. if ( node.parentNode ) {
  226. if ( keepData && jQuery.contains( node.ownerDocument, node ) ) {
  227. setGlobalEval( getAll( node, "script" ) );
  228. }
  229. node.parentNode.removeChild( node );
  230. }
  231. }
  232. return elem;
  233. }
  234. jQuery.extend( {
  235. htmlPrefilter: function( html ) {
  236. return html.replace( rxhtmlTag, "<$1></$2>" );
  237. },
  238. clone: function( elem, dataAndEvents, deepDataAndEvents ) {
  239. var destElements, node, clone, i, srcElements,
  240. inPage = jQuery.contains( elem.ownerDocument, elem );
  241. if ( support.html5Clone || jQuery.isXMLDoc( elem ) ||
  242. !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) {
  243. clone = elem.cloneNode( true );
  244. // IE<=8 does not properly clone detached, unknown element nodes
  245. } else {
  246. fragmentDiv.innerHTML = elem.outerHTML;
  247. fragmentDiv.removeChild( clone = fragmentDiv.firstChild );
  248. }
  249. if ( ( !support.noCloneEvent || !support.noCloneChecked ) &&
  250. ( elem.nodeType === 1 || elem.nodeType === 11 ) && !jQuery.isXMLDoc( elem ) ) {
  251. // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
  252. destElements = getAll( clone );
  253. srcElements = getAll( elem );
  254. // Fix all IE cloning issues
  255. for ( i = 0; ( node = srcElements[ i ] ) != null; ++i ) {
  256. // Ensure that the destination node is not null; Fixes #9587
  257. if ( destElements[ i ] ) {
  258. fixCloneNodeIssues( node, destElements[ i ] );
  259. }
  260. }
  261. }
  262. // Copy the events from the original to the clone
  263. if ( dataAndEvents ) {
  264. if ( deepDataAndEvents ) {
  265. srcElements = srcElements || getAll( elem );
  266. destElements = destElements || getAll( clone );
  267. for ( i = 0; ( node = srcElements[ i ] ) != null; i++ ) {
  268. cloneCopyEvent( node, destElements[ i ] );
  269. }
  270. } else {
  271. cloneCopyEvent( elem, clone );
  272. }
  273. }
  274. // Preserve script evaluation history
  275. destElements = getAll( clone, "script" );
  276. if ( destElements.length > 0 ) {
  277. setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
  278. }
  279. destElements = srcElements = node = null;
  280. // Return the cloned set
  281. return clone;
  282. },
  283. cleanData: function( elems, /* internal */ forceAcceptData ) {
  284. var elem, type, id, data,
  285. i = 0,
  286. internalKey = jQuery.expando,
  287. cache = jQuery.cache,
  288. attributes = support.attributes,
  289. special = jQuery.event.special;
  290. for ( ; ( elem = elems[ i ] ) != null; i++ ) {
  291. if ( forceAcceptData || acceptData( elem ) ) {
  292. id = elem[ internalKey ];
  293. data = id && cache[ id ];
  294. if ( data ) {
  295. if ( data.events ) {
  296. for ( type in data.events ) {
  297. if ( special[ type ] ) {
  298. jQuery.event.remove( elem, type );
  299. // This is a shortcut to avoid jQuery.event.remove's overhead
  300. } else {
  301. jQuery.removeEvent( elem, type, data.handle );
  302. }
  303. }
  304. }
  305. // Remove cache only if it was not already removed by jQuery.event.remove
  306. if ( cache[ id ] ) {
  307. delete cache[ id ];
  308. // Support: IE<9
  309. // IE does not allow us to delete expando properties from nodes
  310. // IE creates expando attributes along with the property
  311. // IE does not have a removeAttribute function on Document nodes
  312. if ( !attributes && typeof elem.removeAttribute !== "undefined" ) {
  313. elem.removeAttribute( internalKey );
  314. // Webkit & Blink performance suffers when deleting properties
  315. // from DOM nodes, so set to undefined instead
  316. // https://code.google.com/p/chromium/issues/detail?id=378607
  317. } else {
  318. elem[ internalKey ] = undefined;
  319. }
  320. deletedIds.push( id );
  321. }
  322. }
  323. }
  324. }
  325. }
  326. } );
  327. jQuery.fn.extend( {
  328. // Keep domManip exposed until 3.0 (gh-2225)
  329. domManip: domManip,
  330. detach: function( selector ) {
  331. return remove( this, selector, true );
  332. },
  333. remove: function( selector ) {
  334. return remove( this, selector );
  335. },
  336. text: function( value ) {
  337. return access( this, function( value ) {
  338. return value === undefined ?
  339. jQuery.text( this ) :
  340. this.empty().append(
  341. ( this[ 0 ] && this[ 0 ].ownerDocument || document ).createTextNode( value )
  342. );
  343. }, null, value, arguments.length );
  344. },
  345. append: function() {
  346. return domManip( this, arguments, function( elem ) {
  347. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  348. var target = manipulationTarget( this, elem );
  349. target.appendChild( elem );
  350. }
  351. } );
  352. },
  353. prepend: function() {
  354. return domManip( this, arguments, function( elem ) {
  355. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  356. var target = manipulationTarget( this, elem );
  357. target.insertBefore( elem, target.firstChild );
  358. }
  359. } );
  360. },
  361. before: function() {
  362. return domManip( this, arguments, function( elem ) {
  363. if ( this.parentNode ) {
  364. this.parentNode.insertBefore( elem, this );
  365. }
  366. } );
  367. },
  368. after: function() {
  369. return domManip( this, arguments, function( elem ) {
  370. if ( this.parentNode ) {
  371. this.parentNode.insertBefore( elem, this.nextSibling );
  372. }
  373. } );
  374. },
  375. empty: function() {
  376. var elem,
  377. i = 0;
  378. for ( ; ( elem = this[ i ] ) != null; i++ ) {
  379. // Remove element nodes and prevent memory leaks
  380. if ( elem.nodeType === 1 ) {
  381. jQuery.cleanData( getAll( elem, false ) );
  382. }
  383. // Remove any remaining nodes
  384. while ( elem.firstChild ) {
  385. elem.removeChild( elem.firstChild );
  386. }
  387. // If this is a select, ensure that it displays empty (#12336)
  388. // Support: IE<9
  389. if ( elem.options && jQuery.nodeName( elem, "select" ) ) {
  390. elem.options.length = 0;
  391. }
  392. }
  393. return this;
  394. },
  395. clone: function( dataAndEvents, deepDataAndEvents ) {
  396. dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
  397. deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
  398. return this.map( function() {
  399. return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
  400. } );
  401. },
  402. html: function( value ) {
  403. return access( this, function( value ) {
  404. var elem = this[ 0 ] || {},
  405. i = 0,
  406. l = this.length;
  407. if ( value === undefined ) {
  408. return elem.nodeType === 1 ?
  409. elem.innerHTML.replace( rinlinejQuery, "" ) :
  410. undefined;
  411. }
  412. // See if we can take a shortcut and just use innerHTML
  413. if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
  414. ( support.htmlSerialize || !rnoshimcache.test( value ) ) &&
  415. ( support.leadingWhitespace || !rleadingWhitespace.test( value ) ) &&
  416. !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
  417. value = jQuery.htmlPrefilter( value );
  418. try {
  419. for ( ; i < l; i++ ) {
  420. // Remove element nodes and prevent memory leaks
  421. elem = this[ i ] || {};
  422. if ( elem.nodeType === 1 ) {
  423. jQuery.cleanData( getAll( elem, false ) );
  424. elem.innerHTML = value;
  425. }
  426. }
  427. elem = 0;
  428. // If using innerHTML throws an exception, use the fallback method
  429. } catch ( e ) {}
  430. }
  431. if ( elem ) {
  432. this.empty().append( value );
  433. }
  434. }, null, value, arguments.length );
  435. },
  436. replaceWith: function() {
  437. var ignored = [];
  438. // Make the changes, replacing each non-ignored context element with the new content
  439. return domManip( this, arguments, function( elem ) {
  440. var parent = this.parentNode;
  441. if ( jQuery.inArray( this, ignored ) < 0 ) {
  442. jQuery.cleanData( getAll( this ) );
  443. if ( parent ) {
  444. parent.replaceChild( elem, this );
  445. }
  446. }
  447. // Force callback invocation
  448. }, ignored );
  449. }
  450. } );
  451. jQuery.each( {
  452. appendTo: "append",
  453. prependTo: "prepend",
  454. insertBefore: "before",
  455. insertAfter: "after",
  456. replaceAll: "replaceWith"
  457. }, function( name, original ) {
  458. jQuery.fn[ name ] = function( selector ) {
  459. var elems,
  460. i = 0,
  461. ret = [],
  462. insert = jQuery( selector ),
  463. last = insert.length - 1;
  464. for ( ; i <= last; i++ ) {
  465. elems = i === last ? this : this.clone( true );
  466. jQuery( insert[ i ] )[ original ]( elems );
  467. // Modern browsers can apply jQuery collections as arrays, but oldIE needs a .get()
  468. push.apply( ret, elems.get() );
  469. }
  470. return this.pushStack( ret );
  471. };
  472. } );
  473. return jQuery;
  474. } );