Fix #635 - ua-ch: prioritize more specific brand name regardless the order

This commit is contained in:
Faisal Salman 2024-01-10 17:14:22 +07:00
parent 0c49d75074
commit b5c62b0c82
2 changed files with 52 additions and 5 deletions

View File

@ -140,7 +140,7 @@
var token = trim(tokens[i]).split(';v='); var token = trim(tokens[i]).split(';v=');
arr[i] = { brand : token[0], version : token[1] }; arr[i] = { brand : token[0], version : token[1] };
} else { } else {
arr[i] = tokens[i]; arr[i] = trim(tokens[i]);
} }
} }
return arr; return arr;
@ -1055,15 +1055,16 @@
switch (this.itemType) { switch (this.itemType) {
case UA_BROWSER: case UA_BROWSER:
var brands = uaCH[FULLVERLIST] || uaCH[BRANDS]; var brands = uaCH[FULLVERLIST] || uaCH[BRANDS], prevName;
if (brands) { if (brands) {
for (var i in brands) { for (var i in brands) {
var brandName = brands[i].brand || brands[i], var brandName = strip(GOOGLE+' ', brands[i].brand || brands[i]),
brandVersion = brands[i].version; brandVersion = brands[i].version;
if (!/not.a.brand/i.test(brandName) && (i < 1 || /chromi/i.test(this.get(NAME)))) { if (!/not.a.brand/i.test(brandName) && (!prevName || (/chrom/i.test(prevName) && !/chromi/i.test(brandName)))) {
this.set(NAME, strip(GOOGLE+' ', brandName)) this.set(NAME, brandName)
.set(VERSION, brandVersion) .set(VERSION, brandVersion)
.set(MAJOR, majorize(brandVersion)); .set(MAJOR, majorize(brandVersion));
prevName = brandName;
} }
} }
} }

View File

@ -512,4 +512,50 @@ describe('Map UA-CH headers', function () {
assert.strictEqual(uap.browser.version, undefined); assert.strictEqual(uap.browser.version, undefined);
assert.strictEqual(uap.browser.major, undefined); assert.strictEqual(uap.browser.major, undefined);
}); });
it('Prioritize more specific brand name regardless the order', function () {
const headers3a = {
'sec-ch-ua-full-version-list' : '"Not_A Brand;v=8, Chromium;v=120.0.6099.131, Google Chrome;v=120.0.6099.132"'
};
const headers3b = {
'sec-ch-ua-full-version-list' : '"Chromium;v=120.0.6099.131, Not_A Brand;v=8, Google Chrome;v=120.0.6099.132"'
};
const headers3c = {
'sec-ch-ua-full-version-list' : '"Google Chrome;v=120.0.6099.132, Chromium;v=120.0.6099.131, Not_A Brand;v=8"'
};
const headers3d = {
'sec-ch-ua-full-version-list' : '"Microsoft Edge;v=120.0.6099.133, Google Chrome;v=120.0.6099.132, Chromium;v=120.0.6099.131, Not_A Brand;v=8"'
};
const headers3e = {
'sec-ch-ua-full-version-list' : '"Chromium;v=120.0.6099.131, Google Chrome;v=120.0.6099.132, Microsoft Edge;v=120.0.6099.133, Not_A Brand;v=8"'
};
const headers3f = {
'sec-ch-ua-full-version-list' : '"Not_A Brand;v=8, Microsoft Edge;v=120.0.6099.133, Google Chrome;v=120.0.6099.132, Chromium;v=120.0.6099.131"'
};
uap = UAParser(headers3a).withClientHints();
assert.strictEqual(uap.browser.name, "Chrome");
assert.strictEqual(uap.browser.version, "120.0.6099.132");
uap = UAParser(headers3b).withClientHints();
assert.strictEqual(uap.browser.name, "Chrome");
assert.strictEqual(uap.browser.version, "120.0.6099.132");
uap = UAParser(headers3c).withClientHints();
assert.strictEqual(uap.browser.name, "Chrome");
assert.strictEqual(uap.browser.version, "120.0.6099.132");
uap = UAParser(headers3d).withClientHints();
assert.strictEqual(uap.browser.name, "Microsoft Edge");
assert.strictEqual(uap.browser.version, "120.0.6099.133");
uap = UAParser(headers3e).withClientHints();
assert.strictEqual(uap.browser.name, "Microsoft Edge");
assert.strictEqual(uap.browser.version, "120.0.6099.133");
uap = UAParser(headers3f).withClientHints();
assert.strictEqual(uap.browser.name, "Microsoft Edge");
assert.strictEqual(uap.browser.version, "120.0.6099.133");
});
}); });