Skip to content

Commit

Permalink
Merge branch 'pr/798'
Browse files Browse the repository at this point in the history
Conflicts:
	src/detect.js
	test/detect.html
  • Loading branch information
madrobby committed Sep 4, 2013
2 parents 8247230 + 25ab34c commit dc57e70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
webkit = ua.match(/WebKit\/([\d.]+)/),
android = ua.match(/(Android)\s+([\d.]+)/),
ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/),
iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/),
touchpad = webos && ua.match(/TouchPad/),
Expand All @@ -19,7 +20,8 @@
playbook = ua.match(/PlayBook/),
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/),
firefox = ua.match(/Firefox\/([\d.]+)/),
safari = webkit && ua.match(/Mobile\//) && !chrome
safari = webkit && ua.match(/Mobile\//) && !chrome,
webview = ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/) && !chrome

// Todo: clean this up with a better OS/browser seperation:
// - discern (more) between multiple browsers on android
Expand All @@ -30,8 +32,9 @@
if (browser.webkit = !!webkit) browser.version = webkit[1]

if (android) os.android = true, os.version = android[2]
if (iphone) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.')
if (iphone && !ipod) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.')
if (ipad) os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.')
if (ipod) os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null
if (webos) os.webos = true, os.version = webos[2]
if (touchpad) os.touchpad = true
if (blackberry) os.blackberry = true, os.version = blackberry[2]
Expand All @@ -44,9 +47,10 @@
if (chrome) browser.chrome = true, browser.version = chrome[1]
if (firefox) browser.firefox = true, browser.version = firefox[1]
if (safari && (ua.match(/Safari/) || !!os.ios)) browser.safari = true
if (webview) browser.webview = true

os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) || (firefox && ua.match(/Tablet/)))
os.phone = !!(!os.tablet && (android || iphone || webos || blackberry || bb10 ||
os.phone = !!(!os.tablet && !os.ipod && (android || iphone || webos || blackberry || bb10 ||
(chrome && ua.match(/Android/)) || (chrome && ua.match(/CriOS\/([\d.]+)/)) || (firefox && ua.match(/Mobile/))))
}

Expand Down
18 changes: 14 additions & 4 deletions test/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h1>Browser detection</h1>
iOS_4_2_iPad: "Mozilla/5.0 (iPad; U; CPU OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C134 Safari/6533.18.5",
iOS_4_3_iPhone_Simulator: "Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5",
iOS_5_0_iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3",
iOS_5_1_iPad_webView: "Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/98176",
iOS_6_0_iPad_mini: "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A406 Safari/8536.25",
iOS_6_1_iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B143 Safari/8536.25",

Expand Down Expand Up @@ -159,16 +160,16 @@ <h1>Browser detection</h1>
})
detect(UA.iOS_3_1_1_iPod, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assertUndefined(os.ipod)
t.assertUndefined(os.iphone)
t.assertTrue(os.ipod)
t.assertEqual("3.1.1", os.version)
t.assertTrue(os.phone)
t.assertFalse(!!os.phone)
t.assertTrue(browser.safari)
})
detect(UA.iOS_3_2_iPad, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assert(!os.iphone)
t.assertFalse(!!os.iphone)
t.assertEqual("3.2", os.version)
t.assertTrue(os.tablet)
t.assertTrue(browser.safari)
Expand Down Expand Up @@ -213,13 +214,21 @@ <h1>Browser detection</h1>
t.assertFalse(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_5_1_iPad_webView, function(os, browser){
t.assert(os.ios)
t.assert(os.ipad)
t.assertEqual("5.1", os.version)
t.assertTrue(browser.webview)
t.assertTrue(browser.safari)
})
detect(UA.iOS_6_1_iPhone, function(os, browser){
t.assert(os.ios)
t.assert(os.iphone)
t.assertEqual("6.1", os.version)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
t.assertTrue(browser.safari)
t.assertFalse(!!browser.webview)
t.assertFalse(!!browser.chrome)
t.assertFalse(!!browser.firefox)
})
Expand Down Expand Up @@ -250,6 +259,7 @@ <h1>Browser detection</h1>
t.assertFalse(!!browser.safari)
t.assertTrue(browser.chrome)
t.assertFalse(!!browser.firefox)
t.assertFalse(!!browser.webview)
})
},

Expand Down

0 comments on commit dc57e70

Please sign in to comment.