Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align character position in a11y tree with the actual rendering #4879

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions css/xterm.css
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,16 @@
}

.xterm .xterm-accessibility-tree {
font-family: monospace;
user-select: text;
white-space: pre;
}

.xterm .xterm-accessibility-tree > div {
transform-origin: left;
width: fit-content;
}

.xterm .live-region {
position: absolute;
left: -9999px;
Expand Down
48 changes: 38 additions & 10 deletions src/browser/AccessibilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ export class AccessibilityManager extends Disposable {
@IRenderService private readonly _renderService: IRenderService
) {
super();
this._accessibilityContainer = this._coreBrowserService.mainDocument.createElement('div');
const doc = this._coreBrowserService.mainDocument;
this._accessibilityContainer = doc.createElement('div');
this._accessibilityContainer.classList.add('xterm-accessibility');

this._rowContainer = this._coreBrowserService.mainDocument.createElement('div');
this._rowContainer = doc.createElement('div');
this._rowContainer.setAttribute('role', 'list');
this._rowContainer.classList.add('xterm-accessibility-tree');
this._rowElements = [];
Expand All @@ -75,10 +76,9 @@ export class AccessibilityManager extends Disposable {
this._rowElements[0].addEventListener('focus', this._topBoundaryFocusListener);
this._rowElements[this._rowElements.length - 1].addEventListener('focus', this._bottomBoundaryFocusListener);

this._refreshRowsDimensions();
this._accessibilityContainer.appendChild(this._rowContainer);

this._liveRegion = this._coreBrowserService.mainDocument.createElement('div');
this._liveRegion = doc.createElement('div');
this._liveRegion.classList.add('live-region');
this._liveRegion.setAttribute('aria-live', 'assertive');
this._accessibilityContainer.appendChild(this._liveRegion);
Expand All @@ -93,12 +93,12 @@ export class AccessibilityManager extends Disposable {
this._rowContainer.classList.add('debug');

// Use a `<div class="xterm">` container so that the css will still apply.
this._debugRootContainer = document.createElement('div');
this._debugRootContainer = doc.createElement('div');
this._debugRootContainer.classList.add('xterm');

this._debugRootContainer.appendChild(document.createTextNode('------start a11y------'));
this._debugRootContainer.appendChild(doc.createTextNode('------start a11y------'));
this._debugRootContainer.appendChild(this._accessibilityContainer);
this._debugRootContainer.appendChild(document.createTextNode('------end a11y------'));
this._debugRootContainer.appendChild(doc.createTextNode('------end a11y------'));

this._terminal.element.insertAdjacentElement('afterend', this._debugRootContainer);
} else {
Expand All @@ -115,9 +115,10 @@ export class AccessibilityManager extends Disposable {
this.register(this._terminal.onKey(e => this._handleKey(e.key)));
this.register(this._terminal.onBlur(() => this._clearLiveRegion()));
this.register(this._renderService.onDimensionsChange(() => this._refreshRowsDimensions()));
this.register(addDisposableDomListener(document, 'selectionchange', () => this._handleSelectionChange()));
this.register(addDisposableDomListener(doc, 'selectionchange', () => this._handleSelectionChange()));
this.register(this._coreBrowserService.onDprChange(() => this._refreshRowsDimensions()));

this._refreshRowsDimensions();
this._refreshRows();
this.register(toDisposable(() => {
if (DEBUG) {
Expand Down Expand Up @@ -192,6 +193,7 @@ export class AccessibilityManager extends Disposable {
}
element.setAttribute('aria-posinset', posInSet);
element.setAttribute('aria-setsize', setSize);
this._alignRowWidth(element);
}
}
this._announceCharacters();
Expand Down Expand Up @@ -270,7 +272,7 @@ export class AccessibilityManager extends Disposable {
return;
}

const selection = document.getSelection();
const selection = this._coreBrowserService.mainDocument.getSelection();
if (!selection) {
return;
}
Expand Down Expand Up @@ -389,19 +391,45 @@ export class AccessibilityManager extends Disposable {
this._refreshRowDimensions(element);
return element;
}

private _refreshRowsDimensions(): void {
if (!this._renderService.dimensions.css.cell.height) {
return;
}
this._accessibilityContainer.style.width = `${this._renderService.dimensions.css.canvas.width}px`;
Object.assign(this._accessibilityContainer.style, {
width: `${this._renderService.dimensions.css.canvas.width}px`,
fontSize: `${this._terminal.options.fontSize}px`
});
if (this._rowElements.length !== this._terminal.rows) {
this._handleResize(this._terminal.rows);
}
for (let i = 0; i < this._terminal.rows; i++) {
this._refreshRowDimensions(this._rowElements[i]);
this._alignRowWidth(this._rowElements[i]);
}
}

private _refreshRowDimensions(element: HTMLElement): void {
element.style.height = `${this._renderService.dimensions.css.cell.height}px`;
}

/**
* Scale the width of a row so that each of the character is (mostly) aligned
* with the actual rendering. This will allow the screen reader to draw
* selection outline at the correct position.
*
* On top of using the "monospace" font and correct font size, the scaling
* here is necessary to handle characters that are not covered by the font
* (e.g. CJK).
*/
private _alignRowWidth(element: HTMLElement): void {
element.style.transform = '';
const width = element.getBoundingClientRect().width;
const lastColumn = this._rowColumns.get(element)?.slice(-1)?.[0];
if (!lastColumn) {
return;
}
const targetWidth = lastColumn * this._renderService.dimensions.css.cell.width;
element.style.transform = `scaleX(${targetWidth / width})`;
}
}