Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repeated value #184

Merged
merged 5 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/imgix-core-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,16 @@

// returns an array of width values used during scrset generation
ImgixClient.prototype._generateTargetWidths = function(widthTolerance, minWidth, maxWidth) {
var resolutions = [];
var INCREMENT_PERCENTAGE = widthTolerance;
var minWidth = Math.floor(minWidth);
var maxWidth = Math.floor(maxWidth);

var resolutions = [minWidth];

if (minWidth === maxWidth) {
return resolutions;
}

var cacheKey = INCREMENT_PERCENTAGE + '/' + minWidth + '/' + maxWidth;

if (cacheKey in this.targetWidthsCache) {
Expand All @@ -238,14 +244,12 @@
return 2 * Math.round(n / 2);
};

var prev = minWidth;
while (prev < maxWidth) {
resolutions.push(ensureEven(prev));
prev *= 1 + (INCREMENT_PERCENTAGE * 2);
var tempWidth = minWidth;
while (resolutions[resolutions.length - 1] < maxWidth) {
tempWidth *= 1 + (INCREMENT_PERCENTAGE * 2);
resolutions.push(Math.min(ensureEven(tempWidth), maxWidth));
}

resolutions.push(maxWidth);

this.targetWidthsCache[cacheKey] = resolutions;

return resolutions;
Expand Down
14 changes: 14 additions & 0 deletions test/test-buildSrcSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,20 @@ describe('SrcSet Builder:', function describeSuite() {
}, Error);
});
});

describe('with widthTolerance, minWidth, and maxWidth values which have caused duplicate values in the past', function describeSuite() {
var client = new ImgixClient({
domain: 'testing.imgix.net',
includeLibraryParam: false
})
var srcset = client.buildSrcSet('image.jpg', {}, {widthTolerance: 0.0999, minWidth: 1000, maxWidth: 1200});

it('should not repeat the largest width when a running value just below maxWidth is reached', function testSpec() {
var srclist = srcset.split(",");
assert.equal(parseInt(srclist[srclist.length - 1].split(" ")[1].slice(0,-1), 10), 1200);
assert.notEqual(srclist[srclist.length - 2], srclist[srclist.length - 1]);
});
});
});
});
});