diff --git a/src/cdk/a11y/focus-trap/event-listener-inert-strategy.ts b/src/cdk/a11y/focus-trap/event-listener-inert-strategy.ts index 5c5be8db7478..8fc31ea17539 100644 --- a/src/cdk/a11y/focus-trap/event-listener-inert-strategy.ts +++ b/src/cdk/a11y/focus-trap/event-listener-inert-strategy.ts @@ -8,7 +8,6 @@ import {FocusTrapInertStrategy} from './focus-trap-inert-strategy'; import {ConfigurableFocusTrap} from './configurable-focus-trap'; -import {closest} from './polyfill'; /** * Lightweight FocusTrapInertStrategy that adds a document focus event @@ -53,7 +52,8 @@ export class EventListenerFocusTrapInertStrategy implements FocusTrapInertStrate // Don't refocus if target was in an overlay, because the overlay might be associated // with an element inside the FocusTrap, ex. mat-select. - if (!focusTrapRoot.contains(target) && closest(target, 'div.cdk-overlay-pane') === null) { + if (target && !focusTrapRoot.contains(target) && + target.closest('div.cdk-overlay-pane') === null) { // Some legacy FocusTrap usages have logic that focuses some element on the page // just before FocusTrap is destroyed. For backwards compatibility, wait // to be sure FocusTrap is still enabled before refocusing. diff --git a/src/cdk/a11y/focus-trap/focus-trap.ts b/src/cdk/a11y/focus-trap/focus-trap.ts index 737c6fc9e795..31c6ac98e611 100644 --- a/src/cdk/a11y/focus-trap/focus-trap.ts +++ b/src/cdk/a11y/focus-trap/focus-trap.ts @@ -271,12 +271,10 @@ export class FocusTrap { return root; } - // Iterate in DOM order. Note that IE doesn't have `children` for SVG so we fall - // back to `childNodes` which includes text nodes, comments etc. - let children = root.children || root.childNodes; + const children = root.children; for (let i = 0; i < children.length; i++) { - let tabbableChild = children[i].nodeType === this._document.ELEMENT_NODE ? + const tabbableChild = children[i].nodeType === this._document.ELEMENT_NODE ? this._getFirstTabbableElement(children[i] as HTMLElement) : null; @@ -295,10 +293,10 @@ export class FocusTrap { } // Iterate in reverse DOM order. - let children = root.children || root.childNodes; + const children = root.children; for (let i = children.length - 1; i >= 0; i--) { - let tabbableChild = children[i].nodeType === this._document.ELEMENT_NODE ? + const tabbableChild = children[i].nodeType === this._document.ELEMENT_NODE ? this._getLastTabbableElement(children[i] as HTMLElement) : null; diff --git a/src/cdk/a11y/focus-trap/polyfill.ts b/src/cdk/a11y/focus-trap/polyfill.ts deleted file mode 100644 index cd89b3900251..000000000000 --- a/src/cdk/a11y/focus-trap/polyfill.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @license - * Copyright Google LLC All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -/** IE 11 compatible closest implementation that is able to start from non-Element Nodes. */ -export function closest(element: EventTarget|Element|null|undefined, selector: string): - Element|null { - if (!(element instanceof Node)) { return null; } - - let curr: Node|null = element; - while (curr != null && !(curr instanceof Element)) { - curr = curr.parentNode; - } - - return curr && (hasNativeClosest ? - curr.closest(selector) : polyfillClosest(curr, selector)) as Element|null; -} - -/** Polyfill for browsers without Element.closest. */ -function polyfillClosest(element: Element, selector: string): Element|null { - let curr: Node|null = element; - while (curr != null && !(curr instanceof Element && matches(curr, selector))) { - curr = curr.parentNode; - } - - return (curr || null) as Element|null; -} - -const hasNativeClosest = typeof Element != 'undefined' && !!Element.prototype.closest; - -/** IE 11 compatible matches implementation. */ -function matches(element: Element, selector: string): boolean { - return element.matches ? - element.matches(selector) : - (element as any)['msMatchesSelector'](selector); -}