Skip to content

Commit 19b2fd5

Browse files
committed
fix(cdk/table): set default role of table to 'table'
1 parent 119684e commit 19b2fd5

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/cdk/table/cell.ts

Lines changed: 12 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,16 @@ 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 role = columnDef._table._elementRef.nativeElement
188+
.getAttribute('role') === 'table' ? 'cell' : 'gridcell';
189+
elementRef.nativeElement.setAttribute('role', role);
190+
}
187191
}
188192
}
189193

@@ -192,11 +196,15 @@ export class CdkFooterCell extends BaseCdkCell {
192196
selector: 'cdk-cell, td[cdk-cell]',
193197
host: {
194198
'class': 'cdk-cell',
195-
'role': 'gridcell',
196199
},
197200
})
198201
export class CdkCell extends BaseCdkCell {
199202
constructor(columnDef: CdkColumnDef, elementRef: ElementRef) {
200203
super(columnDef, elementRef);
204+
if (columnDef._table?._elementRef.nativeElement.nodeType === 1) {
205+
const role = columnDef._table._elementRef.nativeElement
206+
.getAttribute('role') === 'table' ? 'cell' : 'gridcell';
207+
elementRef.nativeElement.setAttribute('role', role);
208+
}
201209
}
202210
}

src/cdk/table/table.spec.ts

Lines changed: 2 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
});

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
@@ -93,7 +93,6 @@ export class MatHeaderCell extends CdkHeaderCell {}
9393
selector: 'mat-footer-cell, td[mat-footer-cell]',
9494
host: {
9595
'class': 'mat-mdc-footer-cell mdc-data-table__cell',
96-
'role': 'gridcell',
9796
},
9897
})
9998
export class MatFooterCell extends CdkFooterCell {}
@@ -103,7 +102,6 @@ export class MatFooterCell extends CdkFooterCell {}
103102
selector: 'mat-cell, td[mat-cell]',
104103
host: {
105104
'class': 'mat-mdc-cell mdc-data-table__cell',
106-
'role': 'gridcell',
107105
},
108106
})
109107
export class MatCell extends CdkCell {}

0 commit comments

Comments
 (0)