Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Landing pull request 397. withinElement rewrite in event. Fixes #6234…

  • Loading branch information...
commit 6926247bf441deaa0441872849bb3786c257a4cf 1 parent 641ad80
@rwaldron rwaldron authored timmywil committed
Showing with 48 additions and 18 deletions.
  1. +11 −18 src/event.js
  2. +37 −0 test/unit/event.js
View
29 src/event.js
@@ -650,34 +650,27 @@ jQuery.Event.prototype = {
// Checks if an event happened on an element within another element
// Used in jQuery.event.special.mouseenter and mouseleave handlers
var withinElement = function( event ) {
+
// Check if mouse(over|out) are still within the same parent element
- var parent = event.relatedTarget;
+ var related = event.relatedTarget,
+ inside = false,
+ eventType = event.type;
- // set the correct event type
event.type = event.data;
- // Firefox sometimes assigns relatedTarget a XUL element
- // which we cannot access the parentNode property of
- try {
+ if ( related !== this ) {
- // Chrome does something similar, the parentNode property
- // can be accessed but is null.
- if ( parent && parent !== document && !parent.parentNode ) {
- return;
+ if ( related ) {
+ inside = jQuery.contains( this, related );
}
- // Traverse up the tree
- while ( parent && parent !== this ) {
- parent = parent.parentNode;
- }
+ if ( !inside ) {
- if ( parent !== this ) {
- // handle event if we actually just moused on to a non sub-element
jQuery.event.handle.apply( this, arguments );
- }
- // assuming we've left the element since we most likely mousedover a xul element
- } catch(e) { }
+ event.type = eventType;
+ }
+ }
},
// In case of event delegation, we only need to rename the event.type,
View
37 test/unit/event.js
@@ -780,6 +780,43 @@ test("mouseover triggers mouseenter", function() {
elem.remove();
});
+test("withinElement implemented with jQuery.contains()", function() {
+
+ expect(1);
+
+ jQuery("#qunit-fixture").append('<div id="jc-outer"><div id="jc-inner"></div></div>');
+
+ jQuery("#jc-outer").bind("mouseenter mouseleave", function( event ) {
+
+ equal( this.id, "jc-outer", this.id + " " + event.type );
+
+ }).trigger("mouseenter");
+
+ jQuery("#jc-inner").trigger("mousenter");
+
+ jQuery("#jc-outer").unbind("mouseenter mouseleave").remove();
+ jQuery("#jc-inner").remove();
+
+});
+
+test("mouseenter, mouseleave don't catch exceptions", function() {
+ expect(2);
+
+ var elem = jQuery("#firstp").hover(function() { throw "an Exception"; });
+
+ try {
+ elem.mouseenter();
+ } catch (e) {
+ equals( e, "an Exception", "mouseenter doesn't catch exceptions" );
+ }
+
+ try {
+ elem.mouseleave();
+ } catch (e) {
+ equals( e, "an Exception", "mouseleave doesn't catch exceptions" );
+ }
+});
+
test("trigger() shortcuts", function() {
expect(6);
Please sign in to comment.
Something went wrong with that request. Please try again.