Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ export class EventDispatcher {

function prepareEventForBubbling(eventInfoWrapper: EventInfoWrapper) {
const event = eventInfoWrapper.getEvent();
const originalStopPropagation = eventInfoWrapper.getEvent().stopPropagation.bind(event);
const stopPropagation = () => {
event[PROPAGATION_STOPPED_SYMBOL] = true;
originalStopPropagation();
};
patchEventInstance(event, 'stopPropagation', stopPropagation);
patchEventInstance(event, 'stopImmediatePropagation', stopPropagation);
Expand All @@ -107,9 +109,11 @@ function propagationStopped(eventInfoWrapper: EventInfoWrapper) {
function prepareEventForReplay(eventInfoWrapper: EventInfoWrapper) {
const event = eventInfoWrapper.getEvent();
const target = eventInfoWrapper.getTargetElement();
const originalPreventDefault = event.preventDefault.bind(event);
patchEventInstance(event, 'target', target);
patchEventInstance(event, 'eventPhase', EventPhase.REPLAY);
patchEventInstance(event, 'preventDefault', () => {
originalPreventDefault();
throw new Error(
PREVENT_DEFAULT_ERROR_MESSAGE + (ngDevMode ? PREVENT_DEFAULT_ERROR_MESSAGE_DETAILS : ''),
);
Expand Down
55 changes: 55 additions & 0 deletions packages/core/test/event_dispatch/event_dispatch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,35 @@ describe('event dispatch', () => {
bottomEl.click();
expect(onClickSpy).toHaveBeenCalledTimes(1);
});
it('should call the original stopPropagation method', async () => {
@Component({
standalone: true,
selector: 'app',
template: `
<div id="top" (click)="onClick($event)">
<div id="bottom" (click)="onClick($event)"></div>
</div>
`,
})
class SimpleComponent {
onClick(e: Event) {
e.stopPropagation();
e.preventDefault();
}
}
configureTestingModule([SimpleComponent]);
fixture = TestBed.createComponent(SimpleComponent);
const nativeElement = fixture.debugElement.nativeElement;
const bottomEl = nativeElement.querySelector('#bottom')!;
const event = new MouseEvent('click', {bubbles: true});
spyOn(event, 'stopPropagation');
const stopPropagation = event.stopPropagation;
spyOn(event, 'preventDefault');
const preventDefault = event.preventDefault;
bottomEl.dispatchEvent(event);
expect(stopPropagation).toHaveBeenCalled();
expect(preventDefault).toHaveBeenCalled();
});
});

describe('manual listening', () => {
Expand Down Expand Up @@ -248,5 +277,31 @@ describe('event dispatch', () => {
bottomEl.click();
expect(onClickSpy).toHaveBeenCalledTimes(1);
});
it('should allow host listening on the window', async () => {
const onClickSpy = jasmine.createSpy();
@Component({
standalone: true,
selector: 'app',
template: `
<div id="top">
<div id="bottom"></div>
</div>
`,
})
class SimpleComponent {
renderer = inject(Renderer2);
destroy!: Function;
@HostListener('window:click', ['$event.target'])
listen(el: Element) {
onClickSpy();
}
}
configureTestingModule([SimpleComponent]);
fixture = TestBed.createComponent(SimpleComponent);
const nativeElement = fixture.debugElement.nativeElement;
const bottomEl = nativeElement.querySelector('#bottom')!;
bottomEl.click();
expect(onClickSpy).toHaveBeenCalledTimes(1);
});
});
});