Skip to content

Commit 11f5218

Browse files
committed
fix(core): fix angular-ui#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 11f5218

File tree

2 files changed

+111
-43
lines changed

2 files changed

+111
-43
lines changed

src/js/core/factories/GridColumn.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,22 @@ 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+
self[name] = ((name === 'minWidth') ? 30 : 9000);
473+
} else if (angular.isString(minOrMaxWidth)) {
474+
if (minOrMaxWidth.match(/^(\d+)$/)) {
475+
self[name] = parseInt(minOrMaxWidth.match(/^(\d+)$/)[1], 10);
476+
} else {
477+
throw new Error(parseErrorMsg);
478+
}
479+
} else {
480+
self[name] = minOrMaxWidth;
481+
}
482+
});
469483

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

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

+95-41
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('GridColumn factory', function () {
3030

3131
buildCols();
3232
}));
33-
33+
3434
describe('buildColumns', function () {
3535
it('should not remove existing sort details on a column', function () {
3636
var sort = { priority: 0, direction: 'asc' };
@@ -192,7 +192,7 @@ describe('GridColumn factory', function () {
192192
expect(col.colDef.visible).toBe(false);
193193
});
194194
});
195-
195+
196196
describe('aggregation', function() {
197197
beforeEach( function() {
198198
grid.options.data = [
@@ -202,19 +202,19 @@ describe('GridColumn factory', function () {
202202
{ name: 'matthew', value: 4 },
203203
{ name: 'murray', value: 5 }
204204
];
205-
grid.options.columnDefs = [ {name: 'name'}, {name: 'value'}];
205+
grid.options.columnDefs = [ {name: 'name'}, {name: 'value'}];
206206
});
207-
207+
208208
it('count, with label', function() {
209209
grid.options.columnDefs[0].aggregationType = uiGridConstants.aggregationTypes.count;
210-
210+
211211
buildCols();
212212
grid.modifyRows(grid.options.data);
213213

214-
// this would be called by the footer cell if it were rendered
214+
// this would be called by the footer cell if it were rendered
215215
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[0].updateAggregationValue);
216216
grid.setVisibleRows(grid.rows);
217-
217+
218218
expect(grid.columns[0].getAggregationValue()).toEqual(5);
219219
expect(grid.columns[0].getAggregationText()).toEqual('total rows: ');
220220
deregFn();
@@ -223,29 +223,29 @@ describe('GridColumn factory', function () {
223223
it('count, without label', function() {
224224
grid.options.columnDefs[0].aggregationType = uiGridConstants.aggregationTypes.count;
225225
grid.options.columnDefs[0].aggregationHideLabel = true;
226-
226+
227227
buildCols();
228228
grid.modifyRows(grid.options.data);
229229

230-
// this would be called by the footer cell if it were rendered
230+
// this would be called by the footer cell if it were rendered
231231
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[0].updateAggregationValue);
232232
grid.setVisibleRows(grid.rows);
233-
233+
234234
expect(grid.columns[0].getAggregationValue()).toEqual(5);
235235
expect(grid.columns[0].getAggregationText()).toEqual('');
236236
deregFn();
237237
});
238238

239239
it('sum, with label', function() {
240240
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.sum;
241-
241+
242242
buildCols();
243243
grid.modifyRows(grid.options.data);
244244

245-
// this would be called by the footer cell if it were rendered
245+
// this would be called by the footer cell if it were rendered
246246
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
247247
grid.setVisibleRows(grid.rows);
248-
248+
249249
expect(grid.columns[1].getAggregationValue()).toEqual(15);
250250
expect(grid.columns[1].getAggregationText()).toEqual('total: ');
251251
deregFn();
@@ -254,29 +254,29 @@ describe('GridColumn factory', function () {
254254
it('sum, without label', function() {
255255
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.sum;
256256
grid.options.columnDefs[1].aggregationHideLabel = true;
257-
257+
258258
buildCols();
259259
grid.modifyRows(grid.options.data);
260260

261-
// this would be called by the footer cell if it were rendered
261+
// this would be called by the footer cell if it were rendered
262262
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
263263
grid.setVisibleRows(grid.rows);
264-
264+
265265
expect(grid.columns[1].getAggregationValue()).toEqual(15);
266266
expect(grid.columns[1].getAggregationText()).toEqual('');
267267
deregFn();
268268
});
269269

270270
it('avg, with label', function() {
271271
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.avg;
272-
272+
273273
buildCols();
274274
grid.modifyRows(grid.options.data);
275275

276-
// this would be called by the footer cell if it were rendered
276+
// this would be called by the footer cell if it were rendered
277277
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
278278
grid.setVisibleRows(grid.rows);
279-
279+
280280
expect(grid.columns[1].getAggregationValue()).toEqual(3);
281281
expect(grid.columns[1].getAggregationText()).toEqual('avg: ');
282282
deregFn();
@@ -285,29 +285,29 @@ describe('GridColumn factory', function () {
285285
it('avg, without label', function() {
286286
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.avg;
287287
grid.options.columnDefs[1].aggregationHideLabel = true;
288-
288+
289289
buildCols();
290290
grid.modifyRows(grid.options.data);
291291

292-
// this would be called by the footer cell if it were rendered
292+
// this would be called by the footer cell if it were rendered
293293
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
294294
grid.setVisibleRows(grid.rows);
295-
295+
296296
expect(grid.columns[1].getAggregationValue()).toEqual(3);
297297
expect(grid.columns[1].getAggregationText()).toEqual('');
298298
deregFn();
299-
});
299+
});
300300

301301
it('min, with label', function() {
302302
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.min;
303-
303+
304304
buildCols();
305305
grid.modifyRows(grid.options.data);
306306

307-
// this would be called by the footer cell if it were rendered
307+
// this would be called by the footer cell if it were rendered
308308
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
309309
grid.setVisibleRows(grid.rows);
310-
310+
311311
expect(grid.columns[1].getAggregationValue()).toEqual(1);
312312
expect(grid.columns[1].getAggregationText()).toEqual('min: ');
313313
deregFn();
@@ -316,29 +316,29 @@ describe('GridColumn factory', function () {
316316
it('min, without label', function() {
317317
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.min;
318318
grid.options.columnDefs[1].aggregationHideLabel = true;
319-
319+
320320
buildCols();
321321
grid.modifyRows(grid.options.data);
322322

323-
// this would be called by the footer cell if it were rendered
323+
// this would be called by the footer cell if it were rendered
324324
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
325325
grid.setVisibleRows(grid.rows);
326-
326+
327327
expect(grid.columns[1].getAggregationValue()).toEqual(1);
328328
expect(grid.columns[1].getAggregationText()).toEqual('');
329329
deregFn();
330330
});
331331

332332
it('max, with label', function() {
333333
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.max;
334-
334+
335335
buildCols();
336336
grid.modifyRows(grid.options.data);
337337

338-
// this would be called by the footer cell if it were rendered
338+
// this would be called by the footer cell if it were rendered
339339
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
340340
grid.setVisibleRows(grid.rows);
341-
341+
342342
expect(grid.columns[1].getAggregationValue()).toEqual(5);
343343
expect(grid.columns[1].getAggregationText()).toEqual('max: ');
344344
deregFn();
@@ -347,14 +347,14 @@ describe('GridColumn factory', function () {
347347
it('max, without label', function() {
348348
grid.options.columnDefs[1].aggregationType = uiGridConstants.aggregationTypes.max;
349349
grid.options.columnDefs[1].aggregationHideLabel = true;
350-
350+
351351
buildCols();
352352
grid.modifyRows(grid.options.data);
353353

354-
// this would be called by the footer cell if it were rendered
354+
// this would be called by the footer cell if it were rendered
355355
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
356356
grid.setVisibleRows(grid.rows);
357-
357+
358358
expect(grid.columns[1].getAggregationValue()).toEqual(5);
359359
expect(grid.columns[1].getAggregationText()).toEqual('');
360360
deregFn();
@@ -370,7 +370,7 @@ describe('GridColumn factory', function () {
370370
buildCols();
371371
grid.modifyRows(grid.options.data);
372372

373-
// this would be called by the footer cell if it were rendered
373+
// this would be called by the footer cell if it were rendered
374374
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
375375
grid.setVisibleRows(grid.rows);
376376

@@ -389,7 +389,7 @@ describe('GridColumn factory', function () {
389389
buildCols();
390390
grid.modifyRows(grid.options.data);
391391

392-
// this would be called by the footer cell if it were rendered
392+
// this would be called by the footer cell if it were rendered
393393
var deregFn = grid.api.core.on.rowsRendered(null, grid.columns[1].updateAggregationValue);
394394
grid.setVisibleRows(grid.rows);
395395

@@ -408,13 +408,13 @@ describe('GridColumn factory', function () {
408408
{ name: 'matthew', value: 4 },
409409
{ name: 'murray', value: 5 }
410410
];
411-
grid.options.columnDefs = [ {name: 'name'}, {name: 'value'}];
411+
grid.options.columnDefs = [ {name: 'name'}, {name: 'value'}];
412412
});
413-
413+
414414
it('raise event', function() {
415415
var sortChanged = false;
416416
grid.api.core.on.sortChanged( $scope, function(){ sortChanged = true; });
417-
417+
418418
grid.columns[0].unsort();
419419
expect( sortChanged ).toEqual(true);
420420
});
@@ -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(col.updateColumnDef(colDef)).toThrow();
582+
colDef.minWidth = '#FFF';
583+
expect(col.updateColumnDef(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(col.updateColumnDef(colDef)).toThrow();
605+
colDef.maxWidth = '#FFF';
606+
expect(col.updateColumnDef(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
});
585-
});
639+
});

0 commit comments

Comments
 (0)