]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/lib/jquery/jquery.migrate.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / resources / lib / jquery / jquery.migrate.js
1 /*!
2  * jQuery Migrate - v3.0.1-pre - 2017-08-17
3  * Copyright jQuery Foundation and other contributors
4  *
5  * Patched for MediaWiki:
6  * - Preserve handler of uncaught exceptions in promise chains
7  *   https://gerrit.wikimedia.org/r/#/c/360999/
8  *   https://github.com/jquery/jquery-migrate/pull/262
9  * - Add mw.track instrumentation for statistics.
10  */
11 ;( function( factory ) {
12         if ( typeof define === "function" && define.amd ) {
13
14                 // AMD. Register as an anonymous module.
15                 define( [ "jquery" ], window, factory );
16         } else if ( typeof module === "object" && module.exports ) {
17
18                 // Node/CommonJS
19                 // eslint-disable-next-line no-undef
20                 module.exports = factory( require( "jquery" ), window );
21         } else {
22
23                 // Browser globals
24                 factory( jQuery, window );
25         }
26 } )( function( jQuery, window ) {
27 "use strict";
28
29
30 jQuery.migrateVersion = "3.0.1-pre";
31
32 /* exported migrateWarn, migrateWarnFunc, migrateWarnProp */
33
34 ( function() {
35
36         // Support: IE9 only
37         // IE9 only creates console object when dev tools are first opened
38         // Also, avoid Function#bind here to simplify PhantomJS usage
39         var log = window.console && window.console.log &&
40                 function() {
41                         window.console.log.apply( window.console, arguments );
42                 },
43                 rbadVersions = /^[12]\./;
44
45         if ( !log ) {
46                 return;
47         }
48
49         // Need jQuery 3.0.0+ and no older Migrate loaded
50         if ( !jQuery || rbadVersions.test( jQuery.fn.jquery ) ) {
51                 log( "JQMIGRATE: jQuery 3.0.0+ REQUIRED" );
52         }
53         if ( jQuery.migrateWarnings ) {
54                 log( "JQMIGRATE: Migrate plugin loaded multiple times" );
55         }
56
57         // Show a message on the console so devs know we're active
58         log( "JQMIGRATE: Migrate is installed" +
59                 ( jQuery.migrateMute ? "" : " with logging active" ) +
60                 ", version " + jQuery.migrateVersion );
61
62 } )();
63
64 var warnedAbout = {};
65
66 // List of warnings already given; public read only
67 jQuery.migrateWarnings = [];
68
69 // Set to false to disable traces that appear with warnings
70 if ( jQuery.migrateTrace === undefined ) {
71         jQuery.migrateTrace = true;
72 }
73
74 // Forget any warnings we've already given; public
75 jQuery.migrateReset = function() {
76         warnedAbout = {};
77         jQuery.migrateWarnings.length = 0;
78 };
79
80 function migrateWarn( msg ) {
81         var console = window.console;
82         if ( !warnedAbout[ msg ] ) {
83                 warnedAbout[ msg ] = true;
84                 jQuery.migrateWarnings.push( msg );
85                 // PATCH: Add instrumentation for statistics --Krinkle
86                 if ( window.mw && window.mw.track ) {
87                         window.mw.track( "mw.deprecate", "jquery-migrate" );
88                 }
89                 if ( console && console.warn && !jQuery.migrateMute ) {
90                         console.warn( "JQMIGRATE: " + msg );
91                         if ( jQuery.migrateTrace && console.trace ) {
92                                 console.trace();
93                         }
94                 }
95         }
96 }
97
98 function migrateWarnProp( obj, prop, value, msg ) {
99         Object.defineProperty( obj, prop, {
100                 configurable: true,
101                 enumerable: true,
102                 get: function() {
103                         migrateWarn( msg );
104                         return value;
105                 },
106                 set: function( newValue ) {
107                         migrateWarn( msg );
108                         value = newValue;
109                 }
110         } );
111 }
112
113 function migrateWarnFunc( obj, prop, newFunc, msg ) {
114         obj[ prop ] = function() {
115                 migrateWarn( msg );
116                 return newFunc.apply( this, arguments );
117         };
118 }
119
120 if ( window.document.compatMode === "BackCompat" ) {
121
122         // JQuery has never supported or tested Quirks Mode
123         migrateWarn( "jQuery is not compatible with Quirks Mode" );
124 }
125
126
127 var oldInit = jQuery.fn.init,
128         oldIsNumeric = jQuery.isNumeric,
129         oldFind = jQuery.find,
130         rattrHashTest = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/,
131         rattrHashGlob = /\[(\s*[-\w]+\s*)([~|^$*]?=)\s*([-\w#]*?#[-\w#]*)\s*\]/g;
132
133 jQuery.fn.init = function( arg1 ) {
134         var args = Array.prototype.slice.call( arguments );
135
136         if ( typeof arg1 === "string" && arg1 === "#" ) {
137
138                 // JQuery( "#" ) is a bogus ID selector, but it returned an empty set before jQuery 3.0
139                 migrateWarn( "jQuery( '#' ) is not a valid selector" );
140                 args[ 0 ] = [];
141         }
142
143         return oldInit.apply( this, args );
144 };
145 jQuery.fn.init.prototype = jQuery.fn;
146
147 jQuery.find = function( selector ) {
148         var args = Array.prototype.slice.call( arguments );
149
150         // Support: PhantomJS 1.x
151         // String#match fails to match when used with a //g RegExp, only on some strings
152         if ( typeof selector === "string" && rattrHashTest.test( selector ) ) {
153
154                 // The nonstandard and undocumented unquoted-hash was removed in jQuery 1.12.0
155                 // First see if qS thinks it's a valid selector, if so avoid a false positive
156                 try {
157                         window.document.querySelector( selector );
158                 } catch ( err1 ) {
159
160                         // Didn't *look* valid to qSA, warn and try quoting what we think is the value
161                         selector = selector.replace( rattrHashGlob, function( _, attr, op, value ) {
162                                 return "[" + attr + op + "\"" + value + "\"]";
163                         } );
164
165                         // If the regexp *may* have created an invalid selector, don't update it
166                         // Note that there may be false alarms if selector uses jQuery extensions
167                         try {
168                                 window.document.querySelector( selector );
169                                 migrateWarn( "Attribute selector with '#' must be quoted: " + args[ 0 ] );
170                                 args[ 0 ] = selector;
171                         } catch ( err2 ) {
172                                 migrateWarn( "Attribute selector with '#' was not fixed: " + args[ 0 ] );
173                         }
174                 }
175         }
176
177         return oldFind.apply( this, args );
178 };
179
180 // Copy properties attached to original jQuery.find method (e.g. .attr, .isXML)
181 var findProp;
182 for ( findProp in oldFind ) {
183         if ( Object.prototype.hasOwnProperty.call( oldFind, findProp ) ) {
184                 jQuery.find[ findProp ] = oldFind[ findProp ];
185         }
186 }
187
188 // The number of elements contained in the matched element set
189 jQuery.fn.size = function() {
190         migrateWarn( "jQuery.fn.size() is deprecated and removed; use the .length property" );
191         return this.length;
192 };
193
194 jQuery.parseJSON = function() {
195         migrateWarn( "jQuery.parseJSON is deprecated; use JSON.parse" );
196         return JSON.parse.apply( null, arguments );
197 };
198
199 jQuery.isNumeric = function( val ) {
200
201         // The jQuery 2.2.3 implementation of isNumeric
202         function isNumeric2( obj ) {
203                 var realStringObj = obj && obj.toString();
204                 return !jQuery.isArray( obj ) && ( realStringObj - parseFloat( realStringObj ) + 1 ) >= 0;
205         }
206
207         var newValue = oldIsNumeric( val ),
208                 oldValue = isNumeric2( val );
209
210         if ( newValue !== oldValue ) {
211                 migrateWarn( "jQuery.isNumeric() should not be called on constructed objects" );
212         }
213
214         return oldValue;
215 };
216
217 migrateWarnFunc( jQuery, "unique", jQuery.uniqueSort,
218         "jQuery.unique is deprecated; use jQuery.uniqueSort" );
219
220 // Now jQuery.expr.pseudos is the standard incantation
221 migrateWarnProp( jQuery.expr, "filters", jQuery.expr.pseudos,
222         "jQuery.expr.filters is deprecated; use jQuery.expr.pseudos" );
223 migrateWarnProp( jQuery.expr, ":", jQuery.expr.pseudos,
224         "jQuery.expr[':'] is deprecated; use jQuery.expr.pseudos" );
225
226
227 var oldAjax = jQuery.ajax;
228
229 jQuery.ajax = function( ) {
230         var jQXHR = oldAjax.apply( this, arguments );
231
232         // Be sure we got a jQXHR (e.g., not sync)
233         if ( jQXHR.promise ) {
234                 migrateWarnFunc( jQXHR, "success", jQXHR.done,
235                         "jQXHR.success is deprecated and removed" );
236                 migrateWarnFunc( jQXHR, "error", jQXHR.fail,
237                         "jQXHR.error is deprecated and removed" );
238                 migrateWarnFunc( jQXHR, "complete", jQXHR.always,
239                         "jQXHR.complete is deprecated and removed" );
240         }
241
242         return jQXHR;
243 };
244
245
246 var oldRemoveAttr = jQuery.fn.removeAttr,
247         oldToggleClass = jQuery.fn.toggleClass,
248         rmatchNonSpace = /\S+/g;
249
250 jQuery.fn.removeAttr = function( name ) {
251         var self = this;
252
253         jQuery.each( name.match( rmatchNonSpace ), function( i, attr ) {
254                 if ( jQuery.expr.match.bool.test( attr ) ) {
255                         migrateWarn( "jQuery.fn.removeAttr no longer sets boolean properties: " + attr );
256                         self.prop( attr, false );
257                 }
258         } );
259
260         return oldRemoveAttr.apply( this, arguments );
261 };
262
263 jQuery.fn.toggleClass = function( state ) {
264
265         // Only deprecating no-args or single boolean arg
266         if ( state !== undefined && typeof state !== "boolean" ) {
267                 return oldToggleClass.apply( this, arguments );
268         }
269
270         migrateWarn( "jQuery.fn.toggleClass( boolean ) is deprecated" );
271
272         // Toggle entire class name of each element
273         return this.each( function() {
274                 var className = this.getAttribute && this.getAttribute( "class" ) || "";
275
276                 if ( className ) {
277                         jQuery.data( this, "__className__", className );
278                 }
279
280                 // If the element has a class name or if we're passed `false`,
281                 // then remove the whole classname (if there was one, the above saved it).
282                 // Otherwise bring back whatever was previously saved (if anything),
283                 // falling back to the empty string if nothing was stored.
284                 if ( this.setAttribute ) {
285                         this.setAttribute( "class",
286                                 className || state === false ?
287                                 "" :
288                                 jQuery.data( this, "__className__" ) || ""
289                         );
290                 }
291         } );
292 };
293
294
295 var internalSwapCall = false;
296
297 // If this version of jQuery has .swap(), don't false-alarm on internal uses
298 if ( jQuery.swap ) {
299         jQuery.each( [ "height", "width", "reliableMarginRight" ], function( _, name ) {
300                 var oldHook = jQuery.cssHooks[ name ] && jQuery.cssHooks[ name ].get;
301
302                 if ( oldHook ) {
303                         jQuery.cssHooks[ name ].get = function() {
304                                 var ret;
305
306                                 internalSwapCall = true;
307                                 ret = oldHook.apply( this, arguments );
308                                 internalSwapCall = false;
309                                 return ret;
310                         };
311                 }
312         } );
313 }
314
315 jQuery.swap = function( elem, options, callback, args ) {
316         var ret, name,
317                 old = {};
318
319         if ( !internalSwapCall ) {
320                 migrateWarn( "jQuery.swap() is undocumented and deprecated" );
321         }
322
323         // Remember the old values, and insert the new ones
324         for ( name in options ) {
325                 old[ name ] = elem.style[ name ];
326                 elem.style[ name ] = options[ name ];
327         }
328
329         ret = callback.apply( elem, args || [] );
330
331         // Revert the old values
332         for ( name in options ) {
333                 elem.style[ name ] = old[ name ];
334         }
335
336         return ret;
337 };
338
339 var oldData = jQuery.data;
340
341 jQuery.data = function( elem, name, value ) {
342         var curData;
343
344         // Name can be an object, and each entry in the object is meant to be set as data
345         if ( name && typeof name === "object" && arguments.length === 2 ) {
346                 curData = jQuery.hasData( elem ) && oldData.call( this, elem );
347                 var sameKeys = {};
348                 for ( var key in name ) {
349                         if ( key !== jQuery.camelCase( key ) ) {
350                                 migrateWarn( "jQuery.data() always sets/gets camelCased names: " + key );
351                                 curData[ key ] = name[ key ];
352                         } else {
353                                 sameKeys[ key ] = name[ key ];
354                         }
355                 }
356
357                 oldData.call( this, elem, sameKeys );
358
359                 return name;
360         }
361
362         // If the name is transformed, look for the un-transformed name in the data object
363         if ( name && typeof name === "string" && name !== jQuery.camelCase( name ) ) {
364                 curData = jQuery.hasData( elem ) && oldData.call( this, elem );
365                 if ( curData && name in curData ) {
366                         migrateWarn( "jQuery.data() always sets/gets camelCased names: " + name );
367                         if ( arguments.length > 2 ) {
368                                 curData[ name ] = value;
369                         }
370                         return curData[ name ];
371                 }
372         }
373
374         return oldData.apply( this, arguments );
375 };
376
377 var oldTweenRun = jQuery.Tween.prototype.run;
378
379 jQuery.Tween.prototype.run = function( ) {
380         if ( jQuery.easing[ this.easing ].length > 1 ) {
381                 migrateWarn(
382                         "easing function " +
383                         "\"jQuery.easing." + this.easing.toString() +
384                         "\" should use only first argument"
385                 );
386
387                 var oldEasing = jQuery.easing[ this.easing ];
388                 jQuery.easing[ this.easing ] = function( percent ) {
389                         return oldEasing.call( jQuery.easing, percent, percent, 0, 1, 1 );
390                 }.bind( this );
391         }
392
393         oldTweenRun.apply( this, arguments );
394 };
395
396 jQuery.fx.interval = jQuery.fx.interval || 13;
397
398 // Support: IE9, Android <=4.4
399 // Avoid false positives on browsers that lack rAF
400 if ( window.requestAnimationFrame ) {
401         migrateWarnProp( jQuery.fx, "interval", jQuery.fx.interval,
402                 "jQuery.fx.interval is deprecated" );
403 }
404
405 var oldLoad = jQuery.fn.load,
406         oldEventAdd = jQuery.event.add,
407         originalFix = jQuery.event.fix;
408
409 jQuery.event.props = [];
410 jQuery.event.fixHooks = {};
411
412 migrateWarnProp( jQuery.event.props, "concat", jQuery.event.props.concat,
413         "jQuery.event.props.concat() is deprecated and removed" );
414
415 jQuery.event.fix = function( originalEvent ) {
416         var event,
417                 type = originalEvent.type,
418                 fixHook = this.fixHooks[ type ],
419                 props = jQuery.event.props;
420
421         if ( props.length ) {
422                 migrateWarn( "jQuery.event.props are deprecated and removed: " + props.join() );
423                 while ( props.length ) {
424                         jQuery.event.addProp( props.pop() );
425                 }
426         }
427
428         if ( fixHook && !fixHook._migrated_ ) {
429                 fixHook._migrated_ = true;
430                 migrateWarn( "jQuery.event.fixHooks are deprecated and removed: " + type );
431                 if ( ( props = fixHook.props ) && props.length ) {
432                         while ( props.length ) {
433                                 jQuery.event.addProp( props.pop() );
434                         }
435                 }
436         }
437
438         event = originalFix.call( this, originalEvent );
439
440         return fixHook && fixHook.filter ? fixHook.filter( event, originalEvent ) : event;
441 };
442
443 jQuery.event.add = function( elem, types ) {
444
445         // This misses the multiple-types case but that seems awfully rare
446         if ( elem === window && types === "load" && window.document.readyState === "complete" ) {
447                 migrateWarn( "jQuery(window).on('load'...) called after load event occurred" );
448         }
449         return oldEventAdd.apply( this, arguments );
450 };
451
452 jQuery.each( [ "load", "unload", "error" ], function( _, name ) {
453
454         jQuery.fn[ name ] = function() {
455                 var args = Array.prototype.slice.call( arguments, 0 );
456
457                 // If this is an ajax load() the first arg should be the string URL;
458                 // technically this could also be the "Anything" arg of the event .load()
459                 // which just goes to show why this dumb signature has been deprecated!
460                 // jQuery custom builds that exclude the Ajax module justifiably die here.
461                 if ( name === "load" && typeof args[ 0 ] === "string" ) {
462                         return oldLoad.apply( this, args );
463                 }
464
465                 migrateWarn( "jQuery.fn." + name + "() is deprecated" );
466
467                 args.splice( 0, 0, name );
468                 if ( arguments.length ) {
469                         return this.on.apply( this, args );
470                 }
471
472                 // Use .triggerHandler here because:
473                 // - load and unload events don't need to bubble, only applied to window or image
474                 // - error event should not bubble to window, although it does pre-1.7
475                 // See http://bugs.jquery.com/ticket/11820
476                 this.triggerHandler.apply( this, args );
477                 return this;
478         };
479
480 } );
481
482 // Trigger "ready" event only once, on document ready
483 jQuery( function() {
484         jQuery( window.document ).triggerHandler( "ready" );
485 } );
486
487 jQuery.event.special.ready = {
488         setup: function() {
489                 if ( this === window.document ) {
490                         migrateWarn( "'ready' event is deprecated" );
491                 }
492         }
493 };
494
495 jQuery.fn.extend( {
496
497         bind: function( types, data, fn ) {
498                 migrateWarn( "jQuery.fn.bind() is deprecated" );
499                 return this.on( types, null, data, fn );
500         },
501         unbind: function( types, fn ) {
502                 migrateWarn( "jQuery.fn.unbind() is deprecated" );
503                 return this.off( types, null, fn );
504         },
505         delegate: function( selector, types, data, fn ) {
506                 migrateWarn( "jQuery.fn.delegate() is deprecated" );
507                 return this.on( types, selector, data, fn );
508         },
509         undelegate: function( selector, types, fn ) {
510                 migrateWarn( "jQuery.fn.undelegate() is deprecated" );
511                 return arguments.length === 1 ?
512                         this.off( selector, "**" ) :
513                         this.off( types, selector || "**", fn );
514         }
515 } );
516
517
518 var oldOffset = jQuery.fn.offset;
519
520 jQuery.fn.offset = function() {
521         var docElem,
522                 elem = this[ 0 ],
523                 origin = { top: 0, left: 0 };
524
525         if ( !elem || !elem.nodeType ) {
526                 migrateWarn( "jQuery.fn.offset() requires a valid DOM element" );
527                 return origin;
528         }
529
530         docElem = ( elem.ownerDocument || window.document ).documentElement;
531         if ( !jQuery.contains( docElem, elem ) ) {
532                 migrateWarn( "jQuery.fn.offset() requires an element connected to a document" );
533                 return origin;
534         }
535
536         return oldOffset.apply( this, arguments );
537 };
538
539
540 var oldParam = jQuery.param;
541
542 jQuery.param = function( data, traditional ) {
543         var ajaxTraditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
544
545         if ( traditional === undefined && ajaxTraditional ) {
546
547                 migrateWarn( "jQuery.param() no longer uses jQuery.ajaxSettings.traditional" );
548                 traditional = ajaxTraditional;
549         }
550
551         return oldParam.call( this, data, traditional );
552 };
553
554 var oldSelf = jQuery.fn.andSelf || jQuery.fn.addBack;
555
556 jQuery.fn.andSelf = function() {
557         migrateWarn( "jQuery.fn.andSelf() is deprecated and removed, use jQuery.fn.addBack()" );
558         return oldSelf.apply( this, arguments );
559 };
560
561
562 var oldDeferred = jQuery.Deferred,
563         tuples = [
564
565                 // Action, add listener, callbacks, .then handlers, final state
566                 [ "resolve", "done", jQuery.Callbacks( "once memory" ),
567                         jQuery.Callbacks( "once memory" ), "resolved" ],
568                 [ "reject", "fail", jQuery.Callbacks( "once memory" ),
569                         jQuery.Callbacks( "once memory" ), "rejected" ],
570                 [ "notify", "progress", jQuery.Callbacks( "memory" ),
571                         jQuery.Callbacks( "memory" ) ]
572         ];
573
574 jQuery.Deferred = function( func ) {
575         var deferred = oldDeferred(),
576                 promise = deferred.promise();
577
578         deferred.pipe = promise.pipe = function( /* fnDone, fnFail, fnProgress */ ) {
579                 var fns = arguments;
580
581                 migrateWarn( "deferred.pipe() is deprecated" );
582
583                 return jQuery.Deferred( function( newDefer ) {
584                         jQuery.each( tuples, function( i, tuple ) {
585                                 var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
586
587                                 // Deferred.done(function() { bind to newDefer or newDefer.resolve })
588                                 // deferred.fail(function() { bind to newDefer or newDefer.reject })
589                                 // deferred.progress(function() { bind to newDefer or newDefer.notify })
590                                 deferred[ tuple[ 1 ] ]( function() {
591                                         var returned = fn && fn.apply( this, arguments );
592                                         if ( returned && jQuery.isFunction( returned.promise ) ) {
593                                                 returned.promise()
594                                                         .done( newDefer.resolve )
595                                                         .fail( newDefer.reject )
596                                                         .progress( newDefer.notify );
597                                         } else {
598                                                 newDefer[ tuple[ 0 ] + "With" ](
599                                                         this === promise ? newDefer.promise() : this,
600                                                         fn ? [ returned ] : arguments
601                                                 );
602                                         }
603                                 } );
604                         } );
605                         fns = null;
606                 } ).promise();
607
608         };
609
610         if ( func ) {
611                 func.call( deferred, deferred );
612         }
613
614         return deferred;
615 };
616
617 // Preserve handler of uncaught exceptions in promise chains
618 jQuery.Deferred.exceptionHook = oldDeferred.exceptionHook;
619
620 return jQuery;
621 } );