PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/manipulation.js

https://gitlab.com/briankiragu/jquery
JavaScript | 478 lines | 348 code | 89 blank | 41 comment | 89 complexity | c522bc432f9b772c86b08e268daa5f55 MD5 | raw file
  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. var
  27. rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi,
  28. // Support: IE 10-11, Edge 10240+
  29. // In IE/Edge using regex groups here causes severe slowdowns.
  30. // See https://connect.microsoft.com/IE/feedback/details/1736512/
  31. rnoInnerhtml = /<script|<style|<link/i,
  32. // checked="checked" or checked
  33. rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
  34. rscriptTypeMasked = /^true\/(.*)/,
  35. rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
  36. function manipulationTarget( elem, content ) {
  37. if ( jQuery.nodeName( elem, "table" ) &&
  38. jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
  39. return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
  40. }
  41. return elem;
  42. }
  43. // Replace/restore the type attribute of script elements for safe DOM manipulation
  44. function disableScript( elem ) {
  45. elem.type = ( elem.getAttribute( "type" ) !== null ) + "/" + elem.type;
  46. return elem;
  47. }
  48. function restoreScript( elem ) {
  49. var match = rscriptTypeMasked.exec( elem.type );
  50. if ( match ) {
  51. elem.type = match[ 1 ];
  52. } else {
  53. elem.removeAttribute( "type" );
  54. }
  55. return elem;
  56. }
  57. function cloneCopyEvent( src, dest ) {
  58. var i, l, type, pdataOld, pdataCur, udataOld, udataCur, events;
  59. if ( dest.nodeType !== 1 ) {
  60. return;
  61. }
  62. // 1. Copy private data: events, handlers, etc.
  63. if ( dataPriv.hasData( src ) ) {
  64. pdataOld = dataPriv.access( src );
  65. pdataCur = dataPriv.set( dest, pdataOld );
  66. events = pdataOld.events;
  67. if ( events ) {
  68. delete pdataCur.handle;
  69. pdataCur.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 = concat.apply( [], 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. isFunction = jQuery.isFunction( value );
  104. // We can't cloneNode fragments that contain checked, in WebKit
  105. if ( isFunction ||
  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 ( isFunction ) {
  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.1, PhantomJS<2
  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 ) {
  153. // Optional AJAX dependency, but won't run scripts if not present
  154. if ( jQuery._evalUrl ) {
  155. jQuery._evalUrl( node.src );
  156. }
  157. } else {
  158. DOMEval( node.textContent.replace( rcleanScript, "" ), doc );
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return collection;
  166. }
  167. function remove( elem, selector, keepData ) {
  168. var node,
  169. nodes = selector ? jQuery.filter( selector, elem ) : elem,
  170. i = 0;
  171. for ( ; ( node = nodes[ i ] ) != null; i++ ) {
  172. if ( !keepData && node.nodeType === 1 ) {
  173. jQuery.cleanData( getAll( node ) );
  174. }
  175. if ( node.parentNode ) {
  176. if ( keepData && jQuery.contains( node.ownerDocument, node ) ) {
  177. setGlobalEval( getAll( node, "script" ) );
  178. }
  179. node.parentNode.removeChild( node );
  180. }
  181. }
  182. return elem;
  183. }
  184. jQuery.extend( {
  185. htmlPrefilter: function( html ) {
  186. return html.replace( rxhtmlTag, "<$1></$2>" );
  187. },
  188. clone: function( elem, dataAndEvents, deepDataAndEvents ) {
  189. var i, l, srcElements, destElements,
  190. clone = elem.cloneNode( true ),
  191. inPage = jQuery.contains( elem.ownerDocument, elem );
  192. // Fix IE cloning issues
  193. if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
  194. !jQuery.isXMLDoc( elem ) ) {
  195. // We eschew Sizzle here for performance reasons: https://jsperf.com/getall-vs-sizzle/2
  196. destElements = getAll( clone );
  197. srcElements = getAll( elem );
  198. for ( i = 0, l = srcElements.length; i < l; i++ ) {
  199. fixInput( srcElements[ i ], destElements[ i ] );
  200. }
  201. }
  202. // Copy the events from the original to the clone
  203. if ( dataAndEvents ) {
  204. if ( deepDataAndEvents ) {
  205. srcElements = srcElements || getAll( elem );
  206. destElements = destElements || getAll( clone );
  207. for ( i = 0, l = srcElements.length; i < l; i++ ) {
  208. cloneCopyEvent( srcElements[ i ], destElements[ i ] );
  209. }
  210. } else {
  211. cloneCopyEvent( elem, clone );
  212. }
  213. }
  214. // Preserve script evaluation history
  215. destElements = getAll( clone, "script" );
  216. if ( destElements.length > 0 ) {
  217. setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
  218. }
  219. // Return the cloned set
  220. return clone;
  221. },
  222. cleanData: function( elems ) {
  223. var data, elem, type,
  224. special = jQuery.event.special,
  225. i = 0;
  226. for ( ; ( elem = elems[ i ] ) !== undefined; i++ ) {
  227. if ( acceptData( elem ) ) {
  228. if ( ( data = elem[ dataPriv.expando ] ) ) {
  229. if ( data.events ) {
  230. for ( type in data.events ) {
  231. if ( special[ type ] ) {
  232. jQuery.event.remove( elem, type );
  233. // This is a shortcut to avoid jQuery.event.remove's overhead
  234. } else {
  235. jQuery.removeEvent( elem, type, data.handle );
  236. }
  237. }
  238. }
  239. // Support: Chrome <= 35-45+
  240. // Assign undefined instead of using delete, see Data#remove
  241. elem[ dataPriv.expando ] = undefined;
  242. }
  243. if ( elem[ dataUser.expando ] ) {
  244. // Support: Chrome <= 35-45+
  245. // Assign undefined instead of using delete, see Data#remove
  246. elem[ dataUser.expando ] = undefined;
  247. }
  248. }
  249. }
  250. }
  251. } );
  252. jQuery.fn.extend( {
  253. detach: function( selector ) {
  254. return remove( this, selector, true );
  255. },
  256. remove: function( selector ) {
  257. return remove( this, selector );
  258. },
  259. text: function( value ) {
  260. return access( this, function( value ) {
  261. return value === undefined ?
  262. jQuery.text( this ) :
  263. this.empty().each( function() {
  264. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  265. this.textContent = value;
  266. }
  267. } );
  268. }, null, value, arguments.length );
  269. },
  270. append: function() {
  271. return domManip( this, arguments, function( elem ) {
  272. if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
  273. var target = manipulationTarget( this, elem );
  274. target.appendChild( elem );
  275. }
  276. } );
  277. },
  278. prepend: 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.insertBefore( elem, target.firstChild );
  283. }
  284. } );
  285. },
  286. before: function() {
  287. return domManip( this, arguments, function( elem ) {
  288. if ( this.parentNode ) {
  289. this.parentNode.insertBefore( elem, this );
  290. }
  291. } );
  292. },
  293. after: function() {
  294. return domManip( this, arguments, function( elem ) {
  295. if ( this.parentNode ) {
  296. this.parentNode.insertBefore( elem, this.nextSibling );
  297. }
  298. } );
  299. },
  300. empty: function() {
  301. var elem,
  302. i = 0;
  303. for ( ; ( elem = this[ i ] ) != null; i++ ) {
  304. if ( elem.nodeType === 1 ) {
  305. // Prevent memory leaks
  306. jQuery.cleanData( getAll( elem, false ) );
  307. // Remove any remaining nodes
  308. elem.textContent = "";
  309. }
  310. }
  311. return this;
  312. },
  313. clone: function( dataAndEvents, deepDataAndEvents ) {
  314. dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
  315. deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
  316. return this.map( function() {
  317. return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
  318. } );
  319. },
  320. html: function( value ) {
  321. return access( this, function( value ) {
  322. var elem = this[ 0 ] || {},
  323. i = 0,
  324. l = this.length;
  325. if ( value === undefined && elem.nodeType === 1 ) {
  326. return elem.innerHTML;
  327. }
  328. // See if we can take a shortcut and just use innerHTML
  329. if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
  330. !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
  331. value = jQuery.htmlPrefilter( value );
  332. try {
  333. for ( ; i < l; i++ ) {
  334. elem = this[ i ] || {};
  335. // Remove element nodes and prevent memory leaks
  336. if ( elem.nodeType === 1 ) {
  337. jQuery.cleanData( getAll( elem, false ) );
  338. elem.innerHTML = value;
  339. }
  340. }
  341. elem = 0;
  342. // If using innerHTML throws an exception, use the fallback method
  343. } catch ( e ) {}
  344. }
  345. if ( elem ) {
  346. this.empty().append( value );
  347. }
  348. }, null, value, arguments.length );
  349. },
  350. replaceWith: function() {
  351. var ignored = [];
  352. // Make the changes, replacing each non-ignored context element with the new content
  353. return domManip( this, arguments, function( elem ) {
  354. var parent = this.parentNode;
  355. if ( jQuery.inArray( this, ignored ) < 0 ) {
  356. jQuery.cleanData( getAll( this ) );
  357. if ( parent ) {
  358. parent.replaceChild( elem, this );
  359. }
  360. }
  361. // Force callback invocation
  362. }, ignored );
  363. }
  364. } );
  365. jQuery.each( {
  366. appendTo: "append",
  367. prependTo: "prepend",
  368. insertBefore: "before",
  369. insertAfter: "after",
  370. replaceAll: "replaceWith"
  371. }, function( name, original ) {
  372. jQuery.fn[ name ] = function( selector ) {
  373. var elems,
  374. ret = [],
  375. insert = jQuery( selector ),
  376. last = insert.length - 1,
  377. i = 0;
  378. for ( ; i <= last; i++ ) {
  379. elems = i === last ? this : this.clone( true );
  380. jQuery( insert[ i ] )[ original ]( elems );
  381. // Support: Android<4.1, PhantomJS<2
  382. // .get() because push.apply(_, arraylike) throws on ancient WebKit
  383. push.apply( ret, elems.get() );
  384. }
  385. return this.pushStack( ret );
  386. };
  387. } );
  388. return jQuery;
  389. } );