Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 156b8e4

Browse files
committed
Merge pull request gridstack#370 from krema/master
Allow negative numbers to be passed to parseHeight function
2 parents aa18c63 + ff0c00b commit 156b8e4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,19 @@ describe('gridstack utils', function() {
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'}));
9393
expect(function() { utils.parseHeight('12.5 df'); }).toThrowError('Invalid height');
94+
9495
});
9596

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

src/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
}

0 commit comments

Comments
 (0)