Skip to content

Commit fa3f536

Browse files
committed
Fix test exceptions on IE.
1 parent d634a9b commit fa3f536

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
5959
@ViewChild(FocusTrap) _focusTrap: FocusTrap;
6060

6161
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
62-
private _elementFocusedBeforeDialogWasOpened: Element = null;
62+
private _elementFocusedBeforeDialogWasOpened: HTMLElement = null;
6363

6464
/** The dialog configuration. */
6565
dialogConfig: MdDialogConfig;
@@ -89,7 +89,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
8989
// ready in instances where change detection has to run first. To deal with this, we simply
9090
// wait for the microtask queue to be empty.
9191
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
92-
this._elementFocusedBeforeDialogWasOpened = document.activeElement;
92+
this._elementFocusedBeforeDialogWasOpened = document.activeElement as HTMLElement;
9393
this._focusTrap.focusFirstTabbableElement();
9494
});
9595

@@ -106,7 +106,14 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
106106
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
107107
// that it doesn't end up back on the <body>.
108108
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
109-
(this._elementFocusedBeforeDialogWasOpened as HTMLElement).focus();
109+
let toFocus = this._elementFocusedBeforeDialogWasOpened;
110+
111+
// We need to check whether the focus method exists at all, because IE seems to throw an
112+
// exception, even if the element is the document.body.
113+
if (toFocus && 'focus' in toFocus) {
114+
toFocus.focus();
115+
}
116+
110117
this._onAnimationStateChange.complete();
111118
});
112119
}

0 commit comments

Comments
 (0)