jquery-migrate-1.0.0.js 16 KB

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