Skip to content

Commit 98e3419

Browse files
committed
fix(a11y): avoid errors when trying to add high contrast class
Found this in some errors logs. The `document.defaultView` can be null in some cases which causes a null pointer error to be thrown. Furthermore, these changes add a few more null checks to ensure that we don't throw if `getComputedStyle` isn't available.
1 parent bbb92a7 commit 98e3419

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/cdk/a11y/high-contrast-mode/high-contrast-mode-detector.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ export class HighContrastModeDetector {
6363
// Get the computed style for the background color, collapsing spaces to normalize between
6464
// browsers. Once we get this color, we no longer need the test element. Access the `window`
6565
// via the document so we can fake it in tests.
66-
const documentWindow = this._document.defaultView!;
66+
const documentWindow = this._document.defaultView || window;
67+
const computedStyle = (documentWindow && documentWindow.getComputedStyle) ?
68+
documentWindow.getComputedStyle(testElement) : null;
6769
const computedColor =
68-
(documentWindow.getComputedStyle(testElement).backgroundColor || '').replace(/ /g, '');
70+
(computedStyle && computedStyle.backgroundColor || '').replace(/ /g, '');
6971
this._document.body.removeChild(testElement);
7072

7173
switch (computedColor) {

0 commit comments

Comments
 (0)