Skip to content

Commit 10835f6

Browse files
committed
fix(platform): change isBrowser check to use Angular PLATFORM_ID
* Use the Angular PLATFORM_ID token as a more reliable check for whether the current platform is browser. This is because the previous check for a global document could be a false positive if another application pollutes the global namespace
1 parent 7857b92 commit 10835f6

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/cdk/platform/platform.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable} from '@angular/core';
9+
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
10+
import {isPlatformBrowser} from '@angular/common';
1011

1112

1213
// Whether the current platform supports the V8 Break Iterator. The V8 check
@@ -20,7 +21,7 @@ const hasV8BreakIterator = (typeof Intl !== 'undefined' && (Intl as any).v8Break
2021
@Injectable({providedIn: 'root'})
2122
export class Platform {
2223
/** Whether the Angular application is being rendered in the browser. */
23-
isBrowser: boolean = typeof document === 'object' && !!document;
24+
isBrowser: boolean = isPlatformBrowser(this._platformId);
2425

2526
/** Whether the current browser is Microsoft Edge. */
2627
EDGE: boolean = this.isBrowser && /(edge)/i.test(navigator.userAgent);
@@ -37,11 +38,11 @@ export class Platform {
3738
// Webkit is part of the userAgent in EdgeHTML, Blink and Trident. Therefore we need to
3839
// ensure that Webkit runs standalone and is not used as another engine's base.
3940
WEBKIT: boolean = this.isBrowser &&
40-
/AppleWebKit/i.test(navigator.userAgent) && !this.BLINK && !this.EDGE && !this.TRIDENT;
41+
/AppleWebKit/i.test(navigator.userAgent) && !this.BLINK && !this.EDGE && !this.TRIDENT;
4142

4243
/** Whether the current platform is Apple iOS. */
4344
IOS: boolean = this.isBrowser && /iPad|iPhone|iPod/.test(navigator.userAgent) &&
44-
!(window as any).MSStream;
45+
!(window as any).MSStream;
4546

4647
/** Whether the current browser is Firefox. */
4748
// It's difficult to detect the plain Gecko engine, because most of the browsers identify
@@ -51,12 +52,16 @@ export class Platform {
5152
FIREFOX: boolean = this.isBrowser && /(firefox|minefield)/i.test(navigator.userAgent);
5253

5354
/** Whether the current platform is Android. */
54-
// Trident on mobile adds the android platform to the userAgent to trick detections.
55+
// Trident on mobile adds the android platform to the userAgent to trick detections.
5556
ANDROID: boolean = this.isBrowser && /android/i.test(navigator.userAgent) && !this.TRIDENT;
5657

5758
/** Whether the current browser is Safari. */
5859
// Safari browsers will include the Safari keyword in their userAgent. Some browsers may fake
5960
// this and just place the Safari keyword in the userAgent. To be more safe about Safari every
6061
// Safari browser should also use Webkit as its layout engine.
6162
SAFARI: boolean = this.isBrowser && /safari/i.test(navigator.userAgent) && this.WEBKIT;
63+
64+
constructor(@Inject(PLATFORM_ID) private _platformId: Object) {
65+
}
6266
}
67+

0 commit comments

Comments
 (0)