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

fix(a11y): update aria-label of textinput on cursor move #5665

Merged
merged 1 commit into from
Nov 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/keyboard/textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ TextInput= function(parentNode, host) {

numberOfExtraLines = number;
};

this.setAriaLabel = function() {
var ariaLabel = "";
if (host.$textInputAriaLabel) {
ariaLabel += `${host.$textInputAriaLabel}, `;
}
if(host.session) {
var row = host.session.selection.cursor.row;
ariaLabel += nls("text-input.aria-label", "Cursor at row $0", [row + 1]);
}
text.setAttribute("aria-label", ariaLabel);
};

this.setAriaOptions = function(options) {
if (options.activeDescendant) {
text.setAttribute("aria-haspopup", "true");
Expand All @@ -88,19 +101,11 @@ TextInput= function(parentNode, host) {
}
if (options.setLabel) {
text.setAttribute("aria-roledescription", nls("text-input.aria-roledescription", "editor"));
var arialLabel = "";
if (host.$textInputAriaLabel) {
arialLabel += `${host.$textInputAriaLabel}, `;
}
if(host.session) {
var row = host.session.selection.cursor.row;
arialLabel += nls("text-input.aria-label", "Cursor at row $0", [row + 1]);
}
text.setAttribute("aria-label", arialLabel);
this.setAriaLabel();
}
};

this.setAriaOptions({role: "textbox"});
this.setAriaOptions({role: "textbox"});

event.addListener(text, "blur", function(e) {
if (ignoreFocusEvents) return;
Expand Down Expand Up @@ -191,6 +196,9 @@ TextInput= function(parentNode, host) {
// sync value of textarea
resetSelection();
});

// if cursor changes position, we need to update the label with the correct row
host.on("changeSelection", this.setAriaLabel);

// Convert from row,column position to the linear position with respect to the current
// block of lines in the textarea.
Expand Down
19 changes: 18 additions & 1 deletion src/keyboard/textinput_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,24 @@ module.exports = {

let text = editor.container.querySelector(".ace_text-input");
assert.equal(text.getAttribute("aria-label"), "super cool editor, Cursor at row 1");
}
},

"test: text input aria label updated on cursor move": function() {
editor.setValue("line1\nline2\nline3", -1);
editor.setOption('enableKeyboardAccessibility', true);
editor.renderer.$loop._flush();

let text = editor.container.querySelector(".ace_text-input");
assert.equal(text.getAttribute("aria-label"), "Cursor at row 1");

editor.focus();
sendEvent("keydown", {key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}});
sendEvent("keydown", {key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}});
sendEvent("keydown", {key: { code: "ArrowRight", key: "ArrowRight", keyCode: 39}});
editor.renderer.$loop._flush();

assert.equal(text.getAttribute("aria-label"), "Cursor at row 3");
},
};


Expand Down
Loading