Skip to content

Commit

Permalink
fix(zone.js): should not clear onhandler when remove capture listener
Browse files Browse the repository at this point in the history
Close angular#54581

Should not clear `onHandler` when remove capture event listeners.
  • Loading branch information
JiaLiPassion committed Feb 27, 2024
1 parent dcb9deb commit 9e2900b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/zone.js/lib/common/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ export function patchEventTarget(
target[symbolEventName] = null;
// in the target, we have an event listener which is added by on_property
// such as target.onclick = function() {}, so we need to clear this internal
// property too if all delegates all removed
if (typeof eventName === 'string') {
// property too if all delegates with capture=false were removed
// https:// github.com/angular/angular/issues/31643
// https://github.com/angular/angular/issues/54581
if (!capture && typeof eventName === 'string') {
const onPropertySymbol = ZONE_SYMBOL_PREFIX + 'ON_PROPERTY' + eventName;
target[onPropertySymbol] = null;
}
Expand Down
35 changes: 35 additions & 0 deletions packages/zone.js/test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,41 @@ describe('Zone', function() {
});
});

it('should not remove onEventListener when removing capture listener', function() {
const button = document.createElement('button');
document.body.append(button);
const createEvt = () => {
const evt = document.createEvent('Event');
evt.initEvent('click', true, true);
return evt;
};
let logs: string[] = [];
const onClickHandler = () => logs.push('onclick');
button.onclick = onClickHandler;
let evt = createEvt();
button.dispatchEvent(evt);
expect(logs).toEqual(['onclick']);
logs = [];
const listener = () => logs.push('click listener');
button.addEventListener('click', listener, {capture: true});
evt = createEvt();
button.dispatchEvent(evt);
expect(logs.sort()).toEqual(['onclick', 'click listener'].sort());
logs = [];
button.removeEventListener('click', listener, true);
evt = createEvt();
button.dispatchEvent(evt);
expect(logs).toEqual(['onclick']);
expect(button.onclick).toBe(onClickHandler);
button.onclick = null;
logs = [];
evt = createEvt();
button.dispatchEvent(evt);
expect(logs).toEqual([]);
expect(button.onclick).toBe(null);
document.body.removeChild(button);
});

describe('should be able to remove eventListener during eventListener callback', function() {
it('should be able to remove eventListener during eventListener callback', function() {
let logs: string[] = [];
Expand Down

0 comments on commit 9e2900b

Please sign in to comment.