Skip to content

Commit 71d75f4

Browse files
authored
fix(cdk/table): set default role of table to 'table' (#22491)
* fix(cdk/table): set default role of table to 'table' * fixup! fix(cdk/table): set default role of table to 'table'
1 parent c76a09e commit 71d75f4

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/cdk/table/cell.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
ContentChild,
1212
Directive,
1313
ElementRef,
14-
Input,
15-
TemplateRef,
1614
Inject,
15+
Input,
1716
Optional,
17+
TemplateRef,
1818
} from '@angular/core';
1919
import {CanStick, CanStickCtor, mixinHasStickyInput} from './can-stick';
2020
import {CDK_TABLE} from './tokens';
@@ -178,12 +178,17 @@ export class CdkHeaderCell extends BaseCdkCell {
178178
selector: 'cdk-footer-cell, td[cdk-footer-cell]',
179179
host: {
180180
'class': 'cdk-footer-cell',
181-
'role': 'gridcell',
182181
},
183182
})
184183
export class CdkFooterCell extends BaseCdkCell {
185184
constructor(columnDef: CdkColumnDef, elementRef: ElementRef) {
186185
super(columnDef, elementRef);
186+
if (columnDef._table?._elementRef.nativeElement.nodeType === 1) {
187+
const tableRole = columnDef._table._elementRef.nativeElement
188+
.getAttribute('role');
189+
const role = (tableRole === 'grid' || tableRole === 'treegrid') ? 'gridcell' : 'cell';
190+
elementRef.nativeElement.setAttribute('role', role);
191+
}
187192
}
188193
}
189194

@@ -192,11 +197,16 @@ export class CdkFooterCell extends BaseCdkCell {
192197
selector: 'cdk-cell, td[cdk-cell]',
193198
host: {
194199
'class': 'cdk-cell',
195-
'role': 'gridcell',
196200
},
197201
})
198202
export class CdkCell extends BaseCdkCell {
199203
constructor(columnDef: CdkColumnDef, elementRef: ElementRef) {
200204
super(columnDef, elementRef);
205+
if (columnDef._table?._elementRef.nativeElement.nodeType === 1) {
206+
const tableRole = columnDef._table._elementRef.nativeElement
207+
.getAttribute('role');
208+
const role = (tableRole === 'grid' || tableRole === 'treegrid') ? 'gridcell' : 'cell';
209+
elementRef.nativeElement.setAttribute('role', role);
210+
}
201211
}
202212
}

src/cdk/table/table.spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('CdkTable', () => {
108108
});
109109

110110
it('with the right accessibility roles', () => {
111-
expect(tableElement.getAttribute('role')).toBe('grid');
111+
expect(tableElement.getAttribute('role')).toBe('table');
112112

113113
expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
114114
const header = getHeaderRows(tableElement)[0];
@@ -119,7 +119,7 @@ describe('CdkTable', () => {
119119
getRows(tableElement).forEach(row => {
120120
expect(row.getAttribute('role')).toBe('row');
121121
getCells(row).forEach(cell => {
122-
expect(cell.getAttribute('role')).toBe('gridcell');
122+
expect(cell.getAttribute('role')).toBe('cell');
123123
});
124124
});
125125
});
@@ -622,6 +622,19 @@ describe('CdkTable', () => {
622622
it('should not clobber an existing table role', () => {
623623
setupTableTestApp(CustomRoleCdkTableApp);
624624
expect(tableElement.getAttribute('role')).toBe('treegrid');
625+
626+
expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
627+
const header = getHeaderRows(tableElement)[0];
628+
getHeaderCells(header).forEach(cell => {
629+
expect(cell.getAttribute('role')).toBe('columnheader');
630+
});
631+
632+
getRows(tableElement).forEach(row => {
633+
expect(row.getAttribute('role')).toBe('row');
634+
getCells(row).forEach(cell => {
635+
expect(cell.getAttribute('role')).toBe('gridcell');
636+
});
637+
});
625638
});
626639

627640
it('should throw an error if two column definitions have the same name', () => {

src/cdk/table/table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
521521
@Optional() @SkipSelf() @Inject(STICKY_POSITIONING_LISTENER)
522522
protected readonly _stickyPositioningListener: StickyPositioningListener) {
523523
if (!role) {
524-
this._elementRef.nativeElement.setAttribute('role', 'grid');
524+
this._elementRef.nativeElement.setAttribute('role', 'table');
525525
}
526526

527527
this._document = _document;

src/material-experimental/mdc-table/cell.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export class MatHeaderCell extends CdkHeaderCell {}
9090
selector: 'mat-footer-cell, td[mat-footer-cell]',
9191
host: {
9292
'class': 'mat-mdc-footer-cell mdc-data-table__cell',
93-
'role': 'gridcell',
9493
},
9594
})
9695
export class MatFooterCell extends CdkFooterCell {}
@@ -100,7 +99,6 @@ export class MatFooterCell extends CdkFooterCell {}
10099
selector: 'mat-cell, td[mat-cell]',
101100
host: {
102101
'class': 'mat-mdc-cell mdc-data-table__cell',
103-
'role': 'gridcell',
104102
},
105103
})
106104
export class MatCell extends CdkCell {}

0 commit comments

Comments
 (0)