Skip to content

Commit

Permalink
Allow negative numbers to be passed to parseHeight function test case
Browse files Browse the repository at this point in the history
  • Loading branch information
krema committed Mar 1, 2016
1 parent d20f670 commit 3ecc1ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
var height = val;
var heightUnit = 'px';
if (height && _.isString(height)) {
var match = height.match(/^([0-9]*\.[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);
var match = height.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);
if (!match) {
throw new Error('Invalid height');
}
Expand Down
12 changes: 11 additions & 1 deletion spec/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,18 @@ describe('gridstack utils', function() {
expect(utils.parseHeight('12.3vh')).toEqual(jasmine.objectContaining({height: 12.3, unit: 'vh'}));
expect(utils.parseHeight('12.3vw')).toEqual(jasmine.objectContaining({height: 12.3, unit: 'vw'}));
expect(utils.parseHeight('12.5')).toEqual(jasmine.objectContaining({height: 12.5, unit: 'px'}));
expect(function() { utils.parseHeight('12.5 df'); }).toThrowError('Invalid height');
});

it('should parse negative height value', function() {
expect(utils.parseHeight(-12)).toEqual(jasmine.objectContaining({height: -12, unit: 'px'}));
expect(utils.parseHeight('-12px')).toEqual(jasmine.objectContaining({height: -12, unit: 'px'}));
expect(utils.parseHeight('-12.3px')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'px'}));
expect(utils.parseHeight('-12.3em')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'em'}));
expect(utils.parseHeight('-12.3rem')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'rem'}));
expect(utils.parseHeight('-12.3vh')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'vh'}));
expect(utils.parseHeight('-12.3vw')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'vw'}));
expect(utils.parseHeight('-12.5')).toEqual(jasmine.objectContaining({height: -12.5, unit: 'px'}));
expect(function() { utils.parseHeight('-12.5 df'); }).toThrowError('Invalid height');
});
});
});

0 comments on commit 3ecc1ca

Please sign in to comment.