From b3bc89c4639a6c6a8d3aac2bf69926617753ec3c Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Sun, 5 Oct 2025 21:01:32 +0700 Subject: [PATCH] [fix] setUA(): remove trailing space from user-agent string --- src/main/ua-parser.js | 9 +++------ test/unit/main.js | 8 ++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/ua-parser.js b/src/main/ua-parser.js index 370116e..8292272 100755 --- a/src/main/ua-parser.js +++ b/src/main/ua-parser.js @@ -206,10 +206,8 @@ return strip(/\\?\"/g, str); }, trim = function (str, len) { - if (isString(str)) { - str = strip(/^\s\s*/, str); - return typeof len === TYPEOF.UNDEFINED ? str : str.substring(0, UA_MAX_LENGTH); - } + str = strip(/^\s\s*/, String(str)); + return typeof len === TYPEOF.UNDEFINED ? str : str.substring(0, len); }; /////////////// @@ -1464,8 +1462,7 @@ ['getResult', createItemFunc(RESULT)], ['getUA', function () { return userAgent; }], ['setUA', function (ua) { - if (isString(ua)) - userAgent = ua.length > UA_MAX_LENGTH ? trim(ua, UA_MAX_LENGTH) : ua; + if (isString(ua)) userAgent = trim(ua, UA_MAX_LENGTH); return this; }] ]) diff --git a/test/unit/main.js b/test/unit/main.js index b99e832..231b5c5 100644 --- a/test/unit/main.js +++ b/test/unit/main.js @@ -163,6 +163,14 @@ describe('Extending Regex', function () { assert.deepEqual(myParser3.setUA(myUA2).getDevice(), {vendor: "MyTab", model: "14 Pro Max", type: "tablet"}); }); +describe('User-agent with trailing space', function () { + it ('trailing space will be trimmed', function () { + const uastring = ' Opera/9.21 (Windows NT 5.1; U; ru) '; + const { ua } = UAParser(uastring); + assert.equal(ua, 'Opera/9.21 (Windows NT 5.1; U; ru) '); + }); +}); + describe('User-agent length', function () { var UA_MAX_LENGTH = 500;