-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from courseload/tag-failures
Mark all failing tags with an ID and include it in the message
- Loading branch information
Showing
3 changed files
with
29 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
}; |