Skip to content

Commit b3bc89c

Browse files
committed
[fix] setUA(): remove trailing space from user-agent string
1 parent 5749302 commit b3bc89c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/main/ua-parser.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,8 @@
206206
return strip(/\\?\"/g, str);
207207
},
208208
trim = function (str, len) {
209-
if (isString(str)) {
210-
str = strip(/^\s\s*/, str);
211-
return typeof len === TYPEOF.UNDEFINED ? str : str.substring(0, UA_MAX_LENGTH);
212-
}
209+
str = strip(/^\s\s*/, String(str));
210+
return typeof len === TYPEOF.UNDEFINED ? str : str.substring(0, len);
213211
};
214212

215213
///////////////
@@ -1464,8 +1462,7 @@
14641462
['getResult', createItemFunc(RESULT)],
14651463
['getUA', function () { return userAgent; }],
14661464
['setUA', function (ua) {
1467-
if (isString(ua))
1468-
userAgent = ua.length > UA_MAX_LENGTH ? trim(ua, UA_MAX_LENGTH) : ua;
1465+
if (isString(ua)) userAgent = trim(ua, UA_MAX_LENGTH);
14691466
return this;
14701467
}]
14711468
])

test/unit/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ describe('Extending Regex', function () {
163163
assert.deepEqual(myParser3.setUA(myUA2).getDevice(), {vendor: "MyTab", model: "14 Pro Max", type: "tablet"});
164164
});
165165

166+
describe('User-agent with trailing space', function () {
167+
it ('trailing space will be trimmed', function () {
168+
const uastring = ' Opera/9.21 (Windows NT 5.1; U; ru) ';
169+
const { ua } = UAParser(uastring);
170+
assert.equal(ua, 'Opera/9.21 (Windows NT 5.1; U; ru) ');
171+
});
172+
});
173+
166174
describe('User-agent length', function () {
167175
var UA_MAX_LENGTH = 500;
168176

0 commit comments

Comments
 (0)