jquery-migrate.js 16 KB

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