Skip to content

Commit

Permalink
fix: throw error when certain srcset modifiers are passed zero (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherwinski authored Feb 25, 2020
1 parent 4772135 commit 2630f96
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
48 changes: 30 additions & 18 deletions src/imgix-core-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@

if ((width) || (height && aspectRatio)) {
return this._buildDPRSrcSet(path, params, options);
}
else {
} else {
return this._buildSrcSetPairs(path, params, options);
}
};
Expand All @@ -185,21 +184,16 @@
var srcset = '';
var currentWidth;
var targetWidths;
var widthTolerance = options["widthTolerance"] || DEFAULT_SRCSET_WIDTH_TOLERANCE;
var minWidth = options["minWidth"] || MIN_SRCSET_WIDTH;
var maxWidth = options["maxWidth"] || MAX_SRCSET_WIDTH;
var customWidths = options["widths"];
var customWidths = options.widths;
var srcsetOptions = validateAndDestructureOptions(options);
var widthTolerance = srcsetOptions[0], minWidth = srcsetOptions[1], maxWidth = srcsetOptions[2];

if (customWidths) {
validateWidths(customWidths);
targetWidths = customWidths;
}
else if (widthTolerance != DEFAULT_SRCSET_WIDTH_TOLERANCE || minWidth != MIN_SRCSET_WIDTH || maxWidth != MAX_SRCSET_WIDTH) {
validateRange(minWidth, maxWidth);
validateWidthTolerance(widthTolerance);
} else if (widthTolerance != DEFAULT_SRCSET_WIDTH_TOLERANCE || minWidth != MIN_SRCSET_WIDTH || maxWidth != MAX_SRCSET_WIDTH) {
targetWidths = _generateTargetWidths(widthTolerance, minWidth, maxWidth);
}
else {
} else {
targetWidths = DEFAULT_SRCSET_WIDTHS;
}

Expand All @@ -216,8 +210,8 @@
var srcset = '';
var targetRatios = [1, 2, 3, 4, 5];
var currentRatio;
var disableVariableQuality = options["disableVariableQuality"] || false;
var quality = params["q"];
var disableVariableQuality = options.disableVariableQuality || false;
var quality = params.q;

if (!disableVariableQuality) {
validateVariableQuality(disableVariableQuality);
Expand All @@ -237,23 +231,41 @@
return srcset.slice(0,-2);
};

function validateAndDestructureOptions(options) {
if (options.widthTolerance !== undefined) {
validateWidthTolerance(options.widthTolerance);
widthTolerance = options.widthTolerance;
} else {
widthTolerance = DEFAULT_SRCSET_WIDTH_TOLERANCE;
}

minWidth = options.minWidth === undefined ? MIN_SRCSET_WIDTH : options.minWidth;
maxWidth = options.maxWidth === undefined ? MAX_SRCSET_WIDTH : options.maxWidth;

// Validate the range unless we're using defaults for both
if (minWidth != MIN_SRCSET_WIDTH || maxWidth != MAX_SRCSET_WIDTH) {
validateRange(minWidth, maxWidth);
}

return [widthTolerance, minWidth, maxWidth];
};

function validateRange(min, max) {
if (!(Number.isInteger(min) && Number.isInteger(max)) || (min < 0 || max < 0)) {
if (!(Number.isInteger(min) && Number.isInteger(max)) || (min <= 0 || max <= 0) || (min > max)) {
throw new Error('The min and max srcset widths can only be passed positive Number values');
}
};

function validateWidthTolerance(widthTolerance) {
if (typeof widthTolerance != 'number' || widthTolerance < 0) {
if (typeof widthTolerance != 'number' || widthTolerance <= 0) {
throw new Error('The srcset widthTolerance argument can only be passed a positive scalar number');
}
};

function validateWidths(customWidths) {
if (!Array.isArray(customWidths) || !customWidths.length) {
throw new Error('The widths argument can only be passed a valid non-empty array of integers');
}
else {
} else {
allPositiveIntegers = customWidths.every(
function(width) {
return Number.isInteger(width) && width > 0
Expand Down
44 changes: 42 additions & 2 deletions test/test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,22 +1074,54 @@ describe('Imgix client:', function describeSuite() {
assert(max <= MAX);
});

it('errors with non-Number width', function testSpec() {
it('errors with non-Number minWidth', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {minWidth: 'abc'});
}, Error);
});

it('errors with negative width', function testSpec() {
it('errors with negative maxWidth', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {maxWidth: -100});
}, Error);
});

it('errors with zero maxWidth', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {maxWidth: 0});
}, Error);
});

it('errors with two invalid widths', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {minWidth: 0, maxWidth: 0});
}, Error);
});

it('errors when the maxWidth is less than the minWidth', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {minWidth: 1000, maxWidth: 500});
}, Error);
});

it('generates a single srcset entry if minWidth and maxWidth are equal', function testSpec() {
var srcset = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false
}).buildSrcSet('image.jpg', {}, {minWidth: 1000, maxWidth: 1000});
assert(srcset, 'https://testing.imgix.net/image.jpg?w=1000 1000w');
});

it('does not include a minWidth or maxWidth URL parameter', function testSpec() {
assert(!srcset.includes('minWidth='))
assert(!srcset.includes('maxWidth='))
Expand Down Expand Up @@ -1191,6 +1223,14 @@ describe('Imgix client:', function describeSuite() {
}).buildSrcSet('image.jpg', {}, {widthTolerance: -0.10});
}, Error);
});

it('errors with zero widthTolerance', function testSpec() {
assert.throws(function() {
new ImgixClient({
domain: 'testing.imgix.net'
}).buildSrcSet('image.jpg', {}, {widthTolerance: 0});
}, Error);
});
});

describe('with a custom list of widths provided', function describeSuite() {
Expand Down

0 comments on commit 2630f96

Please sign in to comment.