mirror of
https://github.com/jquery/jquery.git
synced 2026-04-20 03:01:22 -04:00
Merge branch 'event20' of https://github.com/dmethvin/jquery
* 'event20' of https://github.com/dmethvin/jquery: Clean up comments, including browser support levels. Remove try/catch around focus events needed by oldIE. Remove obsolete event object fixups. Remove oldIE proprietary event methods/properties. Remove oldIE change and submit special-event code.
This commit is contained in:
210
src/event.js
210
src/event.js
@@ -1,5 +1,4 @@
|
||||
var rformElems = /^(?:input|select|textarea)$/i,
|
||||
rkeyEvent = /^key/,
|
||||
var rkeyEvent = /^key/,
|
||||
rmouseEvent = /^(?:mouse|contextmenu)|click/,
|
||||
rfocusMorph = /^(?:focusinfocus|focusoutblur)$/,
|
||||
rtypenamespace = /^([^.]*)(?:\.(.+)|)$/;
|
||||
@@ -52,7 +51,6 @@ jQuery.event = {
|
||||
}
|
||||
|
||||
// Handle multiple events separated by a space
|
||||
// jQuery(...).bind("mouseover mouseout", fn);
|
||||
types = ( types || "" ).match( core_rnotwhite ) || [""];
|
||||
for ( ; t < types.length; t++ ) {
|
||||
|
||||
@@ -87,14 +85,10 @@ jQuery.event = {
|
||||
handlers = events[ type ] = [];
|
||||
handlers.delegateCount = 0;
|
||||
|
||||
// Only use addEventListener/attachEvent if the special events handler returns false
|
||||
// Only use addEventListener if the special events handler returns false
|
||||
if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
|
||||
// Bind the global event handler to the element
|
||||
if ( elem.addEventListener ) {
|
||||
elem.addEventListener( type, eventHandle, false );
|
||||
|
||||
} else if ( elem.attachEvent ) {
|
||||
elem.attachEvent( "on" + type, eventHandle );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,9 +290,8 @@ jQuery.event = {
|
||||
!(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {
|
||||
|
||||
// Call a native DOM method on the target with the same name name as the event.
|
||||
// Can't use an .isFunction() check here because IE6/7 fails that test.
|
||||
// Don't do default actions on window, that's where global variables be (#6170)
|
||||
if ( ontype && elem[ type ] && !jQuery.isWindow( elem ) ) {
|
||||
if ( ontype && jQuery.isFunction( elem[ type ] ) && !jQuery.isWindow( elem ) ) {
|
||||
|
||||
// Don't re-trigger an onFOO event when we call its FOO() method
|
||||
old = elem[ ontype ];
|
||||
@@ -309,12 +302,7 @@ jQuery.event = {
|
||||
|
||||
// Prevent re-triggering of the same event, since we already bubbled it above
|
||||
jQuery.event.triggered = type;
|
||||
try {
|
||||
elem[ type ]();
|
||||
} catch ( e ) {
|
||||
// IE<9 dies on focus/blur to hidden element (#1486,#12518)
|
||||
// only reproducible on winXP IE8 native, not IE9 in IE8 mode
|
||||
}
|
||||
elem[ type ]();
|
||||
jQuery.event.triggered = undefined;
|
||||
|
||||
if ( old ) {
|
||||
@@ -354,7 +342,7 @@ jQuery.event = {
|
||||
|
||||
for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
|
||||
|
||||
// Don't process clicks (ONLY) on disabled elements (#6911, #8165, #11382, #11764)
|
||||
// Ignore clicks (ONLY) on disabled elements (#6911, #8165, #11382, #11764)
|
||||
if ( cur.disabled !== true || event.type !== "click" ) {
|
||||
selMatch = {};
|
||||
matches = [];
|
||||
@@ -439,11 +427,10 @@ jQuery.event = {
|
||||
},
|
||||
|
||||
mouseHooks: {
|
||||
props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "),
|
||||
props: "button buttons clientX clientY offsetX offsetY pageX pageY screenX screenY toElement".split(" "),
|
||||
filter: function( event, original ) {
|
||||
var eventDoc, doc, body,
|
||||
button = original.button,
|
||||
fromElement = original.fromElement;
|
||||
button = original.button;
|
||||
|
||||
// Calculate pageX/Y if missing and clientX/Y available
|
||||
if ( event.pageX == null && original.clientX != null ) {
|
||||
@@ -455,11 +442,6 @@ jQuery.event = {
|
||||
event.pageY = original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 );
|
||||
}
|
||||
|
||||
// Add relatedTarget, if necessary
|
||||
if ( !event.relatedTarget && fromElement ) {
|
||||
event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
|
||||
}
|
||||
|
||||
// Add which for click: 1 === left; 2 === middle; 3 === right
|
||||
// Note: button is not normalized, so don't use it
|
||||
if ( !event.which && button !== undefined ) {
|
||||
@@ -488,19 +470,6 @@ jQuery.event = {
|
||||
event[ prop ] = originalEvent[ prop ];
|
||||
}
|
||||
|
||||
// Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
|
||||
if ( !event.target ) {
|
||||
event.target = originalEvent.srcElement || document;
|
||||
}
|
||||
|
||||
// Target should not be a text node (#504, Safari)
|
||||
if ( event.target.nodeType === 3 ) {
|
||||
event.target = event.target.parentNode;
|
||||
}
|
||||
|
||||
// For mouse/key events, metaKey==false if it's undefined (#3368, #11328; IE6/7/8)
|
||||
event.metaKey = !!event.metaKey;
|
||||
|
||||
return fixHook.filter? fixHook.filter( event, originalEvent ) : event;
|
||||
},
|
||||
|
||||
@@ -522,13 +491,8 @@ jQuery.event = {
|
||||
// Fire native event if possible so blur/focus sequence is correct
|
||||
trigger: function() {
|
||||
if ( this !== document.activeElement && this.focus ) {
|
||||
try {
|
||||
this.focus();
|
||||
return false;
|
||||
} catch ( e ) {
|
||||
// IE<9 dies on focus to hidden element (#1486,#12518)
|
||||
// If this happens, let .trigger() run the handlers
|
||||
}
|
||||
this.focus();
|
||||
return false;
|
||||
}
|
||||
},
|
||||
delegateType: "focusin"
|
||||
@@ -546,7 +510,7 @@ jQuery.event = {
|
||||
beforeunload: {
|
||||
postDispatch: function( event ) {
|
||||
|
||||
// Even when returnValue equals to undefined Firefox will still show alert
|
||||
// Support: Firefox 10+
|
||||
if ( event.result !== undefined ) {
|
||||
event.originalEvent.returnValue = event.result;
|
||||
}
|
||||
@@ -581,26 +545,11 @@ jQuery.event = {
|
||||
// The 1.7 special event interface should provide all the hooks needed now.
|
||||
jQuery.event.handle = jQuery.event.dispatch;
|
||||
|
||||
jQuery.removeEvent = document.removeEventListener ?
|
||||
function( elem, type, handle ) {
|
||||
if ( elem.removeEventListener ) {
|
||||
elem.removeEventListener( type, handle, false );
|
||||
}
|
||||
} :
|
||||
function( elem, type, handle ) {
|
||||
var name = "on" + type;
|
||||
|
||||
if ( elem.detachEvent ) {
|
||||
|
||||
// #8545, #7054, preventing memory leaks for custom events in IE6-8
|
||||
// detachEvent needed property on element, by name of that event, to properly expose it to GC
|
||||
if ( typeof elem[ name ] === "undefined" ) {
|
||||
elem[ name ] = null;
|
||||
}
|
||||
|
||||
elem.detachEvent( name, handle );
|
||||
}
|
||||
};
|
||||
jQuery.removeEvent = function( elem, type, handle ) {
|
||||
if ( elem.removeEventListener ) {
|
||||
elem.removeEventListener( type, handle, false );
|
||||
}
|
||||
};
|
||||
|
||||
jQuery.Event = function( src, props ) {
|
||||
// Allow instantiation without the 'new' keyword
|
||||
@@ -615,7 +564,7 @@ jQuery.Event = function( src, props ) {
|
||||
|
||||
// Events bubbling up the document may have been marked as prevented
|
||||
// by a handler lower down the tree; reflect the correct value.
|
||||
this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false ||
|
||||
this.isDefaultPrevented = ( src.defaultPrevented ||
|
||||
src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse;
|
||||
|
||||
// Event type
|
||||
@@ -649,32 +598,17 @@ jQuery.Event.prototype = {
|
||||
this.isDefaultPrevented = returnTrue;
|
||||
|
||||
var e = this.originalEvent;
|
||||
if ( !e ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if preventDefault exists run it on the original event
|
||||
if ( e.preventDefault ) {
|
||||
if ( e && e.preventDefault ) {
|
||||
e.preventDefault();
|
||||
|
||||
// otherwise set the returnValue property of the original event to false (IE)
|
||||
} else {
|
||||
e.returnValue = false;
|
||||
}
|
||||
},
|
||||
stopPropagation: function() {
|
||||
this.isPropagationStopped = returnTrue;
|
||||
|
||||
var e = this.originalEvent;
|
||||
if ( !e ) {
|
||||
return;
|
||||
}
|
||||
// if stopPropagation exists run it on the original event
|
||||
if ( e.stopPropagation ) {
|
||||
if ( e && e.stopPropagation ) {
|
||||
e.stopPropagation();
|
||||
}
|
||||
// otherwise set the cancelBubble property of the original event to true (IE)
|
||||
e.cancelBubble = true;
|
||||
},
|
||||
stopImmediatePropagation: function() {
|
||||
this.isImmediatePropagationStopped = returnTrue;
|
||||
@@ -686,6 +620,7 @@ jQuery.Event.prototype = {
|
||||
};
|
||||
|
||||
// Create mouseenter/leave events using mouseover/out and event-time checks
|
||||
// Support: Chrome 15+
|
||||
jQuery.each({
|
||||
mouseenter: "mouseover",
|
||||
mouseleave: "mouseout"
|
||||
@@ -712,113 +647,8 @@ jQuery.each({
|
||||
};
|
||||
});
|
||||
|
||||
// IE submit delegation
|
||||
if ( !jQuery.support.submitBubbles ) {
|
||||
|
||||
jQuery.event.special.submit = {
|
||||
setup: function() {
|
||||
// Only need this for delegated form submit events
|
||||
if ( jQuery.nodeName( this, "form" ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lazy-add a submit handler when a descendant form may potentially be submitted
|
||||
jQuery.event.add( this, "click._submit keypress._submit", function( e ) {
|
||||
// Node name check avoids a VML-related crash in IE (#9807)
|
||||
var elem = e.target,
|
||||
form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined;
|
||||
if ( form && !jQuery._data( form, "_submit_attached" ) ) {
|
||||
jQuery.event.add( form, "submit._submit", function( event ) {
|
||||
event._submit_bubble = true;
|
||||
});
|
||||
jQuery._data( form, "_submit_attached", true );
|
||||
}
|
||||
});
|
||||
// return undefined since we don't need an event listener
|
||||
},
|
||||
|
||||
postDispatch: function( event ) {
|
||||
// If form was submitted by the user, bubble the event up the tree
|
||||
if ( event._submit_bubble ) {
|
||||
delete event._submit_bubble;
|
||||
if ( this.parentNode && !event.isTrigger ) {
|
||||
jQuery.event.simulate( "submit", this.parentNode, event, true );
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
teardown: function() {
|
||||
// Only need this for delegated form submit events
|
||||
if ( jQuery.nodeName( this, "form" ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove delegated handlers; cleanData eventually reaps submit handlers attached above
|
||||
jQuery.event.remove( this, "._submit" );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// IE change delegation and checkbox/radio fix
|
||||
if ( !jQuery.support.changeBubbles ) {
|
||||
|
||||
jQuery.event.special.change = {
|
||||
|
||||
setup: function() {
|
||||
|
||||
if ( rformElems.test( this.nodeName ) ) {
|
||||
// IE doesn't fire change on a check/radio until blur; trigger it on click
|
||||
// after a propertychange. Eat the blur-change in special.change.handle.
|
||||
// This still fires onchange a second time for check/radio after blur.
|
||||
if ( this.type === "checkbox" || this.type === "radio" ) {
|
||||
jQuery.event.add( this, "propertychange._change", function( event ) {
|
||||
if ( event.originalEvent.propertyName === "checked" ) {
|
||||
this._just_changed = true;
|
||||
}
|
||||
});
|
||||
jQuery.event.add( this, "click._change", function( event ) {
|
||||
if ( this._just_changed && !event.isTrigger ) {
|
||||
this._just_changed = false;
|
||||
}
|
||||
// Allow triggered, simulated change events (#11500)
|
||||
jQuery.event.simulate( "change", this, event, true );
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Delegated event; lazy-add a change handler on descendant inputs
|
||||
jQuery.event.add( this, "beforeactivate._change", function( e ) {
|
||||
var elem = e.target;
|
||||
|
||||
if ( rformElems.test( elem.nodeName ) && !jQuery._data( elem, "_change_attached" ) ) {
|
||||
jQuery.event.add( elem, "change._change", function( event ) {
|
||||
if ( this.parentNode && !event.isSimulated && !event.isTrigger ) {
|
||||
jQuery.event.simulate( "change", this.parentNode, event, true );
|
||||
}
|
||||
});
|
||||
jQuery._data( elem, "_change_attached", true );
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
handle: function( event ) {
|
||||
var elem = event.target;
|
||||
|
||||
// Swallow native change events from checkbox/radio, we already triggered them above
|
||||
if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) {
|
||||
return event.handleObj.handler.apply( this, arguments );
|
||||
}
|
||||
},
|
||||
|
||||
teardown: function() {
|
||||
jQuery.event.remove( this, "._change" );
|
||||
|
||||
return !rformElems.test( this.nodeName );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Create "bubbling" focus and blur events
|
||||
// Support: Firefox 10+
|
||||
if ( !jQuery.support.focusinBubbles ) {
|
||||
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user