| 1 |
/** |
| 2 |
* Copyright (c) 2017 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import * as Strings from 'browser/LocalizableStrings'; |
| 7 |
import { ITerminal, IRenderDebouncer } from 'browser/Types'; |
| 8 |
import { TimeBasedDebouncer } from 'browser/TimeBasedDebouncer'; |
| 9 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 10 |
import { ICoreBrowserService, IRenderService } from 'browser/services/Services'; |
| 11 |
import { IBuffer } from 'common/buffer/Types'; |
| 12 |
import { IInstantiationService } from 'common/services/Services'; |
| 13 |
import { addDisposableListener } from 'vs/base/browser/dom'; |
| 14 |
|
| 15 |
const MAX_ROWS_TO_READ = 20; |
| 16 |
|
| 17 |
const enum BoundaryPosition { |
| 18 |
TOP, |
| 19 |
BOTTOM |
| 20 |
} |
| 21 |
|
| 22 |
// Turn this on to unhide the accessibility tree and display it under |
| 23 |
// (instead of overlapping with) the terminal. |
| 24 |
const DEBUG = false; |
| 25 |
|
| 26 |
export class AccessibilityManager extends Disposable { |
| 27 |
private _debugRootContainer: HTMLElement | undefined; |
| 28 |
private _accessibilityContainer: HTMLElement; |
| 29 |
|
| 30 |
private _rowContainer: HTMLElement; |
| 31 |
private _rowElements: HTMLElement[]; |
| 32 |
private _rowColumns: WeakMap<HTMLElement, number[]> = new WeakMap(); |
| 33 |
|
| 34 |
private _liveRegion: HTMLElement; |
| 35 |
private _liveRegionLineCount: number = 0; |
| 36 |
private _liveRegionDebouncer: IRenderDebouncer; |
| 37 |
|
| 38 |
private _topBoundaryFocusListener: (e: FocusEvent) => void; |
| 39 |
private _bottomBoundaryFocusListener: (e: FocusEvent) => void; |
| 40 |
|
| 41 |
/** |
| 42 |
* This queue has a character pushed to it for keys that are pressed, if the |
| 43 |
* next character added to the terminal is equal to the key char then it is |
| 44 |
* not announced (added to live region) because it has already been announced |
| 45 |
* by the textarea event (which cannot be canceled). There are some race |
| 46 |
* condition cases if there is typing while data is streaming, but this covers |
| 47 |
* the main case of typing into the prompt and inputting the answer to a |
| 48 |
* question (Y/N, etc.). |
| 49 |
*/ |
| 50 |
private _charsToConsume: string[] = []; |
| 51 |
|
| 52 |
private _charsToAnnounce: string = ''; |
| 53 |
|
| 54 |
constructor( |
| 55 |
private readonly _terminal: ITerminal, |
| 56 |
@IInstantiationService instantiationService: IInstantiationService, |
| 57 |
@ICoreBrowserService private readonly _coreBrowserService: ICoreBrowserService, |
| 58 |
@IRenderService private readonly _renderService: IRenderService |
| 59 |
) { |
| 60 |
super(); |
| 61 |
const doc = this._coreBrowserService.mainDocument; |
| 62 |
this._accessibilityContainer = doc.createElement('div'); |
| 63 |
this._accessibilityContainer.classList.add('xterm-accessibility'); |
| 64 |
|
| 65 |
this._rowContainer = doc.createElement('div'); |
| 66 |
this._rowContainer.setAttribute('role', 'list'); |
| 67 |
this._rowContainer.classList.add('xterm-accessibility-tree'); |
| 68 |
this._rowElements = []; |
| 69 |
for (let i = 0; i < this._terminal.rows; i++) { |
| 70 |
this._rowElements[i] = this._createAccessibilityTreeNode(); |
| 71 |
this._rowContainer.appendChild(this._rowElements[i]); |
| 72 |
} |
| 73 |
|
| 74 |
this._topBoundaryFocusListener = e => this._handleBoundaryFocus(e, BoundaryPosition.TOP); |
| 75 |
this._bottomBoundaryFocusListener = e => this._handleBoundaryFocus(e, BoundaryPosition.BOTTOM); |
| 76 |
this._rowElements[0].addEventListener('focus', this._topBoundaryFocusListener); |
| 77 |
this._rowElements[this._rowElements.length - 1].addEventListener('focus', this._bottomBoundaryFocusListener); |
| 78 |
|
| 79 |
this._accessibilityContainer.appendChild(this._rowContainer); |
| 80 |
|
| 81 |
this._liveRegion = doc.createElement('div'); |
| 82 |
this._liveRegion.classList.add('live-region'); |
| 83 |
this._liveRegion.setAttribute('aria-live', 'assertive'); |
| 84 |
this._accessibilityContainer.appendChild(this._liveRegion); |
| 85 |
this._liveRegionDebouncer = this._register(new TimeBasedDebouncer(this._renderRows.bind(this))); |
| 86 |
|
| 87 |
if (!this._terminal.element) { |
| 88 |
throw new Error('Cannot enable accessibility before Terminal.open'); |
| 89 |
} |
| 90 |
|
| 91 |
if (DEBUG) { |
| 92 |
this._accessibilityContainer.classList.add('debug'); |
| 93 |
this._rowContainer.classList.add('debug'); |
| 94 |
|
| 95 |
// Use a `<div class="xterm">` container so that the css will still apply. |
| 96 |
this._debugRootContainer = doc.createElement('div'); |
| 97 |
this._debugRootContainer.classList.add('xterm'); |
| 98 |
|
| 99 |
this._debugRootContainer.appendChild(doc.createTextNode('------start a11y------')); |
| 100 |
this._debugRootContainer.appendChild(this._accessibilityContainer); |
| 101 |
this._debugRootContainer.appendChild(doc.createTextNode('------end a11y------')); |
| 102 |
|
| 103 |
this._terminal.element.insertAdjacentElement('afterend', this._debugRootContainer); |
| 104 |
} else { |
| 105 |
this._terminal.element.insertAdjacentElement('afterbegin', this._accessibilityContainer); |
| 106 |
} |
| 107 |
|
| 108 |
this._register(this._terminal.onResize(e => this._handleResize(e.rows))); |
| 109 |
this._register(this._terminal.onRender(e => this._refreshRows(e.start, e.end))); |
| 110 |
this._register(this._terminal.onScroll(() => this._refreshRows())); |
| 111 |
// Line feed is an issue as the prompt won't be read out after a command is run |
| 112 |
this._register(this._terminal.onA11yChar(char => this._handleChar(char))); |
| 113 |
this._register(this._terminal.onLineFeed(() => this._handleChar('\n'))); |
| 114 |
this._register(this._terminal.onA11yTab(spaceCount => this._handleTab(spaceCount))); |
| 115 |
this._register(this._terminal.onKey(e => this._handleKey(e.key))); |
| 116 |
this._register(this._terminal.onBlur(() => this._clearLiveRegion())); |
| 117 |
this._register(this._renderService.onDimensionsChange(() => this._refreshRowsDimensions())); |
| 118 |
this._register(addDisposableListener(doc, 'selectionchange', () => this._handleSelectionChange())); |
| 119 |
this._register(this._coreBrowserService.onDprChange(() => this._refreshRowsDimensions())); |
| 120 |
|
| 121 |
this._refreshRowsDimensions(); |
| 122 |
this._refreshRows(); |
| 123 |
this._register(toDisposable(() => { |
| 124 |
if (DEBUG) { |
| 125 |
this._debugRootContainer!.remove(); |
| 126 |
} else { |
| 127 |
this._accessibilityContainer.remove(); |
| 128 |
} |
| 129 |
this._rowElements.length = 0; |
| 130 |
})); |
| 131 |
} |
| 132 |
|
| 133 |
private _handleTab(spaceCount: number): void { |
| 134 |
for (let i = 0; i < spaceCount; i++) { |
| 135 |
this._handleChar(' '); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private _handleChar(char: string): void { |
| 140 |
if (this._liveRegionLineCount < MAX_ROWS_TO_READ + 1) { |
| 141 |
if (this._charsToConsume.length > 0) { |
| 142 |
// Have the screen reader ignore the char if it was just input |
| 143 |
const shiftedChar = this._charsToConsume.shift(); |
| 144 |
if (shiftedChar !== char) { |
| 145 |
this._charsToAnnounce += char; |
| 146 |
} |
| 147 |
} else { |
| 148 |
this._charsToAnnounce += char; |
| 149 |
} |
| 150 |
|
| 151 |
if (char === '\n') { |
| 152 |
this._liveRegionLineCount++; |
| 153 |
if (this._liveRegionLineCount === MAX_ROWS_TO_READ + 1) { |
| 154 |
this._liveRegion.textContent += Strings.tooMuchOutput.get(); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private _clearLiveRegion(): void { |
| 161 |
this._liveRegion.textContent = ''; |
| 162 |
this._liveRegionLineCount = 0; |
| 163 |
} |
| 164 |
|
| 165 |
private _handleKey(keyChar: string): void { |
| 166 |
this._clearLiveRegion(); |
| 167 |
// Only add the char if there is no control character. |
| 168 |
if (!/\p{Control}/u.test(keyChar)) { |
| 169 |
this._charsToConsume.push(keyChar); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private _refreshRows(start?: number, end?: number): void { |
| 174 |
this._liveRegionDebouncer.refresh(start, end, this._terminal.rows); |
| 175 |
} |
| 176 |
|
| 177 |
private _renderRows(start: number, end: number): void { |
| 178 |
const buffer: IBuffer = this._terminal.buffer; |
| 179 |
const setSize = buffer.lines.length.toString(); |
| 180 |
for (let i = start; i <= end; i++) { |
| 181 |
const line = buffer.lines.get(buffer.ydisp + i); |
| 182 |
const columns: number[] = []; |
| 183 |
const lineData = line?.translateToString(true, undefined, undefined, columns) || ''; |
| 184 |
const posInSet = (buffer.ydisp + i + 1).toString(); |
| 185 |
const element = this._rowElements[i]; |
| 186 |
if (element) { |
| 187 |
if (lineData.length === 0) { |
| 188 |
element.textContent = '\u00a0'; |
| 189 |
this._rowColumns.set(element, [0, 1]); |
| 190 |
} else { |
| 191 |
element.textContent = lineData; |
| 192 |
this._rowColumns.set(element, columns); |
| 193 |
} |
| 194 |
element.setAttribute('aria-posinset', posInSet); |
| 195 |
element.setAttribute('aria-setsize', setSize); |
| 196 |
this._alignRowWidth(element); |
| 197 |
} |
| 198 |
} |
| 199 |
this._announceCharacters(); |
| 200 |
} |
| 201 |
|
| 202 |
private _announceCharacters(): void { |
| 203 |
if (this._charsToAnnounce.length === 0) { |
| 204 |
return; |
| 205 |
} |
| 206 |
this._liveRegion.textContent += this._charsToAnnounce; |
| 207 |
this._charsToAnnounce = ''; |
| 208 |
} |
| 209 |
|
| 210 |
private _handleBoundaryFocus(e: FocusEvent, position: BoundaryPosition): void { |
| 211 |
const boundaryElement = e.target as HTMLElement; |
| 212 |
const beforeBoundaryElement = this._rowElements[position === BoundaryPosition.TOP ? 1 : this._rowElements.length - 2]; |
| 213 |
|
| 214 |
// Don't scroll if the buffer top has reached the end in that direction |
| 215 |
const posInSet = boundaryElement.getAttribute('aria-posinset'); |
| 216 |
const lastRowPos = position === BoundaryPosition.TOP ? '1' : `${this._terminal.buffer.lines.length}`; |
| 217 |
if (posInSet === lastRowPos) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
// Don't scroll when the last focused item was not the second row (focus is going the other |
| 222 |
// direction) |
| 223 |
if (e.relatedTarget !== beforeBoundaryElement) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
// Remove old boundary element from array |
| 228 |
let topBoundaryElement: HTMLElement; |
| 229 |
let bottomBoundaryElement: HTMLElement; |
| 230 |
if (position === BoundaryPosition.TOP) { |
| 231 |
topBoundaryElement = boundaryElement; |
| 232 |
bottomBoundaryElement = this._rowElements.pop()!; |
| 233 |
this._rowContainer.removeChild(bottomBoundaryElement); |
| 234 |
} else { |
| 235 |
topBoundaryElement = this._rowElements.shift()!; |
| 236 |
bottomBoundaryElement = boundaryElement; |
| 237 |
this._rowContainer.removeChild(topBoundaryElement); |
| 238 |
} |
| 239 |
|
| 240 |
// Remove listeners from old boundary elements |
| 241 |
topBoundaryElement.removeEventListener('focus', this._topBoundaryFocusListener); |
| 242 |
bottomBoundaryElement.removeEventListener('focus', this._bottomBoundaryFocusListener); |
| 243 |
|
| 244 |
// Add new element to array/DOM |
| 245 |
if (position === BoundaryPosition.TOP) { |
| 246 |
const newElement = this._createAccessibilityTreeNode(); |
| 247 |
this._rowElements.unshift(newElement); |
| 248 |
this._rowContainer.insertAdjacentElement('afterbegin', newElement); |
| 249 |
} else { |
| 250 |
const newElement = this._createAccessibilityTreeNode(); |
| 251 |
this._rowElements.push(newElement); |
| 252 |
this._rowContainer.appendChild(newElement); |
| 253 |
} |
| 254 |
|
| 255 |
// Add listeners to new boundary elements |
| 256 |
this._rowElements[0].addEventListener('focus', this._topBoundaryFocusListener); |
| 257 |
this._rowElements[this._rowElements.length - 1].addEventListener('focus', this._bottomBoundaryFocusListener); |
| 258 |
|
| 259 |
// Scroll up |
| 260 |
this._terminal.scrollLines(position === BoundaryPosition.TOP ? -1 : 1); |
| 261 |
|
| 262 |
// Focus new boundary before element |
| 263 |
this._rowElements[position === BoundaryPosition.TOP ? 1 : this._rowElements.length - 2].focus(); |
| 264 |
|
| 265 |
// Prevent the standard behavior |
| 266 |
e.preventDefault(); |
| 267 |
e.stopImmediatePropagation(); |
| 268 |
} |
| 269 |
|
| 270 |
private _handleSelectionChange(): void { |
| 271 |
if (this._rowElements.length === 0) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
const selection = this._coreBrowserService.mainDocument.getSelection(); |
| 276 |
if (!selection) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
if (selection.isCollapsed) { |
| 281 |
// Only do something when the anchorNode is inside the row container. This |
| 282 |
// behavior mirrors what we do with mouse --- if the mouse clicks |
| 283 |
// somewhere outside of the terminal, we don't clear the selection. |
| 284 |
if (this._rowContainer.contains(selection.anchorNode)) { |
| 285 |
this._terminal.clearSelection(); |
| 286 |
} |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
if (!selection.anchorNode || !selection.focusNode) { |
| 291 |
console.error('anchorNode and/or focusNode are null'); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
// Sort the two selection points in document order. |
| 296 |
let begin = { node: selection.anchorNode, offset: selection.anchorOffset }; |
| 297 |
let end = { node: selection.focusNode, offset: selection.focusOffset }; |
| 298 |
if ((begin.node.compareDocumentPosition(end.node) & Node.DOCUMENT_POSITION_PRECEDING) || (begin.node === end.node && begin.offset > end.offset) ) { |
| 299 |
[begin, end] = [end, begin]; |
| 300 |
} |
| 301 |
|
| 302 |
// Clamp begin/end to the inside of the row container. |
| 303 |
if (begin.node.compareDocumentPosition(this._rowElements[0]) & (Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING)) { |
| 304 |
begin = { node: this._rowElements[0].childNodes[0], offset: 0 }; |
| 305 |
} |
| 306 |
if (!this._rowContainer.contains(begin.node)) { |
| 307 |
// This happens when `begin` is below the last row. |
| 308 |
return; |
| 309 |
} |
| 310 |
const lastRowElement = this._rowElements.slice(-1)[0]; |
| 311 |
if (end.node.compareDocumentPosition(lastRowElement) & (Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_PRECEDING)) { |
| 312 |
end = { |
| 313 |
node: lastRowElement, |
| 314 |
offset: lastRowElement.textContent?.length ?? 0 |
| 315 |
}; |
| 316 |
} |
| 317 |
if (!this._rowContainer.contains(end.node)) { |
| 318 |
// This happens when `end` is above the first row. |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
const toRowColumn = ({ node, offset }: typeof begin): {row: number, column: number} | null => { |
| 323 |
// `node` is either the row element or the Text node inside it. |
| 324 |
const rowElement: any = node instanceof Text ? node.parentNode : node; |
| 325 |
let row = parseInt(rowElement?.getAttribute('aria-posinset'), 10) - 1; |
| 326 |
if (isNaN(row)) { |
| 327 |
console.warn('row is invalid. Race condition?'); |
| 328 |
return null; |
| 329 |
} |
| 330 |
|
| 331 |
const columns = this._rowColumns.get(rowElement); |
| 332 |
if (!columns) { |
| 333 |
console.warn('columns is null. Race condition?'); |
| 334 |
return null; |
| 335 |
} |
| 336 |
|
| 337 |
let column = offset < columns.length ? columns[offset] : columns.slice(-1)[0] + 1; |
| 338 |
if (column >= this._terminal.cols) { |
| 339 |
++row; |
| 340 |
column = 0; |
| 341 |
} |
| 342 |
return { |
| 343 |
row, |
| 344 |
column |
| 345 |
}; |
| 346 |
}; |
| 347 |
|
| 348 |
const beginRowColumn = toRowColumn(begin); |
| 349 |
const endRowColumn = toRowColumn(end); |
| 350 |
|
| 351 |
if (!beginRowColumn || !endRowColumn) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
if (beginRowColumn.row > endRowColumn.row || (beginRowColumn.row === endRowColumn.row && beginRowColumn.column >= endRowColumn.column)) { |
| 356 |
// This should not happen unless we have some bugs. |
| 357 |
throw new Error('invalid range'); |
| 358 |
} |
| 359 |
|
| 360 |
this._terminal.select( |
| 361 |
beginRowColumn.column, |
| 362 |
beginRowColumn.row, |
| 363 |
(endRowColumn.row - beginRowColumn.row) * this._terminal.cols - beginRowColumn.column + endRowColumn.column |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
private _handleResize(rows: number): void { |
| 368 |
// Remove bottom boundary listener |
| 369 |
this._rowElements[this._rowElements.length - 1].removeEventListener('focus', this._bottomBoundaryFocusListener); |
| 370 |
|
| 371 |
// Grow rows as required |
| 372 |
for (let i = this._rowContainer.children.length; i < this._terminal.rows; i++) { |
| 373 |
this._rowElements[i] = this._createAccessibilityTreeNode(); |
| 374 |
this._rowContainer.appendChild(this._rowElements[i]); |
| 375 |
} |
| 376 |
// Shrink rows as required |
| 377 |
while (this._rowElements.length > rows) { |
| 378 |
this._rowContainer.removeChild(this._rowElements.pop()!); |
| 379 |
} |
| 380 |
|
| 381 |
// Add bottom boundary listener |
| 382 |
this._rowElements[this._rowElements.length - 1].addEventListener('focus', this._bottomBoundaryFocusListener); |
| 383 |
|
| 384 |
this._refreshRowsDimensions(); |
| 385 |
} |
| 386 |
|
| 387 |
private _createAccessibilityTreeNode(): HTMLElement { |
| 388 |
const element = this._coreBrowserService.mainDocument.createElement('div'); |
| 389 |
element.setAttribute('role', 'listitem'); |
| 390 |
element.tabIndex = -1; |
| 391 |
this._refreshRowDimensions(element); |
| 392 |
return element; |
| 393 |
} |
| 394 |
|
| 395 |
private _refreshRowsDimensions(): void { |
| 396 |
if (!this._renderService.dimensions.css.cell.height) { |
| 397 |
return; |
| 398 |
} |
| 399 |
Object.assign(this._accessibilityContainer.style, { |
| 400 |
width: `${this._renderService.dimensions.css.canvas.width}px`, |
| 401 |
fontSize: `${this._terminal.options.fontSize}px` |
| 402 |
}); |
| 403 |
if (this._rowElements.length !== this._terminal.rows) { |
| 404 |
this._handleResize(this._terminal.rows); |
| 405 |
} |
| 406 |
for (let i = 0; i < this._terminal.rows; i++) { |
| 407 |
this._refreshRowDimensions(this._rowElements[i]); |
| 408 |
this._alignRowWidth(this._rowElements[i]); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
private _refreshRowDimensions(element: HTMLElement): void { |
| 413 |
element.style.height = `${this._renderService.dimensions.css.cell.height}px`; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Scale the width of a row so that each of the character is (mostly) aligned |
| 418 |
* with the actual rendering. This will allow the screen reader to draw |
| 419 |
* selection outline at the correct position. |
| 420 |
* |
| 421 |
* On top of using the "monospace" font and correct font size, the scaling |
| 422 |
* here is necessary to handle characters that are not covered by the font |
| 423 |
* (e.g. CJK). |
| 424 |
*/ |
| 425 |
private _alignRowWidth(element: HTMLElement): void { |
| 426 |
element.style.transform = ''; |
| 427 |
const width = element.getBoundingClientRect().width; |
| 428 |
const lastColumn = this._rowColumns.get(element)?.slice(-1)?.[0]; |
| 429 |
if (!lastColumn) { |
| 430 |
return; |
| 431 |
} |
| 432 |
const targetWidth = lastColumn * this._renderService.dimensions.css.cell.width; |
| 433 |
element.style.transform = `scaleX(${targetWidth / width})`; |
| 434 |
} |
| 435 |
} |