1
0

Update AccessifyHTML5 to support main element.

This commit is contained in:
Matthias Mees
2013-05-11 14:44:10 +02:00
parent 1281376681
commit f37663053e

View File

@ -1,14 +1,24 @@
(function($) { (function($) {
// https://github.com/yatil/accessifyhtml5.js // Source: https://github.com/yatil/accessifyhtml5.js
var AccessifyHTML5 = function (defaults) { var AccessifyHTML5 = function (defaults, more_fixes) {
"use strict";
var fixes = { var fixes = {
'article' : { 'role': 'article' }, 'article' : {'role': 'article' },
'aside' : { 'role': 'complementary' }, 'aside' : {'role': 'complementary' },
'nav' : { 'role': 'navigation' }, 'nav' : {'role': 'navigation' },
'output' : { 'aria-live': 'polite' }, 'main' : {'role': 'main' },
'section' : { 'role': 'region' }, 'output' : {'aria-live': 'polite' },
'[required]' : { 'aria-required': 'true' } 'section' : {'role': 'region' },
}; '[required]': {'aria-required': 'true' }
},
fix, elems, attr, value, key, obj, i, mo, by_match, el_label,
ATTR_SECURE = /aria-[a-z]+|role|tabindex|title|alt|data-[\w\-]+|lang|style|maxlength|placeholder|pattern|type/,
ID_PREFIX = "acfy-id-",
n_label = 0,
Doc = document;
if (Doc.querySelectorAll) {
if (defaults) { if (defaults) {
if (defaults.header) { if (defaults.header) {
@ -19,20 +29,68 @@
if (defaults.footer) { if (defaults.footer) {
fixes[defaults.footer] = { fixes[defaults.footer] = {
'role': 'contentinfo' 'role': 'contentinfo'
} };
} }
if (defaults.main) { if (defaults.main) {
fixes[defaults.main] = { fixes[defaults.main] = {
'role': 'main' 'role': 'main'
} };
fixes.main = {
'role': ''
};
} }
} }
$.each(fixes, for (mo in more_fixes) {
function(index, item) { fixes[mo] = more_fixes[mo];
$(index).attr(item); }
for (fix in fixes) {
if (fixes.hasOwnProperty(fix)) {
elems = Doc.querySelectorAll(fix);
obj = fixes[fix];
for (i = 0; i < elems.length; i++) {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
attr = key;
value = obj[key];
if (!attr.match(ATTR_SECURE)) {
continue;
}
if (!(typeof value).match(/string|number/)) {
continue;
}
by_match = attr.match(/(describ|label)l?edby/);
if (by_match) {
el_label = Doc.querySelector(value);
if (! el_label) { continue; }
if (! el_label.id) {
el_label.id = ID_PREFIX + n_label;
}
value = el_label.id;
attr = "aria-" + ("label" === by_match[1] ? "labelledby" : "describedby");
n_label++;
}
if (!elems[i].hasAttribute(attr)) {
elems[i].setAttribute(attr, value);
}
}
}
}
}
}
} }
);
}; };
AccessifyHTML5({ AccessifyHTML5({