Skip to content

Commit f4b4b7f

Browse files
annieywwagnermaciel
authored andcommitted
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' (cherry picked from commit 71d75f4)
1 parent 749dcbf commit f4b4b7f

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
@@ -107,7 +107,7 @@ describe('CdkTable', () => {
107107
});
108108

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

112112
expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
113113
const header = getHeaderRows(tableElement)[0];
@@ -118,7 +118,7 @@ describe('CdkTable', () => {
118118
getRows(tableElement).forEach(row => {
119119
expect(row.getAttribute('role')).toBe('row');
120120
getCells(row).forEach(cell => {
121-
expect(cell.getAttribute('role')).toBe('gridcell');
121+
expect(cell.getAttribute('role')).toBe('cell');
122122
});
123123
});
124124
});
@@ -611,6 +611,19 @@ describe('CdkTable', () => {
611611
it('should not clobber an existing table role', () => {
612612
setupTableTestApp(CustomRoleCdkTableApp);
613613
expect(tableElement.getAttribute('role')).toBe('treegrid');
614+
615+
expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
616+
const header = getHeaderRows(tableElement)[0];
617+
getHeaderCells(header).forEach(cell => {
618+
expect(cell.getAttribute('role')).toBe('columnheader');
619+
});
620+
621+
getRows(tableElement).forEach(row => {
622+
expect(row.getAttribute('role')).toBe('row');
623+
getCells(row).forEach(cell => {
624+
expect(cell.getAttribute('role')).toBe('gridcell');
625+
});
626+
});
614627
});
615628

616629
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
@@ -512,7 +512,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
512512
@Optional() @SkipSelf() @Inject(STICKY_POSITIONING_LISTENER)
513513
protected readonly _stickyPositioningListener: StickyPositioningListener) {
514514
if (!role) {
515-
this._elementRef.nativeElement.setAttribute('role', 'grid');
515+
this._elementRef.nativeElement.setAttribute('role', 'table');
516516
}
517517

518518
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)