Skip to content

Commit

Permalink
fix(radio): clear aria attributes from host node (#16938)
Browse files Browse the repository at this point in the history
If the `aria-*` attributes are set on the host node via static bindings, we can end up with both the host and the input having the same `aria-label` which might be read out by a screen reader. These changes clear the attributes from the host.

Fixes #16913.
  • Loading branch information
crisbeto authored and jelbourn committed Sep 9, 2019
1 parent 8d12902 commit 237e030
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/material/radio/radio.spec.ts
Expand Up @@ -22,6 +22,7 @@ describe('MatRadio', () => {
InterleavedRadioGroup,
TranscludingWrapper,
RadioButtonWithPredefinedTabindex,
RadioButtonWithPredefinedAriaAttributes,
]
});

Expand Down Expand Up @@ -778,6 +779,18 @@ describe('MatRadio', () => {
expect(radioButtonEl.getAttribute('tabindex')).toBe('-1');
});

it('should remove the aria attributes from the host element', () => {
const predefinedFixture = TestBed.createComponent(RadioButtonWithPredefinedAriaAttributes);
predefinedFixture.detectChanges();

const radioButtonEl =
predefinedFixture.debugElement.query(By.css('.mat-radio-button'))!.nativeElement;

expect(radioButtonEl.hasAttribute('aria-label')).toBe(false);
expect(radioButtonEl.hasAttribute('aria-describedby')).toBe(false);
expect(radioButtonEl.hasAttribute('aria-labelledby')).toBe(false);
});

});

describe('group interspersed with other tags', () => {
Expand Down Expand Up @@ -991,3 +1004,13 @@ class DefaultRadioButton {}
template: `<mat-radio-button color="warn"></mat-radio-button>`
})
class RadioButtonWithColorBinding {}


@Component({
template: `
<mat-radio-button
aria-label="Radio button"
aria-describedby="something"
aria-labelledby="something-else"></mat-radio-button>`
})
class RadioButtonWithPredefinedAriaAttributes {}
3 changes: 3 additions & 0 deletions src/material/radio/radio.ts
Expand Up @@ -346,6 +346,9 @@ const _MatRadioButtonMixinBase:
// Needs to be -1 so the `focus` event still fires.
'[attr.tabindex]': '-1',
'[attr.id]': 'id',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
// Note: under normal conditions focus shouldn't land on this element, however it may be
// programmatically set, for example inside of a focus trap, in this case we want to forward
// the focus to the native element.
Expand Down

0 comments on commit 237e030

Please sign in to comment.