Skip to content

Commit ae32092

Browse files
crisbetommalerba
authored andcommitted
fix: error when attempting to access Intl API on some versions of Windows (#15693)
On some versions of Windows merely trying to access the `Intl` object can cause IE to throw an error, if the default `Map` object has been overwritten (e.g. when polyfilling). These changes add a `try/catch` around our usages so that users' apps don't crash completely on load. Fixes #15687.
1 parent 110eb19 commit ae32092

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/cdk/platform/platform.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@
99
import {Inject, Injectable, Optional, PLATFORM_ID} from '@angular/core';
1010
import {isPlatformBrowser} from '@angular/common';
1111

12-
1312
// Whether the current platform supports the V8 Break Iterator. The V8 check
1413
// is necessary to detect all Blink based browsers.
15-
const hasV8BreakIterator = (typeof Intl !== 'undefined' && (Intl as any).v8BreakIterator);
14+
let hasV8BreakIterator: boolean;
15+
16+
// We need a try/catch around the reference to `Intl`, because accessing it in some cases can
17+
// cause IE to throw. These cases are tied to particular versions of Windows and can happen if
18+
// the consumer is providing a polyfilled `Map`. See:
19+
// https://github.com/Microsoft/ChakraCore/issues/3189
20+
// https://github.com/angular/material2/issues/15687
21+
try {
22+
hasV8BreakIterator = (typeof Intl !== 'undefined' && (Intl as any).v8BreakIterator);
23+
} catch {
24+
hasV8BreakIterator = false;
25+
}
1626

1727
/**
1828
* Service to detect the current platform by comparing the userAgent strings and
@@ -71,6 +81,6 @@ export class Platform {
7181
* @breaking-change 8.0.0 remove optional decorator
7282
*/
7383
constructor(@Optional() @Inject(PLATFORM_ID) private _platformId?: Object) {
74-
}
84+
}
7585
}
7686

src/lib/core/datetime/native-date-adapter.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ import {DateAdapter, MAT_DATE_LOCALE} from './date-adapter';
1212

1313
// TODO(mmalerba): Remove when we no longer support safari 9.
1414
/** Whether the browser supports the Intl API. */
15-
const SUPPORTS_INTL_API = typeof Intl != 'undefined';
16-
15+
let SUPPORTS_INTL_API: boolean;
16+
17+
// We need a try/catch around the reference to `Intl`, because accessing it in some cases can
18+
// cause IE to throw. These cases are tied to particular versions of Windows and can happen if
19+
// the consumer is providing a polyfilled `Map`. See:
20+
// https://github.com/Microsoft/ChakraCore/issues/3189
21+
// https://github.com/angular/material2/issues/15687
22+
try {
23+
SUPPORTS_INTL_API = typeof Intl != 'undefined';
24+
} catch {
25+
SUPPORTS_INTL_API = false;
26+
}
1727

1828
/** The default month names to use if Intl API is not available. */
1929
const DEFAULT_MONTH_NAMES = {

0 commit comments

Comments
 (0)