Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #19 from nfreear/master
Browse files Browse the repository at this point in the history
Return a hash of success/warning/error messages to aid debugging, and modify accessifyhtml5.js in parallel with JSON Schema,
  • Loading branch information
yatil committed Jul 25, 2013
2 parents 961c60d + 652ecc9 commit f16f10d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
55 changes: 46 additions & 9 deletions accessifyhtml5.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var AccessifyHTML5 = function (defaults, more_fixes) {
'section' : {'role': 'region' },
'[required]': {'aria-required': 'true' }
},
result = { ok:[], warn:[], fail:[] },
error = result.fail,
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/,
ATTR_SECURE = /aria-[a-z]+|role|tabindex|title|alt|data-[\w\-]+|lang|style|maxlength|placeholder|pattern|type|target|accesskey|longdesc/,
ID_PREFIX = "acfy-id-",
n_label = 0,
Doc = document;
Expand Down Expand Up @@ -46,17 +48,33 @@ var AccessifyHTML5 = function (defaults, more_fixes) {
}
}

for (mo in more_fixes) {
fixes[mo] = more_fixes[mo];
// Either replace fixes...
if (more_fixes && more_fixes._CONFIG_
&& more_fixes._CONFIG_.ignore_defaults) {
fixes = more_fixes;
} else {
// ..Or concatenate - the default.
for (mo in more_fixes) {
fixes[mo] = more_fixes[mo];
}
}

for (fix in fixes) {
if (fixes.hasOwnProperty(fix)) {

//Question: should we catch and report (or ignore) bad selector syntax?
elems = Doc.querySelectorAll(fix);
try {
elems = Doc.querySelectorAll(fix);
} catch (ex) {
error.push({ sel:fix, attr:null, val:null,
msg:"Invalid syntax for `document.querySelectorAll` function", ex:ex });
}
obj = fixes[fix];

if (!elems || elems.length < 1) {
result.warn.push({ sel:fix, attr:null, val:null, msg:"Not found" });
}

for (i = 0; i < elems.length; i++) {

for (key in obj) {
Expand All @@ -65,21 +83,33 @@ var AccessifyHTML5 = function (defaults, more_fixes) {
attr = key;
value = obj[key];

if (attr.match(/_?note/)) { // Ignore notes/comments.
continue;
}

if (!attr.match(ATTR_SECURE)) {
//? console.log("Warning: attribute not allowed, ignoring: "+ attr); //Warn?
error.push({ sel:fix, attr:attr, val:null, msg:"Attribute not allowed", re:ATTR_SECURE });
continue;
}
if (!(typeof value).match(/string|number/)) {
//? console.log("Warning: value-type not allowed, ignoring: "+ typeof value); //Warn?
error.push({ sel:fix, attr:attr, val:value, msg:"Value-type not allowed" });
continue;
}

// Connect up 'aria-labelledby'. //Question: do we accept poor spelling/ variations?
by_match = attr.match(/(describ|label)l?edby/);
if (by_match) {
el_label = Doc.querySelector(value); //Not: elems[i].querySel()
try {
el_label = Doc.querySelector(value); //Not: elems[i].querySel()
} catch (ex) {
error.push({ sel:fix, attr:attr, val:value,
msg:"Invalid selector syntax (2) - see 'val'", ex:ex });
}

if (! el_label) { continue; /* Warn? */ }
if (! el_label) {
error.push({ sel:fix, attr:attr, val:value, msg:"Labelledby ref not found - see 'val'" });
continue;
}

if (! el_label.id) {
el_label.id = ID_PREFIX + n_label;
Expand All @@ -93,8 +123,12 @@ var AccessifyHTML5 = function (defaults, more_fixes) {

if (!elems[i].hasAttribute(attr)) {
elems[i].setAttribute(attr, value);
}

result.ok.push({ sel:fix, attr:attr, val:value, msg:"Added" });
}
else {
result.warn.push({ sel:fix, attr:attr, val:value, msg:"Already present, skipped" });
}
}
}

Expand All @@ -103,4 +137,7 @@ var AccessifyHTML5 = function (defaults, more_fixes) {
}
} //End: for (fix in fixes)
}
result.input = fixes;

return result;
};
6 changes: 4 additions & 2 deletions accessifyhtml5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/accessify-qutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var q = {
///ex_ID: 'acfy-id-0',

// Utilities: these need to exist after - see ::tearDown().
log: function (s) {
if (typeof console === 'object') console.log(arguments.length > 1 ? arguments : s)
},
select: function (selector) {
return document.querySelector(selector);
},
Expand Down
7 changes: 6 additions & 1 deletion test/bjs2.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<script>
q.onload(function () {

AccessifyHTML5(false, {
q.res = AccessifyHTML5(false, {
'#ou-org-header': {
role: 'banner',
'aria-label': 'Site logo',
Expand All @@ -44,6 +44,7 @@
"aria-describedby": "#ou-org-footer p"
},

// HTML5 form tests.
"input#fmtest": {
"type": "email",
"placeholder": "[email protected]",
Expand All @@ -52,6 +53,9 @@
"style": "cursor:help"
},

// Error tests.
"body:first": {},

// Security tests.
"body": {
"onload": "alert('Woops!')",
Expand All @@ -63,6 +67,7 @@
}
});

q.log(q.res);
});
</script>

Expand Down

0 comments on commit f16f10d

Please sign in to comment.