Skip to content

Commit 210f3f9

Browse files
crisbetojelbourn
authored andcommitted
fix(grid-list): throw error if invalid value is assigned for rowHeight (#13254)
Currently if an invalid value is set to the grid list's `rowHeight`, we pass it on to the browser which may or may not handle it. These changes will throw an error if an invalid value is passed in, in order to make debugging these kinds of issues easier. Fixes #13252.
1 parent f3bb0c7 commit 210f3f9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/lib/grid-list/grid-list.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,17 @@ describe('MatGridList', () => {
389389
expect(getStyle(tile, 'padding-top')).toBe('200px');
390390
});
391391

392+
it('should throw if an invalid value is set as the `rowHeight`', () => {
393+
const fixture = createComponent(GridListWithUnspecifiedRowHeight);
394+
const gridList = fixture.debugElement.query(By.directive(MatGridList));
395+
396+
expect(() => {
397+
// Note the semicolon at the end which will be an invalid value on some browsers (see #13252).
398+
gridList.componentInstance.rowHeight = '350px;';
399+
fixture.detectChanges();
400+
}).toThrowError(/^Invalid value/);
401+
});
402+
392403
});
393404

394405

src/lib/grid-list/tile-styler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import {MatGridList} from './grid-list';
1010
import {MatGridTile} from './grid-tile';
1111
import {TileCoordinator} from './tile-coordinator';
1212

13+
/**
14+
* RegExp that can be used to check whether a value will
15+
* be allowed inside a CSS `calc()` expression.
16+
*/
17+
const cssCalcAllowedValue = /^-?\d+((\.\d+)?[A-Za-z%$]?)+$/;
18+
1319
/**
1420
* Sets the style properties for an individual tile, given the position calculated by the
1521
* Tile Coordinator.
@@ -162,6 +168,10 @@ export class FixedTileStyler extends TileStyler {
162168
init(gutterSize: string, tracker: TileCoordinator, cols: number, direction: string) {
163169
super.init(gutterSize, tracker, cols, direction);
164170
this.fixedRowHeight = normalizeUnits(this.fixedRowHeight);
171+
172+
if (!cssCalcAllowedValue.test(this.fixedRowHeight)) {
173+
throw Error(`Invalid value "${this.fixedRowHeight}" set as rowHeight.`);
174+
}
165175
}
166176

167177
setRowStyles(tile: MatGridTile, rowIndex: number): void {

0 commit comments

Comments
 (0)