accessibilityjs/index.js

181 строка
5.6 KiB
JavaScript
Исходник Обычный вид История

2017-10-12 13:19:49 +03:00
export function scanForProblems(context, logError, options) {
2017-10-06 05:34:29 +03:00
for (const img of context.querySelectorAll('img')) {
2017-10-06 04:44:08 +03:00
if (!img.hasAttribute('alt')) {
logError(new ImageWithoutAltAttributeError(img))
2017-06-22 16:36:39 +03:00
}
2017-10-06 04:44:08 +03:00
}
2017-06-22 16:36:39 +03:00
2017-10-06 05:34:29 +03:00
for (const a of context.querySelectorAll('a')) {
2017-10-06 04:44:08 +03:00
if (a.hasAttribute('name') || elementIsHidden(a)) {
continue
2017-06-22 16:36:39 +03:00
}
2017-10-06 04:44:08 +03:00
if (a.getAttribute('href') == null && a.getAttribute('role') !== 'button') {
logError(new LinkWithoutLabelOrRoleError(a))
} else if (!accessibleText(a)) {
logError(new ElementWithoutLabelError(a))
2017-06-22 16:36:39 +03:00
}
}
2017-10-06 05:34:29 +03:00
for (const button of context.querySelectorAll('button')) {
2017-10-06 04:44:08 +03:00
if (!elementIsHidden(button) && !accessibleText(button)) {
logError(new ButtonWithoutLabelError(button))
}
2017-06-26 17:58:43 +03:00
}
2017-10-06 05:34:29 +03:00
for (const label of context.querySelectorAll('label')) {
2017-10-06 04:44:08 +03:00
// In case label.control isn't supported by browser, find the control manually (IE)
const control = label.control || document.getElementById(label.getAttribute('for')) || label.querySelector('input')
2017-06-26 17:58:43 +03:00
2017-10-06 04:44:08 +03:00
if (!control && !elementIsHidden(label)) {
logError(new LabelMissingControlError(label))
2017-10-06 04:44:08 +03:00
}
2017-06-22 16:36:39 +03:00
}
2017-06-26 17:58:43 +03:00
for (const input of context.querySelectorAll('input[type=text], input[type=url], input[type=search], input[type=number], textarea')) {
2017-10-06 04:44:08 +03:00
// In case input.labels isn't supported by browser, find the control manually (IE)
const inputLabel = input.labels ? input.labels[0] : input.closest('label') || document.querySelector(`label[for="${input.id}"]`)
2017-10-06 04:44:08 +03:00
if (!inputLabel && !elementIsHidden(input) && !input.hasAttribute('aria-label')) {
logError(new InputMissingLabelError(input))
2017-10-06 04:44:08 +03:00
}
2017-06-26 17:58:43 +03:00
}
2017-10-12 13:19:49 +03:00
if (options && options['ariaPairs']) {
2017-10-13 06:36:32 +03:00
for (const selector in options['ariaPairs']) {
const ARIAAttrsRequired = options['ariaPairs'][selector]
2017-10-12 13:19:49 +03:00
for (const target of context.querySelectorAll(selector)) {
2017-10-13 06:36:32 +03:00
const missingAttrs = []
2017-10-12 13:19:49 +03:00
for (const attr of ARIAAttrsRequired) {
if (!target.hasAttribute(attr)) missingAttrs.push(attr)
}
if (missingAttrs.length > 0) {
logError(new ARIAAttributeMissingError(target, missingAttrs.join(', ')))
}
2017-10-06 04:44:08 +03:00
}
}
}
2017-10-06 04:44:08 +03:00
}
function errorSubclass(fn) {
fn.prototype = new Error()
fn.prototype.constructor = fn
}
function ImageWithoutAltAttributeError(element) {
this.name = 'ImageWithoutAltAttributeError'
this.stack = new Error().stack
this.element = element
2017-10-06 05:34:29 +03:00
this.message = `Missing alt attribute on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(ImageWithoutAltAttributeError)
function ElementWithoutLabelError(element) {
this.name = 'ElementWithoutLabelError'
this.stack = new Error().stack
this.element = element
2017-10-06 05:34:29 +03:00
this.message = `Missing text, title, or aria-label attribute on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(ElementWithoutLabelError)
function LinkWithoutLabelOrRoleError(element) {
this.name = 'LinkWithoutLabelOrRoleError'
this.stack = new Error().stack
this.element = element
2017-10-06 05:34:29 +03:00
this.message = `Missing href or role=button on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(LinkWithoutLabelOrRoleError)
function LabelMissingControlError(element) {
this.name = 'LabelMissingControlError'
2017-10-06 04:44:08 +03:00
this.stack = new Error().stack
this.element = element
2017-10-06 05:34:29 +03:00
this.message = `Label missing control on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(LabelMissingControlError)
function InputMissingLabelError(element) {
this.name = 'InputMissingLabelError'
this.stack = new Error().stack
this.element = element
this.message = `Missing label for or aria-label attribute on ${inspect(element)}`
}
errorSubclass(InputMissingLabelError)
2017-10-06 04:44:08 +03:00
function ButtonWithoutLabelError(element) {
this.name = 'ButtonWithoutLabelError'
this.stack = new Error().stack
this.element = element
2017-10-06 05:34:29 +03:00
this.message = `Missing text or aria-label attribute on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(ButtonWithoutLabelError)
function ARIAAttributeMissingError(element, attr) {
this.name = 'ARIAAttributeMissingError'
this.stack = new Error().stack
this.element = element
2017-10-06 14:17:46 +03:00
this.message = `Missing ${attr} attribute on ${inspect(element)}`
2017-10-06 04:44:08 +03:00
}
errorSubclass(ARIAAttributeMissingError)
function elementIsHidden(element) {
return element.closest('[aria-hidden="true"], [hidden], [style*="display: none"]') != null
}
function isText(value) {
return typeof value === 'string' && !!value.trim()
}
// Public: Check if an element has text visible by sight or screen reader.
//
// Examples
//
// <img alt="github" src="github.png">
// # => true
//
// <span aria-label="Open"></span>
// # => true
//
// <button></button>
// # => false
//
// Returns String text.
function accessibleText(node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
if (isText(node.getAttribute('alt')) || isText(node.getAttribute('aria-label')) || isText(node.getAttribute('title'))) return true
for (let i = 0; i < node.childNodes.length; i++) {
2017-10-06 04:44:08 +03:00
if (accessibleText(node.childNodes[i])) return true
}
2017-10-06 04:44:08 +03:00
break
case Node.TEXT_NODE:
return isText(node.data)
}
}
function inspect(element) {
let tagHTML = element.outerHTML
2017-10-06 04:44:08 +03:00
if (element.innerHTML) tagHTML = tagHTML.replace(element.innerHTML, '...')
const parents = []
2017-10-06 04:44:08 +03:00
while (element) {
if (element.nodeName === 'BODY') break
parents.push(selectors(element))
// Stop traversing when we hit an ID
if (element.id) break
element = element.parentNode
}
2017-10-06 05:34:29 +03:00
return `"${parents.reverse().join(' > ')}". \n\n${tagHTML}`
2017-10-06 04:44:08 +03:00
}
function selectors(element) {
const components = [element.nodeName.toLowerCase()]
if (element.id) components.push(`#${element.id}`)
2017-10-06 04:44:08 +03:00
if (element.classList) {
2017-10-06 05:34:29 +03:00
for (const name of element.classList) {
2017-10-12 17:38:03 +03:00
if (name.match(/^js-/)) components.push(`.${name}`)
}
}
return components.join('')
2017-10-06 04:44:08 +03:00
}