Skip to content

Commit

Permalink
feat: Autocomplete accessibility features (#5008)
Browse files Browse the repository at this point in the history
* Accessible autocomplete

* remove ace_line_id and fix autocompletion tests
  • Loading branch information
aoyku committed Dec 9, 2022
1 parent 5016e90 commit 3b7bb5e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var HashHandler = require("./keyboard/hash_handler").HashHandler;
var AcePopup = require("./autocomplete/popup").AcePopup;
var getAriaId = require("./autocomplete/popup").getAriaId;
var util = require("./autocomplete/util");
var lang = require("./lib/lang");
var dom = require("./lib/dom");
Expand Down Expand Up @@ -54,6 +55,7 @@ var Autocomplete = function() {
this.popup.autoSelect = this.autoSelect;

this.popup.setData(this.completions.filtered, this.completions.filterText);
this.editor.textInput.setAriaOptions({activeDescendant: getAriaId(this.popup.getRow())});

editor.keyBinding.addKeyboardHandler(this.keyboardHandler);

Expand Down
23 changes: 21 additions & 2 deletions src/autocomplete/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var event = require("../lib/event");
var lang = require("../lib/lang");
var dom = require("../lib/dom");

var getAriaId = function(index) {
return `suggest-aria-id:${index}`;
};

var $singleLineEditor = function(el) {
var renderer = new Renderer(el);

Expand Down Expand Up @@ -45,6 +49,10 @@ var AcePopup = function(parentNode) {
popup.renderer.content.style.cursor = "default";
popup.renderer.setStyle("ace_autocomplete");

// Set aria attributes for the popup
popup.renderer.container.setAttribute("role", "listbox");
popup.renderer.container.setAttribute("aria-label", "Autocomplete suggestions");

popup.setOption("displayIndentGuides", false);
popup.setOption("dragDelay", 150);

Expand Down Expand Up @@ -114,11 +122,21 @@ var AcePopup = function(parentNode) {
var row = popup.getRow();
var t = popup.renderer.$textLayer;
var selected = t.element.childNodes[row - t.config.firstRow];
if (selected !== t.selectedNode && t.selectedNode)
var el = document.activeElement; // Active element is textarea of main editor
if (selected !== t.selectedNode && t.selectedNode) {
dom.removeCssClass(t.selectedNode, "ace_selected");
el.removeAttribute("aria-activedescendant");
t.selectedNode.removeAttribute("id");
}
t.selectedNode = selected;
if (selected)
if (selected) {
dom.addCssClass(selected, "ace_selected");
var ariaId = getAriaId(row);
selected.id = ariaId;
popup.renderer.container.setAttribute("aria-activedescendant", ariaId);
el.setAttribute("aria-activedescendant", ariaId);
selected.setAttribute("aria-label", popup.getData(row).value);
}
});
var hideHoverMarker = function() { setHoverMarker(-1); };
var setHoverMarker = function(row, suppressRedraw) {
Expand Down Expand Up @@ -350,3 +368,4 @@ dom.importCssString(`

exports.AcePopup = AcePopup;
exports.$singleLineEditor = $singleLineEditor;
exports.getAriaId = getAriaId;
8 changes: 4 additions & 4 deletions src/autocomplete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ module.exports = {
editor.renderer.$themeId = "./theme/textmate";

editor.execCommand("insertstring", "a");
checkInnerHTML('<d "ace_line ace_selected"><s "ace_completion-highlight">a</s><s "ace_">rraysort</s><s "ace_completion-meta">local</s></d><d "ace_line"><s "ace_completion-highlight">a</s><s "ace_">looooooooooooooooooooooooooooong_word</s><s "ace_completion-meta">local</s></d>', function() {
checkInnerHTML('<d "ace_line ace_selected" role="option" id="suggest-aria-id:0" aria-label="arraysort"><s "ace_completion-highlight">a</s><s "ace_">rraysort</s><s "ace_completion-meta">local</s></d><d "ace_line" role="option"><s "ace_completion-highlight">a</s><s "ace_">looooooooooooooooooooooooooooong_word</s><s "ace_completion-meta">local</s></d>', function() {
editor.execCommand("insertstring", "rr");
checkInnerHTML('<d "ace_line ace_selected"><s "ace_completion-highlight">arr</s><s "ace_">aysort</s><s "ace_completion-meta">local</s></d>', function() {
checkInnerHTML('<d "ace_line ace_selected" role="option" id="suggest-aria-id:0" aria-label="arraysort"><s "ace_completion-highlight">arr</s><s "ace_">aysort</s><s "ace_completion-meta">local</s></d>', function() {
editor.execCommand("insertstring", "r");
checkInnerHTML('<d "ace_line ace_selected"><s "ace_completion-highlight">arr</s><s "ace_">ayso</s><s "ace_completion-highlight">r</s><s "ace_">t</s><s "ace_completion-meta">local</s></d>', function() {
checkInnerHTML('<d "ace_line ace_selected" role="option" id="suggest-aria-id:0" aria-label="arraysort"><s "ace_completion-highlight">arr</s><s "ace_">ayso</s><s "ace_completion-highlight">r</s><s "ace_">t</s><s "ace_completion-meta">local</s></d>', function() {

editor.onCommandKey(null, 0, 13);
assert.equal(editor.getValue(), "arraysort\narraysort alooooooooooooooooooooooooooooong_word");
editor.execCommand("insertstring", " looooooooooooooooooooooooooooong_");
checkInnerHTML('<d "ace_line ace_selected"><s "ace_">a</s><s "ace_completion-highlight">looooooooooooooooooooooooooooong_</s><s "ace_">word</s><s "ace_completion-meta">local</s></d>', function() {
checkInnerHTML('<d "ace_line ace_selected" role="option" id="suggest-aria-id:0" aria-label="alooooooooooooooooooooooooooooong_word"><s "ace_">a</s><s "ace_completion-highlight">looooooooooooooooooooooooooooong_</s><s "ace_">word</s><s "ace_completion-meta">local</s></d>', function() {
editor.onCommandKey(null, 0, 13);
editor.destroy();
editor.container.remove();
Expand Down
18 changes: 17 additions & 1 deletion src/keyboard/textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ var TextInput = function(parentNode, host) {
// FOCUS
// ie9 throws error if document.activeElement is accessed too soon
try { var isFocused = document.activeElement === text; } catch(e) {}


this.setAriaOptions = function(options) {
if (options.activeDescendant) {
text.setAttribute("aria-haspopup", "true");
text.setAttribute("aria-autocomplete", "list");
text.setAttribute("aria-activedescendant", options.activeDescendant);
} else {
text.setAttribute("aria-haspopup", "false");
text.setAttribute("aria-autocomplete", "both");
text.removeAttribute("aria-activedescendant");
}
if (options.role) {
text.setAttribute("role", options.role);
}
};
this.setAriaOptions({role: "textbox"});

event.addListener(text, "blur", function(e) {
if (ignoreFocusEvents) return;
host.onBlur(e);
Expand Down
1 change: 1 addition & 0 deletions src/layer/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ var Text = function(parentEl) {
lineEl.className = "ace_line_group";
} else {
lineEl.className = "ace_line";
lineEl.setAttribute("role", "option");
}
fragment.push(line);

Expand Down

0 comments on commit 3b7bb5e

Please sign in to comment.