Tighten 'extract' parameter validation #158

This commit is contained in:
Lovell Fuller 2015-01-29 22:46:04 +00:00
parent 797d503a99
commit e5a5e2ca7e
3 changed files with 65 additions and 3 deletions

View File

@ -142,7 +142,11 @@ Sharp.prototype.extract = function(topOffset, leftOffset, width, height) {
var suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
var values = arguments;
['topOffset', 'leftOffset', 'width', 'height'].forEach(function(name, index) {
if (typeof values[index] === 'number' && !Number.isNaN(values[index]) && (values[index] % 1 === 0) && values[index] >= 0) {
this.options[name + suffix] = values[index];
} else {
throw new Error('Non-integer value for ' + name + ' of ' + values[index]);
}
}.bind(this));
// Ensure existing rotation occurs before pre-resize extraction
if (suffix === 'Pre' && this.options.angle !== 0) {

View File

@ -1,6 +1,6 @@
{
"name": "sharp",
"version": "0.9.0",
"version": "0.9.1",
"author": "Lovell Fuller <npm@lovell.info>",
"contributors": [
"Pierre Inglebert <pierre.inglebert@gmail.com>",
@ -34,7 +34,7 @@
"vips"
],
"dependencies": {
"bluebird": "^2.8.2",
"bluebird": "^2.9.3",
"color": "^0.7.3",
"nan": "^1.5.1",
"semver": "^4.2.0"

View File

@ -116,4 +116,62 @@ describe('Partial image extraction', function() {
});
});
describe('Invalid parameters', function() {
it('Undefined', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).extract();
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('String top', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Non-integral left', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Negative width - negative', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Null height', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).extract(10, 10, 10, null);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
});
});