Skip to content

Commit

Permalink
inlined most the assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanflorence committed Jan 19, 2015
1 parent a237873 commit 41a5566
Showing 1 changed file with 13 additions and 69 deletions.
82 changes: 13 additions & 69 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,11 @@ var hasAlt = (props) => {
return typeof props.alt === 'string';
};

var altIsRedundant = (alt) => {
return alt.match('image');
};

var isInteractive = (tagName, props) => {
var tag = INTERACTIVE[tagName];
return (typeof tagName === 'function') ? tag(props) : tag;
};

var hasClick = (props) => {
return typeof props.onClick === 'function';
};

var hasRole = (props) => {
return typeof props.role === 'string';
};

var hasTabIndex = (props) => {
return typeof props.tabIndex === 'string';
};

var needsAriaRole = (tagName, props) => {
return (
!isInteractive(tagName, props) &&
hasClick(props) &&
!hasRole(props)
);
};

var needsTabIndex = (tagName, props) => {
return (
!isInteractive(tagName, props) &&
hasClick(props) &&
!hasTabIndex(props)
);
};

var hrefIsHash = (props) => {
return props.href === '#';
};

var needsButtonRole = (props) => {
return (
hasClick(props) &&
hrefIsHash(props)
);
};


var hasLabel = (props) => {
return typeof props['aria-label'] === 'string';
};

var hasLabelledBy = (props) => {
return typeof props['aria-labelled-by'] === 'string';
};

var hasChildTextNode = (props, children) => {
var hasText = false;
React.Children.forEach(children, (child) => {
Expand All @@ -81,37 +29,33 @@ var hasChildTextNode = (props, children) => {
return hasText;
};

var needsLabel = (props, children) => {
return (
hasClick(props) &&
!(
hasLabel(props) ||
hasLabelledBy(props) ||
hasChildTextNode(props, children)
)
);
};

exports.props = {
onClick: {
NO_ROLE: {
msg: 'You have a click handler on a non-interactive element but no `role` DOM property. It will be unclear what this element is supposed to do to a screen-reader user. http://www.w3.org/TR/wai-aria/roles#role_definitions',
test (tagName, props, children) {
return !needsAriaRole(tagName, props);
return !(!isInteractive(tagName, props) && !props.role);
}
},

NO_TABINDEX: {
msg: 'You have a click handler on a non-interactive element but no `tabIndex` DOM property. The element will not be navigable or interactive by keyboard users. http://www.w3.org/TR/wai-aria-practices/#focus_tabindex',
test (tagName, props, children) {
return !needsTabIndex(tagName, props);
return !(
!isInteractive(tagName, props) &&
!props.tabIndex
);
}
},

NO_LABEL: {
msg: 'You have a click handler on an element with no screen-readable text. Add `aria-label` or `aria-labelled-by` attribute, or put some text in the element.',
test (tagName, props, children) {
return !needsLabel(props, children);
return (
props['aria-label'] ||
props['aria-labelled-by'] ||
hasChildTextNode(props, children)
);
}
},

Expand All @@ -135,9 +79,9 @@ exports.props = {
exports.tags = {
a: {
HASH_HREF_NEEDS_BUTTON: {
msg: 'You have a click handler on an anchor with an `href` DOM property and no `role` DOM property. Add `role="button"` to your markup, or you should probably just use a `<button/>`.',
msg: 'You have an anchor with `href="#"` and no `role` DOM property. Add `role="button"` or better yet, use a `<button/>`.',
test (tagName, props, children) {
return !needsButtonRole(props);
return !(!props.role && props.href === '#');
}
}
},
Expand All @@ -154,7 +98,7 @@ exports.tags = {
// TODO: have some way to set localization strings to match against
msg: 'Screen-readers already announce `img` tags as an image, you don\'t need to use the word "image" in the description',
test (tagName, props, children) {
return !(hasAlt(props) && altIsRedundant(props.alt));
return !(hasAlt(props) && props.alt.match('image'));
}
}
}
Expand Down

0 comments on commit 41a5566

Please sign in to comment.