forked from mckamey/cssuseragent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssua.js
More file actions
355 lines (302 loc) · 8.02 KB
/
cssua.js
File metadata and controls
355 lines (302 loc) · 8.02 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* CssUserAgent (cssua.js) v2.1.31
* http://cssuseragent.org
*
* Copyright (c)2006-2015 Stephen M. McKamey.
* Licensed under The MIT License.
*/
/*jshint smarttabs:true, regexp:false, browser:true */
/**
* @type {Object}
*/
var cssua = (
/**
* @param html {Object} root DOM element
* @param userAgent {string} browser userAgent string
* @return {Object}
*/
function(html, userAgent, sa) {
'use strict';
/**
* @const
* @type {string}
*/
var PREFIX = ' ua-';
/**
* @const
* @type {RegExp}
*/
var R_Platform = /\s*([\-\w ]+)[\s\/\:]([\d_]+\b(?:[\-\._\/]\w+)*)/;
/**
* @const
* @type {RegExp}
*/
var R_Version = /([\w\-\.]+[\s\/][v]?[\d_]+\b(?:[\-\._\/]\w+)*)/g;
/**
* @const
* @type {RegExp}
*/
var R_BlackBerry = /\b(?:(blackberry\w*|bb10)|(rim tablet os))(?:\/(\d+\.\d+(?:\.\w+)*))?/;
/**
* @const
* @type {RegExp}
*/
var R_Silk = /\bsilk-accelerated=true\b/;
/**
* @const
* @type {RegExp}
*/
var R_FluidApp = /\bfluidapp\b/;
/**
* @const
* @type {RegExp}
*/
var R_desktop = /(\bwindows\b|\bmacintosh\b|\blinux\b|\bunix\b)/;
/**
* @const
* @type {RegExp}
*/
var R_mobile = /(\bandroid\b|\bipad\b|\bipod\b|\bwindows phone\b|\bwpdesktop\b|\bxblwp7\b|\bzunewp7\b|\bwindows ce\b|\bblackberry\w*|\bbb10\b|\brim tablet os\b|\bmeego|\bwebos\b|\bpalm|\bsymbian|\bj2me\b|\bdocomo\b|\bpda\b|\bchtml\b|\bmidp\b|\bcldc\b|\w*?mobile\w*?|\w*?phone\w*?)/;
/**
* @const
* @type {RegExp}
*/
var R_game = /(\bxbox\b|\bplaystation\b|\bnintendo\s+\w+)/;
/**
* The root CssUserAgent
* @type {Object}
*/
var cssua = {
parse:
/**
* @param uaStr {string}
* @return {Object}
*/
function(uaStr, sa) {
/**
* @type {Object}
*/
var ua = {};
if (sa) {
ua.standalone = sa;
}
uaStr = (''+uaStr).toLowerCase();
if (!uaStr) {
return ua;
}
var i, count, raw = uaStr.split(/[()]/);
for (var j=0, rawCount=raw.length; j<rawCount; j++) {
if (j%2) {
// inside parens covers platform identifiers
var platforms = raw[j].split(';');
for (i=0, count=platforms.length; i<count; i++) {
if (R_Platform.exec(platforms[i])) {
var key = RegExp.$1.split(' ').join('_'),
val = RegExp.$2;
// if duplicate entries favor highest version
if ((!ua[key] || parseFloat(ua[key]) < parseFloat(val))) {
ua[key] = val;
}
}
}
} else {
// outside parens covers most version identifiers
var uas = raw[j].match(R_Version);
if (uas) {
for (i=0, count=uas.length; i<count; i++) {
var parts = uas[i].split(/[\/\s]+/);
if (parts.length && parts[0] !== 'mozilla') {
ua[parts[0].split(' ').join('_')] = parts.slice(1).join('-');
}
}
}
}
}
if (R_mobile.exec(uaStr)) {
// mobile device indicators
ua.mobile = RegExp.$1;
if (R_BlackBerry.exec(uaStr)) {
delete ua[ua.mobile];
ua.blackberry = ua.version || RegExp.$3 || RegExp.$2 || RegExp.$1;
if (RegExp.$1) {
// standardize non-tablet blackberry
ua.mobile = 'blackberry';
} else if (ua.version === '0.0.1') {
// fix playbook 1.0 quirk
ua.blackberry = '7.1.0.0';
}
}
} else if (R_game.exec(uaStr)) {
// game console indicators
ua.game = RegExp.$1;
var game = ua.game.split(' ').join('_');
if (ua.version && !ua[game]) {
ua[game] = ua.version;
}
} else if (R_desktop.exec(uaStr)) {
// desktop OS indicators
ua.desktop = RegExp.$1;
}
// platform naming standardizations
if (ua.intel_mac_os_x) {
ua.mac_os_x = ua.intel_mac_os_x.split('_').join('.');
delete ua.intel_mac_os_x;
} else if (ua.cpu_iphone_os) {
ua.ios = ua.cpu_iphone_os.split('_').join('.');
delete ua.cpu_iphone_os;
} else if (ua.cpu_os) {
ua.ios = ua.cpu_os.split('_').join('.');
delete ua.cpu_os;
} else if (ua.mobile === 'iphone' && !ua.ios) {
ua.ios = '1';
}
// UA naming standardizations
if (ua.opera && ua.version) {
ua.opera = ua.version;
// version/XXX refers to opera
delete ua.blackberry;
} else if (R_Silk.exec(uaStr)) {
ua.silk_accelerated = true;
} else if (R_FluidApp.exec(uaStr)) {
ua.fluidapp = ua.version;
}
if (ua.edge) {
delete ua.applewebkit;
delete ua.safari;
delete ua.chrome;
delete ua.android;
}
if (ua.applewebkit) {
ua.webkit = ua.applewebkit;
delete ua.applewebkit;
if (ua.opr) {
ua.opera = ua.opr;
delete ua.opr;
delete ua.chrome;
}
if (ua.safari) {
if (ua.chrome || ua.crios || ua.fxios || ua.opera || ua.silk || ua.fluidapp || ua.phantomjs || (ua.mobile && !ua.ios)) {
delete ua.safari;
if (ua.vivaldi) {
delete ua.chrome;
}
} else if (ua.version && !ua.rim_tablet_os) {
ua.safari = ua.version;
} else {
ua.safari = ({
'419': '2.0.4',
'417': '2.0.3',
'416': '2.0.2',
'412': '2.0',
'312': '1.3',
'125': '1.2',
'85': '1.0'
})[parseInt(ua.safari, 10)] || ua.safari;
}
}
} else if (ua.msie || ua.trident) {
if (!ua.opera) {
// standardize Internet Explorer
ua.ie = ua.msie || ua.rv;
}
delete ua.msie;
delete ua.android;
if (ua.windows_phone_os) {
// standardize window phone
ua.windows_phone = ua.windows_phone_os;
delete ua.windows_phone_os;
} else if (ua.mobile === 'wpdesktop' || ua.mobile === 'xblwp7' || ua.mobile === 'zunewp7') {
ua.mobile = 'windows desktop';
ua.windows_phone = (+ua.ie < 9) ? '7.0' : (+ua.ie < 10) ? '7.5' : '8.0';
delete ua.windows_nt;
}
} else if (ua.gecko || ua.firefox) {
ua.gecko = ua.rv;
}
if (ua.rv) {
delete ua.rv;
}
if (ua.version) {
delete ua.version;
}
return ua;
},
format:
/**
* @param ua {Object}
* @return {string}
*/
function (ua) {
/**
* @param b {string} browser key
* @param v {string} browser value
* @return {string} formatted CSS classes
*/
function format(b, v) {
b = b.split('.').join('-');
/**
* @type {string}
*/
var css = PREFIX+b;
if (typeof v === 'string') {
v = v.split(' ').join('_').split('.').join('-');
var i = v.indexOf('-');
while (i > 0) {
// loop through chopping last '-' to end off
// concat result onto return string
css += PREFIX+b+'-'+v.substring(0, i);
i = v.indexOf('-', i+1);
}
css += PREFIX+b+'-'+v;
}
return css;
}
/**
* @type {string}
*/
var uaCss = '';
for (var b in ua) {
if (b && ua.hasOwnProperty(b)) {
uaCss += format(b, ua[b]);
}
}
// user-agent classNames
return uaCss;
},
encode:
/**
* Encodes parsed userAgent object as a compact URI-Encoded key-value collection
* @param ua {Object}
* @return {string}
*/
function(ua) {
var query = '';
for (var b in ua) {
if (b && ua.hasOwnProperty(b)) {
if (query) {
query += '&';
}
query += encodeURIComponent(b)+'='+encodeURIComponent(ua[b]);
}
}
return query;
}
};
/**
* @const
* @type {Object}
*/
cssua.userAgent = cssua.ua = cssua.parse(userAgent, sa);
/**
* @const
* @type {string}
*/
var ua = cssua.format(cssua.ua)+' js';
// append CSS classes to HTML node
if (html.className) {
html.className = html.className.replace(/\bno-js\b/g, '') + ua;
} else {
html.className = ua.substr(1);
}
return cssua;
})(document.documentElement, navigator.userAgent, navigator.standalone);