Skip to content

Commit 6926247

Browse files
rwaldrontimmywil
authored andcommitted
Landing pull request 397. withinElement rewrite in event. Fixes #6234, #9357, #9447.
More Details: - jquery#397 - http://bugs.jquery.com/ticket/6234 - http://bugs.jquery.com/ticket/9357 - http://bugs.jquery.com/ticket/9447
1 parent 641ad80 commit 6926247

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

src/event.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -650,34 +650,27 @@ jQuery.Event.prototype = {
650650
// Checks if an event happened on an element within another element
651651
// Used in jQuery.event.special.mouseenter and mouseleave handlers
652652
var withinElement = function( event ) {
653+
653654
// Check if mouse(over|out) are still within the same parent element
654-
var parent = event.relatedTarget;
655+
var related = event.relatedTarget,
656+
inside = false,
657+
eventType = event.type;
655658

656-
// set the correct event type
657659
event.type = event.data;
658660

659-
// Firefox sometimes assigns relatedTarget a XUL element
660-
// which we cannot access the parentNode property of
661-
try {
661+
if ( related !== this ) {
662662

663-
// Chrome does something similar, the parentNode property
664-
// can be accessed but is null.
665-
if ( parent && parent !== document && !parent.parentNode ) {
666-
return;
663+
if ( related ) {
664+
inside = jQuery.contains( this, related );
667665
}
668666

669-
// Traverse up the tree
670-
while ( parent && parent !== this ) {
671-
parent = parent.parentNode;
672-
}
667+
if ( !inside ) {
673668

674-
if ( parent !== this ) {
675-
// handle event if we actually just moused on to a non sub-element
676669
jQuery.event.handle.apply( this, arguments );
677-
}
678670

679-
// assuming we've left the element since we most likely mousedover a xul element
680-
} catch(e) { }
671+
event.type = eventType;
672+
}
673+
}
681674
},
682675

683676
// In case of event delegation, we only need to rename the event.type,

test/unit/event.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,43 @@ test("mouseover triggers mouseenter", function() {
780780
elem.remove();
781781
});
782782

783+
test("withinElement implemented with jQuery.contains()", function() {
784+
785+
expect(1);
786+
787+
jQuery("#qunit-fixture").append('<div id="jc-outer"><div id="jc-inner"></div></div>');
788+
789+
jQuery("#jc-outer").bind("mouseenter mouseleave", function( event ) {
790+
791+
equal( this.id, "jc-outer", this.id + " " + event.type );
792+
793+
}).trigger("mouseenter");
794+
795+
jQuery("#jc-inner").trigger("mousenter");
796+
797+
jQuery("#jc-outer").unbind("mouseenter mouseleave").remove();
798+
jQuery("#jc-inner").remove();
799+
800+
});
801+
802+
test("mouseenter, mouseleave don't catch exceptions", function() {
803+
expect(2);
804+
805+
var elem = jQuery("#firstp").hover(function() { throw "an Exception"; });
806+
807+
try {
808+
elem.mouseenter();
809+
} catch (e) {
810+
equals( e, "an Exception", "mouseenter doesn't catch exceptions" );
811+
}
812+
813+
try {
814+
elem.mouseleave();
815+
} catch (e) {
816+
equals( e, "an Exception", "mouseleave doesn't catch exceptions" );
817+
}
818+
});
819+
783820
test("trigger() shortcuts", function() {
784821
expect(6);
785822

0 commit comments

Comments
 (0)