Skip to content

Commit 7800989

Browse files
committed
Allow selection in the a11y tree and sync the selection to terminal
1 parent 2dd4dfd commit 7800989

File tree

5 files changed

+226
-52
lines changed

5 files changed

+226
-52
lines changed

css/xterm.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@
152152
pointer-events: none;
153153
}
154154

155+
.xterm .xterm-accessibility-tree *::selection {
156+
color: transparent;
157+
}
158+
159+
.xterm .xterm-accessibility-tree {
160+
user-select: text;
161+
white-space: pre;
162+
}
163+
155164
.xterm .live-region {
156165
position: absolute;
157166
left: -9999px;

src/browser/AccessibilityManager.ts

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class AccessibilityManager extends Disposable {
2525

2626
private _rowContainer: HTMLElement;
2727
private _rowElements: HTMLElement[];
28+
private _rowColumns: WeakMap<HTMLElement, number[]> = new WeakMap();
2829

2930
private _liveRegion: HTMLElement;
3031
private _liveRegionLineCount: number = 0;
@@ -53,8 +54,15 @@ export class AccessibilityManager extends Disposable {
5354
@IRenderService private readonly _renderService: IRenderService
5455
) {
5556
super();
57+
58+
// Turn this on to unhide the accessibility tree and display it under
59+
// (instead of overlapping with) the terminal.
60+
const debug = false;
61+
5662
this._accessibilityContainer = document.createElement('div');
57-
this._accessibilityContainer.classList.add('xterm-accessibility');
63+
if (!debug) {
64+
this._accessibilityContainer.classList.add('xterm-accessibility');
65+
}
5866

5967
this._rowContainer = document.createElement('div');
6068
this._rowContainer.setAttribute('role', 'list');
@@ -82,7 +90,13 @@ export class AccessibilityManager extends Disposable {
8290
if (!this._terminal.element) {
8391
throw new Error('Cannot enable accessibility before Terminal.open');
8492
}
85-
this._terminal.element.insertAdjacentElement('afterbegin', this._accessibilityContainer);
93+
if (!debug) {
94+
this._terminal.element.insertAdjacentElement('afterbegin', this._accessibilityContainer);
95+
} else {
96+
this._terminal.element.insertAdjacentElement('afterend', this._accessibilityContainer);
97+
this._accessibilityContainer.insertAdjacentText('beforebegin', '-----start-----');
98+
this._accessibilityContainer.insertAdjacentText('afterend', '------end------');
99+
}
86100

87101
this.register(this._terminal.onResize(e => this._handleResize(e.rows)));
88102
this.register(this._terminal.onRender(e => this._refreshRows(e.start, e.end)));
@@ -94,6 +108,7 @@ export class AccessibilityManager extends Disposable {
94108
this.register(this._terminal.onKey(e => this._handleKey(e.key)));
95109
this.register(this._terminal.onBlur(() => this._clearLiveRegion()));
96110
this.register(this._renderService.onDimensionsChange(() => this._refreshRowsDimensions()));
111+
this.register(addDisposableDomListener(document, 'selectionchange', () => this._handleSelectionChange()));
97112

98113
this._screenDprMonitor = new ScreenDprMonitor(window);
99114
this.register(this._screenDprMonitor);
@@ -171,14 +186,18 @@ export class AccessibilityManager extends Disposable {
171186
const buffer: IBuffer = this._terminal.buffer;
172187
const setSize = buffer.lines.length.toString();
173188
for (let i = start; i <= end; i++) {
174-
const lineData = buffer.translateBufferLineToString(buffer.ydisp + i, true);
189+
const line = buffer.lines.get(buffer.ydisp + i);
190+
const columns: number[] = [];
191+
const lineData = line?.translateToString(true, undefined, undefined, columns) || '';
175192
const posInSet = (buffer.ydisp + i + 1).toString();
176193
const element = this._rowElements[i];
177194
if (element) {
178195
if (lineData.length === 0) {
179196
element.innerText = '\u00a0';
197+
this._rowColumns.set(element, [0]);
180198
} else {
181199
element.textContent = lineData;
200+
this._rowColumns.set(element, columns);
182201
}
183202
element.setAttribute('aria-posinset', posInSet);
184203
element.setAttribute('aria-setsize', setSize);
@@ -255,6 +274,103 @@ export class AccessibilityManager extends Disposable {
255274
e.stopImmediatePropagation();
256275
}
257276

277+
private _handleSelectionChange(): void {
278+
if (this._rowElements.length === 0) {
279+
return;
280+
}
281+
282+
const selection = document.getSelection();
283+
if (!selection) {
284+
return;
285+
}
286+
287+
if (selection.isCollapsed) {
288+
// Only do something when the anchorNode is inside the row container. This
289+
// behavior mirrors what we do with mouse --- if the mouse clicks
290+
// somewhere outside of the terminal, we don't clear the selection.
291+
if (this._rowContainer.contains(selection.anchorNode)) {
292+
this._terminal.clearSelection();
293+
}
294+
return;
295+
}
296+
297+
if (!selection.anchorNode || !selection.focusNode) {
298+
console.error('anchorNode and/or focusNode are null');
299+
return;
300+
}
301+
302+
// Sort the two selection points in document order.
303+
let begin = { node: selection.anchorNode, offset: selection.anchorOffset };
304+
let end = { node: selection.focusNode, offset: selection.focusOffset };
305+
if ((begin.node.compareDocumentPosition(end.node) & Node.DOCUMENT_POSITION_PRECEDING) || (begin.node === end.node && begin.offset > end.offset) ) {
306+
[begin, end] = [end, begin];
307+
}
308+
309+
// Clamp begin/end to the inside of the row container.
310+
if (begin.node.compareDocumentPosition(this._rowElements[0]) & (Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING)) {
311+
begin = { node: this._rowElements[0].childNodes[0], offset: 0 };
312+
}
313+
if (!this._rowContainer.contains(begin.node)) {
314+
// This happens when `begin` is below the last row.
315+
return;
316+
}
317+
const lastRowElement = this._rowElements.slice(-1)[0];
318+
if (end.node.compareDocumentPosition(lastRowElement) & (Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_PRECEDING)) {
319+
end = {
320+
node: lastRowElement,
321+
offset: lastRowElement.textContent?.length ?? 0
322+
};
323+
}
324+
if (!this._rowContainer.contains(end.node)) {
325+
// This happens when `end` is above the first row.
326+
return;
327+
}
328+
329+
const toRowColumn = ({ node, offset }: typeof begin): {row: number, column: number} | null => {
330+
// `node` is either the row element or the Text node inside it.
331+
const rowElement: any = node instanceof Text ? node.parentNode : node;
332+
let row = parseInt(rowElement?.getAttribute('aria-posinset'), 10) - 1;
333+
if (isNaN(row)) {
334+
console.warn('row is invalid. Race condition?');
335+
return null;
336+
}
337+
338+
const columns = this._rowColumns.get(rowElement);
339+
if (!columns) {
340+
console.warn('columns is null. Race condition?');
341+
return null;
342+
}
343+
344+
let column = offset < columns.length ? columns[offset] : columns.slice(-1)[0] + 1;
345+
if (column >= this._terminal.cols) {
346+
++row;
347+
column = 0;
348+
}
349+
return {
350+
row,
351+
column
352+
};
353+
};
354+
355+
const beginRowColumn = toRowColumn(begin);
356+
const endRowColumn = toRowColumn(end);
357+
358+
if (!beginRowColumn || !endRowColumn) {
359+
return;
360+
}
361+
362+
if (beginRowColumn.row > endRowColumn.row || (beginRowColumn.row === endRowColumn.row && beginRowColumn.column >= endRowColumn.column)) {
363+
// This should not happen unless we have some bugs.
364+
throw new Error('invalid range');
365+
}
366+
367+
this._terminal.select(
368+
beginRowColumn.column,
369+
beginRowColumn.row,
370+
(endRowColumn.row - beginRowColumn.row) * this._terminal.cols - beginRowColumn.column + endRowColumn.column
371+
);
372+
}
373+
258374
private _handleResize(rows: number): void {
259375
// Remove bottom boundary listener
260376
this._rowElements[this._rowElements.length - 1].removeEventListener('focus', this._bottomBoundaryFocusListener);

src/common/Types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export interface IBufferLine {
245245
clone(): IBufferLine;
246246
getTrimmedLength(): number;
247247
getNoBgTrimmedLength(): number;
248-
translateToString(trimRight?: boolean, startCol?: number, endCol?: number): string;
248+
translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string;
249249

250250
/* direct access to cell attrs */
251251
getWidth(index: number): number;

src/common/buffer/BufferLine.test.ts

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -331,56 +331,75 @@ describe('BufferLine', function(): void {
331331
describe('translateToString with and w\'o trimming', function(): void {
332332
it('empty line', function(): void {
333333
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
334-
assert.equal(line.translateToString(false), ' ');
335-
assert.equal(line.translateToString(true), '');
334+
const columns: number[] = [];
335+
assert.equal(line.translateToString(false, undefined, undefined, columns), ' ');
336+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
337+
assert.equal(line.translateToString(true, undefined, undefined, columns), '');
338+
assert.deepEqual(columns, []);
336339
});
337340
it('ASCII', function(): void {
341+
const columns: number[] = [];
338342
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
339343
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
340344
line.setCell(2, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
341345
line.setCell(4, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
342346
line.setCell(5, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
343-
assert.equal(line.translateToString(false), 'a a aa ');
344-
assert.equal(line.translateToString(true), 'a a aa');
345-
assert.equal(line.translateToString(false, 0, 5), 'a a a');
346-
assert.equal(line.translateToString(false, 0, 4), 'a a ');
347-
assert.equal(line.translateToString(false, 0, 3), 'a a');
348-
assert.equal(line.translateToString(true, 0, 5), 'a a a');
349-
assert.equal(line.translateToString(true, 0, 4), 'a a ');
350-
assert.equal(line.translateToString(true, 0, 3), 'a a');
347+
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a a aa ');
348+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
349+
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a a aa');
350+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5]);
351+
for (const trimRight of [true, false]) {
352+
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a a a');
353+
assert.deepEqual(columns, [0, 1, 2, 3, 4]);
354+
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a a ');
355+
assert.deepEqual(columns, [0, 1, 2, 3]);
356+
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a a');
357+
assert.deepEqual(columns, [0, 1, 2]);
358+
}
351359

352360
});
353361
it('surrogate', function(): void {
362+
const columns: number[] = [];
354363
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
355364
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
356365
line.setCell(2, CellData.fromCharData([1, '𝄞', 1, '𝄞'.charCodeAt(0)]));
357366
line.setCell(4, CellData.fromCharData([1, '𝄞', 1, '𝄞'.charCodeAt(0)]));
358367
line.setCell(5, CellData.fromCharData([1, '𝄞', 1, '𝄞'.charCodeAt(0)]));
359-
assert.equal(line.translateToString(false), 'a 𝄞 𝄞𝄞 ');
360-
assert.equal(line.translateToString(true), 'a 𝄞 𝄞𝄞');
361-
assert.equal(line.translateToString(false, 0, 5), 'a 𝄞 𝄞');
362-
assert.equal(line.translateToString(false, 0, 4), 'a 𝄞 ');
363-
assert.equal(line.translateToString(false, 0, 3), 'a 𝄞');
364-
assert.equal(line.translateToString(true, 0, 5), 'a 𝄞 𝄞');
365-
assert.equal(line.translateToString(true, 0, 4), 'a 𝄞 ');
366-
assert.equal(line.translateToString(true, 0, 3), 'a 𝄞');
368+
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a 𝄞 𝄞𝄞 ');
369+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9]);
370+
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a 𝄞 𝄞𝄞');
371+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5]);
372+
for (const trimRight of [true, false]) {
373+
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a 𝄞 𝄞');
374+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4]);
375+
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a 𝄞 ');
376+
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
377+
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a 𝄞');
378+
assert.deepEqual(columns, [0, 1, 2, 2]);
379+
}
367380
});
368381
it('combining', function(): void {
382+
const columns: number[] = [];
369383
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
370384
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
371385
line.setCell(2, CellData.fromCharData([1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]));
372386
line.setCell(4, CellData.fromCharData([1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]));
373387
line.setCell(5, CellData.fromCharData([1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]));
374-
assert.equal(line.translateToString(false), 'a e\u0301 e\u0301e\u0301 ');
375-
assert.equal(line.translateToString(true), 'a e\u0301 e\u0301e\u0301');
376-
assert.equal(line.translateToString(false, 0, 5), 'a e\u0301 e\u0301');
377-
assert.equal(line.translateToString(false, 0, 4), 'a e\u0301 ');
378-
assert.equal(line.translateToString(false, 0, 3), 'a e\u0301');
379-
assert.equal(line.translateToString(true, 0, 5), 'a e\u0301 e\u0301');
380-
assert.equal(line.translateToString(true, 0, 4), 'a e\u0301 ');
381-
assert.equal(line.translateToString(true, 0, 3), 'a e\u0301');
388+
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a e\u0301 e\u0301e\u0301 ');
389+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9]);
390+
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a e\u0301 e\u0301e\u0301');
391+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4, 5, 5]);
392+
for (const trimRight of [true, false]) {
393+
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a e\u0301 e\u0301');
394+
assert.deepEqual(columns, [0, 1, 2, 2, 3, 4, 4]);
395+
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a e\u0301 ');
396+
assert.deepEqual(columns, [0, 1, 2, 2, 3]);
397+
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a e\u0301');
398+
assert.deepEqual(columns, [0, 1, 2, 2]);
399+
}
382400
});
383401
it('fullwidth', function(): void {
402+
const columns: number[] = [];
384403
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
385404
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
386405
line.setCell(2, CellData.fromCharData([1, '1', 2, '1'.charCodeAt(0)]));
@@ -389,43 +408,55 @@ describe('BufferLine', function(): void {
389408
line.setCell(6, CellData.fromCharData([0, '', 0, 0]));
390409
line.setCell(7, CellData.fromCharData([1, '1', 2, '1'.charCodeAt(0)]));
391410
line.setCell(8, CellData.fromCharData([0, '', 0, 0]));
392-
assert.equal(line.translateToString(false), 'a 1 11 ');
393-
assert.equal(line.translateToString(true), 'a 1 11');
394-
assert.equal(line.translateToString(false, 0, 7), 'a 1 1');
395-
assert.equal(line.translateToString(false, 0, 6), 'a 1 1');
396-
assert.equal(line.translateToString(false, 0, 5), 'a 1 ');
397-
assert.equal(line.translateToString(false, 0, 4), 'a 1');
398-
assert.equal(line.translateToString(false, 0, 3), 'a 1');
399-
assert.equal(line.translateToString(false, 0, 2), 'a ');
400-
assert.equal(line.translateToString(true, 0, 7), 'a 1 1');
401-
assert.equal(line.translateToString(true, 0, 6), 'a 1 1');
402-
assert.equal(line.translateToString(true, 0, 5), 'a 1 ');
403-
assert.equal(line.translateToString(true, 0, 4), 'a 1');
404-
assert.equal(line.translateToString(true, 0, 3), 'a 1');
405-
assert.equal(line.translateToString(true, 0, 2), 'a ');
411+
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a 1 11 ');
412+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7, 9]);
413+
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a 1 11');
414+
assert.deepEqual(columns, [0, 1, 2, 4, 5, 7]);
415+
for (const trimRight of [true, false]) {
416+
assert.equal(line.translateToString(trimRight, 0, 7, columns), 'a 1 1');
417+
assert.deepEqual(columns, [0, 1, 2, 4, 5]);
418+
assert.equal(line.translateToString(trimRight, 0, 6, columns), 'a 1 1');
419+
assert.deepEqual(columns, [0, 1, 2, 4, 5]);
420+
assert.equal(line.translateToString(trimRight, 0, 5, columns), 'a 1 ');
421+
assert.deepEqual(columns, [0, 1, 2, 4]);
422+
assert.equal(line.translateToString(trimRight, 0, 4, columns), 'a 1');
423+
assert.deepEqual(columns, [0, 1, 2]);
424+
assert.equal(line.translateToString(trimRight, 0, 3, columns), 'a 1');
425+
assert.deepEqual(columns, [0, 1, 2]);
426+
assert.equal(line.translateToString(trimRight, 0, 2, columns), 'a ');
427+
assert.deepEqual(columns, [0, 1]);
428+
}
406429
});
407430
it('space at end', function(): void {
431+
const columns: number[] = [];
408432
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]), false);
409433
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
410434
line.setCell(2, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
411435
line.setCell(4, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
412436
line.setCell(5, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
413437
line.setCell(6, CellData.fromCharData([1, ' ', 1, ' '.charCodeAt(0)]));
414-
assert.equal(line.translateToString(false), 'a a aa ');
415-
assert.equal(line.translateToString(true), 'a a aa ');
438+
assert.equal(line.translateToString(false, undefined, undefined, columns), 'a a aa ');
439+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
440+
assert.equal(line.translateToString(true, undefined, undefined, columns), 'a a aa ');
441+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6]);
416442
});
417443
it('should always return some sane value', function(): void {
444+
const columns: number[] = [];
418445
// sanity check - broken line with invalid out of bound null width cells
419446
// this can atm happen with deleting/inserting chars in inputhandler by "breaking"
420447
// fullwidth pairs --> needs to be fixed after settling BufferLine impl
421448
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE]), false);
422-
assert.equal(line.translateToString(false), ' ');
423-
assert.equal(line.translateToString(true), '');
449+
assert.equal(line.translateToString(false, undefined, undefined, columns), ' ');
450+
assert.deepEqual(columns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
451+
assert.equal(line.translateToString(true, undefined, undefined, columns), '');
452+
assert.deepEqual(columns, []);
424453
});
425454
it('should work with endCol=0', () => {
455+
const columns: number[] = [];
426456
const line = new TestBufferLine(10, CellData.fromCharData([DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE]), false);
427457
line.setCell(0, CellData.fromCharData([1, 'a', 1, 'a'.charCodeAt(0)]));
428-
assert.equal(line.translateToString(true, 0, 0), '');
458+
assert.equal(line.translateToString(true, 0, 0, columns), '');
459+
assert.deepEqual(columns, []);
429460
});
430461
});
431462
describe('addCharToCell', () => {

0 commit comments

Comments
 (0)