Fix #12690. Avoid use of UTF-8 chars in final files.
[jquery.git] / src / event.js
blob153045ac5b02f5fdd117d9f4ec87bcd32dc27955
1 var rformElems = /^(?:textarea|input|select)$/i,
2         rtypenamespace = /^([^\.]*|)(?:\.(.+)|)$/,
3         rhoverHack = /(?:^|\s)hover(\.\S+|)\b/,
4         rkeyEvent = /^key/,
5         rmouseEvent = /^(?:mouse|contextmenu)|click/,
6         rfocusMorph = /^(?:focusinfocus|focusoutblur)$/,
7         hoverHack = function( events ) {
8                 return jQuery.event.special.hover ? events : events.replace( rhoverHack, "mouseenter$1 mouseleave$1" );
9         };
12  * Helper functions for managing events -- not part of the public interface.
13  * Props to Dean Edwards' addEvent library for many of the ideas.
14  */
15 jQuery.event = {
17         add: function( elem, types, handler, data, selector ) {
19                 var elemData, eventHandle, events,
20                         t, tns, type, namespaces, handleObj,
21                         handleObjIn, handlers, special;
23                 // Don't attach events to noData or text/comment nodes (allow plain objects tho)
24                 if ( elem.nodeType === 3 || elem.nodeType === 8 || !types || !handler || !(elemData = jQuery._data( elem )) ) {
25                         return;
26                 }
28                 // Caller can pass in an object of custom data in lieu of the handler
29                 if ( handler.handler ) {
30                         handleObjIn = handler;
31                         handler = handleObjIn.handler;
32                         selector = handleObjIn.selector;
33                 }
35                 // Make sure that the handler has a unique ID, used to find/remove it later
36                 if ( !handler.guid ) {
37                         handler.guid = jQuery.guid++;
38                 }
40                 // Init the element's event structure and main handler, if this is the first
41                 events = elemData.events;
42                 if ( !events ) {
43                         elemData.events = events = {};
44                 }
45                 eventHandle = elemData.handle;
46                 if ( !eventHandle ) {
47                         elemData.handle = eventHandle = function( e ) {
48                                 // Discard the second event of a jQuery.event.trigger() and
49                                 // when an event is called after a page has unloaded
50                                 return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
51                                         jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
52                                         undefined;
53                         };
54                         // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
55                         eventHandle.elem = elem;
56                 }
58                 // Handle multiple events separated by a space
59                 // jQuery(...).bind("mouseover mouseout", fn);
60                 types = jQuery.trim( hoverHack(types) ).split( " " );
61                 for ( t = 0; t < types.length; t++ ) {
63                         tns = rtypenamespace.exec( types[t] ) || [];
64                         type = tns[1];
65                         namespaces = ( tns[2] || "" ).split( "." ).sort();
67                         // If event changes its type, use the special event handlers for the changed type
68                         special = jQuery.event.special[ type ] || {};
70                         // If selector defined, determine special event api type, otherwise given type
71                         type = ( selector ? special.delegateType : special.bindType ) || type;
73                         // Update special based on newly reset type
74                         special = jQuery.event.special[ type ] || {};
76                         // handleObj is passed to all event handlers
77                         handleObj = jQuery.extend({
78                                 type: type,
79                                 origType: tns[1],
80                                 data: data,
81                                 handler: handler,
82                                 guid: handler.guid,
83                                 selector: selector,
84                                 needsContext: selector && jQuery.expr.match.needsContext.test( selector ),
85                                 namespace: namespaces.join(".")
86                         }, handleObjIn );
88                         // Init the event handler queue if we're the first
89                         handlers = events[ type ];
90                         if ( !handlers ) {
91                                 handlers = events[ type ] = [];
92                                 handlers.delegateCount = 0;
94                                 // Only use addEventListener/attachEvent if the special events handler returns false
95                                 if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
96                                         // Bind the global event handler to the element
97                                         if ( elem.addEventListener ) {
98                                                 elem.addEventListener( type, eventHandle, false );
100                                         } else if ( elem.attachEvent ) {
101                                                 elem.attachEvent( "on" + type, eventHandle );
102                                         }
103                                 }
104                         }
106                         if ( special.add ) {
107                                 special.add.call( elem, handleObj );
109                                 if ( !handleObj.handler.guid ) {
110                                         handleObj.handler.guid = handler.guid;
111                                 }
112                         }
114                         // Add to the element's handler list, delegates in front
115                         if ( selector ) {
116                                 handlers.splice( handlers.delegateCount++, 0, handleObj );
117                         } else {
118                                 handlers.push( handleObj );
119                         }
121                         // Keep track of which events have ever been used, for event optimization
122                         jQuery.event.global[ type ] = true;
123                 }
125                 // Nullify elem to prevent memory leaks in IE
126                 elem = null;
127         },
129         global: {},
131         // Detach an event or set of events from an element
132         remove: function( elem, types, handler, selector, mappedTypes ) {
134                 var t, tns, type, origType, namespaces, origCount,
135                         j, events, special, eventType, handleObj,
136                         elemData = jQuery.hasData( elem ) && jQuery._data( elem );
138                 if ( !elemData || !(events = elemData.events) ) {
139                         return;
140                 }
142                 // Once for each type.namespace in types; type may be omitted
143                 types = jQuery.trim( hoverHack( types || "" ) ).split(" ");
144                 for ( t = 0; t < types.length; t++ ) {
145                         tns = rtypenamespace.exec( types[t] ) || [];
146                         type = origType = tns[1];
147                         namespaces = tns[2];
149                         // Unbind all events (on this namespace, if provided) for the element
150                         if ( !type ) {
151                                 for ( type in events ) {
152                                         jQuery.event.remove( elem, type + types[ t ], handler, selector, true );
153                                 }
154                                 continue;
155                         }
157                         special = jQuery.event.special[ type ] || {};
158                         type = ( selector? special.delegateType : special.bindType ) || type;
159                         eventType = events[ type ] || [];
160                         origCount = eventType.length;
161                         namespaces = namespaces ? new RegExp("(^|\\.)" + namespaces.split(".").sort().join("\\.(?:.*\\.|)") + "(\\.|$)") : null;
163                         // Remove matching events
164                         for ( j = 0; j < eventType.length; j++ ) {
165                                 handleObj = eventType[ j ];
167                                 if ( ( mappedTypes || origType === handleObj.origType ) &&
168                                          ( !handler || handler.guid === handleObj.guid ) &&
169                                          ( !namespaces || namespaces.test( handleObj.namespace ) ) &&
170                                          ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {
171                                         eventType.splice( j--, 1 );
173                                         if ( handleObj.selector ) {
174                                                 eventType.delegateCount--;
175                                         }
176                                         if ( special.remove ) {
177                                                 special.remove.call( elem, handleObj );
178                                         }
179                                 }
180                         }
182                         // Remove generic event handler if we removed something and no more handlers exist
183                         // (avoids potential for endless recursion during removal of special event handlers)
184                         if ( eventType.length === 0 && origCount !== eventType.length ) {
185                                 if ( !special.teardown || special.teardown.call( elem, namespaces, elemData.handle ) === false ) {
186                                         jQuery.removeEvent( elem, type, elemData.handle );
187                                 }
189                                 delete events[ type ];
190                         }
191                 }
193                 // Remove the expando if it's no longer used
194                 if ( jQuery.isEmptyObject( events ) ) {
195                         delete elemData.handle;
197                         // removeData also checks for emptiness and clears the expando if empty
198                         // so use it instead of delete
199                         jQuery.removeData( elem, "events", true );
200                 }
201         },
203         // Events that are safe to short-circuit if no handlers are attached.
204         // Native DOM events should not be added, they may have inline handlers.
205         customEvent: {
206                 "getData": true,
207                 "setData": true,
208                 "changeData": true
209         },
211         trigger: function( event, data, elem, onlyHandlers ) {
212                 // Don't do events on text and comment nodes
213                 if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
214                         return;
215                 }
217                 // Event object or event type
218                 var cache, exclusive, i, cur, old, ontype, special, handle, eventPath, bubbleType,
219                         type = event.type || event,
220                         namespaces = [];
222                 // focus/blur morphs to focusin/out; ensure we're not firing them right now
223                 if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
224                         return;
225                 }
227                 if ( type.indexOf( "!" ) >= 0 ) {
228                         // Exclusive events trigger only for the exact event (no namespaces)
229                         type = type.slice(0, -1);
230                         exclusive = true;
231                 }
233                 if ( type.indexOf( "." ) >= 0 ) {
234                         // Namespaced trigger; create a regexp to match event type in handle()
235                         namespaces = type.split(".");
236                         type = namespaces.shift();
237                         namespaces.sort();
238                 }
240                 if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) {
241                         // No jQuery handlers for this event type, and it can't have inline handlers
242                         return;
243                 }
245                 // Caller can pass in an Event, Object, or just an event type string
246                 event = typeof event === "object" ?
247                         // jQuery.Event object
248                         event[ jQuery.expando ] ? event :
249                         // Object literal
250                         new jQuery.Event( type, event ) :
251                         // Just the event type (string)
252                         new jQuery.Event( type );
254                 event.type = type;
255                 event.isTrigger = true;
256                 event.exclusive = exclusive;
257                 event.namespace = namespaces.join( "." );
258                 event.namespace_re = event.namespace? new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)") : null;
259                 ontype = type.indexOf( ":" ) < 0 ? "on" + type : "";
261                 // Handle a global trigger
262                 if ( !elem ) {
264                         // TODO: Stop taunting the data cache; remove global events and always attach to document
265                         cache = jQuery.cache;
266                         for ( i in cache ) {
267                                 if ( cache[ i ].events && cache[ i ].events[ type ] ) {
268                                         jQuery.event.trigger( event, data, cache[ i ].handle.elem, true );
269                                 }
270                         }
271                         return;
272                 }
274                 // Clean up the event in case it is being reused
275                 event.result = undefined;
276                 if ( !event.target ) {
277                         event.target = elem;
278                 }
280                 // Clone any incoming data and prepend the event, creating the handler arg list
281                 data = data != null ? jQuery.makeArray( data ) : [];
282                 data.unshift( event );
284                 // Allow special events to draw outside the lines
285                 special = jQuery.event.special[ type ] || {};
286                 if ( special.trigger && special.trigger.apply( elem, data ) === false ) {
287                         return;
288                 }
290                 // Determine event propagation path in advance, per W3C events spec (#9951)
291                 // Bubble up to document, then to window; watch for a global ownerDocument var (#9724)
292                 eventPath = [[ elem, special.bindType || type ]];
293                 if ( !onlyHandlers && !special.noBubble && !jQuery.isWindow( elem ) ) {
295                         bubbleType = special.delegateType || type;
296                         cur = rfocusMorph.test( bubbleType + type ) ? elem : elem.parentNode;
297                         for ( old = elem; cur; cur = cur.parentNode ) {
298                                 eventPath.push([ cur, bubbleType ]);
299                                 old = cur;
300                         }
302                         // Only add window if we got to document (e.g., not plain obj or detached DOM)
303                         if ( old === (elem.ownerDocument || document) ) {
304                                 eventPath.push([ old.defaultView || old.parentWindow || window, bubbleType ]);
305                         }
306                 }
308                 // Fire handlers on the event path
309                 for ( i = 0; i < eventPath.length && !event.isPropagationStopped(); i++ ) {
311                         cur = eventPath[i][0];
312                         event.type = eventPath[i][1];
314                         handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" );
315                         if ( handle ) {
316                                 handle.apply( cur, data );
317                         }
318                         // Note that this is a bare JS function and not a jQuery handler
319                         handle = ontype && cur[ ontype ];
320                         if ( handle && jQuery.acceptData( cur ) && handle.apply && handle.apply( cur, data ) === false ) {
321                                 event.preventDefault();
322                         }
323                 }
324                 event.type = type;
326                 // If nobody prevented the default action, do it now
327                 if ( !onlyHandlers && !event.isDefaultPrevented() ) {
329                         if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) &&
330                                 !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {
332                                 // Call a native DOM method on the target with the same name name as the event.
333                                 // Can't use an .isFunction() check here because IE6/7 fails that test.
334                                 // Don't do default actions on window, that's where global variables be (#6170)
335                                 // IE<9 dies on focus/blur to hidden element (#1486)
336                                 if ( ontype && elem[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) && !jQuery.isWindow( elem ) ) {
338                                         // Don't re-trigger an onFOO event when we call its FOO() method
339                                         old = elem[ ontype ];
341                                         if ( old ) {
342                                                 elem[ ontype ] = null;
343                                         }
345                                         // Prevent re-triggering of the same event, since we already bubbled it above
346                                         jQuery.event.triggered = type;
347                                         elem[ type ]();
348                                         jQuery.event.triggered = undefined;
350                                         if ( old ) {
351                                                 elem[ ontype ] = old;
352                                         }
353                                 }
354                         }
355                 }
357                 return event.result;
358         },
360         dispatch: function( event ) {
362                 // Make a writable jQuery.Event from the native event object
363                 event = jQuery.event.fix( event || window.event );
365                 var i, j, cur, ret, selMatch, matched, matches, handleObj, sel, related,
366                         handlers = ( (jQuery._data( this, "events" ) || {} )[ event.type ] || []),
367                         delegateCount = handlers.delegateCount,
368                         args = core_slice.call( arguments ),
369                         run_all = !event.exclusive && !event.namespace,
370                         special = jQuery.event.special[ event.type ] || {},
371                         handlerQueue = [];
373                 // Use the fix-ed jQuery.Event rather than the (read-only) native event
374                 args[0] = event;
375                 event.delegateTarget = this;
377                 // Call the preDispatch hook for the mapped type, and let it bail if desired
378                 if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) {
379                         return;
380                 }
382                 // Determine handlers that should run if there are delegated events
383                 // Avoid non-left-click bubbling in Firefox (#3861)
384                 if ( delegateCount && !(event.button && event.type === "click") ) {
386                         for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
388                                 // Don't process clicks (ONLY) on disabled elements (#6911, #8165, #11382, #11764)
389                                 if ( cur.disabled !== true || event.type !== "click" ) {
390                                         selMatch = {};
391                                         matches = [];
392                                         for ( i = 0; i < delegateCount; i++ ) {
393                                                 handleObj = handlers[ i ];
394                                                 sel = handleObj.selector;
396                                                 if ( selMatch[ sel ] === undefined ) {
397                                                         selMatch[ sel ] = handleObj.needsContext ?
398                                                                 jQuery( sel, this ).index( cur ) >= 0 :
399                                                                 jQuery.find( sel, this, null, [ cur ] ).length;
400                                                 }
401                                                 if ( selMatch[ sel ] ) {
402                                                         matches.push( handleObj );
403                                                 }
404                                         }
405                                         if ( matches.length ) {
406                                                 handlerQueue.push({ elem: cur, matches: matches });
407                                         }
408                                 }
409                         }
410                 }
412                 // Add the remaining (directly-bound) handlers
413                 if ( handlers.length > delegateCount ) {
414                         handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) });
415                 }
417                 // Run delegates first; they may want to stop propagation beneath us
418                 for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
419                         matched = handlerQueue[ i ];
420                         event.currentTarget = matched.elem;
422                         for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) {
423                                 handleObj = matched.matches[ j ];
425                                 // Triggered event must either 1) be non-exclusive and have no namespace, or
426                                 // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
427                                 if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {
429                                         event.data = handleObj.data;
430                                         event.handleObj = handleObj;
432                                         ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler )
433                                                         .apply( matched.elem, args );
435                                         if ( ret !== undefined ) {
436                                                 event.result = ret;
437                                                 if ( ret === false ) {
438                                                         event.preventDefault();
439                                                         event.stopPropagation();
440                                                 }
441                                         }
442                                 }
443                         }
444                 }
446                 // Call the postDispatch hook for the mapped type
447                 if ( special.postDispatch ) {
448                         special.postDispatch.call( this, event );
449                 }
451                 return event.result;
452         },
454         // Includes some event props shared by KeyEvent and MouseEvent
455         // *** attrChange attrName relatedNode srcElement  are not normalized, non-W3C, deprecated, will be removed in 1.8 ***
456         props: "attrChange attrName relatedNode srcElement altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which".split(" "),
458         fixHooks: {},
460         keyHooks: {
461                 props: "char charCode key keyCode".split(" "),
462                 filter: function( event, original ) {
464                         // Add which for key events
465                         if ( event.which == null ) {
466                                 event.which = original.charCode != null ? original.charCode : original.keyCode;
467                         }
469                         return event;
470                 }
471         },
473         mouseHooks: {
474                 props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "),
475                 filter: function( event, original ) {
476                         var eventDoc, doc, body,
477                                 button = original.button,
478                                 fromElement = original.fromElement;
480                         // Calculate pageX/Y if missing and clientX/Y available
481                         if ( event.pageX == null && original.clientX != null ) {
482                                 eventDoc = event.target.ownerDocument || document;
483                                 doc = eventDoc.documentElement;
484                                 body = eventDoc.body;
486                                 event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 );
487                                 event.pageY = original.clientY + ( doc && doc.scrollTop  || body && body.scrollTop  || 0 ) - ( doc && doc.clientTop  || body && body.clientTop  || 0 );
488                         }
490                         // Add relatedTarget, if necessary
491                         if ( !event.relatedTarget && fromElement ) {
492                                 event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
493                         }
495                         // Add which for click: 1 === left; 2 === middle; 3 === right
496                         // Note: button is not normalized, so don't use it
497                         if ( !event.which && button !== undefined ) {
498                                 event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
499                         }
501                         return event;
502                 }
503         },
505         fix: function( event ) {
506                 if ( event[ jQuery.expando ] ) {
507                         return event;
508                 }
510                 // Create a writable copy of the event object and normalize some properties
511                 var i, prop,
512                         originalEvent = event,
513                         fixHook = jQuery.event.fixHooks[ event.type ] || {},
514                         copy = fixHook.props ? this.props.concat( fixHook.props ) : this.props;
516                 event = jQuery.Event( originalEvent );
518                 for ( i = copy.length; i; ) {
519                         prop = copy[ --i ];
520                         event[ prop ] = originalEvent[ prop ];
521                 }
523                 // Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
524                 if ( !event.target ) {
525                         event.target = originalEvent.srcElement || document;
526                 }
528                 // Target should not be a text node (#504, Safari)
529                 if ( event.target.nodeType === 3 ) {
530                         event.target = event.target.parentNode;
531                 }
533                 // For mouse/key events, metaKey==false if it's undefined (#3368, #11328; IE6/7/8)
534                 event.metaKey = !!event.metaKey;
536                 return fixHook.filter? fixHook.filter( event, originalEvent ) : event;
537         },
539         special: {
540                 load: {
541                         // Prevent triggered image.load events from bubbling to window.load
542                         noBubble: true
543                 },
545                 focus: {
546                         delegateType: "focusin"
547                 },
548                 blur: {
549                         delegateType: "focusout"
550                 },
552                 beforeunload: {
553                         setup: function( data, namespaces, eventHandle ) {
554                                 // We only want to do this special case on windows
555                                 if ( jQuery.isWindow( this ) ) {
556                                         this.onbeforeunload = eventHandle;
557                                 }
558                         },
560                         teardown: function( namespaces, eventHandle ) {
561                                 if ( this.onbeforeunload === eventHandle ) {
562                                         this.onbeforeunload = null;
563                                 }
564                         }
565                 }
566         },
568         simulate: function( type, elem, event, bubble ) {
569                 // Piggyback on a donor event to simulate a different one.
570                 // Fake originalEvent to avoid donor's stopPropagation, but if the
571                 // simulated event prevents default then we do the same on the donor.
572                 var e = jQuery.extend(
573                         new jQuery.Event(),
574                         event,
575                         { type: type,
576                                 isSimulated: true,
577                                 originalEvent: {}
578                         }
579                 );
580                 if ( bubble ) {
581                         jQuery.event.trigger( e, null, elem );
582                 } else {
583                         jQuery.event.dispatch.call( elem, e );
584                 }
585                 if ( e.isDefaultPrevented() ) {
586                         event.preventDefault();
587                 }
588         }
591 // Some plugins are using, but it's undocumented/deprecated and will be removed.
592 // The 1.7 special event interface should provide all the hooks needed now.
593 jQuery.event.handle = jQuery.event.dispatch;
595 jQuery.removeEvent = document.removeEventListener ?
596         function( elem, type, handle ) {
597                 if ( elem.removeEventListener ) {
598                         elem.removeEventListener( type, handle, false );
599                 }
600         } :
601         function( elem, type, handle ) {
602                 var name = "on" + type;
604                 if ( elem.detachEvent ) {
606                         // #8545, #7054, preventing memory leaks for custom events in IE6-8
607                         // detachEvent needed property on element, by name of that event, to properly expose it to GC
608                         if ( typeof elem[ name ] === "undefined" ) {
609                                 elem[ name ] = null;
610                         }
612                         elem.detachEvent( name, handle );
613                 }
614         };
616 jQuery.Event = function( src, props ) {
617         // Allow instantiation without the 'new' keyword
618         if ( !(this instanceof jQuery.Event) ) {
619                 return new jQuery.Event( src, props );
620         }
622         // Event object
623         if ( src && src.type ) {
624                 this.originalEvent = src;
625                 this.type = src.type;
627                 // Events bubbling up the document may have been marked as prevented
628                 // by a handler lower down the tree; reflect the correct value.
629                 this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false ||
630                         src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse;
632         // Event type
633         } else {
634                 this.type = src;
635         }
637         // Put explicitly provided properties onto the event object
638         if ( props ) {
639                 jQuery.extend( this, props );
640         }
642         // Create a timestamp if incoming event doesn't have one
643         this.timeStamp = src && src.timeStamp || jQuery.now();
645         // Mark it as fixed
646         this[ jQuery.expando ] = true;
649 function returnFalse() {
650         return false;
652 function returnTrue() {
653         return true;
656 // jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
657 // http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
658 jQuery.Event.prototype = {
659         preventDefault: function() {
660                 this.isDefaultPrevented = returnTrue;
662                 var e = this.originalEvent;
663                 if ( !e ) {
664                         return;
665                 }
667                 // if preventDefault exists run it on the original event
668                 if ( e.preventDefault ) {
669                         e.preventDefault();
671                 // otherwise set the returnValue property of the original event to false (IE)
672                 } else {
673                         e.returnValue = false;
674                 }
675         },
676         stopPropagation: function() {
677                 this.isPropagationStopped = returnTrue;
679                 var e = this.originalEvent;
680                 if ( !e ) {
681                         return;
682                 }
683                 // if stopPropagation exists run it on the original event
684                 if ( e.stopPropagation ) {
685                         e.stopPropagation();
686                 }
687                 // otherwise set the cancelBubble property of the original event to true (IE)
688                 e.cancelBubble = true;
689         },
690         stopImmediatePropagation: function() {
691                 this.isImmediatePropagationStopped = returnTrue;
692                 this.stopPropagation();
693         },
694         isDefaultPrevented: returnFalse,
695         isPropagationStopped: returnFalse,
696         isImmediatePropagationStopped: returnFalse
699 // Create mouseenter/leave events using mouseover/out and event-time checks
700 jQuery.each({
701         mouseenter: "mouseover",
702         mouseleave: "mouseout"
703 }, function( orig, fix ) {
704         jQuery.event.special[ orig ] = {
705                 delegateType: fix,
706                 bindType: fix,
708                 handle: function( event ) {
709                         var ret,
710                                 target = this,
711                                 related = event.relatedTarget,
712                                 handleObj = event.handleObj,
713                                 selector = handleObj.selector;
715                         // For mousenter/leave call the handler if related is outside the target.
716                         // NB: No relatedTarget if the mouse left/entered the browser window
717                         if ( !related || (related !== target && !jQuery.contains( target, related )) ) {
718                                 event.type = handleObj.origType;
719                                 ret = handleObj.handler.apply( this, arguments );
720                                 event.type = fix;
721                         }
722                         return ret;
723                 }
724         };
727 // IE submit delegation
728 if ( !jQuery.support.submitBubbles ) {
730         jQuery.event.special.submit = {
731                 setup: function() {
732                         // Only need this for delegated form submit events
733                         if ( jQuery.nodeName( this, "form" ) ) {
734                                 return false;
735                         }
737                         // Lazy-add a submit handler when a descendant form may potentially be submitted
738                         jQuery.event.add( this, "click._submit keypress._submit", function( e ) {
739                                 // Node name check avoids a VML-related crash in IE (#9807)
740                                 var elem = e.target,
741                                         form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined;
742                                 if ( form && !jQuery._data( form, "_submit_attached" ) ) {
743                                         jQuery.event.add( form, "submit._submit", function( event ) {
744                                                 event._submit_bubble = true;
745                                         });
746                                         jQuery._data( form, "_submit_attached", true );
747                                 }
748                         });
749                         // return undefined since we don't need an event listener
750                 },
752                 postDispatch: function( event ) {
753                         // If form was submitted by the user, bubble the event up the tree
754                         if ( event._submit_bubble ) {
755                                 delete event._submit_bubble;
756                                 if ( this.parentNode && !event.isTrigger ) {
757                                         jQuery.event.simulate( "submit", this.parentNode, event, true );
758                                 }
759                         }
760                 },
762                 teardown: function() {
763                         // Only need this for delegated form submit events
764                         if ( jQuery.nodeName( this, "form" ) ) {
765                                 return false;
766                         }
768                         // Remove delegated handlers; cleanData eventually reaps submit handlers attached above
769                         jQuery.event.remove( this, "._submit" );
770                 }
771         };
774 // IE change delegation and checkbox/radio fix
775 if ( !jQuery.support.changeBubbles ) {
777         jQuery.event.special.change = {
779                 setup: function() {
781                         if ( rformElems.test( this.nodeName ) ) {
782                                 // IE doesn't fire change on a check/radio until blur; trigger it on click
783                                 // after a propertychange. Eat the blur-change in special.change.handle.
784                                 // This still fires onchange a second time for check/radio after blur.
785                                 if ( this.type === "checkbox" || this.type === "radio" ) {
786                                         jQuery.event.add( this, "propertychange._change", function( event ) {
787                                                 if ( event.originalEvent.propertyName === "checked" ) {
788                                                         this._just_changed = true;
789                                                 }
790                                         });
791                                         jQuery.event.add( this, "click._change", function( event ) {
792                                                 if ( this._just_changed && !event.isTrigger ) {
793                                                         this._just_changed = false;
794                                                 }
795                                                 // Allow triggered, simulated change events (#11500)
796                                                 jQuery.event.simulate( "change", this, event, true );
797                                         });
798                                 }
799                                 return false;
800                         }
801                         // Delegated event; lazy-add a change handler on descendant inputs
802                         jQuery.event.add( this, "beforeactivate._change", function( e ) {
803                                 var elem = e.target;
805                                 if ( rformElems.test( elem.nodeName ) && !jQuery._data( elem, "_change_attached" ) ) {
806                                         jQuery.event.add( elem, "change._change", function( event ) {
807                                                 if ( this.parentNode && !event.isSimulated && !event.isTrigger ) {
808                                                         jQuery.event.simulate( "change", this.parentNode, event, true );
809                                                 }
810                                         });
811                                         jQuery._data( elem, "_change_attached", true );
812                                 }
813                         });
814                 },
816                 handle: function( event ) {
817                         var elem = event.target;
819                         // Swallow native change events from checkbox/radio, we already triggered them above
820                         if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) {
821                                 return event.handleObj.handler.apply( this, arguments );
822                         }
823                 },
825                 teardown: function() {
826                         jQuery.event.remove( this, "._change" );
828                         return !rformElems.test( this.nodeName );
829                 }
830         };
833 // Create "bubbling" focus and blur events
834 if ( !jQuery.support.focusinBubbles ) {
835         jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
837                 // Attach a single capturing handler while someone wants focusin/focusout
838                 var attaches = 0,
839                         handler = function( event ) {
840                                 jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true );
841                         };
843                 jQuery.event.special[ fix ] = {
844                         setup: function() {
845                                 if ( attaches++ === 0 ) {
846                                         document.addEventListener( orig, handler, true );
847                                 }
848                         },
849                         teardown: function() {
850                                 if ( --attaches === 0 ) {
851                                         document.removeEventListener( orig, handler, true );
852                                 }
853                         }
854                 };
855         });
858 jQuery.fn.extend({
860         on: function( types, selector, data, fn, /*INTERNAL*/ one ) {
861                 var origFn, type;
863                 // Types can be a map of types/handlers
864                 if ( typeof types === "object" ) {
865                         // ( types-Object, selector, data )
866                         if ( typeof selector !== "string" ) { // && selector != null
867                                 // ( types-Object, data )
868                                 data = data || selector;
869                                 selector = undefined;
870                         }
871                         for ( type in types ) {
872                                 this.on( type, selector, data, types[ type ], one );
873                         }
874                         return this;
875                 }
877                 if ( data == null && fn == null ) {
878                         // ( types, fn )
879                         fn = selector;
880                         data = selector = undefined;
881                 } else if ( fn == null ) {
882                         if ( typeof selector === "string" ) {
883                                 // ( types, selector, fn )
884                                 fn = data;
885                                 data = undefined;
886                         } else {
887                                 // ( types, data, fn )
888                                 fn = data;
889                                 data = selector;
890                                 selector = undefined;
891                         }
892                 }
893                 if ( fn === false ) {
894                         fn = returnFalse;
895                 } else if ( !fn ) {
896                         return this;
897                 }
899                 if ( one === 1 ) {
900                         origFn = fn;
901                         fn = function( event ) {
902                                 // Can use an empty set, since event contains the info
903                                 jQuery().off( event );
904                                 return origFn.apply( this, arguments );
905                         };
906                         // Use same guid so caller can remove using origFn
907                         fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ );
908                 }
909                 return this.each( function() {
910                         jQuery.event.add( this, types, fn, data, selector );
911                 });
912         },
913         one: function( types, selector, data, fn ) {
914                 return this.on( types, selector, data, fn, 1 );
915         },
916         off: function( types, selector, fn ) {
917                 var handleObj, type;
918                 if ( types && types.preventDefault && types.handleObj ) {
919                         // ( event )  dispatched jQuery.Event
920                         handleObj = types.handleObj;
921                         jQuery( types.delegateTarget ).off(
922                                 handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType,
923                                 handleObj.selector,
924                                 handleObj.handler
925                         );
926                         return this;
927                 }
928                 if ( typeof types === "object" ) {
929                         // ( types-object [, selector] )
930                         for ( type in types ) {
931                                 this.off( type, selector, types[ type ] );
932                         }
933                         return this;
934                 }
935                 if ( selector === false || typeof selector === "function" ) {
936                         // ( types [, fn] )
937                         fn = selector;
938                         selector = undefined;
939                 }
940                 if ( fn === false ) {
941                         fn = returnFalse;
942                 }
943                 return this.each(function() {
944                         jQuery.event.remove( this, types, fn, selector );
945                 });
946         },
948         bind: function( types, data, fn ) {
949                 return this.on( types, null, data, fn );
950         },
951         unbind: function( types, fn ) {
952                 return this.off( types, null, fn );
953         },
955         live: function( types, data, fn ) {
956                 jQuery( this.context ).on( types, this.selector, data, fn );
957                 return this;
958         },
959         die: function( types, fn ) {
960                 jQuery( this.context ).off( types, this.selector || "**", fn );
961                 return this;
962         },
964         delegate: function( selector, types, data, fn ) {
965                 return this.on( types, selector, data, fn );
966         },
967         undelegate: function( selector, types, fn ) {
968                 // ( namespace ) or ( selector, types [, fn] )
969                 return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn );
970         },
972         trigger: function( type, data ) {
973                 return this.each(function() {
974                         jQuery.event.trigger( type, data, this );
975                 });
976         },
977         triggerHandler: function( type, data ) {
978                 if ( this[0] ) {
979                         return jQuery.event.trigger( type, data, this[0], true );
980                 }
981         },
983         toggle: function( fn ) {
984                 // Save reference to arguments for access in closure
985                 var args = arguments,
986                         guid = fn.guid || jQuery.guid++,
987                         i = 0,
988                         toggler = function( event ) {
989                                 // Figure out which function to execute
990                                 var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
991                                 jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
993                                 // Make sure that clicks stop
994                                 event.preventDefault();
996                                 // and execute the function
997                                 return args[ lastToggle ].apply( this, arguments ) || false;
998                         };
1000                 // link all the functions, so any of them can unbind this click handler
1001                 toggler.guid = guid;
1002                 while ( i < args.length ) {
1003                         args[ i++ ].guid = guid;
1004                 }
1006                 return this.click( toggler );
1007         },
1009         hover: function( fnOver, fnOut ) {
1010                 return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
1011         }
1014 jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
1015         "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
1016         "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {
1018         // Handle event binding
1019         jQuery.fn[ name ] = function( data, fn ) {
1020                 if ( fn == null ) {
1021                         fn = data;
1022                         data = null;
1023                 }
1025                 return arguments.length > 0 ?
1026                         this.on( name, null, data, fn ) :
1027                         this.trigger( name );
1028         };
1030         if ( rkeyEvent.test( name ) ) {
1031                 jQuery.event.fixHooks[ name ] = jQuery.event.keyHooks;
1032         }
1034         if ( rmouseEvent.test( name ) ) {
1035                 jQuery.event.fixHooks[ name ] = jQuery.event.mouseHooks;
1036         }