Skip to content

Commit 2ece428

Browse files
JeanMechethePunderWoman
authored andcommitted
fix(platform-browser): DomEventsPlugin should always be the last plugin to be called for supports(). (#50394)
This fixes the issues when `BrowserModule` is not the first module imported. Fixes #37149 #37850 PR Close #50394
1 parent dbcc281 commit 2ece428

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

packages/platform-browser/src/dom/events/dom_events.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import {DOCUMENT} from '@angular/common';
1010
import {Inject, Injectable, type ListenerOptions} from '@angular/core';
11-
12-
import {EventManagerPlugin} from './event_manager';
11+
import {EventManagerPlugin} from './event_manager_plugin';
1312

1413
@Injectable()
1514
export class DomEventsPlugin extends EventManagerPlugin {

packages/platform-browser/src/dom/events/event_manager.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import {
1717

1818
import {RuntimeErrorCode} from '../../errors';
1919

20+
import type {EventManagerPlugin} from './event_manager_plugin';
21+
22+
// Temporart re-export until we can change files that import from this file in G3.
23+
export {EventManagerPlugin} from './event_manager_plugin';
24+
25+
import {DomEventsPlugin} from './dom_events';
26+
2027
/**
2128
* The injection token for plugins of the `EventManager` service.
2229
*
@@ -47,7 +54,15 @@ export class EventManager {
4754
plugins.forEach((plugin) => {
4855
plugin.manager = this;
4956
});
50-
this._plugins = plugins.slice().reverse();
57+
58+
const otherPlugins = plugins.filter((p) => !(p instanceof DomEventsPlugin));
59+
this._plugins = otherPlugins.slice().reverse();
60+
61+
// DomEventsPlugin.supports() always returns true, it should always be the last plugin.
62+
const domEventPlugin = plugins.find((p) => p instanceof DomEventsPlugin);
63+
if (domEventPlugin) {
64+
this._plugins.push(domEventPlugin);
65+
}
5166
}
5267

5368
/**
@@ -98,34 +113,3 @@ export class EventManager {
98113
return plugin;
99114
}
100115
}
101-
102-
/**
103-
* The plugin definition for the `EventManager` class
104-
*
105-
* It can be used as a base class to create custom manager plugins, i.e. you can create your own
106-
* class that extends the `EventManagerPlugin` one.
107-
*
108-
* @publicApi
109-
*/
110-
export abstract class EventManagerPlugin {
111-
// TODO: remove (has some usage in G3)
112-
constructor(private _doc: any) {}
113-
114-
// Using non-null assertion because it's set by EventManager's constructor
115-
manager!: EventManager;
116-
117-
/**
118-
* Should return `true` for every event name that should be supported by this plugin
119-
*/
120-
abstract supports(eventName: string): boolean;
121-
122-
/**
123-
* Implement the behaviour for the supported events
124-
*/
125-
abstract addEventListener(
126-
element: HTMLElement,
127-
eventName: string,
128-
handler: Function,
129-
options?: ListenerOptions,
130-
): Function;
131-
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import type {ListenerOptions} from '@angular/core';
10+
import type {EventManager} from './event_manager';
11+
12+
/**
13+
* The plugin definition for the `EventManager` class
14+
*
15+
* It can be used as a base class to create custom manager plugins, i.e. you can create your own
16+
* class that extends the `EventManagerPlugin` one.
17+
*
18+
* @publicApi
19+
*/
20+
export abstract class EventManagerPlugin {
21+
// TODO: remove (has some usage in G3)
22+
constructor(private _doc: any) {}
23+
24+
// Using non-null assertion because it's set by EventManager's constructor
25+
manager!: EventManager;
26+
27+
/**
28+
* Should return `true` for every event name that should be supported by this plugin
29+
*/
30+
abstract supports(eventName: string): boolean;
31+
32+
/**
33+
* Implement the behaviour for the supported events
34+
*/
35+
abstract addEventListener(
36+
element: HTMLElement,
37+
eventName: string,
38+
handler: Function,
39+
options?: ListenerOptions,
40+
): Function;
41+
}

packages/platform-browser/src/dom/events/hammer_gestures.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
ɵConsole as Console,
2020
} from '@angular/core';
2121

22-
import {EVENT_MANAGER_PLUGINS, EventManagerPlugin} from './event_manager';
22+
import {EVENT_MANAGER_PLUGINS} from './event_manager';
23+
import {EventManagerPlugin} from './event_manager_plugin';
2324

2425
/**
2526
* Supported HammerJS recognizer event names.

packages/platform-browser/src/dom/events/key_events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/common';
1010
import {Inject, Injectable, type ListenerOptions, NgZone} from '@angular/core';
1111

12-
import {EventManagerPlugin} from './event_manager';
12+
import {EventManagerPlugin} from './event_manager_plugin';
1313

1414
/**
1515
* Defines supported modifiers for key events.

packages/platform-browser/src/platform-browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export {Title} from './browser/title';
2020
export {disableDebugTools, enableDebugTools} from './browser/tools/tools';
2121
export {By} from './dom/debug/by';
2222
export {REMOVE_STYLES_ON_COMPONENT_DESTROY} from './dom/dom_renderer';
23-
export {EVENT_MANAGER_PLUGINS, EventManager, EventManagerPlugin} from './dom/events/event_manager';
23+
export {EVENT_MANAGER_PLUGINS, EventManager} from './dom/events/event_manager';
24+
export {EventManagerPlugin} from './dom/events/event_manager_plugin';
2425
export {
2526
HAMMER_GESTURE_CONFIG,
2627
HAMMER_LOADER,

packages/platform-browser/test/dom/events/event_manager_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import {ɵgetDOM as getDOM} from '@angular/common';
1010
import {NgZone} from '@angular/core';
1111
import {DomEventsPlugin} from '../../../src/dom/events/dom_events';
12-
import {EventManager, EventManagerPlugin} from '../../../src/dom/events/event_manager';
12+
import {EventManager} from '../../../src/dom/events/event_manager';
13+
import {EventManagerPlugin} from '../../../src/dom/events/event_manager_plugin';
1314

1415
import {TestBed} from '@angular/core/testing';
1516
import {isNode, createMouseEvent, el} from '@angular/private/testing';

0 commit comments

Comments
 (0)