Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
fix to make img element allow role='presentation' with non-empty alt
Browse files Browse the repository at this point in the history
 - closes #298
  • Loading branch information
ipip2005 committed Oct 21, 2016
1 parent 4d3d66e commit 8cf9251
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 108 deletions.
15 changes: 2 additions & 13 deletions src/reactA11yImgHasAltRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ALT_STRING: string = 'alt';

export function getFailureStringNoAlt(tagName: string): string {
return `<${tagName}> elements must have an non-empty alt attribute or \
use empty alt attribute and role='presentation' for presentational images. \
use empty alt attribute as well as role='presentation' for decorative/presentational images. \
A reference for the presentation role can be found at https://www.w3.org/TR/wai-aria/roles#presentation.`;
}

Expand All @@ -24,11 +24,6 @@ export function getFailureStringEmptyAltAndNotPresentationRole(tagName: string):
Add more details in alt attribute or specify role attribute to equal 'presentation' when 'alt' attribute is empty.`;
}

export function getFailureStringNonEmptyAltAndPresentationRole(tagName: string): string {
return `The value of alt attribute in <${tagName}> tag is non-empty and role value is presentation. \
Remove role='presentation' or specify 'alt' attributeto be empty when role attributes equals 'presentation'.`;
}

/**
* Enforces that img elements have alt text.
*/
Expand Down Expand Up @@ -101,13 +96,7 @@ class ImgHasAltWalker extends Lint.RuleWalker {
const isPresentationRole: boolean = !!roleAttributeValue.toLowerCase().match(/\bpresentation\b/);
const isEmptyAlt: boolean = isEmpty(altAttribute) || getStringLiteral(altAttribute) === '';

if (!isEmptyAlt && isPresentationRole) { // <img alt='altValue' role='presentation' />
this.addFailure(this.createFailure(
node.getStart(),
node.getWidth(),
getFailureStringNonEmptyAltAndPresentationRole(tagName)
));
} else if (isEmptyAlt && !isPresentationRole) { // <img alt='' />
if (isEmptyAlt && !isPresentationRole) { // <img alt='' />
this.addFailure(this.createFailure(
node.getStart(),
node.getWidth(),
Expand Down
89 changes: 5 additions & 84 deletions src/tests/reactA11yImgHasAltRuleTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestHelper } from './TestHelper';
import {
getFailureStringNoAlt,
getFailureStringNonEmptyAltAndPresentationRole,
getFailureStringEmptyAltAndNotPresentationRole
} from '../reactA11yImgHasAltRule';

Expand Down Expand Up @@ -39,6 +38,11 @@ describe('reactA11yImgHasAlt', () => {
const fileName: string = fileDirectory + 'ImgElementHasNonEmptyAltValueAndNotPresentationRole.tsx';
TestHelper.assertNoViolation(ruleName, fileName);
});

it('when the img element has non-empty alt value and presentation role', () => {
const fileName: string = fileDirectory + 'ImgElementHasNonEmptyAltValueAndPresentationRole.tsx';
TestHelper.assertNoViolation(ruleName, fileName);
});
});

describe('should fail', () => {
Expand Down Expand Up @@ -101,41 +105,6 @@ describe('reactA11yImgHasAlt', () => {
]
);
});

it('when the img element has non-empty alt value and presentation role', () => {
const fileName: string = fileDirectory + 'ImgElementHasNonEmptyAltValueAndPresentationRole.tsx';

TestHelper.assertViolations(
ruleName,
fileName,
[
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 5 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 6 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 7 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 8 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
}
]
);
});
});
});

Expand Down Expand Up @@ -200,54 +169,6 @@ describe('reactA11yImgHasAlt', () => {
);
});

it('when custom element or img has non-empty alt value and presentation role', () => {
const fileName: string = fileDirectory + 'CustomElementHasNonEmptyAltValueAndPresentationRole.tsx';

TestHelper.assertViolationsWithOptions(
ruleName,
options,
fileName,
[
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 6 },
failure: getFailureStringNonEmptyAltAndPresentationRole('Picture')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 7 },
failure: getFailureStringNonEmptyAltAndPresentationRole('Picture')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 8 },
failure: getFailureStringNonEmptyAltAndPresentationRole('Picture')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 9 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 10 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
},
{
name: fileName,
ruleName: ruleName,
startPosition: { character: 11, line: 11 },
failure: getFailureStringNonEmptyAltAndPresentationRole('img')
}
]
);
});

it('when custom element or img has empty alt value and not presentation role', () => {
const fileName: string = fileDirectory + 'CustomElementHasEmptyAltValueAndNotPresentationRole.tsx';

Expand Down

This file was deleted.

0 comments on commit 8cf9251

Please sign in to comment.