]> source.dussan.org Git - jquery.git/commitdiff
Fix #12383. All selectors should be delegateTarget-relative
authorDave Methvin <dave.methvin@gmail.com>
Tue, 28 Aug 2012 14:07:16 +0000 (10:07 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Tue, 28 Aug 2012 14:07:16 +0000 (10:07 -0400)
src/event.js
test/unit/event.js

index c22f5f872bef3de4b86d8cb4508e2ee9cf3c1d2a..0c7d827dc59e5fd5e7d76e1880b23e31e28b6c66 100644 (file)
@@ -361,7 +361,7 @@ jQuery.event = {
                // Make a writable jQuery.Event from the native event object
                event = jQuery.event.fix( event || window.event );
 
-               var i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related,
+               var i, j, cur, ret, selMatch, matched, matches, handleObj, sel, related,
                        handlers = ( (jQuery._data( this, "events" ) || {} )[ event.type ] || []),
                        delegateCount = handlers.delegateCount,
                        args = [].slice.call( arguments ),
@@ -382,23 +382,18 @@ jQuery.event = {
                // Avoid non-left-click bubbling in Firefox (#3861)
                if ( delegateCount && !(event.button && event.type === "click") ) {
 
-                       // Pregenerate a single jQuery object for reuse with .is()
-                       jqcur = jQuery(this);
-                       jqcur.context = this;
-
                        for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
 
                                // Don't process clicks (ONLY) on disabled elements (#6911, #8165, #xxxx)
                                if ( cur.disabled !== true || event.type !== "click" ) {
                                        selMatch = {};
                                        matches = [];
-                                       jqcur[0] = cur;
                                        for ( i = 0; i < delegateCount; i++ ) {
                                                handleObj = handlers[ i ];
                                                sel = handleObj.selector;
 
                                                if ( selMatch[ sel ] === undefined ) {
-                                                       selMatch[ sel ] = jqcur.is( sel );
+                                                       selMatch[ sel ] = jQuery( sel, this ).index( cur ) >= 0;
                                                }
                                                if ( selMatch[ sel ] ) {
                                                        matches.push( handleObj );
index 74cf1b9b704f9923ebb3af52108dbd9a92c04110..5f609247ae35e9255b6fe25ae0774f51758eace7 100644 (file)
@@ -2343,9 +2343,10 @@ test("jQuery.off using dispatched jQuery.Event", function() {
 });
 
 test( "delegated event with delegateTarget-relative selector", function() {
-       expect(2);
-       var markup = jQuery("<ul><li><a id=\"a0\"></a><ul id=\"ul0\"><li><a id=\"a0_0\"></a></li><li><a id=\"a0_1\"></a></li></ul></li></ul>").appendTo("#qunit-fixture");
+       expect(3);
+       var markup = jQuery("<ul><li><a id=\"a0\"></a><ul id=\"ul0\"><li class=test><a id=\"a0_0\"></a></li><li><a id=\"a0_1\"></a></li></ul></li></ul>").appendTo("#qunit-fixture");
 
+       // Positional selector (#11315)
        markup
                .on( "click", ">li>a", function() {
                        ok( this.id === "a0", "child li was clicked" );
@@ -2356,7 +2357,24 @@ test( "delegated event with delegateTarget-relative selector", function() {
                        })
                .end()
                .find("a").click().end()
-               .remove();
+               .find("#ul0").off();
+       
+       // Non-positional selector (#12383)
+       markup = markup.wrap("<div />").parent();
+       markup
+               .find("#ul0")
+               .on( "click", "div li a", function() {
+                       ok( false, "div is ABOVE the delegation point!" );
+               })
+               .on( "click", "ul a", function() {
+                       ok( false, "ul is the delegation point!" );
+               })
+               .on( "click", "li.test a", function() {
+                       ok( true, "li.test is below the delegation point." );
+               })
+               .find("#a0_0").click();
+       
+       markup.remove();
 });
 
 test("stopPropagation() stops directly-bound events on delegated target", function() {