-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: consistently parse tabindex, following HTML 5 spec (#4637)
Ensures tabindex is correctly parsed as an integer using regex instead of parseInt to comply with HTML standards. Closes #4632
- Loading branch information
1 parent
ec7c6c8
commit 645a850
Showing
13 changed files
with
93 additions
and
31 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
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,10 +1,12 @@ | ||
import { parseTabindex } from '../../core/utils'; | ||
|
||
function tabindexEvaluate(node, options, virtualNode) { | ||
const tabIndex = parseInt(virtualNode.attr('tabindex'), 10); | ||
const tabIndex = parseTabindex(virtualNode.attr('tabindex')); | ||
|
||
// an invalid tabindex will either return 0 or -1 (based on the element) so | ||
// will never be above 0 | ||
// @see https://www.w3.org/TR/html51/editing.html#the-tabindex-attribute | ||
return isNaN(tabIndex) ? true : tabIndex <= 0; | ||
return tabIndex === null || tabIndex <= 0; | ||
} | ||
|
||
export default tabindexEvaluate; |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Parses a tabindex value to return an integer if valid, or null if invalid. | ||
* @method parseTabindex | ||
* @memberof axe.utils | ||
* @param {string|null} str | ||
* @return {number|null} | ||
*/ | ||
function parseTabindex(value) { | ||
if (typeof value !== 'string') { | ||
return null; | ||
} | ||
|
||
// spec: https://html.spec.whatwg.org/#rules-for-parsing-integers | ||
const match = value.trim().match(/^([-+]?\d+)/); | ||
if (match) { | ||
return Number(match[1]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default parseTabindex; |
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,6 +1,8 @@ | ||
import { parseTabindex } from '../core/utils'; | ||
|
||
function noNegativeTabindexMatches(node, virtualNode) { | ||
const tabindex = parseInt(virtualNode.attr('tabindex'), 10); | ||
return isNaN(tabindex) || tabindex >= 0; | ||
const tabindex = parseTabindex(virtualNode.attr('tabindex')); | ||
return tabindex === null || tabindex >= 0; | ||
} | ||
|
||
export default noNegativeTabindexMatches; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
describe('axe.utils.parseTabindex', function () { | ||
'use strict'; | ||
|
||
it('should return 0 for "0"', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('0'), 0); | ||
}); | ||
|
||
it('should return 1 for "+1"', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('+1'), 1); | ||
}); | ||
|
||
it('should return -1 for "-1"', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('-1'), -1); | ||
}); | ||
|
||
it('should return null for null', function () { | ||
assert.strictEqual(axe.utils.parseTabindex(null), null); | ||
}); | ||
|
||
it('should return null for an empty string', function () { | ||
assert.strictEqual(axe.utils.parseTabindex(''), null); | ||
}); | ||
|
||
it('should return null for a whitespace string', function () { | ||
assert.strictEqual(axe.utils.parseTabindex(' '), null); | ||
}); | ||
|
||
it('should return null for non-numeric strings', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('abc'), null); | ||
}); | ||
|
||
it('should return the first valid digit(s) for decimal numbers', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('2.5'), 2); | ||
}); | ||
|
||
it('should return 123 for "123abc"', function () { | ||
assert.strictEqual(axe.utils.parseTabindex('123abc'), 123); | ||
}); | ||
}); |