Skip to content

Commit

Permalink
Update ua module and its doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
cssmagic committed Dec 28, 2015
1 parent 3e8842c commit 749eb06
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 29 deletions.
8 changes: 6 additions & 2 deletions doc/api-ua.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

* `'7.0'` - 对 iOS 7.0.1 的探测结果。
* `'4.4'` - 对 Android 4.4.4 的探测结果。
* `''` - 对 Windows、Mac OS、WinPhone 的探测结果
* `''` - 对 Windows、Mac OS、WinPhone 等系统的探测结果

***

Expand All @@ -79,8 +79,10 @@
* `'uc'` - UC 浏览器
* `'baidu-app'` - 百度客户端
* `'baidu-browser'` - 百度浏览器
* `'mqq'` - 手机 QQ 浏览器
* `'m-qq-browser'` - 手机 QQ 浏览器
* `'miui'` - 小米浏览器
* `'ie-mobile'` - IE 移动版
* `'edge'` - 微软 Edge 浏览器
* `'wechat'` - 微信
* `'weibo'` - 微博
* `'chrome-webview'` - 采用 Chrome 内核的 WebView
Expand All @@ -95,6 +97,7 @@
* `'chrome'` - Chrome 内核(新版 Android 浏览器与 Opera 的内核)
* `'gecko'` - Firefox 的内核
* `'presto'` - 旧版 Opera 的内核
* `'edge'` - Edge 内核

对于无法识别的引擎,其值一律为空字符串。

Expand All @@ -111,3 +114,4 @@
* `'533.1'` - 对 Android 2.2.2 内置浏览器引擎 WebKit 版本的探测结果。
* `'43.0'` - 对 Chrome 43.0.2357.65 引擎版本的探测结果。
* `''` - 对 Firefox 引擎版本的探测结果。
* `''` - 对 Edge 引擎版本的探测结果。
20 changes: 18 additions & 2 deletions src/ua.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void function (window, _ext) {
ua.osVersion = (/\bandroid(?:_os)?[\/: ]?(\d+\.\d)\d*\b/.exec(s) || [0, ''])[1]
}
}
// fix - Windows Phone might pretend to be iOS or Android
if (_.str.include(s, 'windows phone')) {
ua.isIOS = ua.isAndroid = false
ua.osVersion = ''
}
if (ua.osVersion && !_.str.include(ua.osVersion, '.')) ua.osVersion += '.0'

// summery
Expand All @@ -61,7 +66,7 @@ void function (window, _ext) {
} else if (_.str.include(s, 'baidubrowser')) {
browser = 'baidu-browser'
} else if (_.str.include(s, 'mqqbrowser')) {
browser = 'mqq'
browser = 'm-qq-browser'
} else if (_.str.include(s, 'miuibrowser')) {
browser = 'miui'
} else if (_.str.include(s, '_weibo_') || _.str.include(s, ' weibo ')) {
Expand All @@ -70,6 +75,10 @@ void function (window, _ext) {
browser = 'firefox'
} else if (_.str.include(s, 'opera')) {
browser = 'opera'
} else if (_.str.include(s, ' edge/')) {
browser = 'edge'
} else if (_.str.include(s, 'iemobile')) {
browser = 'ie-mobile'
}
// these two must be the last
else if (ua.isChrome) {
Expand Down Expand Up @@ -102,12 +111,19 @@ void function (window, _ext) {
engine = 'webkit'
} else if (ua.isIOS) {
engine = 'webkit'
} else if (ua.isAndroid && browser === 'mqq') {
} else if (ua.isAndroid && browser === 'm-qq-browser') {
engine = 'webkit'
}
if (browser === 'firefox' && !ua.isIOS) engine = 'gecko'
if (browser === 'opera' && !ua.isIOS && _.str.include(s, 'presto')) engine = 'presto'
}
// fix Windows Phone, IE Mobile and Edge
if (browser === 'edge') {
engine = 'edge'
engineVersion = ''
} else if (browser === 'ie-mobile') {
engine = engineVersion = ''
}

// output
ua.browser = browser
Expand Down
98 changes: 73 additions & 25 deletions test/test-ua.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('UA', function () {
describe('iOS Safari', function () {
it('recognizes iphone_ios_40', function () {
var ua = _detect(iphone_ios_40)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.true
Expand All @@ -93,7 +93,7 @@ describe('UA', function () {
})
it('recognizes ipod_ios_30', function () {
var ua = _detect(ipod_ios_30)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.false
Expand All @@ -106,7 +106,7 @@ describe('UA', function () {
})
it('recognizes ipad_ios_322', function () {
var ua = _detect(ipad_ios_322)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.false
Expand All @@ -119,7 +119,7 @@ describe('UA', function () {
})
it('recognizes iphone_ios_60', function () {
var ua = _detect(iphone_ios_60)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.true
Expand All @@ -146,7 +146,7 @@ describe('UA', function () {
describe('iOS Third-Party Browser', function () {
it('recognizes uc_ios_421', function () {
var ua = _detect(uc_ios_421)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('4.2')
Expand All @@ -156,7 +156,7 @@ describe('UA', function () {
})
it('recognizes uc_ios_501', function () {
var ua = _detect(uc_ios_501)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('5.0')
Expand All @@ -166,21 +166,21 @@ describe('UA', function () {
})
it('recognizes qq_ios_511', function () {
var ua = _detect(qq_ios_511)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.true
expect(!!ua.isIPod).to.be.false
expect(!!ua.isIPad).to.be.false
expect(ua.osVersion).to.equal('5.1')
expect(ua.browser).to.equal('mqq')
expect(ua.browser).to.equal('m-qq-browser')
expect(ua.engine).to.equal('webkit')
expect(ua.engineVersion).to.equal('534.46')
})
it('recognizes gc_ios_511', function () {
var ua = _detect(gc_ios_511)
expect(!!ua.isChrome).to.be.true
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.true
expect(!!ua.isAndroid).to.be.false
expect(!!ua.isIPhone).to.be.true
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('UA', function () {
describe('Android Browser', function () {
it('recognizes adr_21', function () {
var ua = _detect(adr_21)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.1')
Expand All @@ -228,7 +228,7 @@ describe('UA', function () {
})
it('recognizes adr_22', function () {
var ua = _detect(adr_22)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.2')
Expand All @@ -238,7 +238,7 @@ describe('UA', function () {
})
it('recognizes adr_234', function () {
var ua = _detect(adr_234)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.3')
Expand All @@ -248,7 +248,7 @@ describe('UA', function () {
})
it('recognizes adr_403', function () {
var ua = _detect(adr_403)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('4.0')
Expand All @@ -259,7 +259,7 @@ describe('UA', function () {

it('recognizes gc_adr_404', function () {
var ua = _detect(gc_adr_404)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('4.0')
Expand All @@ -272,7 +272,7 @@ describe('UA', function () {
describe('Android Third-Party Browser', function () {
it('recognizes uc_adr_233', function () {
var ua = _detect(uc_adr_233)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.3')
Expand All @@ -282,7 +282,7 @@ describe('UA', function () {
})
it('recognizes uc_adr_234', function () {
var ua = _detect(uc_adr_234)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.3')
Expand All @@ -292,7 +292,7 @@ describe('UA', function () {
})
it('recognizes uc_adr_404', function () {
var ua = _detect(uc_adr_404)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('4.0')
Expand All @@ -302,7 +302,7 @@ describe('UA', function () {
})
it('recognizes uc_adr_00', function () {
var ua = _detect(uc_adr_00)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('')
Expand All @@ -313,18 +313,18 @@ describe('UA', function () {

it('recognizes qq_adr_422', function () {
var ua = _detect(qq_adr_422)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('4.2')
expect(ua.browser).to.equal('mqq')
expect(ua.browser).to.equal('m-qq-browser')
expect(ua.engine).to.equal('webkit')
expect(ua.engineVersion).to.equal('')
})

it('recognizes ff_adr_00', function () {
var ua = _detect(ff_adr_00)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('')
Expand All @@ -335,7 +335,7 @@ describe('UA', function () {

it('recognizes op_adr_404', function () {
var ua = _detect(op_adr_404)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('4.0')
Expand All @@ -346,7 +346,7 @@ describe('UA', function () {

it('recognizes xx_adr_235', function () {
var ua = _detect(xx_adr_235)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.3')
Expand All @@ -356,7 +356,7 @@ describe('UA', function () {
})
it('recognizes xx_adr_22', function () {
var ua = _detect(xx_adr_22)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('2.2')
Expand All @@ -371,7 +371,7 @@ describe('UA', function () {
describe('Android Chrome WebView', function () {
it('recognizes wv_adr_511', function () {
var ua = _detect(wv_adr_511)
expect(!!ua.isMobileDevice).to.be.true
expect(ua.isMobileDevice).to.be.true
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.true
expect(ua.osVersion).to.equal('5.1')
Expand All @@ -380,6 +380,54 @@ describe('UA', function () {
expect(ua.engineVersion).to.equal('43.0')
})
})

// other
var ff_mac = {str: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:41.0) Gecko/20100101 Firefox/41.0'}
var chrome_mac = {str: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
var ie_wp81 = {str: 'Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 520) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537'}
var edge_wp100 = {str: 'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; DEVICE INFO) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.xx'}
describe('Other UA', function () {
it('recognizes ff_mac', function () {
var ua = _detect(ff_mac)
expect(ua.isMobileDevice).to.be.false
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('')
expect(ua.browser).to.equal('firefox')
expect(ua.engine).to.equal('gecko')
expect(ua.engineVersion).to.equal('')
})
it('recognizes chrome_mac', function () {
var ua = _detect(chrome_mac)
expect(ua.isMobileDevice).to.be.false
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('')
expect(ua.browser).to.equal('chrome')
expect(ua.engine).to.equal('chrome')
expect(ua.engineVersion).to.equal('47.0')
})
it('recognizes ie_wp81', function () {
var ua = _detect(ie_wp81)
expect(ua.isMobileDevice).to.be.false
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('')
expect(ua.browser).to.equal('ie-mobile')
expect(ua.engine).to.equal('')
expect(ua.engineVersion).to.equal('')
})
it('recognizes edge_wp100', function () {
var ua = _detect(edge_wp100)
expect(ua.isMobileDevice).to.be.false
expect(!!ua.isIOS).to.be.false
expect(!!ua.isAndroid).to.be.false
expect(ua.osVersion).to.equal('')
expect(ua.browser).to.equal('edge')
expect(ua.engine).to.equal('edge')
expect(ua.engineVersion).to.equal('')
})
})
})
})

0 comments on commit 749eb06

Please sign in to comment.