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 a accessibility issue to allow screenreader to read a label when … #6197

Merged
merged 4 commits into from
Mar 31, 2020
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
4 changes: 4 additions & 0 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ <h2>Configuration</h2>
simply <code>true</code>), focusing of the editor is also
disallowed.</dd>

<dt id="option_screenReaderLabel"><code><strong>screenReaderLabel</strong>: string</code></dt>
<dd>This label is read by the screenreaders when CodeMirror text area is focused. This
is helpful for accessibility.</dd>

<dt id="option_showCursorWhenSelecting"><code><strong>showCursorWhenSelecting</strong>: boolean</code></dt>
<dd>Whether the cursor should be drawn when a selection is
active. Defaults to false.</dd>
Expand Down
6 changes: 6 additions & 0 deletions src/edit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export function defineOptions(CodeMirror) {
}
cm.display.input.readOnlyChanged(val)
})

option("screenReaderLabel", null, (cm, val) => {
val = (val === '') ? null : val
cm.display.input.screenReaderLabelChanged(val)
})

option("disableInput", false, (cm, val) => {if (!val) cm.display.input.reset()}, true)
option("dragDrop", true, dragDropChanged)
option("allowDropFileTypes", null)
Expand Down
9 changes: 9 additions & 0 deletions src/input/ContentEditableInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ export default class ContentEditableInput {
on(div, "cut", onCopyCut)
}

screenReaderLabelChanged(label) {
// Label for screenreaders, accessibility
if(label) {
this.div.setAttribute('aria-label', label)
} else {
this.div.removeAttribute('aria-label')
}
}

prepareSelection() {
let result = prepareSelection(this.cm, false)
result.focus = document.activeElement == this.div
Expand Down
11 changes: 10 additions & 1 deletion src/input/TextareaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,21 @@ export default class TextareaInput {

createField(_display) {
// Wraps and hides input textarea
this.wrapper = hiddenTextarea()
this.wrapper = hiddenTextarea(this.cm.options.screenReaderLabel)
// The semihidden textarea that is focused when the editor is
// focused, and receives input.
this.textarea = this.wrapper.firstChild
}

screenReaderLabelChanged(label) {
// Label for screenreaders, accessibility
if(label) {
this.textarea.setAttribute('aria-label', label)
} else {
this.textarea.removeAttribute('aria-label')
}
}

prepareSelection() {
// Redraw the selection and/or cursor
let cm = this.cm, display = cm.display, doc = cm.doc
Expand Down
2 changes: 1 addition & 1 deletion src/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ export function hiddenTextarea() {
if (ios) te.style.border = "1px solid black"
disableBrowserMagic(te)
return div
}
}