Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing issue with a delegated listener not stopping a normal listener #64

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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