From 5d437ea861bbd39f967b018578d4e00357be4b81 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 15 Aug 2017 23:48:08 +0200 Subject: [PATCH] refactor: remove usages of `if in` checks (#6436) This is something that came up in https://github.com/angular/material2/pull/2907#discussion_r130968388. We shouldn't be using the `if ('someString' in obj)` checks, because they can be broken by minifiers that rename class members. --- src/lib/core/option/option.ts | 2 +- src/lib/datepicker/datepicker.ts | 4 +++- src/lib/dialog/dialog-container.ts | 2 +- src/lib/slide-toggle/slide-toggle.spec.ts | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/core/option/option.ts b/src/lib/core/option/option.ts index 09a88598f47..aeb02e12d77 100644 --- a/src/lib/core/option/option.ts +++ b/src/lib/core/option/option.ts @@ -145,7 +145,7 @@ export class MdOption { focus(): void { const element = this._getHostElement(); - if ('focus' in element) { + if (typeof element.focus === 'function') { element.focus(); } } diff --git a/src/lib/datepicker/datepicker.ts b/src/lib/datepicker/datepicker.ts index 55a8c4a0219..7e91ed6c755 100644 --- a/src/lib/datepicker/datepicker.ts +++ b/src/lib/datepicker/datepicker.ts @@ -278,7 +278,9 @@ export class MdDatepicker implements OnDestroy { if (this._calendarPortal && this._calendarPortal.isAttached) { this._calendarPortal.detach(); } - if (this._focusedElementBeforeOpen && 'focus' in this._focusedElementBeforeOpen) { + if (this._focusedElementBeforeOpen && + typeof this._focusedElementBeforeOpen.focus === 'function') { + this._focusedElementBeforeOpen.focus(); this._focusedElementBeforeOpen = null; } diff --git a/src/lib/dialog/dialog-container.ts b/src/lib/dialog/dialog-container.ts index 816b4ce11ea..235f46cd717 100644 --- a/src/lib/dialog/dialog-container.ts +++ b/src/lib/dialog/dialog-container.ts @@ -150,7 +150,7 @@ export class MdDialogContainer extends BasePortalHost { const toFocus = this._elementFocusedBeforeDialogWasOpened; // We need the extra check, because IE can set the `activeElement` to null in some cases. - if (toFocus && 'focus' in toFocus) { + if (toFocus && typeof toFocus.focus === 'function') { toFocus.focus(); } diff --git a/src/lib/slide-toggle/slide-toggle.spec.ts b/src/lib/slide-toggle/slide-toggle.spec.ts index 981aaa5c27a..eda08374dc1 100644 --- a/src/lib/slide-toggle/slide-toggle.spec.ts +++ b/src/lib/slide-toggle/slide-toggle.spec.ts @@ -699,7 +699,7 @@ describe('MdSlideToggle with forms', () => { })); it('should prevent the form from submit when being required', () => { - if ('reportValidity' in inputElement === false) { + if (typeof (inputElement as any).reportValidity === 'undefined') { // If the browser does not report the validity then the tests will break. // e.g Safari 8 on Mobile. return;