This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
env.js
151 lines (132 loc) · 3.94 KB
/
env.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals navigator:false */
/**
* @module utils/env
*/
const userAgent = navigator.userAgent.toLowerCase();
/**
* A namespace containing environment and browser information.
*
* @namespace
*/
const env = {
/**
* Indicates that the application is running on Macintosh.
*
* @static
* @type {Boolean}
*/
isMac: isMac( userAgent ),
/**
* Indicates that the application is running in Microsoft Edge.
*
* @static
* @type {Boolean}
*/
isEdge: isEdge( userAgent ),
/**
* Indicates that the application is running in Firefox (Gecko).
*
* @static
* @type {Boolean}
*/
isGecko: isGecko( userAgent ),
/**
* Indicates that the application is running in Safari.
*
* @static
* @type {Boolean}
*/
isSafari: isSafari( userAgent ),
/**
* Indicates that the application is running on Android mobile device.
*
* @static
* @type {Boolean}
*/
isAndroid: isAndroid( userAgent ),
/**
* Environment features information.
*
* @memberOf module:utils/env~env
* @namespace
*/
features: {
/**
* Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
* More information about unicode properties might be found
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
*
* @type {Boolean}
*/
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
}
};
export default env;
/**
* Checks if User Agent represented by the string is running on Macintosh.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is running on Macintosh or not.
*/
export function isMac( userAgent ) {
return userAgent.indexOf( 'macintosh' ) > -1;
}
/**
* Checks if User Agent represented by the string is Microsoft Edge.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Edge or not.
*/
export function isEdge( userAgent ) {
return !!userAgent.match( /edge\/(\d+.?\d*)/ );
}
/**
* Checks if User Agent represented by the string is Firefox (Gecko).
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Firefox or not.
*/
export function isGecko( userAgent ) {
return !!userAgent.match( /gecko\/\d+/ );
}
/**
* Checks if User Agent represented by the string is Safari.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Safari or not.
*/
export function isSafari( userAgent ) {
return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
}
/**
* Checks if User Agent represented by the string is Android mobile device.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Safari or not.
*/
export function isAndroid( userAgent ) {
return userAgent.indexOf( 'android' ) > -1;
}
/**
* Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
* More information about unicode properties might be found
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
*
* @returns {Boolean}
*/
export function isRegExpUnicodePropertySupported() {
let isSupported = false;
// Feature detection for Unicode properties. Added in ES2018. Currently Firefox and Edge do not support it.
// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
try {
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
} catch ( error ) {
// Firefox throws a SyntaxError when the group is unsupported.
}
return isSupported;
}