jquery.migrate.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*!
  2. * jQuery Migrate - v1.0.0 - 2013-01-14
  3. * https://github.com/jquery/jquery-migrate
  4. * Copyright 2005, 2013 jQuery Foundation, Inc. and other contributors; Licensed MIT
  5. */
  6. (function( jQuery, window, undefined ) {
  7. "use strict";
  8. var warnedAbout = {};
  9. // List of warnings already given; public read only
  10. jQuery.migrateWarnings = [];
  11. // Set to true to prevent console output; migrateWarnings still maintained
  12. // jQuery.migrateMute = false;
  13. // Forget any warnings we've already given; public
  14. jQuery.migrateReset = function() {
  15. warnedAbout = {};
  16. jQuery.migrateWarnings.length = 0;
  17. };
  18. function migrateWarn( msg) {
  19. if ( !warnedAbout[ msg ] ) {
  20. warnedAbout[ msg ] = true;
  21. jQuery.migrateWarnings.push( msg );
  22. if ( window.console && console.warn && !jQuery.migrateMute ) {
  23. }
  24. }
  25. }
  26. function migrateWarnProp( obj, prop, value, msg ) {
  27. if ( Object.defineProperty ) {
  28. // On ES5 browsers (non-oldIE), warn if the code tries to get prop;
  29. // allow property to be overwritten in case some other plugin wants it
  30. try {
  31. Object.defineProperty( obj, prop, {
  32. configurable: true,
  33. enumerable: true,
  34. get: function() {
  35. migrateWarn( msg );
  36. return value;
  37. },
  38. set: function( newValue ) {
  39. migrateWarn( msg );
  40. value = newValue;
  41. }
  42. });
  43. return;
  44. } catch( err ) {
  45. // IE8 is a dope about Object.defineProperty, can't warn there
  46. }
  47. }
  48. // Non-ES5 (or broken) browser; just set the property
  49. jQuery._definePropertyBroken = true;
  50. obj[ prop ] = value;
  51. }
  52. if ( document.compatMode === "BackCompat" ) {
  53. // jQuery has never supported or tested Quirks Mode
  54. migrateWarn( "jQuery is not compatible with Quirks Mode" );
  55. }
  56. var attrFn = {},
  57. attr = jQuery.attr,
  58. valueAttrGet = jQuery.attrHooks.value && jQuery.attrHooks.value.get ||
  59. function() { return null; },
  60. valueAttrSet = jQuery.attrHooks.value && jQuery.attrHooks.value.set ||
  61. function() { return undefined; },
  62. rnoType = /^(?:input|button)$/i,
  63. rnoAttrNodeType = /^[238]$/,
  64. rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i,
  65. ruseDefault = /^(?:checked|selected)$/i;
  66. // jQuery.attrFn
  67. migrateWarnProp( jQuery, "attrFn", attrFn, "jQuery.attrFn is deprecated" );
  68. jQuery.attr = function( elem, name, value, pass ) {
  69. var lowerName = name.toLowerCase(),
  70. nType = elem && elem.nodeType;
  71. if ( pass ) {
  72. migrateWarn("jQuery.fn.attr( props, pass ) is deprecated");
  73. if ( elem && !rnoAttrNodeType.test( nType ) && jQuery.isFunction( jQuery.fn[ name ] ) ) {
  74. return jQuery( elem )[ name ]( value );
  75. }
  76. }
  77. // Warn if user tries to set `type` since it breaks on IE 6/7/8
  78. if ( name === "type" && value !== undefined && rnoType.test( elem.nodeName ) ) {
  79. migrateWarn("Can't change the 'type' of an input or button in IE 6/7/8");
  80. }
  81. // Restore boolHook for boolean property/attribute synchronization
  82. if ( !jQuery.attrHooks[ lowerName ] && rboolean.test( lowerName ) ) {
  83. jQuery.attrHooks[ lowerName ] = {
  84. get: function( elem, name ) {
  85. // Align boolean attributes with corresponding properties
  86. // Fall back to attribute presence where some booleans are not supported
  87. var attrNode,
  88. property = jQuery.prop( elem, name );
  89. return property === true || typeof property !== "boolean" &&
  90. ( attrNode = elem.getAttributeNode(name) ) && attrNode.nodeValue !== false ?
  91. name.toLowerCase() :
  92. undefined;
  93. },
  94. set: function( elem, value, name ) {
  95. var propName;
  96. if ( value === false ) {
  97. // Remove boolean attributes when set to false
  98. jQuery.removeAttr( elem, name );
  99. } else {
  100. // value is true since we know at this point it's type boolean and not false
  101. // Set boolean attributes to the same name and set the DOM property
  102. propName = jQuery.propFix[ name ] || name;
  103. if ( propName in elem ) {
  104. // Only set the IDL specifically if it already exists on the element
  105. elem[ propName ] = true;
  106. }
  107. elem.setAttribute( name, name.toLowerCase() );
  108. }
  109. return name;
  110. }
  111. };
  112. // Warn only for attributes that can remain distinct from their properties post-1.9
  113. if ( ruseDefault.test( lowerName ) ) {
  114. migrateWarn( "jQuery.fn.attr(" + lowerName + ") may use property instead of attribute" );
  115. }
  116. }
  117. return attr.call( jQuery, elem, name, value );
  118. };
  119. // attrHooks: value
  120. jQuery.attrHooks.value = {
  121. get: function( elem, name ) {
  122. var nodeName = ( elem.nodeName || "" ).toLowerCase();
  123. if ( nodeName === "button" ) {
  124. return valueAttrGet.apply( this, arguments );
  125. }
  126. if ( nodeName !== "input" && nodeName !== "option" ) {
  127. migrateWarn("property-based jQuery.fn.attr('value') is deprecated");
  128. }
  129. return name in elem ?
  130. elem.value :
  131. null;
  132. },
  133. set: function( elem, value ) {
  134. var nodeName = ( elem.nodeName || "" ).toLowerCase();
  135. if ( nodeName === "button" ) {
  136. return valueAttrSet.apply( this, arguments );
  137. }
  138. if ( nodeName !== "input" && nodeName !== "option" ) {
  139. migrateWarn("property-based jQuery.fn.attr('value', val) is deprecated");
  140. }
  141. // Does not return so that setAttribute is also used
  142. elem.value = value;
  143. }
  144. };
  145. var matched, browser,
  146. oldInit = jQuery.fn.init,
  147. // Note this does NOT include the # XSS fix from 1.7!
  148. rquickExpr = /^(?:.*(<[\w\W]+>)[^>]*|#([\w\-]*))$/;
  149. // $(html) "looks like html" rule change
  150. jQuery.fn.init = function( selector, context, rootjQuery ) {
  151. var match;
  152. if ( selector && typeof selector === "string" && !jQuery.isPlainObject( context ) &&
  153. (match = rquickExpr.exec( selector )) && match[1] ) {
  154. // This is an HTML string according to the "old" rules; is it still?
  155. if ( selector.charAt( 0 ) !== "<" ) {
  156. migrateWarn("$(html) HTML strings must start with '<' character");
  157. }
  158. // Now process using loose rules; let pre-1.8 play too
  159. if ( context && context.context ) {
  160. // jQuery object as context; parseHTML expects a DOM object
  161. context = context.context;
  162. }
  163. if ( jQuery.parseHTML ) {
  164. return oldInit.call( this, jQuery.parseHTML( jQuery.trim(selector), context, true ),
  165. context, rootjQuery );
  166. }
  167. }
  168. return oldInit.apply( this, arguments );
  169. };
  170. jQuery.fn.init.prototype = jQuery.fn;
  171. jQuery.uaMatch = function( ua ) {
  172. ua = ua.toLowerCase();
  173. var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
  174. /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
  175. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
  176. /(msie) ([\w.]+)/.exec( ua ) ||
  177. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
  178. [];
  179. return {
  180. browser: match[ 1 ] || "",
  181. version: match[ 2 ] || "0"
  182. };
  183. };
  184. matched = jQuery.uaMatch( navigator.userAgent );
  185. browser = {};
  186. if ( matched.browser ) {
  187. browser[ matched.browser ] = true;
  188. browser.version = matched.version;
  189. }
  190. // Chrome is Webkit, but Webkit is also Safari.
  191. if ( browser.chrome ) {
  192. browser.webkit = true;
  193. } else if ( browser.webkit ) {
  194. browser.safari = true;
  195. }
  196. jQuery.browser = browser;
  197. // Warn if the code tries to get jQuery.browser
  198. migrateWarnProp( jQuery, "browser", browser, "jQuery.browser is deprecated" );
  199. jQuery.sub = function() {
  200. function jQuerySub( selector, context ) {
  201. return new jQuerySub.fn.init( selector, context );
  202. }
  203. jQuery.extend( true, jQuerySub, this );
  204. jQuerySub.superclass = this;
  205. jQuerySub.fn = jQuerySub.prototype = this();
  206. jQuerySub.fn.constructor = jQuerySub;
  207. jQuerySub.sub = this.sub;
  208. jQuerySub.fn.init = function init( selector, context ) {
  209. if ( context && context instanceof jQuery && !(context instanceof jQuerySub) ) {
  210. context = jQuerySub( context );
  211. }
  212. return jQuery.fn.init.call( this, selector, context, rootjQuerySub );
  213. };
  214. jQuerySub.fn.init.prototype = jQuerySub.fn;
  215. var rootjQuerySub = jQuerySub(document);
  216. migrateWarn( "jQuery.sub() is deprecated" );
  217. return jQuerySub;
  218. };
  219. var oldFnData = jQuery.fn.data;
  220. jQuery.fn.data = function( name ) {
  221. var ret, evt,
  222. elem = this[0];
  223. // Handles 1.7 which has this behavior and 1.8 which doesn't
  224. if ( elem && name === "events" && arguments.length === 1 ) {
  225. ret = jQuery.data( elem, name );
  226. evt = jQuery._data( elem, name );
  227. if ( ( ret === undefined || ret === evt ) && evt !== undefined ) {
  228. migrateWarn("Use of jQuery.fn.data('events') is deprecated");
  229. return evt;
  230. }
  231. }
  232. return oldFnData.apply( this, arguments );
  233. };
  234. var rscriptType = /\/(java|ecma)script/i,
  235. oldSelf = jQuery.fn.andSelf || jQuery.fn.addBack,
  236. oldFragment = jQuery.buildFragment;
  237. jQuery.fn.andSelf = function() {
  238. migrateWarn("jQuery.fn.andSelf() replaced by jQuery.fn.addBack()");
  239. return oldSelf.apply( this, arguments );
  240. };
  241. // Since jQuery.clean is used internally on older versions, we only shim if it's missing
  242. if ( !jQuery.clean ) {
  243. jQuery.clean = function( elems, context, fragment, scripts ) {
  244. // Set context per 1.8 logic
  245. context = context || document;
  246. context = !context.nodeType && context[0] || context;
  247. context = context.ownerDocument || context;
  248. migrateWarn("jQuery.clean() is deprecated");
  249. var i, elem, handleScript, jsTags,
  250. ret = [];
  251. jQuery.merge( ret, jQuery.buildFragment( elems, context ).childNodes );
  252. // Complex logic lifted directly from jQuery 1.8
  253. if ( fragment ) {
  254. // Special handling of each script element
  255. handleScript = function( elem ) {
  256. // Check if we consider it executable
  257. if ( !elem.type || rscriptType.test( elem.type ) ) {
  258. // Detach the script and store it in the scripts array (if provided) or the fragment
  259. // Return truthy to indicate that it has been handled
  260. return scripts ?
  261. scripts.push( elem.parentNode ? elem.parentNode.removeChild( elem ) : elem ) :
  262. fragment.appendChild( elem );
  263. }
  264. };
  265. for ( i = 0; (elem = ret[i]) != null; i++ ) {
  266. // Check if we're done after handling an executable script
  267. if ( !( jQuery.nodeName( elem, "script" ) && handleScript( elem ) ) ) {
  268. // Append to fragment and handle embedded scripts
  269. fragment.appendChild( elem );
  270. if ( typeof elem.getElementsByTagName !== "undefined" ) {
  271. // handleScript alters the DOM, so use jQuery.merge to ensure snapshot iteration
  272. jsTags = jQuery.grep( jQuery.merge( [], elem.getElementsByTagName("script") ), handleScript );
  273. // Splice the scripts into ret after their former ancestor and advance our index beyond them
  274. ret.splice.apply( ret, [i + 1, 0].concat( jsTags ) );
  275. i += jsTags.length;
  276. }
  277. }
  278. }
  279. }
  280. return ret;
  281. };
  282. }
  283. jQuery.buildFragment = function( elems, context, scripts, selection ) {
  284. var ret,
  285. warning = "jQuery.buildFragment() is deprecated";
  286. // Set context per 1.8 logic
  287. context = context || document;
  288. context = !context.nodeType && context[0] || context;
  289. context = context.ownerDocument || context;
  290. try {
  291. ret = oldFragment.call( jQuery, elems, context, scripts, selection );
  292. // jQuery < 1.8 required arrayish context; jQuery 1.9 fails on it
  293. } catch( x ) {
  294. ret = oldFragment.call( jQuery, elems, context.nodeType ? [ context ] : context[ 0 ], scripts, selection );
  295. // Success from tweaking context means buildFragment was called by the user
  296. migrateWarn( warning );
  297. }
  298. // jQuery < 1.9 returned an object instead of the fragment itself
  299. if ( !ret.fragment ) {
  300. migrateWarnProp( ret, "fragment", ret, warning );
  301. migrateWarnProp( ret, "cacheable", false, warning );
  302. }
  303. return ret;
  304. };
  305. var eventAdd = jQuery.event.add,
  306. eventRemove = jQuery.event.remove,
  307. eventTrigger = jQuery.event.trigger,
  308. oldToggle = jQuery.fn.toggle,
  309. oldLive = jQuery.fn.live,
  310. oldDie = jQuery.fn.die,
  311. ajaxEvents = "ajaxStart|ajaxStop|ajaxSend|ajaxComplete|ajaxError|ajaxSuccess",
  312. rajaxEvent = new RegExp( "\\b(?:" + ajaxEvents + ")\\b" ),
  313. rhoverHack = /(?:^|\s)hover(\.\S+|)\b/,
  314. hoverHack = function( events ) {
  315. if ( typeof( events ) != "string" || jQuery.event.special.hover ) {
  316. return events;
  317. }
  318. if ( rhoverHack.test( events ) ) {
  319. migrateWarn("'hover' pseudo-event is deprecated, use 'mouseenter mouseleave'");
  320. }
  321. return events && events.replace( rhoverHack, "mouseenter$1 mouseleave$1" );
  322. };
  323. // Event props removed in 1.9, put them back if needed; no practical way to warn them
  324. if ( jQuery.event.props && jQuery.event.props[ 0 ] !== "attrChange" ) {
  325. jQuery.event.props.unshift( "attrChange", "attrName", "relatedNode", "srcElement" );
  326. }
  327. // Undocumented jQuery.event.handle was "deprecated" in jQuery 1.7
  328. migrateWarnProp( jQuery.event, "handle", jQuery.event.dispatch, "jQuery.event.handle is undocumented and deprecated" );
  329. // Support for 'hover' pseudo-event and ajax event warnings
  330. jQuery.event.add = function( elem, types, handler, data, selector ){
  331. if ( elem !== document && rajaxEvent.test( types ) ) {
  332. migrateWarn( "AJAX events should be attached to document: " + types );
  333. }
  334. eventAdd.call( this, elem, hoverHack( types || "" ), handler, data, selector );
  335. };
  336. jQuery.event.remove = function( elem, types, handler, selector, mappedTypes ){
  337. eventRemove.call( this, elem, hoverHack( types ) || "", handler, selector, mappedTypes );
  338. };
  339. jQuery.fn.error = function() {
  340. var args = Array.prototype.slice.call( arguments, 0);
  341. migrateWarn("jQuery.fn.error() is deprecated");
  342. args.splice( 0, 0, "error" );
  343. if ( arguments.length ) {
  344. return this.bind.apply( this, args );
  345. }
  346. // error event should not bubble to window, although it does pre-1.7
  347. this.triggerHandler.apply( this, args );
  348. return this;
  349. };
  350. jQuery.fn.toggle = function( fn, fn2 ) {
  351. // Don't mess with animation or css toggles
  352. if ( !jQuery.isFunction( fn ) || !jQuery.isFunction( fn2 ) ) {
  353. return oldToggle.apply( this, arguments );
  354. }
  355. migrateWarn("jQuery.fn.toggle(handler, handler...) is deprecated");
  356. // Save reference to arguments for access in closure
  357. var args = arguments,
  358. guid = fn.guid || jQuery.guid++,
  359. i = 0,
  360. toggler = function( event ) {
  361. // Figure out which function to execute
  362. var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
  363. jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
  364. // Make sure that clicks stop
  365. event.preventDefault();
  366. // and execute the function
  367. return args[ lastToggle ].apply( this, arguments ) || false;
  368. };
  369. // link all the functions, so any of them can unbind this click handler
  370. toggler.guid = guid;
  371. while ( i < args.length ) {
  372. args[ i++ ].guid = guid;
  373. }
  374. return this.click( toggler );
  375. };
  376. jQuery.fn.live = function( types, data, fn ) {
  377. migrateWarn("jQuery.fn.live() is deprecated");
  378. if ( oldLive ) {
  379. return oldLive.apply( this, arguments );
  380. }
  381. jQuery( this.context ).on( types, this.selector, data, fn );
  382. return this;
  383. };
  384. jQuery.fn.die = function( types, fn ) {
  385. migrateWarn("jQuery.fn.die() is deprecated");
  386. if ( oldDie ) {
  387. return oldDie.apply( this, arguments );
  388. }
  389. jQuery( this.context ).off( types, this.selector || "**", fn );
  390. return this;
  391. };
  392. // Turn global events into document-triggered events
  393. jQuery.event.trigger = function( event, data, elem, onlyHandlers ){
  394. if ( !elem & !rajaxEvent.test( event ) ) {
  395. migrateWarn( "Global events are undocumented and deprecated" );
  396. }
  397. return eventTrigger.call( this, event, data, elem || document, onlyHandlers );
  398. };
  399. jQuery.each( ajaxEvents.split("|"),
  400. function( _, name ) {
  401. jQuery.event.special[ name ] = {
  402. setup: function() {
  403. var elem = this;
  404. // The document needs no shimming; must be !== for oldIE
  405. if ( elem !== document ) {
  406. jQuery.event.add( document, name + "." + jQuery.guid, function() {
  407. jQuery.event.trigger( name, null, elem, true );
  408. });
  409. jQuery._data( this, name, jQuery.guid++ );
  410. }
  411. return false;
  412. },
  413. teardown: function() {
  414. if ( this !== document ) {
  415. jQuery.event.remove( document, name + "." + jQuery._data( this, name ) );
  416. }
  417. return false;
  418. }
  419. };
  420. }
  421. );
  422. })( jQuery, window );