Skip to content

Commit

Permalink
Detect Safari on OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn authored and mislav committed Feb 16, 2014
1 parent fef90ef commit e557acb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var os = this.os = {}, browser = this.browser = {},
webkit = ua.match(/Web[kK]it[\/]{0,1}([\d.]+)/),
android = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
osx = !!ua.match(/\(Macintosh\; Intel /),
ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/),
iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
Expand All @@ -21,8 +22,8 @@
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/),
firefox = ua.match(/Firefox\/([\d.]+)/),
ie = ua.match(/MSIE\s([\d.]+)/),
safari = webkit && ua.match(/Mobile\//) && !chrome,
webview = ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/) && !chrome
webview = !chrome && ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/),
safari = webview || ua.match(/Version\/([\d.]+)([^S](Safari)|[^M]*(Mobile)[^S]*(Safari))/)

// Todo: clean this up with a better OS/browser seperation:
// - discern (more) between multiple browsers on android
Expand All @@ -48,7 +49,7 @@
if (chrome) browser.chrome = true, browser.version = chrome[1]
if (firefox) browser.firefox = true, browser.version = firefox[1]
if (ie) browser.ie = true, browser.version = ie[1]
if (safari && (ua.match(/Safari/) || !!os.ios)) browser.safari = true
if (safari && (osx || os.ios)) {browser.safari = true; if (osx) browser.version = safari[1]}
if (webview) browser.webview = true

os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) ||
Expand Down
28 changes: 28 additions & 0 deletions test/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ <h1>Zepto Detect unit tests</h1>
(function(){

var UA = {
Safari_OSX_7_0_1: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11",
Safari_OSX_7_0: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9) AppleWebKit/537.71 (KHTML, like Gecko) Version/7.0 Safari/537.71",
Safari_OSX_6_0: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25",

WebOS_1_4_0_Pre: "Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.1",
WebOS_1_4_0_Pixi: "Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pixi/1.1",
WebOS_1_2_9_Pixi: "Mozilla/5.0 (webOS/Palm webOS 1.2.9; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pixi/1.0",
Expand Down Expand Up @@ -92,6 +96,30 @@ <h1>Zepto Detect unit tests</h1>
}

Evidence('ZeptoDetectTest', {
testSafariOSX: function(t){
detect(UA.Safari_OSX_7_0_1, function(os, browser){
t.assertUndefined(os.osx)
t.assert(!os.ipad)
t.assertTrue(browser.webkit)
t.assertTrue(browser.safari)
t.assertFalse(!!browser.chrome)
t.assertEqual("7.0.1",browser.version)
});
detect(UA.Safari_OSX_7_0, function(os, browser){
t.assert(!os.ipad)
t.assertTrue(browser.webkit)
t.assertTrue(browser.safari)
t.assertFalse(!!browser.chrome)
t.assertEqual("7.0",browser.version)
});
detect(UA.Safari_OSX_6_0, function(os, browser){
t.assert(!os.ipad)
t.assertTrue(browser.webkit)
t.assertTrue(browser.safari)
t.assertFalse(!!browser.chrome)
t.assertEqual("6.0",browser.version)
});
},

testWebOS: function(t){
detect(UA.WebOS_1_4_0_Pre, function(os, browser){
Expand Down

0 comments on commit e557acb

Please sign in to comment.