-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(label): Prevent label rule from crashing on input without type #678…
… (#730)
- Loading branch information
1 parent
9c7b9f1
commit 4498680
Showing
4 changed files
with
67 additions
and
14 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,14 +1,7 @@ | ||
// :not([type='hidden']) | ||
// :not([type='image']) | ||
// :not([type='button']) | ||
// :not([type='submit']) | ||
// :not([type='reset']) | ||
if (node.nodeName.toLowerCase() !== 'input') { | ||
if (node.nodeName.toLowerCase() !== 'input' || | ||
node.hasAttribute('type') === false) { | ||
return true; | ||
} | ||
var t = node.getAttribute('type').toLowerCase(); | ||
if (t === 'hidden' || t === 'image' || t === 'button' || t === 'submit' || | ||
t === 'reset') { | ||
return false; | ||
} | ||
return true; | ||
|
||
var type = node.getAttribute('type').toLowerCase(); | ||
return ['hidden', 'image', 'button', 'submit', 'reset'].includes(type) === false; |
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,60 @@ | ||
describe('label-matches', function () { | ||
'use strict'; | ||
|
||
var fixture = document.getElementById('fixture'); | ||
var rule; | ||
|
||
beforeEach(function () { | ||
rule = axe._audit.rules.find(function (rule) { | ||
return rule.id === 'label'; | ||
}); | ||
}); | ||
|
||
it('returns true for non-input elements', function () { | ||
fixture.innerHTML = '<textarea></textarea>'; | ||
var target = fixture.querySelector('textarea'); | ||
|
||
assert.isTrue(rule.matches(target)); | ||
}); | ||
|
||
it('returns true for input elements without type', function () { | ||
fixture.innerHTML = '<input />'; | ||
var target = fixture.querySelector('input'); | ||
|
||
assert.isTrue(rule.matches(target)); | ||
}); | ||
|
||
it('returns false for input buttons', function () { | ||
fixture.innerHTML = '<input type="button" />' + | ||
'<input type="submit" />' + | ||
'<input type="image" />' + | ||
'<input type="reset" />'; | ||
|
||
var targets = Array.from(fixture.querySelectorAll('input')); | ||
targets.forEach(function (target) { | ||
assert.isFalse(rule.matches(target)); | ||
}); | ||
}); | ||
|
||
it('returns false for input elements type=hidden', function () { | ||
fixture.innerHTML = '<input type="hidden" />'; | ||
var target = fixture.querySelector('input'); | ||
|
||
assert.isFalse(rule.matches(target)); | ||
}); | ||
|
||
it('returns true for other input types', function () { | ||
fixture.innerHTML = '<input type="text" />' + | ||
'<input type="password" />' + | ||
'<input type="url" />' + | ||
'<input type="range" />' + | ||
'<input type="date" />' + | ||
'<input type="checkbox" />' + | ||
'<input type="radio" />'; | ||
|
||
var targets = Array.from(fixture.querySelectorAll('input')); | ||
targets.forEach(function (target) { | ||
assert.isTrue(rule.matches(target)); | ||
}); | ||
}); | ||
}); |