@@ -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,29 @@ 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 ! ! node && this . cellTypes . includes ( node . getAttribute ( 'data-semantic-type' ) ) ;
1211+ }
1212+
1213+ /**
1214+ * @param {HTMLElement } node The node to check for being a row node
1215+ * @returns {boolean } True if the node is a row node
1216+ */
1217+ protected isRow ( node : HTMLElement ) : boolean {
1218+ return ! ! node && node . getAttribute ( 'data-semantic-type' ) === 'row' ;
1219+ }
1220+
11921221 /**
11931222 * @param {HTMLElement } node A node that may be in a table cell
11941223 * @returns {HTMLElement } The HTML node for the table cell containing it, or null
11951224 */
11961225 protected tableCell ( node : HTMLElement ) : HTMLElement {
11971226 while ( node && node !== this . node ) {
1198- if ( node . getAttribute ( 'data-semantic-role' ) === 'table' ) {
1227+ if ( this . isCell ( node ) ) {
11991228 return node ;
12001229 }
12011230 node = node . parentNode as HTMLElement ;
@@ -1204,27 +1233,25 @@ export class SpeechExplorer
12041233 }
12051234
12061235 /**
1207- * @param {HTMLElement } node An HTML node that is a cell of a table
1236+ * @param {HTMLElement } cell An HTML node that is a cell of a table
12081237 * @returns {HTMLElement } The HTML node for semantic table element containing the cell
12091238 */
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 ;
1239+ protected cellTable ( cell : HTMLElement ) : HTMLElement {
1240+ const row = this . getParent ( cell ) ;
1241+ return this . isRow ( row ) ? this . getParent ( row ) : row ;
12181242 }
12191243
12201244 /**
12211245 * @param {HTMLElement } cell The HTML node for a semantic table cell
12221246 * @returns {[number, number] } The row and column numbers for the cell in its table (0-based)
12231247 */
12241248 protected cellPosition ( cell : HTMLElement ) : [ number , number ] {
1225- const row = this . getNode ( this . parentId ( cell ) ) ;
1249+ const row = this . getParent ( cell ) ;
12261250 const j = this . childArray ( row ) . indexOf ( this . nodeId ( cell ) ) ;
1227- const table = this . getNode ( this . parentId ( row ) ) ;
1251+ if ( ! this . isRow ( row ) ) {
1252+ return [ j , 1 ] ;
1253+ }
1254+ const table = this . getParent ( row ) ;
12281255 const i = this . childArray ( table ) . indexOf ( this . nodeId ( row ) ) ;
12291256 return [ i , j ] ;
12301257 }
@@ -1237,6 +1264,9 @@ export class SpeechExplorer
12371264 */
12381265 protected cellAt ( table : HTMLElement , i : number , j : number ) : HTMLElement {
12391266 const row = this . getNode ( this . childArray ( table ) [ i ] ) ;
1267+ if ( ! this . isRow ( row ) ) {
1268+ return j === 1 ? row : null ;
1269+ }
12401270 const cell = this . getNode ( this . childArray ( row ) [ j ] ) ;
12411271 return cell ;
12421272 }
0 commit comments