|
5 | 5 | import * as Dom from './dom';
|
6 | 6 | import window from 'global/window';
|
7 | 7 |
|
8 |
| -const USER_AGENT = window.navigator && window.navigator.userAgent || ''; |
9 |
| -const webkitVersionMap = (/AppleWebKit\/([\d.]+)/i).exec(USER_AGENT); |
10 |
| -const appleWebkitVersion = webkitVersionMap ? parseFloat(webkitVersionMap.pop()) : null; |
11 |
| - |
12 | 8 | /**
|
13 | 9 | * Whether or not this device is an iPod.
|
14 | 10 | *
|
15 | 11 | * @static
|
16 |
| - * @const |
17 | 12 | * @type {Boolean}
|
18 | 13 | */
|
19 |
| -export const IS_IPOD = (/iPod/i).test(USER_AGENT); |
| 14 | +export let IS_IPOD = false; |
20 | 15 |
|
21 | 16 | /**
|
22 | 17 | * The detected iOS version - or `null`.
|
23 | 18 | *
|
24 | 19 | * @static
|
25 |
| - * @const |
26 | 20 | * @type {string|null}
|
27 | 21 | */
|
28 |
| -export const IOS_VERSION = (function() { |
29 |
| - const match = USER_AGENT.match(/OS (\d+)_/i); |
30 |
| - |
31 |
| - if (match && match[1]) { |
32 |
| - return match[1]; |
33 |
| - } |
34 |
| - return null; |
35 |
| -}()); |
| 22 | +export let IOS_VERSION = null; |
36 | 23 |
|
37 | 24 | /**
|
38 | 25 | * Whether or not this is an Android device.
|
39 | 26 | *
|
40 | 27 | * @static
|
41 |
| - * @const |
42 | 28 | * @type {Boolean}
|
43 | 29 | */
|
44 |
| -export const IS_ANDROID = (/Android/i).test(USER_AGENT); |
| 30 | +export let IS_ANDROID = false; |
45 | 31 |
|
46 | 32 | /**
|
47 |
| - * The detected Android version - or `null`. |
| 33 | + * The detected Android version - or `null` if not Android or indeterminable. |
48 | 34 | *
|
49 | 35 | * @static
|
50 |
| - * @const |
51 | 36 | * @type {number|string|null}
|
52 | 37 | */
|
53 |
| -export const ANDROID_VERSION = (function() { |
54 |
| - // This matches Android Major.Minor.Patch versions |
55 |
| - // ANDROID_VERSION is Major.Minor as a Number, if Minor isn't available, then only Major is returned |
56 |
| - const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i); |
57 |
| - |
58 |
| - if (!match) { |
59 |
| - return null; |
60 |
| - } |
61 |
| - |
62 |
| - const major = match[1] && parseFloat(match[1]); |
63 |
| - const minor = match[2] && parseFloat(match[2]); |
64 |
| - |
65 |
| - if (major && minor) { |
66 |
| - return parseFloat(match[1] + '.' + match[2]); |
67 |
| - } else if (major) { |
68 |
| - return major; |
69 |
| - } |
70 |
| - return null; |
71 |
| -}()); |
| 38 | +export let ANDROID_VERSION; |
72 | 39 |
|
73 | 40 | /**
|
74 |
| - * Whether or not this is a native Android browser. |
| 41 | + * Whether or not this is Mozilla Firefox. |
75 | 42 | *
|
76 | 43 | * @static
|
77 |
| - * @const |
78 | 44 | * @type {Boolean}
|
79 | 45 | */
|
80 |
| -export const IS_NATIVE_ANDROID = IS_ANDROID && ANDROID_VERSION < 5 && appleWebkitVersion < 537; |
| 46 | +export let IS_FIREFOX = false; |
81 | 47 |
|
82 | 48 | /**
|
83 |
| - * Whether or not this is Mozilla Firefox. |
| 49 | + * Whether or not this is Microsoft Edge. |
84 | 50 | *
|
85 | 51 | * @static
|
86 |
| - * @const |
87 | 52 | * @type {Boolean}
|
88 | 53 | */
|
89 |
| -export const IS_FIREFOX = (/Firefox/i).test(USER_AGENT); |
| 54 | +export let IS_EDGE = false; |
90 | 55 |
|
91 | 56 | /**
|
92 |
| - * Whether or not this is Microsoft Edge. |
| 57 | + * Whether or not this is any Chromium Browser |
93 | 58 | *
|
94 | 59 | * @static
|
95 |
| - * @const |
96 | 60 | * @type {Boolean}
|
97 | 61 | */
|
98 |
| -export const IS_EDGE = (/Edg/i).test(USER_AGENT); |
| 62 | +export let IS_CHROMIUM = false; |
99 | 63 |
|
100 | 64 | /**
|
101 |
| - * Whether or not this is Google Chrome. |
| 65 | + * Whether or not this is any Chromium browser that is not Edge. |
102 | 66 | *
|
103 | 67 | * This will also be `true` for Chrome on iOS, which will have different support
|
104 | 68 | * as it is actually Safari under the hood.
|
105 | 69 | *
|
| 70 | + * Depreacted, as the behaviour to not match Edge was to prevent Legacy Edge's UA matching. |
| 71 | + * IS_CHROMIUM should be used instead. |
| 72 | + * "Chromium but not Edge" could be explicitly tested with IS_CHROMIUM && !IS_EDGE |
| 73 | + * |
106 | 74 | * @static
|
107 |
| - * @const |
| 75 | + * @deprecated |
108 | 76 | * @type {Boolean}
|
109 | 77 | */
|
110 |
| -export const IS_CHROME = !IS_EDGE && ((/Chrome/i).test(USER_AGENT) || (/CriOS/i).test(USER_AGENT)); |
| 78 | +export let IS_CHROME = false; |
111 | 79 |
|
112 | 80 | /**
|
113 |
| - * The detected Google Chrome version - or `null`. |
| 81 | + * The detected Chromium version - or `null`. |
114 | 82 | *
|
115 | 83 | * @static
|
116 |
| - * @const |
117 | 84 | * @type {number|null}
|
118 | 85 | */
|
119 |
| -export const CHROME_VERSION = (function() { |
120 |
| - const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/); |
| 86 | +export let CHROMIUM_VERSION = null; |
121 | 87 |
|
122 |
| - if (match && match[2]) { |
123 |
| - return parseFloat(match[2]); |
124 |
| - } |
125 |
| - return null; |
126 |
| -}()); |
| 88 | +/** |
| 89 | + * The detected Google Chrome version - or `null`. |
| 90 | + * This has always been the _Chromium_ version, i.e. would return on Chromium Edge. |
| 91 | + * Depreacted, use CHROMIUM_VERSION instead. |
| 92 | + * |
| 93 | + * @static |
| 94 | + * @deprecated |
| 95 | + * @type {number|null} |
| 96 | + */ |
| 97 | +export let CHROME_VERSION = null; |
127 | 98 |
|
128 | 99 | /**
|
129 | 100 | * The detected Internet Explorer version - or `null`.
|
130 | 101 | *
|
131 | 102 | * @static
|
132 |
| - * @const |
| 103 | + * @deprecated |
133 | 104 | * @type {number|null}
|
134 | 105 | */
|
135 |
| -export const IE_VERSION = (function() { |
136 |
| - const result = (/MSIE\s(\d+)\.\d/).exec(USER_AGENT); |
137 |
| - let version = result && parseFloat(result[1]); |
138 |
| - |
139 |
| - if (!version && (/Trident\/7.0/i).test(USER_AGENT) && (/rv:11.0/).test(USER_AGENT)) { |
140 |
| - // IE 11 has a different user agent string than other IE versions |
141 |
| - version = 11.0; |
142 |
| - } |
143 |
| - |
144 |
| - return version; |
145 |
| -}()); |
| 106 | +export let IE_VERSION = null; |
146 | 107 |
|
147 | 108 | /**
|
148 | 109 | * Whether or not this is desktop Safari.
|
149 | 110 | *
|
150 | 111 | * @static
|
151 |
| - * @const |
152 | 112 | * @type {Boolean}
|
153 | 113 | */
|
154 |
| -export const IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE; |
| 114 | +export let IS_SAFARI = false; |
155 | 115 |
|
156 | 116 | /**
|
157 | 117 | * Whether or not this is a Windows machine.
|
158 | 118 | *
|
159 | 119 | * @static
|
160 |
| - * @const |
161 | 120 | * @type {Boolean}
|
162 | 121 | */
|
163 |
| -export const IS_WINDOWS = (/Windows/i).test(USER_AGENT); |
| 122 | +export let IS_WINDOWS = false; |
164 | 123 |
|
165 | 124 | /**
|
166 |
| - * Whether or not this device is touch-enabled. |
| 125 | + * Whether or not this device is an iPad. |
167 | 126 | *
|
168 | 127 | * @static
|
169 |
| - * @const |
170 | 128 | * @type {Boolean}
|
171 | 129 | */
|
172 |
| -export const TOUCH_ENABLED = Boolean(Dom.isReal() && ( |
173 |
| - 'ontouchstart' in window || |
174 |
| - window.navigator.maxTouchPoints || |
175 |
| - window.DocumentTouch && window.document instanceof window.DocumentTouch)); |
| 130 | +export let IS_IPAD = false; |
176 | 131 |
|
177 | 132 | /**
|
178 |
| - * Whether or not this device is an iPad. |
| 133 | + * Whether or not this device is an iPhone. |
179 | 134 | *
|
180 | 135 | * @static
|
181 |
| - * @const |
182 | 136 | * @type {Boolean}
|
183 | 137 | */
|
184 |
| -export const IS_IPAD = (/iPad/i).test(USER_AGENT) || |
185 |
| - (IS_SAFARI && TOUCH_ENABLED && !(/iPhone/i).test(USER_AGENT)); |
| 138 | +// The Facebook app's UIWebView identifies as both an iPhone and iPad, so |
| 139 | +// to identify iPhones, we need to exclude iPads. |
| 140 | +// http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/ |
| 141 | +export let IS_IPHONE = false; |
186 | 142 |
|
187 | 143 | /**
|
188 |
| - * Whether or not this device is an iPhone. |
| 144 | + * Whether or not this device is touch-enabled. |
189 | 145 | *
|
190 | 146 | * @static
|
191 | 147 | * @const
|
192 | 148 | * @type {Boolean}
|
193 | 149 | */
|
194 |
| -// The Facebook app's UIWebView identifies as both an iPhone and iPad, so |
195 |
| -// to identify iPhones, we need to exclude iPads. |
196 |
| -// http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/ |
197 |
| -export const IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD; |
| 150 | +export const TOUCH_ENABLED = Boolean(Dom.isReal() && ( |
| 151 | + 'ontouchstart' in window || |
| 152 | + window.navigator.maxTouchPoints || |
| 153 | + window.DocumentTouch && window.document instanceof window.DocumentTouch)); |
| 154 | + |
| 155 | +const UAD = window.navigator && window.navigator.userAgentData; |
| 156 | + |
| 157 | +if (UAD) { |
| 158 | + // If userAgentData is present, use it instead of userAgent to avoid warnings |
| 159 | + // Currently only implemented on Chromium |
| 160 | + // userAgentData does not expose Android version, so ANDROID_VERSION remains `null` |
| 161 | + |
| 162 | + IS_ANDROID = UAD.platform === 'Android'; |
| 163 | + IS_EDGE = Boolean(UAD.brands.find(b => b.brand === 'Microsoft Edge')); |
| 164 | + IS_CHROMIUM = Boolean(UAD.brands.find(b => b.brand === 'Chromium')); |
| 165 | + IS_CHROME = !IS_EDGE && IS_CHROMIUM; |
| 166 | + CHROMIUM_VERSION = CHROME_VERSION = (UAD.brands.find(b => b.brand === 'Chromium') || {}).version || null; |
| 167 | + IS_WINDOWS = UAD.platform === 'Windows'; |
| 168 | +} |
| 169 | + |
| 170 | +// If the broser is not Chromium, either userAgentData is not present which could be an old Chromium browser, |
| 171 | +// or it's a browser that has added userAgentData since that we don't have tests for yet. In either case, |
| 172 | +// the checks need to be made agiainst the regular userAgent string. |
| 173 | +if (!IS_CHROMIUM) { |
| 174 | + const USER_AGENT = window.navigator && window.navigator.userAgent || ''; |
| 175 | + |
| 176 | + IS_IPOD = (/iPod/i).test(USER_AGENT); |
| 177 | + |
| 178 | + IOS_VERSION = (function() { |
| 179 | + const match = USER_AGENT.match(/OS (\d+)_/i); |
| 180 | + |
| 181 | + if (match && match[1]) { |
| 182 | + return match[1]; |
| 183 | + } |
| 184 | + return null; |
| 185 | + }()); |
| 186 | + |
| 187 | + IS_ANDROID = (/Android/i).test(USER_AGENT); |
| 188 | + |
| 189 | + ANDROID_VERSION = (function() { |
| 190 | + // This matches Android Major.Minor.Patch versions |
| 191 | + // ANDROID_VERSION is Major.Minor as a Number, if Minor isn't available, then only Major is returned |
| 192 | + const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i); |
| 193 | + |
| 194 | + if (!match) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + const major = match[1] && parseFloat(match[1]); |
| 199 | + const minor = match[2] && parseFloat(match[2]); |
| 200 | + |
| 201 | + if (major && minor) { |
| 202 | + return parseFloat(match[1] + '.' + match[2]); |
| 203 | + } else if (major) { |
| 204 | + return major; |
| 205 | + } |
| 206 | + return null; |
| 207 | + }()); |
| 208 | + |
| 209 | + IS_FIREFOX = (/Firefox/i).test(USER_AGENT); |
| 210 | + |
| 211 | + IS_EDGE = (/Edg/i).test(USER_AGENT); |
| 212 | + |
| 213 | + IS_CHROMIUM = ((/Chrome/i).test(USER_AGENT) || (/CriOS/i).test(USER_AGENT)); |
| 214 | + |
| 215 | + IS_CHROME = !IS_EDGE && IS_CHROMIUM; |
| 216 | + |
| 217 | + CHROMIUM_VERSION = CHROME_VERSION = (function() { |
| 218 | + const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/); |
| 219 | + |
| 220 | + if (match && match[2]) { |
| 221 | + return parseFloat(match[2]); |
| 222 | + } |
| 223 | + return null; |
| 224 | + }()); |
| 225 | + |
| 226 | + IE_VERSION = (function() { |
| 227 | + const result = (/MSIE\s(\d+)\.\d/).exec(USER_AGENT); |
| 228 | + let version = result && parseFloat(result[1]); |
| 229 | + |
| 230 | + if (!version && (/Trident\/7.0/i).test(USER_AGENT) && (/rv:11.0/).test(USER_AGENT)) { |
| 231 | + // IE 11 has a different user agent string than other IE versions |
| 232 | + version = 11.0; |
| 233 | + } |
| 234 | + |
| 235 | + return version; |
| 236 | + }()); |
| 237 | + |
| 238 | + IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE; |
| 239 | + |
| 240 | + IS_WINDOWS = (/Windows/i).test(USER_AGENT); |
| 241 | + |
| 242 | + IS_IPAD = (/iPad/i).test(USER_AGENT) || |
| 243 | + (IS_SAFARI && TOUCH_ENABLED && !(/iPhone/i).test(USER_AGENT)); |
| 244 | + |
| 245 | + IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD; |
| 246 | +} |
198 | 247 |
|
199 | 248 | /**
|
200 | 249 | * Whether or not this is an iOS device.
|
|
0 commit comments