Skip to content

Commit a16ca58

Browse files
committed
Handle table navigation in more cases, as per Volker's review
1 parent 20068cc commit a16ca58

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

ts/a11y/explorer/KeyExplorer.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ export class SpeechExplorer
399399
*/
400400
protected pendingIndex: number[] = [];
401401

402+
/**
403+
* The possible types for a "table" cell
404+
*/
405+
protected cellTypes: string[] = ['cell', 'line'];
406+
402407
/********************************************************************/
403408
/*
404409
* The event handlers
@@ -1181,6 +1186,14 @@ export class SpeechExplorer
11811186
return id ? this.node.querySelector(`[data-semantic-id="${id}"]`) : null;
11821187
}
11831188

1189+
/**
1190+
* @param {HTMLElement} node The HTML node whose parent is to be found
1191+
* @returns {HTMLElement} The HTML node of the parent node
1192+
*/
1193+
protected getParent(node: HTMLElement): HTMLElement {
1194+
return this.getNode(this.parentId(node));
1195+
}
1196+
11841197
/**
11851198
* @param {HTMLElement} node The node whose child array we want
11861199
* @returns {string[]} The array of semantic IDs of its children
@@ -1189,13 +1202,31 @@ export class SpeechExplorer
11891202
return node ? node.getAttribute('data-semantic-children').split(/,/) : [];
11901203
}
11911204

1205+
/**
1206+
* @param {HTMLElement} node The node to check for being a cell node
1207+
* @returns {boolean} True if the node is a cell node
1208+
*/
1209+
protected isCell(node: HTMLElement): boolean {
1210+
return (
1211+
!!node && this.cellTypes.includes(node.getAttribute('data-semantic-type'))
1212+
);
1213+
}
1214+
1215+
/**
1216+
* @param {HTMLElement} node The node to check for being a row node
1217+
* @returns {boolean} True if the node is a row node
1218+
*/
1219+
protected isRow(node: HTMLElement): boolean {
1220+
return !!node && node.getAttribute('data-semantic-type') === 'row';
1221+
}
1222+
11921223
/**
11931224
* @param {HTMLElement} node A node that may be in a table cell
11941225
* @returns {HTMLElement} The HTML node for the table cell containing it, or null
11951226
*/
11961227
protected tableCell(node: HTMLElement): HTMLElement {
11971228
while (node && node !== this.node) {
1198-
if (node.getAttribute('data-semantic-role') === 'table') {
1229+
if (this.isCell(node)) {
11991230
return node;
12001231
}
12011232
node = node.parentNode as HTMLElement;
@@ -1204,27 +1235,25 @@ export class SpeechExplorer
12041235
}
12051236

12061237
/**
1207-
* @param {HTMLElement} node An HTML node that is a cell of a table
1238+
* @param {HTMLElement} cell An HTML node that is a cell of a table
12081239
* @returns {HTMLElement} The HTML node for semantic table element containing the cell
12091240
*/
1210-
protected cellTable(node: HTMLElement): HTMLElement {
1211-
while (node && node !== this.node) {
1212-
if (node.getAttribute('data-semantic-type') === 'table') {
1213-
return node;
1214-
}
1215-
node = node.parentNode as HTMLElement;
1216-
}
1217-
return null;
1241+
protected cellTable(cell: HTMLElement): HTMLElement {
1242+
const row = this.getParent(cell);
1243+
return this.isRow(row) ? this.getParent(row) : row;
12181244
}
12191245

12201246
/**
12211247
* @param {HTMLElement} cell The HTML node for a semantic table cell
12221248
* @returns {[number, number]} The row and column numbers for the cell in its table (0-based)
12231249
*/
12241250
protected cellPosition(cell: HTMLElement): [number, number] {
1225-
const row = this.getNode(this.parentId(cell));
1251+
const row = this.getParent(cell);
12261252
const j = this.childArray(row).indexOf(this.nodeId(cell));
1227-
const table = this.getNode(this.parentId(row));
1253+
if (!this.isRow(row)) {
1254+
return [j, 1];
1255+
}
1256+
const table = this.getParent(row);
12281257
const i = this.childArray(table).indexOf(this.nodeId(row));
12291258
return [i, j];
12301259
}
@@ -1237,6 +1266,9 @@ export class SpeechExplorer
12371266
*/
12381267
protected cellAt(table: HTMLElement, i: number, j: number): HTMLElement {
12391268
const row = this.getNode(this.childArray(table)[i]);
1269+
if (!this.isRow(row)) {
1270+
return j === 1 ? row : null;
1271+
}
12401272
const cell = this.getNode(this.childArray(row)[j]);
12411273
return cell;
12421274
}

0 commit comments

Comments
 (0)