Skip to content

Commit

Permalink
Assume that not explicitly mentioned lower versions are unsupported.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Feb 27, 2017
1 parent 9fdc904 commit 44a3049
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
17 changes: 14 additions & 3 deletions lib/preprocessCaniuseData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ module.exports = function preprocessCaniuseData(stats) {
Object.keys(stats).forEach(caniuseId => {
let highestVersionSeen;
let highestVersionIsSupported;
let lowestVersionSeen;

function registerHighVersion(version, isSupported) {
function registerVersion(version, isSupported) {
version = addZeroesIfMinorOrPatchIsMissing(version);
if (!highestVersionSeen || semver.gt(version, highestVersionSeen)) {
highestVersionSeen = version;
highestVersionIsSupported = isSupported;
}
if (!lowestVersionSeen || semver.lt(version, lowestVersionSeen)) {
lowestVersionSeen = version;
}
}

isSupportedByCaniuseIdAndVersion[caniuseId] = {};
Expand All @@ -39,11 +43,11 @@ module.exports = function preprocessCaniuseData(stats) {
} else if (/^\d+(\.\d+){0,2}$/.test(version)) {
// Exact major or major.minor or major.minor.patch version
isSupportedByCaniuseIdAndVersion[caniuseId][version] = isSupported;
registerHighVersion(version, isSupported);
registerVersion(version, isSupported);
} else {
const matchRange = version.match(/^(\d+(?:\.\d+){0,2})-(\d+(?:\.\d+){0,2})$/);
if (matchRange) {
registerHighVersion(matchRange[2], isSupported);
registerVersion(matchRange[2], isSupported);
const range = new semver.Range(matchRange[1] + ' - ' + matchRange[2]);
(checkSupportedRangeByCaniuseId[caniuseId] = checkSupportedRangeByCaniuseId[caniuseId] || []).push((major, minor, patch) => {
if (range.test(buildVersion(major, minor, patch))) {
Expand All @@ -62,6 +66,13 @@ module.exports = function preprocessCaniuseData(stats) {
}
});
}
if (lowestVersionSeen) {
(checkSupportedRangeByCaniuseId[caniuseId] = checkSupportedRangeByCaniuseId[caniuseId] || []).push((major, minor, patch) => {
if (semver.lt(buildVersion(major, minor, patch), highestVersionSeen)) {
return false;
}
});
}
});

return function checkSupported(caniuseId, major, minor, patch) {
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ describe('with an empty policy', function () {
});
});

describe('with a browser that caniuse-db has data about in a version that is not explicitly mentioned', function () {
describe('with a browser that caniuse-db has data about in a version that is older than all the explicitly mentioned ones', function () {
// Chrome 1
beforeEach(() => {
userAgentString = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.36 Safari/525.19';
});

it('should leave the CSP string unchanged', function () {
return expect('script-src somewhere.com/with/a/path', 'to come out as', 'script-src somewhere.com/with/a/path');
it('should assume that CSP is not supported and strip the header', function () {
return expect('script-src somewhere.com/with/a/path', 'to come out as', undefined);
});
});

Expand Down
52 changes: 47 additions & 5 deletions test/preprocessCaniuseData.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ describe('preprocessCaniuseData', function () {

it('should let exact major.minor versions through', function () {
expect({ie: { '11.1': 'y' }}, 'to support', 'ie', 11, 1)
.and('to be undecided about', 'ie', 11, 0);
.and('not to support', 'ie', 11, 0);
});

it('should let exact major.minor.patch versions through', function () {
expect({ie: { '11.1.2': 'y' }}, 'to support', 'ie', 11, 1, 2)
.and('to be undecided about', 'ie', 11, 1);
.and('not to support', 'ie', 11, 1);
});

it('should let exact major versions through', function () {
Expand All @@ -57,14 +57,14 @@ describe('preprocessCaniuseData', function () {
it('should explode version ranges spanning several minor versions', function () {
expect({ie: { '10.1-10.2': 'y' }}, 'to support', 'ie', 10, 1)
.and('to support', 'ie', 10, 2)
.and('to be undecided about', 'ie', 10, 0);
.and('not to support', 'ie', 10, 0);
});

it('should explode version ranges spanning several major and minor versions', function () {
expect({ie: { '10.3-12.1': 'y' }}, 'to support', 'ie', 10, 3)
.and('to support', 'ie', 10, 20)
.and('to support', 'ie', 10, 20, 4)
.and('to be undecided about', 'ie', 10, 0)
.and('not to support', 'ie', 10, 0)
.and('to support', 'ie', 11, 0)
.and('to support', 'ie', 11, 20)
.and('to support', 'ie', 12, 0)
Expand All @@ -76,7 +76,7 @@ describe('preprocessCaniuseData', function () {
.and('to support', 'ie', 11)
.and('not to support', 'ie', 10, 0)
.and('not to support', 'ie', 10, 1)
.and('to be undecided about', 'ie', 10, 2)
.and('not to support', 'ie', 10, 2)
.and('to support', 'ie', 10, 3)
.and('to support', 'ie', 10, 20)
.and('to support', 'ie', 11, 0)
Expand Down Expand Up @@ -127,6 +127,48 @@ describe('preprocessCaniuseData', function () {
});
});

describe('when there is only data about newer versions of the browser being queried', function () {
describe('when the lowest known version is given as major', function () {
it('should assume that the older version is not supported when the higher version is not', function () {
expect({ie: { '10': 'n' }}, 'not to support', 'ie', 9);
});

it('should assume that the older version is not supported when the higher version is', function () {
expect({ie: { '10': 'y' }}, 'not to support', 'ie', 9);
});
});

describe('when the highest known version is given as major.minor', function () {
it('should assume that the older version is not supported when the higher version is not', function () {
expect({ie: { '10.5': 'n' }}, 'not to support', 'ie', 9);
});

it('should assume that the older version is not supported when the higher version is', function () {
expect({ie: { '10.5': 'y' }}, 'not to support', 'ie', 9);
});
});

describe('when the highest known version is given as major.minor.patch', function () {
it('should assume that the older version is not supported when the higher version is not', function () {
expect({ie: { '10.5.8': 'n' }}, 'not to support', 'ie', 9);
});

it('should assume that the older version is not supported when the higher version is', function () {
expect({ie: { '10.5.8': 'y' }}, 'not to support', 'ie', 9);
});
});

describe('when the highest known version is given as a range', function () {
it('should assume that the older version is not supported when the higher version is not', function () {
expect({ie: { '10.5.2-10.5.8': 'n' }}, 'not to support', 'ie', 9);
});

it('should assume that the older version is not supported when the higher version is', function () {
expect({ie: { '10.5.2-10.5.8': 'y' }}, 'not to support', 'ie', 9);
});
});
});

describe('when there is no data about the browser being queried', function () {
it('should return undefined', function () {
expect({}, 'to be undecided about', 'ie', 10, 0);
Expand Down

0 comments on commit 44a3049

Please sign in to comment.