Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grid-list): tile being pulled outside the grid if no gap can be found #9128

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/lib/grid-list/grid-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('MatGridList', () => {
GridListWithComplexLayout,
GridListWithFootersWithoutLines,
GridListWithFooterContainingTwoLines,
GridListWithoutMatchingGap,
],
});

Expand Down Expand Up @@ -287,6 +288,15 @@ describe('MatGridList', () => {
expect(getStyle(tile, 'height')).toBe('400px');
});

it('should ensure that all tiles are inside the grid when there are no matching gaps', () => {
const fixture = TestBed.createComponent(GridListWithoutMatchingGap);
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));

fixture.detectChanges();
expect(tiles.every(tile => getComputedLeft(tile) >= 0))
.toBe(true, 'Expected none of the tiles to have a negative `left`');
});

});


Expand All @@ -298,7 +308,7 @@ function getStyle(el: DebugElement, prop: string): string {
function getComputedLeft(element: DebugElement): number {
// While the other properties in this test use `getComputedStyle`, we use `getBoundingClientRect`
// for left because iOS Safari doesn't support using `getComputedStyle` to get the calculated
// `left` balue when using CSS `calc`. We subtract the `left` of the document body because
// `left` value when using CSS `calc`. We subtract the `left` of the document body because
// browsers, by default, add a margin to the body (typically 8px).
let elementRect = element.nativeElement.getBoundingClientRect();
let bodyRect = document.body.getBoundingClientRect();
Expand Down Expand Up @@ -458,3 +468,13 @@ class GridListWithFootersWithoutLines { }
</mat-grid-tile>
</mat-grid-list>`})
class GridListWithFooterContainingTwoLines { }

@Component({template: `
<mat-grid-list cols="5">
<mat-grid-tile [rowspan]="1" [colspan]="3">1</mat-grid-tile>
<mat-grid-tile [rowspan]="2" [colspan]="2">2</mat-grid-tile>
<mat-grid-tile [rowspan]="1" [colspan]="2">3</mat-grid-tile>
<mat-grid-tile [rowspan]="2" [colspan]="2">4</mat-grid-tile>
</mat-grid-list>
`})
class GridListWithoutMatchingGap { }
5 changes: 4 additions & 1 deletion src/lib/grid-list/tile-coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export class TileCoordinator {

// Continue iterating until we find a gap wide enough for this tile.
} while (gapEndIndex - gapStartIndex < tileCols);
return gapStartIndex;

// If we still didn't manage to find a gap, ensure that the index is
// at least zero so the tile doesn't get pulled out of the grid.
return Math.max(gapStartIndex, 0);
}

/** Move "down" to the next row. */
Expand Down