Skip to content

Commit

Permalink
Merge pull request #64 from canjs/delegate-propagation
Browse files Browse the repository at this point in the history
fixing issue with a delegated listener not stopping a normal listener
  • Loading branch information
phillipskevin authored Mar 25, 2019
2 parents a4d6769 + 395453e commit 9bbfb69
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
30 changes: 29 additions & 1 deletion can-dom-events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', f

var paragraphClickHandler = function paragraphClickHandler() {
domEvents.removeDelegateListener(grandparent, 'click', 'p', paragraphClickHandler);
assert.ok(false, 'ev.stopPropagation works');
assert.ok(false, stopMethod + ' works');
};

var inputClickHandler = function inputClickHandler(event) {
Expand All @@ -244,4 +244,32 @@ unit.test('domEvents.addDelegateListener call inner-most handler first (#62)', f
domEvents.dispatch(child, 'click');
done();
});

unit.test('domEvents.addDelegateListener should call the actual ev.' + stopMethod + ' (#62)', function (assert) {
var done = assert.async();
var parent = document.createElement('p');
var child = document.createElement('input');

parent.appendChild(child);

var qf = document.querySelector('#qunit-fixture');
qf.appendChild(parent);

var delegatedClickHandler = function delegatedClickHandler(event) {
event[stopMethod]();
domEvents.removeDelegateListener(parent, 'click', 'input', delegatedClickHandler);
assert.ok(true, 'inner-most click handler called first');
};

var documentClickHandler = function documentClickHandler() {
domEvents.removeEventListener(document, 'click', documentClickHandler);
assert.ok(false, stopMethod + ' works');
};

domEvents.addDelegateListener(parent, 'click', 'input', delegatedClickHandler);
domEvents.addEventListener(document, 'click', documentClickHandler);

domEvents.dispatch(child, 'click');
done();
});
});
9 changes: 8 additions & 1 deletion helpers/-make-delegate-event-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ function makeDelegator (domEvents) {
var handler = this.delegated[eventType] = function(ev){
var cur = ev.target;
var propagate = true;
ev.stopPropagation = ev.stopImmediatePropagation = function() {
var origStopPropagation = ev.stopPropagation;
ev.stopPropagation = function() {
origStopPropagation.apply(this, arguments);
propagate = false;
};
var origStopImmediatePropagation = ev.stopImmediatePropagation;
ev.stopImmediatePropagation = function() {
origStopImmediatePropagation.apply(this, arguments);
propagate = false;
};
do {
Expand Down

0 comments on commit 9bbfb69

Please sign in to comment.