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(cdk/table): set explicit role on all cells #29987

Merged
merged 1 commit into from
Nov 11, 2024
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
3 changes: 1 addition & 2 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ describe('CdkTable', () => {
getRows(tableElement).forEach(row => {
expect(row.getAttribute('role')).toBe('row');
getCells(row).forEach(cell => {
// Native role of TD elements is row.
expect(cell.getAttribute('role')).toBe(null);
expect(cell.getAttribute('role')).toBe('cell');
});
});
});
Expand Down
9 changes: 5 additions & 4 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,12 @@ export class CdkTable<T>

/** Aria role to apply to the table's cells based on the table's own role. */
_getCellRole(): string | null {
// Perform this lazily in case the table's role was updated by a directive after construction.
if (this._cellRoleInternal === undefined) {
// Perform this lazily in case the table's role was updated by a directive after construction.
const role = this._elementRef.nativeElement.getAttribute('role');
const cellRole = role === 'grid' || role === 'treegrid' ? 'gridcell' : 'cell';
this._cellRoleInternal = this._isNativeHtmlTable && cellRole === 'cell' ? null : cellRole;
// Note that we set `role="cell"` even on native `td` elements,
// because some browsers seem to require it. See #29784.
const tableRole = this._elementRef.nativeElement.getAttribute('role');
return tableRole === 'grid' || tableRole === 'treegrid' ? 'gridcell' : 'cell';
}

return this._cellRoleInternal;
Expand Down
Loading