From 3ecc1caab95ed32712ed7f93e0d7d7d252555f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Kremser?= Date: Mon, 29 Feb 2016 23:53:59 -0800 Subject: [PATCH] Allow negative numbers to be passed to parseHeight function test case --- dist/gridstack.js | 2 +- spec/utils-spec.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dist/gridstack.js b/dist/gridstack.js index 6e61887..f537d10 100644 --- a/dist/gridstack.js +++ b/dist/gridstack.js @@ -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'); } diff --git a/spec/utils-spec.js b/spec/utils-spec.js index d383e77..3b2b73b 100644 --- a/spec/utils-spec.js +++ b/spec/utils-spec.js @@ -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'); + }); }); });