Skip to content

Commit f4cfc2d

Browse files
crisbetokara
authored andcommitted
fix(tabs): infinite loop when selectedIndex is set to NaN (#2389)
1 parent 99963c4 commit f4cfc2d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/lib/tabs/tab-group.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ describe('MdTabGroup', () => {
129129
fixture.detectChanges();
130130
expect(component.selectedIndex).toBe(2);
131131
});
132+
133+
it('should not crash when setting the selected index to NaN', () => {
134+
let component = fixture.debugElement.componentInstance;
135+
136+
expect(() => {
137+
component.selectedIndex = NaN;
138+
fixture.detectChanges();
139+
}).not.toThrow();
140+
});
132141
});
133142

134143
describe('dynamic binding tabs', () => {
@@ -244,19 +253,19 @@ describe('MdTabGroup', () => {
244253
* Checks that the `selectedIndex` has been updated; checks that the label and body have their
245254
* respective `active` classes
246255
*/
247-
function checkSelectedIndex(index: number, fixture: ComponentFixture<any>) {
256+
function checkSelectedIndex(expectedIndex: number, fixture: ComponentFixture<any>) {
248257
fixture.detectChanges();
249258

250259
let tabComponent: MdTabGroup = fixture.debugElement
251260
.query(By.css('md-tab-group')).componentInstance;
252-
expect(tabComponent.selectedIndex).toBe(index);
261+
expect(tabComponent.selectedIndex).toBe(expectedIndex);
253262

254263
let tabLabelElement = fixture.debugElement
255-
.query(By.css(`.md-tab-label:nth-of-type(${index + 1})`)).nativeElement;
264+
.query(By.css(`.md-tab-label:nth-of-type(${expectedIndex + 1})`)).nativeElement;
256265
expect(tabLabelElement.classList.contains('md-tab-label-active')).toBe(true);
257266

258267
let tabContentElement = fixture.debugElement
259-
.query(By.css(`md-tab-body:nth-of-type(${index + 1})`)).nativeElement;
268+
.query(By.css(`md-tab-body:nth-of-type(${expectedIndex + 1})`)).nativeElement;
260269
expect(tabContentElement.classList.contains('md-tab-body-active')).toBe(true);
261270
}
262271

src/lib/tabs/tab-group.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export class MdTabGroup {
116116
* a new selected tab should transition in (from the left or right).
117117
*/
118118
ngAfterContentChecked(): void {
119-
// Clamp the next selected index to the bounds of 0 and the tabs length.
119+
// Clamp the next selected index to the bounds of 0 and the tabs length. Note the `|| 0`, which
120+
// ensures that values like NaN can't get through and which would otherwise throw the
121+
// component into an infinite loop (since Math.max(NaN, 0) === NaN).
120122
this._indexToSelect =
121-
Math.min(this._tabs.length - 1, Math.max(this._indexToSelect, 0));
123+
Math.min(this._tabs.length - 1, Math.max(this._indexToSelect || 0, 0));
122124

123125
// If there is a change in selected index, emit a change event. Should not trigger if
124126
// the selected index has not yet been initialized.

0 commit comments

Comments
 (0)