Skip to content

Commit 3ecc1ca

Browse files
committed
Allow negative numbers to be passed to parseHeight function test case
1 parent d20f670 commit 3ecc1ca

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

dist/gridstack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
var height = val;
100100
var heightUnit = 'px';
101101
if (height && _.isString(height)) {
102-
var match = height.match(/^([0-9]*\.[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);
102+
var match = height.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);
103103
if (!match) {
104104
throw new Error('Invalid height');
105105
}

spec/utils-spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@ describe('gridstack utils', function() {
9090
expect(utils.parseHeight('12.3vh')).toEqual(jasmine.objectContaining({height: 12.3, unit: 'vh'}));
9191
expect(utils.parseHeight('12.3vw')).toEqual(jasmine.objectContaining({height: 12.3, unit: 'vw'}));
9292
expect(utils.parseHeight('12.5')).toEqual(jasmine.objectContaining({height: 12.5, unit: 'px'}));
93-
expect(function() { utils.parseHeight('12.5 df'); }).toThrowError('Invalid height');
9493
});
9594

95+
it('should parse negative height value', function() {
96+
expect(utils.parseHeight(-12)).toEqual(jasmine.objectContaining({height: -12, unit: 'px'}));
97+
expect(utils.parseHeight('-12px')).toEqual(jasmine.objectContaining({height: -12, unit: 'px'}));
98+
expect(utils.parseHeight('-12.3px')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'px'}));
99+
expect(utils.parseHeight('-12.3em')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'em'}));
100+
expect(utils.parseHeight('-12.3rem')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'rem'}));
101+
expect(utils.parseHeight('-12.3vh')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'vh'}));
102+
expect(utils.parseHeight('-12.3vw')).toEqual(jasmine.objectContaining({height: -12.3, unit: 'vw'}));
103+
expect(utils.parseHeight('-12.5')).toEqual(jasmine.objectContaining({height: -12.5, unit: 'px'}));
104+
expect(function() { utils.parseHeight('-12.5 df'); }).toThrowError('Invalid height');
105+
});
96106
});
97107
});

0 commit comments

Comments
 (0)