From 0cd6572f61c9667a56d0d33bfed1d50468acf415 Mon Sep 17 00:00:00 2001 From: leonsenft Date: Mon, 14 Jan 2019 16:32:22 -0800 Subject: [PATCH] fix(Core): composite key event bindings ignore synthetic events to avoid crash The `KeyEventsPlugin` responsible for handling composite key event bindings, such as `(keyup.enter)`, will now ignore any events that aren't a real `KeyBoardEvent`. Previously the plugin assumed all events were so that it could access the `keyCode` property. This would lead to a crash in the event that a synthetic or custom event with the same `type` was dispatched on the element. Fixes https://github.com/dart-lang/angular/issues/1694. PiperOrigin-RevId: 229279612 --- angular/CHANGELOG.md | 6 ++++++ angular/lib/src/platform/dom/events/key_events.dart | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/angular/CHANGELOG.md b/angular/CHANGELOG.md index 9b67979e21..de9e9486cd 100644 --- a/angular/CHANGELOG.md +++ b/angular/CHANGELOG.md @@ -20,6 +20,12 @@ * The template compiler now outputs the full path to a template file when it reports an error. +* [#1694][]: Composite `keyup` and `keydown` event bindings now ignore + synthetic events (e.g. those triggered by a mouse click) instead of throwing + a `TypeError`. + +[#1694]: https://github.com/dart-lang/angular/issues/1694 + ## 5.2.0 ### Breaking changes diff --git a/angular/lib/src/platform/dom/events/key_events.dart b/angular/lib/src/platform/dom/events/key_events.dart index a876becd96..44b64653d8 100644 --- a/angular/lib/src/platform/dom/events/key_events.dart +++ b/angular/lib/src/platform/dom/events/key_events.dart @@ -135,7 +135,11 @@ class KeyEventsPlugin extends EventManagerPlugin { static Function _eventCallback( dynamic element, String fullKey, Function handler) { return (event) { - if (_getEventFullKey(event as KeyboardEvent) == fullKey) { + // Check that the event is a real `KeyboardEvent` and not a synthetic + // event that happens to be share the same `type` property. This ensures + // that the event has a `keyCode` property we can use to construct the + // full key name. + if (event is KeyboardEvent && _getEventFullKey(event) == fullKey) { handler(event); } };