-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathplatform.ts
More file actions
executable file
·103 lines (85 loc) · 4.07 KB
/
platform.ts
File metadata and controls
executable file
·103 lines (85 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @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.dev/license
*/
import {inject, Injectable, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
// Whether the current platform supports the V8 Break Iterator. The V8 check
// is necessary to detect all Blink based browsers.
let hasV8BreakIterator: boolean;
// We need a try/catch around the reference to `Intl`, because accessing it in some cases can
// cause IE to throw. These cases are tied to particular versions of Windows and can happen if
// the consumer is providing a polyfilled `Map`. See:
// https://github.com/Microsoft/ChakraCore/issues/3189
// https://github.com/angular/components/issues/15687
interface IntlWithV8BreakIterator {
v8BreakIterator?: boolean;
}
try {
hasV8BreakIterator =
typeof Intl !== 'undefined' && !!(Intl as IntlWithV8BreakIterator).v8BreakIterator;
} catch {
hasV8BreakIterator = false;
}
interface WindowWithChrome extends Window {
chrome?: unknown;
}
/**
* Service to detect the current platform by comparing the userAgent strings and
* checking browser-specific global properties.
*/
@Injectable({providedIn: 'root'})
export class Platform {
private _platformId = inject(PLATFORM_ID);
// We want to use the Angular platform check because if the Document is shimmed
// without the navigator, the following checks will fail. This is preferred because
// sometimes the Document may be shimmed without the user's knowledge or intention
/** Whether the Angular application is being rendered in the browser. */
isBrowser: boolean = this._platformId
? isPlatformBrowser(this._platformId)
: typeof document === 'object' && !!document;
/** Whether the current browser is Microsoft Edge. */
EDGE: boolean = this.isBrowser && /(edge)/i.test(navigator.userAgent);
/** Whether the current rendering engine is Microsoft Trident. */
TRIDENT: boolean = this.isBrowser && /(msie|trident)/i.test(navigator.userAgent);
// EdgeHTML and Trident mock Blink specific things and need to be excluded from this check.
/** Whether the current rendering engine is Blink. */
BLINK: boolean =
this.isBrowser &&
!!((window as WindowWithChrome).chrome || hasV8BreakIterator) &&
typeof CSS !== 'undefined' &&
!this.EDGE &&
!this.TRIDENT;
// Webkit is part of the userAgent in EdgeHTML, Blink and Trident. Therefore we need to
// ensure that Webkit runs standalone and is not used as another engine's base.
/** Whether the current rendering engine is WebKit. */
WEBKIT: boolean =
this.isBrowser &&
/AppleWebKit/i.test(navigator.userAgent) &&
!this.BLINK &&
!this.EDGE &&
!this.TRIDENT;
/** Whether the current platform is Apple iOS. */
IOS: boolean =
this.isBrowser && /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window);
// It's difficult to detect the plain Gecko engine, because most of the browsers identify
// them self as Gecko-like browsers and modify the userAgent's according to that.
// Since we only cover one explicit Firefox case, we can simply check for Firefox
// instead of having an unstable check for Gecko.
/** Whether the current browser is Firefox. */
FIREFOX: boolean = this.isBrowser && /(firefox|minefield)/i.test(navigator.userAgent);
/** Whether the current platform is Android. */
// Trident on mobile adds the android platform to the userAgent to trick detections.
ANDROID: boolean = this.isBrowser && /android/i.test(navigator.userAgent) && !this.TRIDENT;
// Safari browsers will include the Safari keyword in their userAgent. Some browsers may fake
// this and just place the Safari keyword in the userAgent. To be more safe about Safari every
// Safari browser should also use Webkit as its layout engine.
/** Whether the current browser is Safari. */
SAFARI: boolean = this.isBrowser && /safari/i.test(navigator.userAgent) && this.WEBKIT;
/** Backwards-compatible constructor. */
constructor(..._args: unknown[]);
constructor() {}
}