Skip to content

Commit e44b077

Browse files
JiaLiPassiondylhunn
authored andcommitted
fix(zone.js): should not clear onhandler when remove capture listener (#54602)
Close #54581 Should not clear `onHandler` when remove capture event listeners. PR Close #54602
1 parent eae75ff commit e44b077

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

packages/zone.js/lib/common/events.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,10 @@ export function patchEventTarget(
576576
target[symbolEventName] = null;
577577
// in the target, we have an event listener which is added by on_property
578578
// such as target.onclick = function() {}, so we need to clear this internal
579-
// property too if all delegates all removed
580-
if (typeof eventName === 'string') {
579+
// property too if all delegates with capture=false were removed
580+
// https:// github.com/angular/angular/issues/31643
581+
// https://github.com/angular/angular/issues/54581
582+
if (!capture && typeof eventName === 'string') {
581583
const onPropertySymbol = ZONE_SYMBOL_PREFIX + 'ON_PROPERTY' + eventName;
582584
target[onPropertySymbol] = null;
583585
}

packages/zone.js/test/browser/browser.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,41 @@ describe('Zone', function() {
20462046
});
20472047
});
20482048

2049+
it('should not remove onEventListener when removing capture listener', function() {
2050+
const button = document.createElement('button');
2051+
document.body.append(button);
2052+
const createEvt = () => {
2053+
const evt = document.createEvent('Event');
2054+
evt.initEvent('click', true, true);
2055+
return evt;
2056+
};
2057+
let logs: string[] = [];
2058+
const onClickHandler = () => logs.push('onclick');
2059+
button.onclick = onClickHandler;
2060+
let evt = createEvt();
2061+
button.dispatchEvent(evt);
2062+
expect(logs).toEqual(['onclick']);
2063+
logs = [];
2064+
const listener = () => logs.push('click listener');
2065+
button.addEventListener('click', listener, {capture: true});
2066+
evt = createEvt();
2067+
button.dispatchEvent(evt);
2068+
expect(logs.sort()).toEqual(['onclick', 'click listener'].sort());
2069+
logs = [];
2070+
button.removeEventListener('click', listener, true);
2071+
evt = createEvt();
2072+
button.dispatchEvent(evt);
2073+
expect(logs).toEqual(['onclick']);
2074+
expect(button.onclick).toBe(onClickHandler);
2075+
button.onclick = null;
2076+
logs = [];
2077+
evt = createEvt();
2078+
button.dispatchEvent(evt);
2079+
expect(logs).toEqual([]);
2080+
expect(button.onclick).toBe(null);
2081+
document.body.removeChild(button);
2082+
});
2083+
20492084
describe('should be able to remove eventListener during eventListener callback', function() {
20502085
it('should be able to remove eventListener during eventListener callback', function() {
20512086
let logs: string[] = [];

0 commit comments

Comments
 (0)