Skip to content

Commit afd717d

Browse files
committed
Fix #12383. All selectors should be delegateTarget-relative
1 parent c078b83 commit afd717d

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/event.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ jQuery.event = {
361361
// Make a writable jQuery.Event from the native event object
362362
event = jQuery.event.fix( event || window.event );
363363

364-
var i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related,
364+
var i, j, cur, ret, selMatch, matched, matches, handleObj, sel, related,
365365
handlers = ( (jQuery._data( this, "events" ) || {} )[ event.type ] || []),
366366
delegateCount = handlers.delegateCount,
367367
args = [].slice.call( arguments ),
@@ -382,23 +382,18 @@ jQuery.event = {
382382
// Avoid non-left-click bubbling in Firefox (#3861)
383383
if ( delegateCount && !(event.button && event.type === "click") ) {
384384

385-
// Pregenerate a single jQuery object for reuse with .is()
386-
jqcur = jQuery(this);
387-
jqcur.context = this;
388-
389385
for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
390386

391387
// Don't process clicks (ONLY) on disabled elements (#6911, #8165, #xxxx)
392388
if ( cur.disabled !== true || event.type !== "click" ) {
393389
selMatch = {};
394390
matches = [];
395-
jqcur[0] = cur;
396391
for ( i = 0; i < delegateCount; i++ ) {
397392
handleObj = handlers[ i ];
398393
sel = handleObj.selector;
399394

400395
if ( selMatch[ sel ] === undefined ) {
401-
selMatch[ sel ] = jqcur.is( sel );
396+
selMatch[ sel ] = jQuery( sel, this ).index( cur ) >= 0;
402397
}
403398
if ( selMatch[ sel ] ) {
404399
matches.push( handleObj );

test/unit/event.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -2343,9 +2343,10 @@ test("jQuery.off using dispatched jQuery.Event", function() {
23432343
});
23442344

23452345
test( "delegated event with delegateTarget-relative selector", function() {
2346-
expect(2);
2347-
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");
2346+
expect(3);
2347+
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");
23482348

2349+
// Positional selector (#11315)
23492350
markup
23502351
.on( "click", ">li>a", function() {
23512352
ok( this.id === "a0", "child li was clicked" );
@@ -2356,7 +2357,24 @@ test( "delegated event with delegateTarget-relative selector", function() {
23562357
})
23572358
.end()
23582359
.find("a").click().end()
2359-
.remove();
2360+
.find("#ul0").off();
2361+
2362+
// Non-positional selector (#12383)
2363+
markup = markup.wrap("<div />").parent();
2364+
markup
2365+
.find("#ul0")
2366+
.on( "click", "div li a", function() {
2367+
ok( false, "div is ABOVE the delegation point!" );
2368+
})
2369+
.on( "click", "ul a", function() {
2370+
ok( false, "ul is the delegation point!" );
2371+
})
2372+
.on( "click", "li.test a", function() {
2373+
ok( true, "li.test is below the delegation point." );
2374+
})
2375+
.find("#a0_0").click();
2376+
2377+
markup.remove();
23602378
});
23612379

23622380
test("stopPropagation() stops directly-bound events on delegated target", function() {

0 commit comments

Comments
 (0)