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
6 (function( jQuery, window, undefined ) {
7 // See http://bugs.jquery.com/ticket/13335
13 // List of warnings already given; public read only
14 jQuery.migrateWarnings = [];
16 // Set to true to prevent console output; migrateWarnings still maintained
17 // jQuery.migrateMute = false;
19 // Show a message on the console so devs know we're active
20 if ( !jQuery.migrateMute && window.console && console.log ) {
21 console.log("JQMIGRATE: Logging is active");
24 // Set to false to disable traces that appear with warnings
25 if ( jQuery.migrateTrace === undefined ) {
26 jQuery.migrateTrace = true;
29 // Forget any warnings we've already given; public
30 jQuery.migrateReset = function() {
32 jQuery.migrateWarnings.length = 0;
35 function migrateWarn( msg) {
36 if ( !warnedAbout[ msg ] ) {
37 warnedAbout[ msg ] = true;
38 jQuery.migrateWarnings.push( msg );
39 if ( window.console && console.warn && !jQuery.migrateMute ) {
40 console.warn( "JQMIGRATE: " + msg );
41 if ( jQuery.migrateTrace && console.trace ) {
48 function migrateWarnProp( obj, prop, value, msg ) {
49 if ( Object.defineProperty ) {
50 // On ES5 browsers (non-oldIE), warn if the code tries to get prop;
51 // allow property to be overwritten in case some other plugin wants it
53 Object.defineProperty( obj, prop, {
60 set: function( newValue ) {
67 // IE8 is a dope about Object.defineProperty, can't warn there
71 // Non-ES5 (or broken) browser; just set the property
72 jQuery._definePropertyBroken = true;
76 if ( document.compatMode === "BackCompat" ) {
77 // jQuery has never supported or tested Quirks Mode
78 migrateWarn( "jQuery is not compatible with Quirks Mode" );
82 var attrFn = jQuery( "<input/>", { size: 1 } ).attr("size") && jQuery.attrFn,
83 oldAttr = jQuery.attr,
84 valueAttrGet = jQuery.attrHooks.value && jQuery.attrHooks.value.get ||
85 function() { return null; },
86 valueAttrSet = jQuery.attrHooks.value && jQuery.attrHooks.value.set ||
87 function() { return undefined; },
88 rnoType = /^(?:input|button)$/i,
89 rnoAttrNodeType = /^[238]$/,
90 rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i,
91 ruseDefault = /^(?:checked|selected)$/i;
94 migrateWarnProp( jQuery, "attrFn", attrFn || {}, "jQuery.attrFn is deprecated" );
96 jQuery.attr = function( elem, name, value, pass ) {
97 var lowerName = name.toLowerCase(),
98 nType = elem && elem.nodeType;
101 // Since pass is used internally, we only warn for new jQuery
102 // versions where there isn't a pass arg in the formal params
103 if ( oldAttr.length < 4 ) {
104 migrateWarn("jQuery.fn.attr( props, pass ) is deprecated");
106 if ( elem && !rnoAttrNodeType.test( nType ) &&
107 (attrFn ? name in attrFn : jQuery.isFunction(jQuery.fn[name])) ) {
108 return jQuery( elem )[ name ]( value );
112 // Warn if user tries to set `type`, since it breaks on IE 6/7/8; by checking
113 // for disconnected elements we don't warn on $( "<button>", { type: "button" } ).
114 if ( name === "type" && value !== undefined && rnoType.test( elem.nodeName ) && elem.parentNode ) {
115 migrateWarn("Can't change the 'type' of an input or button in IE 6/7/8");
118 // Restore boolHook for boolean property/attribute synchronization
119 if ( !jQuery.attrHooks[ lowerName ] && rboolean.test( lowerName ) ) {
120 jQuery.attrHooks[ lowerName ] = {
121 get: function( elem, name ) {
122 // Align boolean attributes with corresponding properties
123 // Fall back to attribute presence where some booleans are not supported
125 property = jQuery.prop( elem, name );
126 return property === true || typeof property !== "boolean" &&
127 ( attrNode = elem.getAttributeNode(name) ) && attrNode.nodeValue !== false ?
132 set: function( elem, value, name ) {
134 if ( value === false ) {
135 // Remove boolean attributes when set to false
136 jQuery.removeAttr( elem, name );
138 // value is true since we know at this point it's type boolean and not false
139 // Set boolean attributes to the same name and set the DOM property
140 propName = jQuery.propFix[ name ] || name;
141 if ( propName in elem ) {
142 // Only set the IDL specifically if it already exists on the element
143 elem[ propName ] = true;
146 elem.setAttribute( name, name.toLowerCase() );
152 // Warn only for attributes that can remain distinct from their properties post-1.9
153 if ( ruseDefault.test( lowerName ) ) {
154 migrateWarn( "jQuery.fn.attr('" + lowerName + "') may use property instead of attribute" );
158 return oldAttr.call( jQuery, elem, name, value );
162 jQuery.attrHooks.value = {
163 get: function( elem, name ) {
164 var nodeName = ( elem.nodeName || "" ).toLowerCase();
165 if ( nodeName === "button" ) {
166 return valueAttrGet.apply( this, arguments );
168 if ( nodeName !== "input" && nodeName !== "option" ) {
169 migrateWarn("jQuery.fn.attr('value') no longer gets properties");
171 return name in elem ?
175 set: function( elem, value ) {
176 var nodeName = ( elem.nodeName || "" ).toLowerCase();
177 if ( nodeName === "button" ) {
178 return valueAttrSet.apply( this, arguments );
180 if ( nodeName !== "input" && nodeName !== "option" ) {
181 migrateWarn("jQuery.fn.attr('value', val) no longer sets properties");
183 // Does not return so that setAttribute is also used
189 var matched, browser,
190 oldInit = jQuery.fn.init,
191 oldParseJSON = jQuery.parseJSON,
192 // Note this does NOT include the #9521 XSS fix from 1.7!
193 rquickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*|#([\w\-]*))$/;
195 // $(html) "looks like html" rule change
196 jQuery.fn.init = function( selector, context, rootjQuery ) {
199 if ( selector && typeof selector === "string" && !jQuery.isPlainObject( context ) &&
200 (match = rquickExpr.exec( selector )) && match[1] ) {
201 // This is an HTML string according to the "old" rules; is it still?
202 if ( selector.charAt( 0 ) !== "<" ) {
203 migrateWarn("$(html) HTML strings must start with '<' character");
205 // Now process using loose rules; let pre-1.8 play too
206 if ( context && context.context ) {
207 // jQuery object as context; parseHTML expects a DOM object
208 context = context.context;
210 if ( jQuery.parseHTML ) {
211 return oldInit.call( this, jQuery.parseHTML( jQuery.trim(selector), context, true ),
212 context, rootjQuery );
215 return oldInit.apply( this, arguments );
217 jQuery.fn.init.prototype = jQuery.fn;
219 // Let $.parseJSON(falsy_value) return null
220 jQuery.parseJSON = function( json ) {
221 if ( !json && json !== null ) {
222 migrateWarn("jQuery.parseJSON requires a valid JSON string");
225 return oldParseJSON.apply( this, arguments );
228 jQuery.uaMatch = function( ua ) {
229 ua = ua.toLowerCase();
231 var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
232 /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
233 /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
234 /(msie) ([\w.]+)/.exec( ua ) ||
235 ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
239 browser: match[ 1 ] || "",
240 version: match[ 2 ] || "0"
244 // Don't clobber any existing jQuery.browser in case it's different
245 if ( !jQuery.browser ) {
246 matched = jQuery.uaMatch( navigator.userAgent );
249 if ( matched.browser ) {
250 browser[ matched.browser ] = true;
251 browser.version = matched.version;
254 // Chrome is Webkit, but Webkit is also Safari.
255 if ( browser.chrome ) {
256 browser.webkit = true;
257 } else if ( browser.webkit ) {
258 browser.safari = true;
261 jQuery.browser = browser;
264 // Warn if the code tries to get jQuery.browser
265 migrateWarnProp( jQuery, "browser", jQuery.browser, "jQuery.browser is deprecated" );
267 jQuery.sub = function() {
268 function jQuerySub( selector, context ) {
269 return new jQuerySub.fn.init( selector, context );
271 jQuery.extend( true, jQuerySub, this );
272 jQuerySub.superclass = this;
273 jQuerySub.fn = jQuerySub.prototype = this();
274 jQuerySub.fn.constructor = jQuerySub;
275 jQuerySub.sub = this.sub;
276 jQuerySub.fn.init = function init( selector, context ) {
277 if ( context && context instanceof jQuery && !(context instanceof jQuerySub) ) {
278 context = jQuerySub( context );
281 return jQuery.fn.init.call( this, selector, context, rootjQuerySub );
283 jQuerySub.fn.init.prototype = jQuerySub.fn;
284 var rootjQuerySub = jQuerySub(document);
285 migrateWarn( "jQuery.sub() is deprecated" );
290 // Ensure that $.ajax gets the new parseJSON defined in core.js
293 "text json": jQuery.parseJSON
298 var oldFnData = jQuery.fn.data;
300 jQuery.fn.data = function( name ) {
304 // Handles 1.7 which has this behavior and 1.8 which doesn't
305 if ( elem && name === "events" && arguments.length === 1 ) {
306 ret = jQuery.data( elem, name );
307 evt = jQuery._data( elem, name );
308 if ( ( ret === undefined || ret === evt ) && evt !== undefined ) {
309 migrateWarn("Use of jQuery.fn.data('events') is deprecated");
313 return oldFnData.apply( this, arguments );
317 var rscriptType = /\/(java|ecma)script/i,
318 oldSelf = jQuery.fn.andSelf || jQuery.fn.addBack;
320 jQuery.fn.andSelf = function() {
321 migrateWarn("jQuery.fn.andSelf() replaced by jQuery.fn.addBack()");
322 return oldSelf.apply( this, arguments );
325 // Since jQuery.clean is used internally on older versions, we only shim if it's missing
326 if ( !jQuery.clean ) {
327 jQuery.clean = function( elems, context, fragment, scripts ) {
328 // Set context per 1.8 logic
329 context = context || document;
330 context = !context.nodeType && context[0] || context;
331 context = context.ownerDocument || context;
333 migrateWarn("jQuery.clean() is deprecated");
335 var i, elem, handleScript, jsTags,
338 jQuery.merge( ret, jQuery.buildFragment( elems, context ).childNodes );
340 // Complex logic lifted directly from jQuery 1.8
342 // Special handling of each script element
343 handleScript = function( elem ) {
344 // Check if we consider it executable
345 if ( !elem.type || rscriptType.test( elem.type ) ) {
346 // Detach the script and store it in the scripts array (if provided) or the fragment
347 // Return truthy to indicate that it has been handled
349 scripts.push( elem.parentNode ? elem.parentNode.removeChild( elem ) : elem ) :
350 fragment.appendChild( elem );
354 for ( i = 0; (elem = ret[i]) != null; i++ ) {
355 // Check if we're done after handling an executable script
356 if ( !( jQuery.nodeName( elem, "script" ) && handleScript( elem ) ) ) {
357 // Append to fragment and handle embedded scripts
358 fragment.appendChild( elem );
359 if ( typeof elem.getElementsByTagName !== "undefined" ) {
360 // handleScript alters the DOM, so use jQuery.merge to ensure snapshot iteration
361 jsTags = jQuery.grep( jQuery.merge( [], elem.getElementsByTagName("script") ), handleScript );
363 // Splice the scripts into ret after their former ancestor and advance our index beyond them
364 ret.splice.apply( ret, [i + 1, 0].concat( jsTags ) );
375 var eventAdd = jQuery.event.add,
376 eventRemove = jQuery.event.remove,
377 eventTrigger = jQuery.event.trigger,
378 oldToggle = jQuery.fn.toggle,
379 oldLive = jQuery.fn.live,
380 oldDie = jQuery.fn.die,
381 ajaxEvents = "ajaxStart|ajaxStop|ajaxSend|ajaxComplete|ajaxError|ajaxSuccess",
382 rajaxEvent = new RegExp( "\\b(?:" + ajaxEvents + ")\\b" ),
383 rhoverHack = /(?:^|\s)hover(\.\S+|)\b/,
384 hoverHack = function( events ) {
385 if ( typeof( events ) !== "string" || jQuery.event.special.hover ) {
388 if ( rhoverHack.test( events ) ) {
389 migrateWarn("'hover' pseudo-event is deprecated, use 'mouseenter mouseleave'");
391 return events && events.replace( rhoverHack, "mouseenter$1 mouseleave$1" );
394 // Event props removed in 1.9, put them back if needed; no practical way to warn them
395 if ( jQuery.event.props && jQuery.event.props[ 0 ] !== "attrChange" ) {
396 jQuery.event.props.unshift( "attrChange", "attrName", "relatedNode", "srcElement" );
399 // Undocumented jQuery.event.handle was "deprecated" in jQuery 1.7
400 if ( jQuery.event.dispatch ) {
401 migrateWarnProp( jQuery.event, "handle", jQuery.event.dispatch, "jQuery.event.handle is undocumented and deprecated" );
404 // Support for 'hover' pseudo-event and ajax event warnings
405 jQuery.event.add = function( elem, types, handler, data, selector ){
406 if ( elem !== document && rajaxEvent.test( types ) ) {
407 migrateWarn( "AJAX events should be attached to document: " + types );
409 eventAdd.call( this, elem, hoverHack( types || "" ), handler, data, selector );
411 jQuery.event.remove = function( elem, types, handler, selector, mappedTypes ){
412 eventRemove.call( this, elem, hoverHack( types ) || "", handler, selector, mappedTypes );
415 jQuery.fn.error = function() {
416 var args = Array.prototype.slice.call( arguments, 0);
417 migrateWarn("jQuery.fn.error() is deprecated");
418 args.splice( 0, 0, "error" );
419 if ( arguments.length ) {
420 return this.bind.apply( this, args );
422 // error event should not bubble to window, although it does pre-1.7
423 this.triggerHandler.apply( this, args );
427 jQuery.fn.toggle = function( fn, fn2 ) {
429 // Don't mess with animation or css toggles
430 if ( !jQuery.isFunction( fn ) || !jQuery.isFunction( fn2 ) ) {
431 return oldToggle.apply( this, arguments );
433 migrateWarn("jQuery.fn.toggle(handler, handler...) is deprecated");
435 // Save reference to arguments for access in closure
436 var args = arguments,
437 guid = fn.guid || jQuery.guid++,
439 toggler = function( event ) {
440 // Figure out which function to execute
441 var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
442 jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
444 // Make sure that clicks stop
445 event.preventDefault();
447 // and execute the function
448 return args[ lastToggle ].apply( this, arguments ) || false;
451 // link all the functions, so any of them can unbind this click handler
453 while ( i < args.length ) {
454 args[ i++ ].guid = guid;
457 return this.click( toggler );
460 jQuery.fn.live = function( types, data, fn ) {
461 migrateWarn("jQuery.fn.live() is deprecated");
463 return oldLive.apply( this, arguments );
465 jQuery( this.context ).on( types, this.selector, data, fn );
469 jQuery.fn.die = function( types, fn ) {
470 migrateWarn("jQuery.fn.die() is deprecated");
472 return oldDie.apply( this, arguments );
474 jQuery( this.context ).off( types, this.selector || "**", fn );
478 // Turn global events into document-triggered events
479 jQuery.event.trigger = function( event, data, elem, onlyHandlers ){
480 if ( !elem && !rajaxEvent.test( event ) ) {
481 migrateWarn( "Global events are undocumented and deprecated" );
483 return eventTrigger.call( this, event, data, elem || document, onlyHandlers );
485 jQuery.each( ajaxEvents.split("|"),
486 function( _, name ) {
487 jQuery.event.special[ name ] = {
491 // The document needs no shimming; must be !== for oldIE
492 if ( elem !== document ) {
493 jQuery.event.add( document, name + "." + jQuery.guid, function() {
494 jQuery.event.trigger( name, null, elem, true );
496 jQuery._data( this, name, jQuery.guid++ );
500 teardown: function() {
501 if ( this !== document ) {
502 jQuery.event.remove( document, name + "." + jQuery._data( this, name ) );
511 })( jQuery, window );