From 4498680170e6afa0b73f7e21418ba5313293121c Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Tue, 13 Feb 2018 13:22:45 +0100 Subject: [PATCH] fix(label): Prevent label rule from crashing on input without type #678 (#730) --- lib/rules/label-matches.js | 17 ++---- .../label-title-only/label-title-only.html | 2 +- test/integration/rules/label/label.html | 2 +- test/rule-matches/label-matches.js | 60 +++++++++++++++++++ 4 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 test/rule-matches/label-matches.js diff --git a/lib/rules/label-matches.js b/lib/rules/label-matches.js index 4feb6d3131..f81893add8 100644 --- a/lib/rules/label-matches.js +++ b/lib/rules/label-matches.js @@ -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; \ No newline at end of file diff --git a/test/integration/rules/label-title-only/label-title-only.html b/test/integration/rules/label-title-only/label-title-only.html index c3e9c4dbfe..07f35302d9 100644 --- a/test/integration/rules/label-title-only/label-title-only.html +++ b/test/integration/rules/label-title-only/label-title-only.html @@ -29,6 +29,6 @@ - + diff --git a/test/integration/rules/label/label.html b/test/integration/rules/label/label.html index 148835c7c9..044f164a78 100644 --- a/test/integration/rules/label/label.html +++ b/test/integration/rules/label/label.html @@ -28,7 +28,7 @@ - + diff --git a/test/rule-matches/label-matches.js b/test/rule-matches/label-matches.js new file mode 100644 index 0000000000..c3d163ba96 --- /dev/null +++ b/test/rule-matches/label-matches.js @@ -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 = ''; + var target = fixture.querySelector('textarea'); + + assert.isTrue(rule.matches(target)); + }); + + it('returns true for input elements without type', function () { + fixture.innerHTML = ''; + var target = fixture.querySelector('input'); + + assert.isTrue(rule.matches(target)); + }); + + it('returns false for input buttons', function () { + fixture.innerHTML = '' + + '' + + '' + + ''; + + 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 = ''; + var target = fixture.querySelector('input'); + + assert.isFalse(rule.matches(target)); + }); + + it('returns true for other input types', function () { + fixture.innerHTML = '' + + '' + + '' + + '' + + '' + + '' + + ''; + + var targets = Array.from(fixture.querySelectorAll('input')); + targets.forEach(function (target) { + assert.isTrue(rule.matches(target)); + }); + }); +}); \ No newline at end of file