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

Mark all failing tags with an ID and include it in the message #14

Merged
merged 3 commits into from May 13, 2015
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
node_modules/
4 changes: 2 additions & 2 deletions lib/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var k = () => {};
var captureWarnings = (fn) => {
var _warn = console.warn;
var msgs = {};
console.warn = (msg) => msgs[msg] = true;
console.warn = (id, msg) => msgs[msg] = true;
fn();
console.warn = _warn;
return msgs;
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('props', () => {
<div onClick={k}><img src="#" alt="Foo"/></div>;
});
});

it('warns if there is an image with an empty alt attribute', () => {
expectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
<div onClick={k}><img src="#" alt=""/></div>;
Expand Down
45 changes: 27 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
var React = require('react');
var assertions = require('./assertions');

var assertAccessibility = (tagName, props, children, log) => {
var assertAccessibility = (tagName, props, children) => {
var key;
var failures = [];

var tagTests = assertions.tags[tagName];
if (tagTests)
for (key in tagTests)
log(tagTests[key].test(tagName, props, children), tagTests[key].msg);
var tagTests = assertions.tags[tagName] || [];
for (key in tagTests)
if (tagTests[key] && !tagTests[key].test(tagName, props, children))
failures.push(tagTests[key].msg);

var propTests;
for (var propName in props) {
if (props[propName] === null || props[propName] === undefined) continue;
propTests = assertions.props[propName];
if (propTests)
for (key in propTests)
log(propTests[key].test(tagName, props, children), propTests[key].msg);
propTests = assertions.props[propName] || [];
for (key in propTests)
if (propTests[key] && !propTests[key].test(tagName, props, children))
failures.push(propTests[key].msg);
}
return failures;
};

var error = (passed, msg) => {
if (!passed)
throw new Error(msg);
var error = (id, msg) => {
throw new Error('#' + id + ": " + msg);
};

var warn = (passed, msg) => {
if (!passed)
console.warn(msg);
var warn = (id, msg) => {
console.warn('#' + id, msg);
};

var nextId = 0;
module.exports = (options) => {
var _createElement = React.createElement;
var log = options && options.throw ? error : warn;
React.createElement = function (type, _props, ...children) {
var props = _props || {};
if (typeof type === 'string') {
var props = _props || {};
assertAccessibility(type, props, children, log);
var failures = assertAccessibility(type, props, children);
if (failures.length) {
// Generate an id if one doesn't exist
props.id = (props.id || 'a11y-' + nextId++);

for (var i = 0; i < failures.length; i++)
log(props.id, failures[i]);
}
}
return _createElement.apply(this, arguments);
// make sure props with the id is passed down, even if no props were passed in.
return _createElement.apply(this, [type, props].concat(children));
};
};