Skip to content

Commit 08e9d70

Browse files
westonpacekara
authored andcommitted
fix(icon): add caching of md-icon aria-label (#2649)
Closes #2642
1 parent ad0df31 commit 08e9d70

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/lib/icon/icon.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,21 @@ describe('MdIcon', () => {
333333
expect(mdIconElement.getAttribute('aria-label')).toBe('hand');
334334
});
335335

336+
it('should not set aria label unless it actually changed', () => {
337+
let fixture = TestBed.createComponent(MdIconLigatureTestApp);
338+
339+
const testComponent = fixture.componentInstance;
340+
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
341+
testComponent.iconName = 'home';
342+
343+
fixture.detectChanges();
344+
expect(mdIconElement.getAttribute('aria-label')).toBe('home');
345+
346+
mdIconElement.removeAttribute('aria-label');
347+
fixture.detectChanges();
348+
expect(mdIconElement.getAttribute('aria-label')).toBeFalsy();
349+
});
350+
336351
it('should use alt tag if aria label is not specified', () => {
337352
let fixture = TestBed.createComponent(MdIconLigatureWithAriaBindingTestApp);
338353

src/lib/icon/icon.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
9696

9797
private _previousFontSetClass: string;
9898
private _previousFontIconClass: string;
99+
private _previousAriaLabel: string;
99100

100101
constructor(
101102
private _elementRef: ElementRef,
@@ -176,7 +177,8 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
176177

177178
private _updateAriaLabel() {
178179
const ariaLabel = this._getAriaLabel();
179-
if (ariaLabel) {
180+
if (ariaLabel && ariaLabel !== this._previousAriaLabel) {
181+
this._previousAriaLabel = ariaLabel;
180182
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'aria-label', ariaLabel);
181183
}
182184
}

0 commit comments

Comments
 (0)