From 89cffac4a5a27bbcb7a8b253495c8efd74a3336f Mon Sep 17 00:00:00 2001 From: Hannes Diercks Date: Fri, 13 Dec 2013 13:17:10 +0100 Subject: [PATCH 1/2] Make testable and add tests. --- Makefile | 16 +- README.md | 18 ++- package.json | 1 + src/bowser.js | 136 ++++++++-------- src/useragents.js | 388 ++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 64 ++++++++ 6 files changed, 554 insertions(+), 69 deletions(-) create mode 100644 src/useragents.js create mode 100644 test/test.js diff --git a/Makefile b/Makefile index 09b69fe..d5cd37f 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,16 @@ boosh: - node make/build.js \ No newline at end of file + node make/build.js + +REPORTER = dot + +test: + @NODE_ENV=test ./node_modules/.bin/mocha \ + --reporter $(REPORTER) \ + +test-w: + @NODE_ENV=test ./node_modules/.bin/mocha \ + --reporter $(REPORTER) \ + --growl \ + --watch + +.PHONY: test test-w diff --git a/README.md b/README.md index ba6e427..82f79d1 100644 --- a/README.md +++ b/README.md @@ -60,4 +60,20 @@ else if (bowser.c) { } else { // unsupported (bowser.x) -} \ No newline at end of file +} +``` + +Building +-------- + +Simply `$ npm install` and `$ make` inside the bowser folder. + + +Testing +------- +We started a list `src/useragents.js` with example user agents and their expected bowser object. + +Whenever you add support for new browsers or notice a bug / mismatch, please update the list and +check if all tests are still passing. + +To run the test call `$ make test` and hope for green light ;) diff --git a/package.json b/package.json index cb4ed31..6f78cb2 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,6 @@ } , "devDependencies": { "smoosh": "*" + , "mocha": "*" } } diff --git a/src/bowser.js b/src/bowser.js index 86a2d5c..5ab09c2 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -4,69 +4,64 @@ else this[name] = definition() }('bowser', function () { /** - * navigator.userAgent => - * Chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.57 Safari/534.24" - * Opera: "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.7; U; en) Presto/2.7.62 Version/11.01" - * Safari: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1" - * IE: "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)" - * IE>=11: "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; Media Center PC 6.0; rv:11.0) like Gecko" - * Firefox: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0" - * iPhone: "Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5" - * iPad: "Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5", - * Android: "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" - * Touchpad: "Mozilla/5.0 (hp-tabled;Linux;hpwOS/3.0.5; U; en-US)) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/234.83 Safari/534.6 TouchPad/1.0" - * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" + * See useragents.js for examples of navigator.userAgent */ - var ua = navigator.userAgent - , t = true - , ie = /(msie|trident)/i.test(ua) - , chrome = /chrome|crios/i.test(ua) - , phantom = /phantom/i.test(ua) - , safari = /safari/i.test(ua) && !chrome && !phantom - , iphone = /iphone/i.test(ua) - , ipad = /ipad/i.test(ua) - , touchpad = /touchpad/i.test(ua) - , android = /android/i.test(ua) - , opera = /opera/i.test(ua) || /opr/i.test(ua) - , firefox = /firefox/i.test(ua) - , gecko = /gecko\//i.test(ua) - , seamonkey = /seamonkey\//i.test(ua) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i - , o + var t = true, + v /* temporary placeholder for versions. */ - function detect() { + function detect(ua) { - if (ie) return { + var ie = /(msie|trident)/i.test(ua) + , chrome = /chrome|crios/i.test(ua) + , phantom = /phantom/i.test(ua) + , safari = /safari/i.test(ua) && !chrome && !phantom + , iphone = /iphone/i.test(ua) + , ipad = /ipad/i.test(ua) + , touchpad = /touchpad/i.test(ua) + , android = /android/i.test(ua) + , opera = /opera/i.test(ua) || /opr/i.test(ua) + , firefox = /firefox/i.test(ua) + , gecko = /gecko\//i.test(ua) + , seamonkey = /seamonkey\//i.test(ua) + , webkitVersion = /version\/(\d+(\.\d+)?)/i + , firefoxVersion = /firefox[ \/](\d+(\.\d+)?)/i + , o = {} + + 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 + o = { + name: 'Opera' + , opera: t + , version: v + } + } else if (ie) o = { name: 'Internet Explorer' , msie: t , version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2] } - if (opera) return { - name: 'Opera' - , opera: t - , version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i)[1] - } - if (chrome) return { + else if (chrome) o = { name: 'Chrome' , webkit: t , chrome: t , version: ua.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)[1] } - if (phantom) return { + else if (phantom) o = { name: 'PhantomJS' , webkit: t , phantom: t - , version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1] + , version: ua.match(/phantomjs\/(\d+(\.\d+)?)/i)[1] } - if (touchpad) return { + else if (touchpad) o = { name: 'TouchPad' , webkit: t , touchpad: t , version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1] } - if (iphone || ipad) { + else if (iphone || ipad) { o = { name : iphone ? 'iPhone' : 'iPad' , webkit: t @@ -79,61 +74,68 @@ if (webkitVersion.test(ua)) { o.version = ua.match(webkitVersion)[1] } - return o } - if (android) return { + else if (android) o = { name: 'Android' , webkit: t , android: t , mobile: t , version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1] } - if (safari) return { + else if (safari) o = { name: 'Safari' , webkit: t , safari: t - , version: ua.match(webkitVersion)[1] + , version: ((v = ua.match(webkitVersion)) ? v[1] : 0) } - if (gecko) { + else if (gecko) { o = { name: 'Gecko' , gecko: t , mozilla: t - , version: ua.match(firefoxVersion)[1] + , version: ((v = ua.match(firefoxVersion)) && v? v[1] : 0) } if (firefox) { o.name = 'Firefox'; o.firefox = t; } - return o } - if (seamonkey) return { + else if (seamonkey) o = { name: 'SeaMonkey' , seamonkey: t , version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1] } - return {} + + // Graded Browser Support + // http://developer.yahoo.com/yui/articles/gbs + if ((o.msie && o.version >= 8) || + (o.chrome && o.version >= 10) || + (o.firefox && o.version >= 4.0) || + (o.safari && o.version >= 5) || + (o.opera && o.version >= 10.0)) { + o.a = t; + } + + else if ((o.msie && o.version < 8) || + (o.chrome && o.version < 10) || + (o.firefox && o.version < 4.0) || + (o.safari && o.version < 5) || + (o.opera && o.version < 10.0)) { + o.c = t + } else o.x = t + + return o } - var bowser = detect() + var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent : '') - // Graded Browser Support - // http://developer.yahoo.com/yui/articles/gbs - if ((bowser.msie && bowser.version >= 8) || - (bowser.chrome && bowser.version >= 10) || - (bowser.firefox && bowser.version >= 4.0) || - (bowser.safari && bowser.version >= 5) || - (bowser.opera && bowser.version >= 10.0)) { - bowser.a = t; - } - else if ((bowser.msie && bowser.version < 8) || - (bowser.chrome && bowser.version < 10) || - (bowser.firefox && bowser.version < 4.0) || - (bowser.safari && bowser.version < 5) || - (bowser.opera && bowser.version < 10.0)) { - bowser.c = t - } else bowser.x = t + /* + * Set our detect method to the main bowser object so we can + * reuse it to test other user agents. + * This is needed to implement future tests. + */ + bowser._detect = detect; return bowser }); diff --git a/src/useragents.js b/src/useragents.js new file mode 100644 index 0000000..ba66622 --- /dev/null +++ b/src/useragents.js @@ -0,0 +1,388 @@ +/** + * Example User Agents and their expected bowser objects. + * Most of them where found at http://www.useragentstring.com/ + * + * @see test/test.js + * @author hannes.diercks@jimdo.com + */ +module.exports.useragents = { + Chrome: { + 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36': { + chrome: true + , version: '30.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36': { + chrome: true + , version: '29.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36': { + chrome: true + , version: '29.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36': { + chrome: true + , version: '29.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36': { + chrome: true + , version: '28.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.57 Safari/534.24': { + chrome: true + , version: '11.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.462.0 Safari/534.3': { + chrome: true + , version: '6.0' + , webkit: true + , c: true + } + } + , Opera: { + 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14': { + opera: true + , version: '12.14' + , a: true + } + , 'Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14': { + opera: true + , version: '12.14' + , a: true + } + , 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.14': { + opera: true + , version: '12.14' + , a: true + } + , 'Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02': { + opera: true + , version: '12.02' + , a: true + } + , 'Opera/9.80 (X11; Linux i686; U; es-ES) Presto/2.8.131 Version/11.11': { + opera: true + , version: '11.11' + , a: true + } + , 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.7; U; en) Presto/2.7.62 Version/11.01': { + opera: true + , version: '11.01' + , a: true + } + , 'Opera/9.80 (Windows NT 5.2; U; zh-cn) Presto/2.6.30 Version/10.63': { + opera: true + , version: '10.63' + , a: true + } + , 'Opera/9.80 (X11; Linux i686; U; it) Presto/2.5.24 Version/10.54': { + opera: true + , version: '10.54' + , a: true + } + , 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1': { + opera: true + , version: '9.70' + , c: true + } + , 'Opera/9.63 (X11; Linux i686)': { + opera: true + , version: '9.63' + , c: true + } + , 'Mozilla/5.0 (X11; Linux i686; U; en) Opera 8.52': { + opera: true + , version: '8.52' + , c: true + } + } + , Safari: { + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2': { + safari: true + , version: '5.1' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1': { + safari: true + , version: '5.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5': { + safari: true + , version: '5.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+': { + safari: true + , version: '5.0' + , webkit: true + , a: true + } + , 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-en) AppleWebKit/533.16 (KHTML, like Gecko) Version/4.1 Safari/533.16': { + safari: true + , version: '4.1' + , webkit: true + , c: true + } + , 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_6_1; en_GB, en_US) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10': { + safari: true + , version: '4.0' + , webkit: true + , c: true + } + , 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; de-de) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.28.3': { + safari: true + , version: '3.2' + , webkit: true + , c: true + } + } + , 'Internet Explorer': { + 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; Media Center PC 6.0; rv:11.0) like Gecko': { + msie: true + , version: '11.0' + , a: true + } + , 'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0': { + msie: true + , version: '10.6' + , a: true + } + , 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50727; WOW64)': { + msie: true + , version: '10.0' + , a: true + } + , 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)': { + msie: true + , version: '9.0' + , a: true + } + , 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)': { + msie: true + , version: '8.0' + , a: true + } + , 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)': { + msie: true + , version: '7.0' + , c: true + } + , 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)': { + msie: true + , version: '6.1' + , c: true + } + , 'Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)': { + msie: true + , version: '6.0' + , c: true + } + , 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)': { + msie: true + , version: '5.01' + , c: true + } + } + , Firefox: { + 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0': { + mozilla: true + , gecko: true + , firefox: true + , version: '25.0' + , a: true + } + , 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0': { + mozilla: true + , gecko: true + , firefox: true + , version: '24.0' + , a: true + } + , 'Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0': { + mozilla: true + , gecko: true + , firefox: true + , version: '21.0' + , a: true + } + , 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:15.0) Gecko/20120910144328 Firefox/15.0.2': { + mozilla: true + , gecko: true + , firefox: true + , version: '15.0' + , a: true + } + , 'Mozilla/5.0 (Windows; U; Windows NT 6.1; WOW64; en-US; rv:2.0.4) Gecko/20120718 AskTbAVR-IDW/3.12.5.17700 Firefox/14.0.1': { + mozilla: true + , gecko: true + , firefox: true + , version: '14.0' + , a: true + } + , 'Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0 FirePHP/0.6': { + mozilla: true + , gecko: true + , firefox: true + , version: '6.0' + , a: true + } + , 'Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20100101 Firefox/4.2a1pre': { + mozilla: true + , gecko: true + , firefox: true + , version: '4.2' + , a: true + } + , 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0': { + mozilla: true + , gecko: true + , firefox: true + , version: '4.0' + , a: true + } + , 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2b1) Gecko/20091014 Firefox/3.6b1 GTB5': { + mozilla: true + , gecko: true + , firefox: true + , version: '3.6' + , c: true + } + , 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.0.15) Gecko/2009101601 Firefox 2.1 (.NET CLR 3.5.30729)': { + mozilla: true + , gecko: true + , firefox: true + , version: '2.1' + , c: true + } + , 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20061014 Firefox/1.5.0.7': { + mozilla: true + , gecko: true + , firefox: true + , version: '1.5' + , c: true + } + } + , iPhone: { + 'Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5': { + ios: true + , version: '5.0' + , iphone: true + , ipad: false + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3': { + ios: true + , version: '3.0' + , iphone: true + , ipad: false + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B5097d Safari/6531.22.7': { + ios: true + , version: '4.0' + , iphone: true + , ipad: false + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_2; en-gb) AppleWebKit/526+ (KHTML, like Gecko) Version/3.1 iPhone': { + ios: true + , version: '3.1' + , iphone: true + , ipad: false + , mobile: true + , webkit: true + , x: true + } + } + , iPad: { + 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25': { + ios: true + , version: '6.0' + , iphone: false + , ipad: true + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 Safari/7534.48.3': { + ios: true + , version: '5.1' + , iphone: false + , ipad: true + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5': { + ios: true + , version: '5.0' + , iphone: false + , ipad: true + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; es-es) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B360 Safari/531.21.10': { + ios: true + , version: '4.0' + , iphone: false + , ipad: true + , mobile: true + , webkit: true + , x: true + } + } + , Android: { + 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1': { + android: true + , webkit: true + , version: 4.0 + , mobile: true + , x: true + } + , 'Mozilla/5.0 (Linux; U; Android 1.6; ar-us; SonyEricssonX10i Build/R2BA026) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1': { + android: true + , webkit: true + , version: 3.1 + , mobile: true + , x: true + } + } + , TouchPad: { + 'Mozilla/5.0 (hp-tabled;Linux;hpwOS/3.0.5; U; en-US)) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/234.83 Safari/534.6 TouchPad/1.0': { + touchpad: true + , version: '1.0' + , webkit: true + , x: true + } + } + , PhantomJS: { + 'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34': { + phantom: true + , webkit: true + , version: '1.5' + , x: true + } + } +}; \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..d2702af --- /dev/null +++ b/test/test.js @@ -0,0 +1,64 @@ +/** + * Loop through all entries in our user agents object and test everything. + * + * @see src/useragents.js + * @author hannes.diercks@jimdo.com + */ + +var g + , ua + , p + , assert = require('assert') + , browser = require('../src/bowser').browser + , allUserAgents = require('../src/useragents').useragents + +/** + * Get the length of an object. + * http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array + * + * @param {Object} obj + * @return {Number} + */ +function objLength(obj) { + var size = 0 + , key + for (key in obj) { + if (obj.hasOwnProperty(key)) size++ + } + return size +} + +/* Groups */ +for (g in allUserAgents) { (function(group, userAgents) { + describe(group, function() { + + /* User Agents */ + for (ua in userAgents) { (function(userAgent, expections) { + describe('#' + userAgent, function() { + + expections.name = group + + /* Get the result from bowser. */ + var result = browser._detect(userAgent) + , expectionLength = objLength(expections) + + /* At first, check if the result has the correct length. */ + it('Should have ' + expectionLength + ' properties', function() { + assert.equal(objLength(result), expectionLength) + }) + + /* Properties */ + for (p in expections) { (function(property, value, resultValue) { + + /* Now ensure correctness of every property. */ + it('\'s Property "' + property + '" should be ' + value, function() { + assert.equal(resultValue, value) + }) + + })(p, expections[p], result[p])} + + }) + })(ua, userAgents[ua])} + + }) +})(g, allUserAgents[g])} From 8668678f8a226c4d3c457570c4a0ef99f93158cd Mon Sep 17 00:00:00 2001 From: Hannes Diercks Date: Mon, 16 Dec 2013 06:52:33 +0100 Subject: [PATCH 2/2] Add support for ipod, windowsphone, webos and blackberry. --- README.md | 14 ++++++-- src/bowser.js | 38 +++++++++++++++++++-- src/useragents.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 82f79d1..37effa3 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,21 @@ if (bowser.msie && bowser.version <= 6) { Detected Browsers ----- - * msie - * safari[webkit] + * android + * blackberry * chrome[webkit] * firefox[gecko] + * ipad + * iphone + * ipod + * msie * opera + * phantomjs + * safari[webkit] + * seamonkey + * touchpad + * webos + * windows phone Notes ---- diff --git a/src/bowser.js b/src/bowser.js index 5ab09c2..2df391d 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -18,17 +18,27 @@ , safari = /safari/i.test(ua) && !chrome && !phantom , iphone = /iphone/i.test(ua) , ipad = /ipad/i.test(ua) + , ipod = /ipod/i.test(ua) , touchpad = /touchpad/i.test(ua) , android = /android/i.test(ua) , opera = /opera/i.test(ua) || /opr/i.test(ua) , firefox = /firefox/i.test(ua) , gecko = /gecko\//i.test(ua) , seamonkey = /seamonkey\//i.test(ua) + , webos = /webos/i.test(ua) + , windowsphone = /windows phone/i.test(ua) + , blackberry = /blackberry/i.test(ua) , webkitVersion = /version\/(\d+(\.\d+)?)/i , firefoxVersion = /firefox[ \/](\d+(\.\d+)?)/i , o = {} - if (opera) { + if (windowsphone) o = { + name: 'Windows Phone' + , windowsphone: t + , mobile: t + , version: ua.match(/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] @@ -61,20 +71,42 @@ , touchpad: t , version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1] } - else if (iphone || ipad) { + else if (iphone || ipad || ipod) { + if (ipod) iphone = false o = { - name : iphone ? 'iPhone' : 'iPad' + name : iphone ? 'iPhone' : ipad ? 'iPad' : 'iPod' , webkit: t , mobile: t , ios: t , iphone: iphone , ipad: ipad + , ipod: ipod } // WTF: version is not part of user agent in web apps if (webkitVersion.test(ua)) { o.version = ua.match(webkitVersion)[1] } } + else if (blackberry) { + o = { + name: 'BlackBerry' + , blackberry: t + , mobile: t + } + if ((v = ua.match(webkitVersion))) { + o.version = v[1] + o.webkit = t + } else { + o.version = ua.match(/blackberry[\d]+\/(\d+(\.\d+)?)/i)[1] + } + } + else if (webos) o = { + name: 'WebOS' + , mobile: t + , webkit: t + , webos: t + , version: (ua.match(webkitVersion) || ua.match(/wosbrowser\/(\d+(\.\d+)?)/i))[1] + } else if (android) o = { name: 'Android' , webkit: t diff --git a/src/useragents.js b/src/useragents.js index ba66622..dfbb2b6 100644 --- a/src/useragents.js +++ b/src/useragents.js @@ -283,6 +283,7 @@ module.exports.useragents = { , version: '5.0' , iphone: true , ipad: false + , ipod: false , mobile: true , webkit: true , x: true @@ -292,6 +293,7 @@ module.exports.useragents = { , version: '3.0' , iphone: true , ipad: false + , ipod: false , mobile: true , webkit: true , x: true @@ -301,6 +303,7 @@ module.exports.useragents = { , version: '4.0' , iphone: true , ipad: false + , ipod: false , mobile: true , webkit: true , x: true @@ -310,6 +313,7 @@ module.exports.useragents = { , version: '3.1' , iphone: true , ipad: false + , ipod: false , mobile: true , webkit: true , x: true @@ -321,6 +325,7 @@ module.exports.useragents = { , version: '6.0' , iphone: false , ipad: true + , ipod: false , mobile: true , webkit: true , x: true @@ -330,6 +335,7 @@ module.exports.useragents = { , version: '5.1' , iphone: false , ipad: true + , ipod: false , mobile: true , webkit: true , x: true @@ -339,6 +345,7 @@ module.exports.useragents = { , version: '5.0' , iphone: false , ipad: true + , ipod: false , mobile: true , webkit: true , x: true @@ -348,11 +355,91 @@ module.exports.useragents = { , version: '4.0' , iphone: false , ipad: true + , ipod: false , mobile: true , webkit: true , x: true } } + , iPod: { + 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5': { + ios: true + , version: '5.0' + , iphone: false + , ipad: false + , ipod: true + , mobile: true + , webkit: true + , x: true + } + , 'Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3': { + ios: true + , version: '3.0' + , iphone: false + , ipad: false + , ipod: true + , mobile: true + , webkit: true + , x: true + } + } + , BlackBerry: { + 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+': { + blackberry: true + , version: '7.1' + , webkit: true + , mobile: true + , x: true + } + , 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.450 Mobile Safari/534.8+': { + blackberry: true + , version: '6.0' + , webkit: true + , mobile: true + , x: true + } + , 'BlackBerry9800/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba': { + blackberry: true + , version: '5.0' + , mobile: true + , x: true + } + , 'BlackBerry8320/4.5.0.52 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179': { + blackberry: true + , version: '4.5' + , mobile: true + , x: true + } + } + , 'Windows Phone': { + 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)': { + windowsphone: true + , version: '10.0' + , mobile: true + , x: true + } + , 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)': { + windowsphone: true + , version: '9.0' + , mobile: true + , x: true + } + , 'Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; Nokia;N70)': { + windowsphone: true + , version: '7.0' + , mobile: true + , x: true + } + } + , WebOS: { + 'Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0': { + webos: true + , webkit: true + , version: '1.0' + , mobile: true + , x: true + } + } , Android: { 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1': { android: true