From 5ced60e02eb55d6b0d154b8dd775c0f3753c0ae5 Mon Sep 17 00:00:00 2001 From: Asa Ayers Date: Tue, 3 Mar 2015 20:13:43 -0500 Subject: [PATCH 1/2] Mark all failing tags with an ID and include it in the message --- .gitignore | 1 + lib/__tests__/index-test.js | 5 ++--- lib/index.js | 41 ++++++++++++++++++++++--------------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 1521c8b..3b9923c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist +node_modules/ diff --git a/lib/__tests__/index-test.js b/lib/__tests__/index-test.js index bc6dbaa..58eb386 100644 --- a/lib/__tests__/index-test.js +++ b/lib/__tests__/index-test.js @@ -1,4 +1,3 @@ -var React = require('react'); var assert = require('assert'); require('../index')(); var assertions = require('../assertions'); @@ -8,7 +7,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; @@ -77,7 +76,7 @@ describe('props', () => {
Foo
; }); }); - + it('warns if there is an image with an empty alt attribute', () => { expectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
; diff --git a/lib/index.js b/lib/index.js index d7ff51b..53a9106 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,42 +1,49 @@ 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) { - 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) { 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); }; }; - From e52b7e5c83409f64e121b156044a62cf5bb8514d Mon Sep 17 00:00:00 2001 From: Asa Ayers Date: Tue, 3 Mar 2015 20:25:49 -0500 Subject: [PATCH 2/2] Make sure props with the id is passed down, even if no props were passed in --- lib/__tests__/index-test.js | 1 + lib/index.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/__tests__/index-test.js b/lib/__tests__/index-test.js index 58eb386..76d1c9e 100644 --- a/lib/__tests__/index-test.js +++ b/lib/__tests__/index-test.js @@ -1,3 +1,4 @@ +var React = require('react'); var assert = require('assert'); require('../index')(); var assertions = require('../assertions'); diff --git a/lib/index.js b/lib/index.js index 53a9106..f1e136b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,8 +33,8 @@ 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 || {}; var failures = assertAccessibility(type, props, children); if (failures.length) { // Generate an id if one doesn't exist @@ -44,6 +44,7 @@ module.exports = (options) => { 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)); }; };