1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00

made version extraction less error-prone/more future-proof, will now always check

if result of regex match is truthy and array bounds are correct before actually
accessing the version number
This commit is contained in:
BendingBender 2014-02-21 01:23:58 +01:00
parent 24dd55d9b4
commit 0da898f906
3 changed files with 54 additions and 42 deletions

View File

@ -16,6 +16,11 @@
var t = true,
v /* temporary placeholder for versions. */
function getVersion(ua, regex, matchedIdx) {
var match = ua.match(regex);
return (match && match.length > matchedIdx && match[matchedIdx]) || 0;
}
function detect(ua) {
var ie = /(msie|trident)/i.test(ua)
@ -46,13 +51,12 @@
name: 'Windows Phone'
, windowsphone: t
, mobile: t
, version: ua.match(/iemobile\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /iemobile\/(\d+(\.\d+)?)/i, 1)
}
else if (opera) {
if ((v = ua.match(webkitVersion)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opr\/(\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opera[ \/](\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else v = 0
v = getVersion(ua, webkitVersion, 1) ||
getVersion(ua, /opr\/(\d+(\.\d+)?)/i, 1) ||
getVersion(ua, /opera[ \/](\d+(\.\d+)?)/i, 1);
o = {
name: 'Opera'
, opera: t
@ -65,16 +69,17 @@
if (chrome) {
o.webkit = t
}
} else if (ie) o = {
}
else if (ie) o = {
name: 'Internet Explorer'
, msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
, version: getVersion(ua, /(msie |rv:)(\d+(\.\d+)?)/i, 2)
}
else if (chrome) o = {
name: 'Chrome'
, webkit: t
, chrome: t
, version: ua.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /(?:chrome|crios)\/(\d+(\.\d+)?)/i, 1)
, ipad: ipad
, iphone: iphone
, ipod: ipod
@ -86,20 +91,20 @@
name: 'PhantomJS'
, webkit: t
, phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /phantomjs\/(\d+(\.\d+)?)/i, 1)
}
else if (touchpad) o = {
name: 'TouchPad'
, webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /touchpad\/(\d+(\.\d+)?)/i, 1)
}
else if (silk) o = {
name: 'Amazon Silk'
, webkit: t
, android: t
, mobile: t
, version : ua.match(/silk\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /silk\/(\d+(\.\d+)?)/i, 1)
}
else if (iphone || ipad || ipod) {
o = {
@ -113,7 +118,7 @@
}
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]
o.version = getVersion(ua, webkitVersion, 1)
}
}
else if (blackberry) {
@ -122,11 +127,11 @@
, blackberry: t
, mobile: t
}
if ((v = ua.match(webkitVersion))) {
o.version = v[1]
if ((v = getVersion(ua, webkitVersion, 1))) {
o.version = v
o.webkit = t
} else {
o.version = ua.match(/blackberry[\d]+\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /blackberry[\d]+\/(\d+(\.\d+)?)/i, 1)
}
}
else if (webos) o = {
@ -134,19 +139,19 @@
, mobile: t
, webkit: t
, webos: t
, version: (ua.match(webkitVersion) || ua.match(/wosbrowser\/(\d+(\.\d+)?)/i))[1]
, version: (getVersion(ua, webkitVersion, 1) || getVersion(ua, /wosbrowser\/(\d+(\.\d+)?)/i, 1))
}
else if (gecko) {
o = {
name: 'Gecko'
, gecko: t
, mozilla: t
, version: ((v = ua.match(firefoxVersion)) && v? v[1] : 0)
, version: getVersion(ua, firefoxVersion, 1)
}
if (seamonkey) {
o.name = 'SeaMonkey'
o.seamonkey = t
o.version = ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /seamonkey\/(\d+(\.\d+)?)/i, 1)
} else if (firefox) {
o.name = 'Firefox'
o.firefox = t
@ -154,21 +159,23 @@
if (android) {
o.android = t
o.mobile = t
} else if (!android && firefox && /\((mobile|tablet);.*rv:[\d\.]+\)/i.test(ua)) {
o.firefoxos = t
o.mobile = t
}
}
else if (android) o = {
name: 'Android'
, webkit: t
, android: t
, mobile: t
, version: ua.match(webkitVersion)[1]
, version: getVersion(ua, webkitVersion, 1)
}
else if (safari) o = {
name: 'Safari'
, webkit: t
, safari: t
, version: ((v = ua.match(webkitVersion)) ? v[1] : 0)
, version: getVersion(ua, webkitVersion, 1)
}
// Graded Browser Support

2
bowser.min.js vendored
View File

@ -3,4 +3,4 @@
* https://github.com/ded/bowser
* MIT License | (c) Dustin Diaz 2013
*/
!function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports.browser=t():this[e]=t()}("bowser",function(){function n(n){var r=/(msie|trident)/i.test(n),i=/chrome|crios/i.test(n),s=/phantom/i.test(n),o=/iphone/i.test(n),u=/ipad/i.test(n),a=/ipod/i.test(n),f=/touchpad/i.test(n),l=/silk/i.test(n),c=/safari/i.test(n)&&!i&&!s&&!l,h=/android/i.test(n),p=/opera/i.test(n)||/opr/i.test(n),d=/firefox/i.test(n),m=/gecko\//i.test(n),g=/seamonkey\//i.test(n),y=/webos/i.test(n),b=/windows phone/i.test(n),w=/blackberry/i.test(n),E=/version\/(\d+(\.\d+)?)/i,S=/firefox[ \/](\d+(\.\d+)?)/i,x=/mobile/i.test(n),T={};return a&&(o=!1),b?T={name:"Windows Phone",windowsphone:e,mobile:e,version:n.match(/iemobile\/(\d+(\.\d+)?)/i)[1]}:p?((t=n.match(E))&&t.length>1?t=t[1]:(t=n.match(/opr\/(\d+(\.\d+)?)/i))&&t.length>1?t=t[1]:(t=n.match(/opera[ \/](\d+(\.\d+)?)/i))&&t.length>1?t=t[1]:t=0,T={name:"Opera",opera:e,version:t},h&&(T.android=e,T.mobile=e),i&&(T.webkit=e)):r?T={name:"Internet Explorer",msie:e,version:n.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]}:i?T={name:"Chrome",webkit:e,chrome:e,version:n.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)[1],ipad:u,iphone:o,ipod:a,ios:o||u||a,android:h,mobile:x}:s?T={name:"PhantomJS",webkit:e,phantom:e,version:n.match(/phantomjs\/(\d+(\.\d+)?)/i)[1]}:f?T={name:"TouchPad",webkit:e,touchpad:e,version:n.match(/touchpad\/(\d+(\.\d+)?)/i)[1]}:l?T={name:"Amazon Silk",webkit:e,android:e,mobile:e,version:n.match(/silk\/(\d+(\.\d+)?)/i)[1]}:o||u||a?(T={name:o?"iPhone":u?"iPad":"iPod",webkit:e,mobile:e,ios:e,iphone:o,ipad:u,ipod:a},E.test(n)&&(T.version=n.match(E)[1])):w?(T={name:"BlackBerry",blackberry:e,mobile:e},(t=n.match(E))?(T.version=t[1],T.webkit=e):T.version=n.match(/blackberry[\d]+\/(\d+(\.\d+)?)/i)[1]):y?T={name:"WebOS",mobile:e,webkit:e,webos:e,version:(n.match(E)||n.match(/wosbrowser\/(\d+(\.\d+)?)/i))[1]}:m?(T={name:"Gecko",gecko:e,mozilla:e,version:(t=n.match(S))&&t?t[1]:0},g?(T.name="SeaMonkey",T.seamonkey=e,T.version=n.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]):d&&(T.name="Firefox",T.firefox=e),h&&(T.android=e,T.mobile=e)):h?T={name:"Android",webkit:e,android:e,mobile:e,version:n.match(E)[1]}:c&&(T={name:"Safari",webkit:e,safari:e,version:(t=n.match(E))?t[1]:0}),T.msie&&T.version>=9||T.chrome&&T.version>=20||T.firefox&&T.version>=10||T.safari&&T.version>=5||T.opera&&T.version>=10?T.a=e:T.msie&&T.version<9||T.chrome&&T.version<20||T.firefox&&T.version<10||T.safari&&T.version<5||T.opera&&T.version<10?T.c=e:T.x=e,T}var e=!0,t,r=n(typeof navigator!="undefined"?navigator.userAgent:"");return r._detect=n,r})
!function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports.browser=t():this[e]=t()}("bowser",function(){function n(e,t,n){var r=e.match(t);return r&&r.length>n&&r[n]||0}function r(r){var i=/(msie|trident)/i.test(r),s=/chrome|crios/i.test(r),o=/phantom/i.test(r),u=/iphone/i.test(r),a=/ipad/i.test(r),f=/ipod/i.test(r),l=/touchpad/i.test(r),c=/silk/i.test(r),h=/safari/i.test(r)&&!s&&!o&&!c,p=/android/i.test(r),d=/opera/i.test(r)||/opr/i.test(r),m=/firefox/i.test(r),g=/gecko\//i.test(r),y=/seamonkey\//i.test(r),b=/webos/i.test(r),w=/windows phone/i.test(r),E=/blackberry/i.test(r),S=/version\/(\d+(\.\d+)?)/i,x=/firefox[ \/](\d+(\.\d+)?)/i,T=/mobile/i.test(r),N={};return f&&(u=!1),w?N={name:"Windows Phone",windowsphone:e,mobile:e,version:n(r,/iemobile\/(\d+(\.\d+)?)/i,1)}:d?(t=n(r,S,1)||n(r,/opr\/(\d+(\.\d+)?)/i,1)||n(r,/opera[ \/](\d+(\.\d+)?)/i,1),N={name:"Opera",opera:e,version:t},p&&(N.android=e,N.mobile=e),s&&(N.webkit=e)):i?N={name:"Internet Explorer",msie:e,version:n(r,/(msie |rv:)(\d+(\.\d+)?)/i,2)}:s?N={name:"Chrome",webkit:e,chrome:e,version:n(r,/(?:chrome|crios)\/(\d+(\.\d+)?)/i,1),ipad:a,iphone:u,ipod:f,ios:u||a||f,android:p,mobile:T}:o?N={name:"PhantomJS",webkit:e,phantom:e,version:n(r,/phantomjs\/(\d+(\.\d+)?)/i,1)}:l?N={name:"TouchPad",webkit:e,touchpad:e,version:n(r,/touchpad\/(\d+(\.\d+)?)/i,1)}:c?N={name:"Amazon Silk",webkit:e,android:e,mobile:e,version:n(r,/silk\/(\d+(\.\d+)?)/i,1)}:u||a||f?(N={name:u?"iPhone":a?"iPad":"iPod",webkit:e,mobile:e,ios:e,iphone:u,ipad:a,ipod:f},S.test(r)&&(N.version=n(r,S,1))):E?(N={name:"BlackBerry",blackberry:e,mobile:e},(t=n(r,S,1))?(N.version=t,N.webkit=e):N.version=n(r,/blackberry[\d]+\/(\d+(\.\d+)?)/i,1)):b?N={name:"WebOS",mobile:e,webkit:e,webos:e,version:n(r,S,1)||n(r,/wosbrowser\/(\d+(\.\d+)?)/i,1)}:g?(N={name:"Gecko",gecko:e,mozilla:e,version:n(r,x,1)},y?(N.name="SeaMonkey",N.seamonkey=e,N.version=n(r,/seamonkey\/(\d+(\.\d+)?)/i,1)):m&&(N.name="Firefox",N.firefox=e),p?(N.android=e,N.mobile=e):!p&&m&&/\((mobile|tablet);.*rv:[\d\.]+\)/i.test(r)&&(N.firefoxos=e,N.mobile=e)):p?N={name:"Android",webkit:e,android:e,mobile:e,version:n(r,S,1)}:h&&(N={name:"Safari",webkit:e,safari:e,version:n(r,S,1)}),N.msie&&N.version>=9||N.chrome&&N.version>=20||N.firefox&&N.version>=10||N.safari&&N.version>=5||N.opera&&N.version>=10?N.a=e:N.msie&&N.version<9||N.chrome&&N.version<20||N.firefox&&N.version<10||N.safari&&N.version<5||N.opera&&N.version<10?N.c=e:N.x=e,N}var e=!0,t,i=r(typeof navigator!="undefined"?navigator.userAgent:"");return i._detect=r,i})

View File

@ -10,6 +10,11 @@
var t = true,
v /* temporary placeholder for versions. */
function getVersion(ua, regex, matchedIdx) {
var match = ua.match(regex);
return (match && match.length > matchedIdx && match[matchedIdx]) || 0;
}
function detect(ua) {
var ie = /(msie|trident)/i.test(ua)
@ -40,13 +45,12 @@
name: 'Windows Phone'
, windowsphone: t
, mobile: t
, version: ua.match(/iemobile\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /iemobile\/(\d+(\.\d+)?)/i, 1)
}
else if (opera) {
if ((v = ua.match(webkitVersion)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opr\/(\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opera[ \/](\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else v = 0
v = getVersion(ua, webkitVersion, 1) ||
getVersion(ua, /opr\/(\d+(\.\d+)?)/i, 1) ||
getVersion(ua, /opera[ \/](\d+(\.\d+)?)/i, 1);
o = {
name: 'Opera'
, opera: t
@ -59,16 +63,17 @@
if (chrome) {
o.webkit = t
}
} else if (ie) o = {
}
else if (ie) o = {
name: 'Internet Explorer'
, msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
, version: getVersion(ua, /(msie |rv:)(\d+(\.\d+)?)/i, 2)
}
else if (chrome) o = {
name: 'Chrome'
, webkit: t
, chrome: t
, version: ua.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /(?:chrome|crios)\/(\d+(\.\d+)?)/i, 1)
, ipad: ipad
, iphone: iphone
, ipod: ipod
@ -80,20 +85,20 @@
name: 'PhantomJS'
, webkit: t
, phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /phantomjs\/(\d+(\.\d+)?)/i, 1)
}
else if (touchpad) o = {
name: 'TouchPad'
, webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /touchpad\/(\d+(\.\d+)?)/i, 1)
}
else if (silk) o = {
name: 'Amazon Silk'
, webkit: t
, android: t
, mobile: t
, version : ua.match(/silk\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /silk\/(\d+(\.\d+)?)/i, 1)
}
else if (iphone || ipad || ipod) {
o = {
@ -107,7 +112,7 @@
}
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]
o.version = getVersion(ua, webkitVersion, 1)
}
}
else if (blackberry) {
@ -116,11 +121,11 @@
, blackberry: t
, mobile: t
}
if ((v = ua.match(webkitVersion))) {
o.version = v[1]
if ((v = getVersion(ua, webkitVersion, 1))) {
o.version = v
o.webkit = t
} else {
o.version = ua.match(/blackberry[\d]+\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /blackberry[\d]+\/(\d+(\.\d+)?)/i, 1)
}
}
else if (webos) o = {
@ -128,19 +133,19 @@
, mobile: t
, webkit: t
, webos: t
, version: (ua.match(webkitVersion) || ua.match(/wosbrowser\/(\d+(\.\d+)?)/i))[1]
, version: (getVersion(ua, webkitVersion, 1) || getVersion(ua, /wosbrowser\/(\d+(\.\d+)?)/i, 1))
}
else if (gecko) {
o = {
name: 'Gecko'
, gecko: t
, mozilla: t
, version: ((v = ua.match(firefoxVersion)) && v? v[1] : 0)
, version: getVersion(ua, firefoxVersion, 1)
}
if (seamonkey) {
o.name = 'SeaMonkey'
o.seamonkey = t
o.version = ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /seamonkey\/(\d+(\.\d+)?)/i, 1)
} else if (firefox) {
o.name = 'Firefox'
o.firefox = t
@ -158,13 +163,13 @@
, webkit: t
, android: t
, mobile: t
, version: ua.match(webkitVersion)[1]
, version: getVersion(ua, webkitVersion, 1)
}
else if (safari) o = {
name: 'Safari'
, webkit: t
, safari: t
, version: ((v = ua.match(webkitVersion)) ? v[1] : 0)
, version: getVersion(ua, webkitVersion, 1)
}
// Graded Browser Support