Skip to content

Commit 5b03cb2

Browse files
committed
fix(core): fix #4180 by adding validity check for minWidth and maxWidth
- String and number are both allowed. - Valid String value will parse into number properly. - Invalid String value will throw an error.
1 parent ff9de89 commit 5b03cb2

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/js/core/factories/GridColumn.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,23 @@ angular.module('ui.grid')
464464
}
465465
}
466466

467-
self.minWidth = !colDef.minWidth ? 30 : colDef.minWidth;
468-
self.maxWidth = !colDef.maxWidth ? 9000 : colDef.maxWidth;
467+
['minWidth', 'maxWidth'].forEach(function (name) {
468+
var minOrMaxWidth = colDef[name];
469+
var parseErrorMsg = "Cannot parse column " + name + " '" + minOrMaxWidth + "' for column named '" + colDef.name + "'";
470+
471+
if (!angular.isString(minOrMaxWidth) && !angular.isNumber(minOrMaxWidth)) {
472+
//Sets default minWidth and maxWidth values
473+
self[name] = ((name === 'minWidth') ? 30 : 9000);
474+
} else if (angular.isString(minOrMaxWidth)) {
475+
if (minOrMaxWidth.match(/^(\d+)$/)) {
476+
self[name] = parseInt(minOrMaxWidth.match(/^(\d+)$/)[1], 10);
477+
} else {
478+
throw new Error(parseErrorMsg);
479+
}
480+
} else {
481+
self[name] = minOrMaxWidth;
482+
}
483+
});
469484

470485
//use field if it is defined; name if it is not
471486
self.field = (colDef.field === undefined) ? colDef.name : colDef.field;

test/unit/core/factories/GridColumn.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,52 @@ describe('GridColumn factory', function () {
560560
expect(updateCol(colDef.width)).toThrow();
561561
});
562562

563+
it ('should set the value of minWidth to 30 when colDef.minWidth is undefined', invalidMinOrMaxWidthDef(undefined, 'minWidth'));
564+
it ('should set the value of minWidth to 30 when colDef.minWidth is null', invalidMinOrMaxWidthDef(null, 'minWidth'));
565+
it ('should set the value of minWidth to 30 when colDef.minWidth is an object', invalidMinOrMaxWidthDef({}, 'minWidth'));
566+
567+
it ('should set the value of minWidth to the parsed integer colDef.minWidth when it is a string', function () {
568+
colDef.minWidth = '90';
569+
col.updateColumnDef(colDef);
570+
expect(col.minWidth).toBe(90);
571+
});
572+
573+
it ('should set the value of minWidth to colDef.minWidth when it is a number', function () {
574+
colDef.minWidth = 90;
575+
col.updateColumnDef(colDef);
576+
expect(col.minWidth).toBe(90);
577+
});
578+
579+
it ('should throw when colDef.minWidth is an invalid string', function () {
580+
colDef.minWidth = 'e1%';
581+
expect(updateCol(col, colDef)).toThrow();
582+
colDef.minWidth = '#FFF';
583+
expect(updateCol(col, colDef)).toThrow();
584+
});
585+
586+
it ('should set the value of maxWidth to 9000 when colDef.maxWidth is undefined', invalidMinOrMaxWidthDef(undefined, 'maxWidth'));
587+
it ('should set the value of maxWidth to 9000 when colDef.maxWidth is null', invalidMinOrMaxWidthDef(null, 'maxWidth'));
588+
it ('should set the value of maxWidth to 9000 when colDef.maxWidth is an object', invalidMinOrMaxWidthDef({}, 'maxWidth'));
589+
590+
it ('should set the value of maxWidth to the parsed integer colDef.maxWidth when it is a string', function () {
591+
colDef.maxWidth = '200';
592+
col.updateColumnDef(colDef);
593+
expect(col.maxWidth).toBe(200);
594+
});
595+
596+
it ('should set the value of maxWidth to colDef.maxWidth when it is a number', function () {
597+
colDef.maxWidth = 200;
598+
col.updateColumnDef(colDef);
599+
expect(col.maxWidth).toBe(200);
600+
});
601+
602+
it ('should throw when colDef.maxWidth is an invalid string', function () {
603+
colDef.maxWidth = 'e1%';
604+
expect(updateCol(col, colDef)).toThrow();
605+
colDef.maxWidth = '#FFF';
606+
expect(updateCol(col, colDef)).toThrow();
607+
});
608+
563609
function widthEqualsColDefWidth(expected) {
564610
return function () {
565611
colDef.width = expected;
@@ -581,5 +627,13 @@ describe('GridColumn factory', function () {
581627
col.updateColumnDef(colDef);
582628
};
583629
}
630+
631+
function invalidMinOrMaxWidthDef(width, minOrMax) {
632+
return function () {
633+
colDef[minOrMax] = width;
634+
col.updateColumnDef(colDef);
635+
expect(col[minOrMax]).toBe(minOrMax === 'minWidth' ? 30 : 9000);
636+
};
637+
}
584638
});
585639
});

0 commit comments

Comments
 (0)