Skip to content

Commit 6ff93a8

Browse files
authored
fix(a11y): update aria-label of textinput on cursor move (#5665)
1 parent 25d3fee commit 6ff93a8

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/keyboard/textinput.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ TextInput= function(parentNode, host) {
7373

7474
numberOfExtraLines = number;
7575
};
76+
77+
this.setAriaLabel = function() {
78+
var ariaLabel = "";
79+
if (host.$textInputAriaLabel) {
80+
ariaLabel += `${host.$textInputAriaLabel}, `;
81+
}
82+
if(host.session) {
83+
var row = host.session.selection.cursor.row;
84+
ariaLabel += nls("text-input.aria-label", "Cursor at row $0", [row + 1]);
85+
}
86+
text.setAttribute("aria-label", ariaLabel);
87+
};
88+
7689
this.setAriaOptions = function(options) {
7790
if (options.activeDescendant) {
7891
text.setAttribute("aria-haspopup", "true");
@@ -88,19 +101,11 @@ TextInput= function(parentNode, host) {
88101
}
89102
if (options.setLabel) {
90103
text.setAttribute("aria-roledescription", nls("text-input.aria-roledescription", "editor"));
91-
var arialLabel = "";
92-
if (host.$textInputAriaLabel) {
93-
arialLabel += `${host.$textInputAriaLabel}, `;
94-
}
95-
if(host.session) {
96-
var row = host.session.selection.cursor.row;
97-
arialLabel += nls("text-input.aria-label", "Cursor at row $0", [row + 1]);
98-
}
99-
text.setAttribute("aria-label", arialLabel);
104+
this.setAriaLabel();
100105
}
101106
};
102107

103-
this.setAriaOptions({role: "textbox"});
108+
this.setAriaOptions({role: "textbox"});
104109

105110
event.addListener(text, "blur", function(e) {
106111
if (ignoreFocusEvents) return;
@@ -191,6 +196,9 @@ TextInput= function(parentNode, host) {
191196
// sync value of textarea
192197
resetSelection();
193198
});
199+
200+
// if cursor changes position, we need to update the label with the correct row
201+
host.on("changeSelection", this.setAriaLabel);
194202

195203
// Convert from row,column position to the linear position with respect to the current
196204
// block of lines in the textarea.

src/keyboard/textinput_test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,24 @@ module.exports = {
789789

790790
let text = editor.container.querySelector(".ace_text-input");
791791
assert.equal(text.getAttribute("aria-label"), "super cool editor, Cursor at row 1");
792-
}
792+
},
793+
794+
"test: text input aria label updated on cursor move": function() {
795+
editor.setValue("line1\nline2\nline3", -1);
796+
editor.setOption('enableKeyboardAccessibility', true);
797+
editor.renderer.$loop._flush();
798+
799+
let text = editor.container.querySelector(".ace_text-input");
800+
assert.equal(text.getAttribute("aria-label"), "Cursor at row 1");
801+
802+
editor.focus();
803+
sendEvent("keydown", {key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}});
804+
sendEvent("keydown", {key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}});
805+
sendEvent("keydown", {key: { code: "ArrowRight", key: "ArrowRight", keyCode: 39}});
806+
editor.renderer.$loop._flush();
807+
808+
assert.equal(text.getAttribute("aria-label"), "Cursor at row 3");
809+
},
793810
};
794811

795812

0 commit comments

Comments
 (0)