Skip to content

Commit 7bf0b34

Browse files
committed
[fix] - Handle dynamic tabIndex expression values.
Fixes #50
1 parent 7cf8cfb commit 7bf0b34

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/rules/onclick-has-focus.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
88
import isInteractiveElement from '../util/isInteractiveElement';
99
import getAttribute from '../util/getAttribute';
1010
import getNodeType from '../util/getNodeType';
11-
import getAttributeValue from '../util/getAttributeValue';
11+
import getTabIndex from '../util/getTabIndex';
1212

1313
// ----------------------------------------------------------------------------
1414
// Rule Definition
@@ -31,7 +31,7 @@ module.exports = context => ({
3131
return;
3232
} else if (isInteractiveElement(type, attributes)) {
3333
return;
34-
} else if (!isNaN(Number(getAttributeValue(getAttribute(attributes, 'tabIndex'))))) {
34+
} else if (getTabIndex(getAttribute(attributes, 'tabIndex')) !== undefined) {
3535
return;
3636
}
3737

src/util/getTabIndex.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
import getAttributeValue, { getLiteralAttributeValue } from './getAttributeValue';
4+
5+
6+
7+
/**
8+
* Returns the tabIndex value.
9+
*/
10+
export default function getTabIndex(tabIndex) {
11+
// First test if we can extract a literal value
12+
// to see if it's a valid tabIndex. If not, then just see if
13+
// one exists as an expression.
14+
const literalTabIndex = getLiteralAttributeValue(tabIndex);
15+
if (literalTabIndex !== undefined || literalTabIndex !== null) {
16+
return isNaN(Number(literalTabIndex)) ? undefined : literalTabIndex;
17+
}
18+
19+
return getAttributeValue(tabIndex);
20+
}

src/util/isInteractiveElement.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import getAttribute from './getAttribute';
44
import getAttributeValue, { getLiteralAttributeValue } from './getAttributeValue';
5+
import getTabIndex from './getTabIndex';
56
import DOMElements from './attributes/DOM';
67

78

@@ -10,8 +11,8 @@ import DOMElements from './attributes/DOM';
1011
const interactiveMap = {
1112
a: attributes => {
1213
const href = getAttributeValue(getAttribute(attributes, 'href'));
13-
const tabIndex = getAttributeValue(getAttribute(attributes, 'tabIndex'));
14-
return Boolean(href) || !isNaN(Number(tabIndex));
14+
const tabIndex = getTabIndex(getAttribute(attributes, 'tabIndex'));
15+
return href !== undefined || tabIndex !== undefined;
1516
},
1617
// This is same as `a` interactivity function
1718
area: attributes => interactiveMap.a(attributes),

tests/src/rules/onclick-has-focus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ruleTester.run('onclick-has-focus', rule, {
5454
{ code: '<area href="#" onClick={() => void 0} className="foo" />', parserOptions },
5555
{ code: '<textarea onClick={() => void 0} className="foo" />', parserOptions },
5656
{ code: '<a tabIndex="0" onClick={() => void 0} />', parserOptions },
57+
{ code: '<a tabIndex={dynamicTabIndex} onClick={() => void 0} />', parserOptions },
5758
{ code: '<a tabIndex={0} onClick={() => void 0} />', parserOptions },
5859
{ code: '<a role="button" href="#" onClick={() => void 0} />', parserOptions },
5960
{ code: '<a onClick={() => void 0} href="http://x.y.z" />', parserOptions },

0 commit comments

Comments
 (0)