From 551fbf100c26bfe9f3f626200c4173124c064fca Mon Sep 17 00:00:00 2001 From: Dumitru Uzun Date: Thu, 20 Nov 2014 12:32:31 +0200 Subject: [PATCH 1/6] Keep global scope clean with AMD. Always check for jQuery global. If an AMD environment, don't export `UAParser` to global scope. Even in AMD environment jQuery exports to global scope. We should catch it even then. It also can happen then one joins the jQuery source file with other jQuery plugins in a CommonJS env. --- src/ua-parser.js | 61 +++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index fe36687..9ad8ac1 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -2,7 +2,7 @@ * UAParser.js v0.7.3 * Lightweight JavaScript-based User-Agent string parser * https://github.com/faisalman/ua-parser-js - * + * * Copyright © 2012-2014 Faisal Salman * Dual licensed under GPLv2 & MIT */ @@ -259,7 +259,7 @@ ], [[NAME, 'Yandex'], VERSION, MAJOR], [ /(comodo_dragon)\/((\d+)?[\w\.]+)/i // Comodo Dragon - ], [[NAME, /_/g, ' '], VERSION, MAJOR], [ + ], [[NAME, /_/g, ' '], VERSION, MAJOR], [ /(chrome|omniweb|arora|[tizenoka]{5}\s?browser)\/v?((\d+)?[\w\.]+)/i, // Chrome/OmniWeb/Arora/Tizen/Nokia @@ -548,16 +548,16 @@ /(nexus\s[45])/i, // LG /lg[e;\s\/-]+(\w+)*/i ], [MODEL, [VENDOR, 'LG'], [TYPE, MOBILE]], [ - + /android.+(ideatab[a-z0-9\-\s]+)/i // Lenovo ], [MODEL, [VENDOR, 'Lenovo'], [TYPE, TABLET]], [ - + /linux;.+((jolla));/i // Jolla ], [VENDOR, MODEL, [TYPE, MOBILE]], [ - + /((pebble))app\/[\d\.]+\s/i // Pebble ], [VENDOR, MODEL, [TYPE, WEARABLE]], [ - + /android.+;\s(glass)\s\d/i // Google Glass ], [MODEL, [VENDOR, 'Google'], [TYPE, WEARABLE]], [ @@ -631,7 +631,7 @@ /(ip[honead]+)(?:.*os\s*([\w]+)*\slike\smac|;\sopera)/i // iOS ], [[NAME, 'iOS'], [VERSION, /_/g, '.']], [ - /(mac\sos\sx)\s?([\w\s\.]+\w)*/i, + /(mac\sos\sx)\s?([\w\s\.]+\w)*/i, /(macintosh|mac(?=_powerpc)\s)/i // Mac OS ], [[NAME, 'Mac OS'], [VERSION, /_/g, '.']], [ @@ -700,7 +700,7 @@ UAParser.BROWSER = { NAME : NAME, MAJOR : MAJOR, - VERSION : VERSION + VERSION : VERSION }; UAParser.CPU = { ARCHITECTURE : ARCHITECTURE @@ -722,7 +722,7 @@ }; UAParser.OS = { NAME : NAME, - VERSION : VERSION + VERSION : VERSION }; @@ -739,30 +739,37 @@ } exports.UAParser = UAParser; } else { - // browser env - window.UAParser = UAParser; - // requirejs env (optional) + // requirejs env if (typeof(define) === FUNC_TYPE && define.amd) { define(function () { return UAParser; }); } - // jQuery/Zepto specific (optional) - var $ = window.jQuery || window.Zepto; - if (typeof($) !== UNDEF_TYPE) { - var parser = new UAParser(); - $.ua = parser.getResult(); - $.ua.get = function() { - return parser.getUA(); - }; - $.ua.set = function (uastring) { - parser.setUA(uastring); - var result = parser.getResult(); - for (var prop in result) { - $.ua[prop] = result[prop]; - } - }; + else { + // browser env + window.UAParser = UAParser; } } + // jQuery/Zepto specific (optional) + // Note: + // In AMD env the global scope should be kept clean, but jQuery is an exception. + // jQuery always exports to global scope, unless jQuery.noConflict(true) is used, + // and we should catch that. + var $ = window.jQuery || window.Zepto; + if (typeof($) !== UNDEF_TYPE) { + var parser = new UAParser(); + $.ua = parser.getResult(); + $.ua.get = function() { + return parser.getUA(); + }; + $.ua.set = function (uastring) { + parser.setUA(uastring); + var result = parser.getResult(); + for (var prop in result) { + $.ua[prop] = result[prop]; + } + }; + } + })(this); From 744c9bf0bd35a72eb2fcf83ca45fe2a977ee40e4 Mon Sep 17 00:00:00 2001 From: sgautrea Date: Tue, 2 Dec 2014 16:08:47 -0500 Subject: [PATCH 2/6] Update ua-parser.js Adding regex for Nexus 9 tablet --- src/ua-parser.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ua-parser.js b/src/ua-parser.js index 9ad8ac1..47aa574 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -507,6 +507,9 @@ /(alcatel|geeksphone|huawei|lenovo|nexian|panasonic|(?=;\s)sony)[_\s-]?([\w-]+)*/i // Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony ], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [ + + /(nexus\s9)/i, // HTC Nexus 9 + ], [MODEL, [VENDOR, 'HTC'], [TYPE, TABLET]], [ /[\s\(;](xbox(?:\sone)?)[\s\);]/i // Microsoft Xbox ], [MODEL, [VENDOR, 'Microsoft'], [TYPE, CONSOLE]], [ From 083db7b7d2f545fa688558aba4b765d5d4178a33 Mon Sep 17 00:00:00 2001 From: Shane Gautreau Date: Tue, 2 Dec 2014 16:46:04 -0500 Subject: [PATCH 3/6] Adding test for Nexus 9 recognition --- test/device-test.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/device-test.json b/test/device-test.json index 67b2c7c..5487f6a 100644 --- a/test/device-test.json +++ b/test/device-test.json @@ -9,6 +9,16 @@ "type" : "mobile" } }, + { + "desc" : "HTC Nexus 9", + "ua" : "Mozilla/5.0 (Linux; Android 5.0; Nexus 9 Build/LRX21R) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Mobile Crosswalk/7.36.154.13 Safari/537.36", + "expect" : + { + "vendor" : "HTC", + "model" : "Nexus 9", + "type" : "tablet" + } + }, { "desc" : "LG Nexus 4", "ua" : "Mozilla/5.0 (Linux; Android 4.2.1; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19", From 7cae802f3c627563132f73df66036d2f553c008e Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Wed, 28 Jan 2015 13:01:03 -0600 Subject: [PATCH 4/6] fixes typeof --- src/ua-parser.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index 47aa574..d9ba3cf 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -80,11 +80,11 @@ props = args[i + 1]; // odd sequence (1,3,5,..) // construct object barebones - if (typeof(result) === UNDEF_TYPE) { + if (typeof result === UNDEF_TYPE) { result = {}; for (p in props) { q = props[p]; - if (typeof(q) === OBJ_TYPE) { + if (typeof q === OBJ_TYPE) { result[q[0]] = undefined; } else { result[q] = undefined; @@ -101,9 +101,9 @@ match = matches[++k]; q = props[p]; // check if given property is actually array - if (typeof(q) === OBJ_TYPE && q.length > 0) { + if (typeof q === OBJ_TYPE && q.length > 0) { if (q.length == 2) { - if (typeof(q[1]) == FUNC_TYPE) { + if (typeof q[1] == FUNC_TYPE) { // assign modified match result[q[0]] = q[1].call(this, match); } else { @@ -112,7 +112,7 @@ } } else if (q.length == 3) { // check whether function or regex - if (typeof(q[1]) === FUNC_TYPE && !(q[1].exec && q[1].test)) { + if (typeof q[1] === FUNC_TYPE && !(q[1].exec && q[1].test)) { // call function (usually string mapper) result[q[0]] = match ? q[1].call(this, match, q[2]) : undefined; } else { @@ -137,7 +137,7 @@ for (var i in map) { // check if array - if (typeof(map[i]) === OBJ_TYPE && map[i].length > 0) { + if (typeof map[i] === OBJ_TYPE && map[i].length > 0) { for (var j = 0; j < map[i].length; j++) { if (util.has(map[i][j], str)) { return (i === UNKNOWN) ? undefined : i; @@ -735,15 +735,15 @@ // check js environment - if (typeof(exports) !== UNDEF_TYPE) { + if (typeof exports !== UNDEF_TYPE) { // nodejs env - if (typeof(module) !== UNDEF_TYPE && module.exports) { + if (typeof module !== UNDEF_TYPE && module.exports) { exports = module.exports = UAParser; } exports.UAParser = UAParser; } else { // requirejs env - if (typeof(define) === FUNC_TYPE && define.amd) { + if (typeof define === FUNC_TYPE && define.amd) { define(function () { return UAParser; }); @@ -760,7 +760,7 @@ // jQuery always exports to global scope, unless jQuery.noConflict(true) is used, // and we should catch that. var $ = window.jQuery || window.Zepto; - if (typeof($) !== UNDEF_TYPE) { + if (typeof $ !== UNDEF_TYPE) { var parser = new UAParser(); $.ua = parser.getResult(); $.ua.get = function() { From 3c911f2dd4b7b48f64b812746ed8f9bac5ba9edd Mon Sep 17 00:00:00 2001 From: Demis Palma Date: Fri, 27 Feb 2015 17:42:20 +0000 Subject: [PATCH 5/6] Character class may not be used inside character range Additional information and examples at http://stackoverflow.com/questions/15321938/regex-character-class-inside-character-range-failing-intellijs-jslint-inspectio --- src/ua-parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index d9ba3cf..4601555 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -541,10 +541,10 @@ /(nokia)[\s_-]?([\w-]+)*/i ], [[VENDOR, 'Nokia'], MODEL, [TYPE, MOBILE]], [ - /android\s3\.[\s\w-;]{10}(a\d{3})/i // Acer + /android\s3\.[\s\w;-]{10}(a\d{3})/i // Acer ], [MODEL, [VENDOR, 'Acer'], [TYPE, TABLET]], [ - /android\s3\.[\s\w-;]{10}(lg?)-([06cv9]{3,4})/i // LG Tablet + /android\s3\.[\s\w;-]{10}(lg?)-([06cv9]{3,4})/i // LG Tablet ], [[VENDOR, 'LG'], MODEL, [TYPE, TABLET]], [ /(lg) netcast\.tv/i // LG SmartTV ], [VENDOR, MODEL, [TYPE, SMARTTV]], [ From 89a3a21ce3b24d1ea8bb9bbf31a506679a27b508 Mon Sep 17 00:00:00 2001 From: Demis Palma Date: Fri, 27 Feb 2015 17:57:27 +0000 Subject: [PATCH 6/6] Unneeded comma Removed last comma in array literal --- src/ua-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index d9ba3cf..068f13c 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -508,7 +508,7 @@ // Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony ], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [ - /(nexus\s9)/i, // HTC Nexus 9 + /(nexus\s9)/i // HTC Nexus 9 ], [MODEL, [VENDOR, 'HTC'], [TYPE, TABLET]], [ /[\s\(;](xbox(?:\sone)?)[\s\);]/i // Microsoft Xbox