Skip to content

Commit

Permalink
Add detection for Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
madrobby committed Aug 28, 2013
1 parent c65ecf2 commit daa516f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
rimtabletos = ua.match(/(RIM\sTablet\sOS)\s([\d.]+)/),
playbook = ua.match(/PlayBook/),
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/),
firefox = ua.match(/Firefox\/([\d.]+)/)
firefox = ua.match(/Firefox\/([\d.]+)/),
safari = webkit && ua.match(/Mobile\//) && !chrome

// Todo: clean this up with a better OS/browser seperation:
// - discern (more) between multiple browsers on android
Expand All @@ -42,6 +43,7 @@
if (!silk && os.android && ua.match(/Kindle Fire/)) browser.silk = true
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

os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) || (firefox && ua.match(/Tablet/)))
os.phone = !!(!os.tablet && (android || iphone || webos || blackberry || bb10 ||
Expand Down
61 changes: 51 additions & 10 deletions test/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ <h1>Browser detection</h1>
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_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; CPI iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B143 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",

iOS_3_2_iPad_2: "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10",

iOS_7_0_iPhone: "Mozilla 5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KTHML, like Gecko) Version/7.0 Mobile/11A449d Safari/9537.53",
iOS_7_0_iPhone_Chrome: "Mozilla 5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/536.26 (KHTM, like Gecko) CriOS/28.0.1500.17 Mobile/11A4449d Safari/8536.25",

Android_1_5: "Mozilla/5.0 (Linux; U; Android 1.5; de-; HTC Magic Build/PLAT-RC33) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1",
Android_2_1: "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; Nexus One Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 Chrome/4.1.249.1025",
Android_4_1_1: "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03O) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
Expand Down Expand Up @@ -105,12 +108,14 @@ <h1>Browser detection</h1>
t.assertTrue(browser.webkit)
t.assertEqual("1.5", os.version)
t.assertTrue(os.phone)
t.assertFalse(!!browser.safari)
})
detect(UA.Android_2_1, function(os, browser){
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertEqual("2.1", os.version)
t.assertTrue(os.phone)
t.assertFalse(!!browser.safari)
})
detect(UA.Android_4_1_1, function(os, browser){
t.assertTrue(os.android)
Expand All @@ -120,13 +125,15 @@ <h1>Browser detection</h1>
t.assertTrue(os.phone)
t.assertFalse(!!os.iphone)
t.assertTrue(browser.chrome)
t.assertFalse(!!browser.safari)
})
detect(UA.Android_4_1_1_Tablet, function(os, browser){
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertEqual("4.1.1", os.version)
t.assertTrue(os.tablet)
t.assertTrue(browser.chrome)
t.assertFalse(!!browser.safari)
})
detect(UA.Android_4_3, function(os, browser){
t.assertTrue(os.android)
Expand All @@ -136,6 +143,7 @@ <h1>Browser detection</h1>
t.assertTrue(os.phone)
t.assertFalse(!!os.iphone)
t.assertTrue(browser.chrome)
t.assertFalse(!!browser.safari)
})
},

Expand All @@ -147,69 +155,102 @@ <h1>Browser detection</h1>
t.assertEqual("3.0", os.version)
t.assertEqual("420.1", browser.version)
t.assertTrue(os.phone)
t.assertTrue(browser.safari)
})
detect(UA.iOS_3_1_1_iPod, function(os){
detect(UA.iOS_3_1_1_iPod, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assertUndefined(os.ipod)
t.assertEqual("3.1.1", os.version)
t.assertTrue(os.phone)
console.log(browser)
t.assertTrue(browser.safari)
})
detect(UA.iOS_3_2_iPad, function(os){
detect(UA.iOS_3_2_iPad, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assert(!os.iphone)
t.assertEqual("3.2", os.version)
t.assertTrue(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_3_2_iPad_2, function(os){
detect(UA.iOS_3_2_iPad_2, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assert(!os.iphone)
t.assertEqual("3.2", os.version)
t.assertTrue(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_4_0_iPhone, function(os){
detect(UA.iOS_4_0_iPhone, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assert(!os.ipad)
t.assertEqual("4.0", os.version)
t.assertTrue(os.phone)
t.assertTrue(browser.safari)
})
detect(UA.iOS_4_2_iPad, function(os){
detect(UA.iOS_4_2_iPad, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assertEqual("4.2", os.version)
t.assertFalse(os.phone)
t.assertTrue(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_4_3_iPhone_Simulator, function(os){
detect(UA.iOS_4_3_iPhone_Simulator, function(os, browser){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assertEqual("4.3", os.version)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_5_0_iPhone, function(os){
detect(UA.iOS_5_0_iPhone, function(os, browser){
t.assert(os.ios)
t.assert(os.iphone)
t.assertEqual("5.0", os.version)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_6_1_iPhone, function(os){
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.chrome)
t.assertFalse(!!browser.firefox)
})
detect(UA.iOS_6_0_iPad_mini, function(os){
detect(UA.iOS_6_0_iPad_mini, function(os, browser){
t.assert(os.ios)
t.assert(os.ipad)
t.assertEqual("6.0", os.version)
t.assertFalse(os.phone)
t.assertTrue(os.tablet)
t.assertTrue(browser.safari)
})
detect(UA.iOS_7_0_iPhone, function(os, browser){
t.assert(os.ios)
t.assert(os.iphone)
t.assertEqual("7.0", os.version)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
t.assertTrue(browser.safari)
t.assertFalse(!!browser.chrome)
t.assertFalse(!!browser.firefox)
})
detect(UA.iOS_7_0_iPhone_Chrome, function(os, browser){
t.assert(os.ios)
t.assert(os.iphone)
t.assertEqual("7.0", os.version)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
t.assertFalse(!!browser.safari)
t.assertTrue(browser.chrome)
t.assertFalse(!!browser.firefox)
})
},

Expand Down

0 comments on commit daa516f

Please sign in to comment.