Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(aria): properly calculate columnheader and rowheader (#38160)
Adam Gastineaubec07c0e5a87
diff --git a/packages/injected/src/roleUtils.ts b/packages/injected/src/roleUtils.tsindex 582c266f49..12f79efef2 100644--- a/packages/injected/src/roleUtils.ts+++ b/packages/injected/src/roleUtils.ts@@ -174,13 +174,41 @@ const kImplicitRoleByTagName: { [tagName: string]: (e: Element) => AriaRole | nu'TEXTAREA': () => 'textbox','TFOOT': () => 'rowgroup','TH': (e: Element) => {- if (e.getAttribute('scope') === 'col')+ const scope = e.getAttribute('scope');+ if (scope === 'col' || scope === 'colgroup')return 'columnheader';- if (e.getAttribute('scope') === 'row')+ if (scope === 'row' || scope === 'rowgroup')return 'rowheader';- const table = closestCrossShadow(e, 'table');- const role = table ? getExplicitAriaRole(table) : '';- return (role === 'grid' || role === 'treegrid') ? 'gridcell' : 'cell';++ const nextSibling = e.nextElementSibling;+ const prevSibling = e.previousElementSibling;++ const row = !!e.parentElement && elementSafeTagName(e.parentElement) === 'TR' ? e.parentElement : undefined;++ // Chromium/Safari: A TH that is the only cell in a table is not labeling any content, thus it's technically not a header. Do not assign a role.+ // Firefox: Follows the spec and assigns `columnheader`. We prioritize Chrome/Safari semantics.+ if (!nextSibling && !prevSibling) {+ if (row) {+ const table = closestCrossShadow(row, 'table') as HTMLTableElement | undefined;+ // If there's only one row in the table, this TH has no column to head+ if (table && table.rows.length <= 1)+ return null;+ }+ return 'columnheader';+ }++ // Tables are built up incrementally by iterating over them in a particular pattern. In order to emulate this,+ // we check only immediate siblings and occasionally the parent row+ // This doesn't seem to directly follow the spec, but matches Chromium behavior+ // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/accessibility/ax_node_object.cc;l=1585-1623+ if (isHeaderCell(nextSibling) && isHeaderCell(prevSibling))+ return 'columnheader';++ if (isNonEmptyDataCell(nextSibling) || isNonEmptyDataCell(prevSibling))+ return 'rowheader';++ // As long as we didn't exclude it above, it's still a TH, so default to columnheader+ return 'columnheader';},'THEAD': () => 'rowgroup','TIME': () => 'time',@@ -188,6 +216,16 @@ const kImplicitRoleByTagName: { [tagName: string]: (e: Element) => AriaRole | nu'UL': () => 'list',};+function isHeaderCell(element: Element | null): boolean {+ return !!element && elementSafeTagName(element) === 'TH';+}++function isNonEmptyDataCell(element: Element | null): boolean {+ if (!element || elementSafeTagName(element) !== 'TD')+ return false;+ return !!(element.textContent?.trim() || element.children.length > 0);+}+const kPresentationInheritanceParents: { [tagName: string]: string[] } = {'DD': ['DL', 'DIV'],'DIV': ['DL'],diff --git a/tests/assets/axe-core/implicit-role.js b/tests/assets/axe-core/implicit-role.jsindex c02f0e2ed4..2ba324c488 100644--- a/tests/assets/axe-core/implicit-role.js+++ b/tests/assets/axe-core/implicit-role.js@@ -407,6 +407,7 @@ module.exports = [role: 'combobox',},+ // ---------- Table related cases ----------{html: '<table><td id="target"></td></table>',target: '#target',@@ -436,4 +437,94 @@ module.exports = [target: '#target',role: 'columnheader',},+ // All TH in thead row -> columnheader+ {+ html: '<table><thead><tr><th id="target">A</th><th>B</th></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>',+ target: '#target',+ role: 'columnheader',+ },++ {+ html: '<table><thead><tr><th>A</th><th id="target">B</th></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>',+ target: '#target',+ role: 'columnheader',+ },+ // TH with TD sibling in same row -> rowheader+ {+ html: '<table><thead><tr><th id="target">A</th><td>B</td></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>',+ target: '#target',+ role: 'rowheader',+ },+ // TH in tbody with TD sibling -> rowheader+ {+ html: '<table><thead><tr><th>A</th><th>B</th></thead><tbody><tr><th id="target">1</th><td>2</td></tr><tr><td>3</td><td>4</td></tr></tbody></table>',+ target: '#target',+ role: 'rowheader',+ },+ // TH in tbody, second row, with TD sibling -> rowheader+ {+ html: '<table><thead><tr><th>A</th><th>B</th></thead><tbody><tr><th>1</th><td>2</td></tr><tr><th id="target">3</th><td>4</td></tr></tbody></table>',+ target: '#target',+ role: 'rowheader',+ },+ // TH with TD above and below in same column -> rowheader+ {+ html: '<table><thead><tr><th>A</th><th>B</th></thead><tbody><tr><td>5</td><td>6</td></tr><tr><th id="target">1</th><td>2</td></tr><tr><td>3</td><td>4</td></tr></tbody></table>',+ target: '#target',+ role: 'rowheader',+ },+ // TH only cell in row -> no role+ {+ html: '<table><tr><th id="target">Only</th></tr></table>',+ target: '#target',+ role: null,+ },+ // TH only cell in row, but table has multiple rows -> columnheader+ {+ html: '<table><tr><th id="target">Header</th></tr><tr><td>Data</td></tr></table>',+ target: '#target',+ role: 'columnheader',+ },+ // TH surrounded by other TH -> columnheader+ {+ html: '<table><tr><th>A</th><th id="target">B</th><th>C</th></tr></table>',+ target: '#target',+ role: 'columnheader',+ },+ // Row with first cell as TD -> rowheader+ {+ html: '<table><tr><td>A</td><th id="target">B</th><th>C</th></tr></table>',+ target: '#target',+ role: 'rowheader',+ },+ // Row with last cell as TD -> rowheader+ {+ html: '<table><tr><th>A</th><th id="target">B</th><td>C</td></tr></table>',+ target: '#target',+ role: 'rowheader',+ },+ // scope="col" -> columnheader+ {+ html: '<table><tr><th id="target" scope="col">A</th><td>B</td></tr></table>',+ target: '#target',+ role: 'columnheader',+ },+ // scope="row" -> rowheader+ {+ html: '<table><tr><th id="target" scope="row">A</th><th>B</th></tr></table>',+ target: '#target',+ role: 'rowheader',+ },+ // scope="colgroup" -> columnheader+ {+ html: '<table><tr><th id="target" scope="colgroup">A</th><td>B</td></tr></table>',+ target: '#target',+ role: 'columnheader',+ },+ // scope="rowgroup" -> rowheader+ {+ html: '<table><tr><th id="target" scope="rowgroup">A</th><th>B</th></tr></table>',+ target: '#target',+ role: 'rowheader',+ },];