From 1b6ba63b42b25fb8d1afecc8609bde79de028d91 Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Thu, 28 Nov 2019 17:24:30 +0100 Subject: [PATCH 1/2] chore(has-alt): use virtualNode instead of node --- lib/checks/shared/has-alt.js | 10 ++++++---- test/checks/shared/has-alt.js | 17 +++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/checks/shared/has-alt.js b/lib/checks/shared/has-alt.js index 270a49d4cd..795c7dc3b9 100644 --- a/lib/checks/shared/has-alt.js +++ b/lib/checks/shared/has-alt.js @@ -1,4 +1,6 @@ -var nn = node.nodeName.toLowerCase(); -return ( - node.hasAttribute('alt') && (nn === 'img' || nn === 'input' || nn === 'area') -); +const { nodeName } = virtualNode.props; +if (['img', 'input', 'area'].includes(nodeName) === false) { + return false; +} + +return typeof virtualNode.attr('alt') === 'string'; diff --git a/test/checks/shared/has-alt.js b/test/checks/shared/has-alt.js index eb67ea434d..1e7f7c879a 100644 --- a/test/checks/shared/has-alt.js +++ b/test/checks/shared/has-alt.js @@ -2,23 +2,24 @@ describe('has-alt', function() { 'use strict'; var fixture = document.getElementById('fixture'); + var checkSetup = axe.testUtils.checkSetup; afterEach(function() { fixture.innerHTML = ''; }); it('should return true if an alt is present', function() { - var node = document.createElement('img'); - node.setAttribute('alt', 'woohoo'); - fixture.appendChild(node); + var checkArgs = checkSetup('woohoo'); + assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs)); + }); - assert.isTrue(checks['has-alt'].evaluate(node)); + it('should return true if an empty alt is present', function() { + var checkArgs = checkSetup(''); + assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs)); }); it('should return false if an alt is not present', function() { - var node = document.createElement('img'); - fixture.appendChild(node); - - assert.isFalse(checks['has-alt'].evaluate(node)); + var checkArgs = checkSetup(''); + assert.isFalse(checks['has-alt'].evaluate.apply(null, checkArgs)); }); }); From 99e043bfea88ec11b6decbb176880a00d49d00bc Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Wed, 4 Dec 2019 13:04:33 +0100 Subject: [PATCH 2/2] chore(has-alt): ignore null alt --- lib/checks/shared/has-alt.js | 4 ++-- test/checks/shared/has-alt.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/checks/shared/has-alt.js b/lib/checks/shared/has-alt.js index 795c7dc3b9..c7ced0a6e9 100644 --- a/lib/checks/shared/has-alt.js +++ b/lib/checks/shared/has-alt.js @@ -1,6 +1,6 @@ const { nodeName } = virtualNode.props; -if (['img', 'input', 'area'].includes(nodeName) === false) { +if (!['img', 'input', 'area'].includes(nodeName)) { return false; } -return typeof virtualNode.attr('alt') === 'string'; +return virtualNode.hasAttr('alt'); diff --git a/test/checks/shared/has-alt.js b/test/checks/shared/has-alt.js index 1e7f7c879a..28cb1ffa6a 100644 --- a/test/checks/shared/has-alt.js +++ b/test/checks/shared/has-alt.js @@ -18,6 +18,11 @@ describe('has-alt', function() { assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs)); }); + it('should return true if a null alt is present', function() { + var checkArgs = checkSetup(''); + assert.isTrue(checks['has-alt'].evaluate.apply(null, checkArgs)); + }); + it('should return false if an alt is not present', function() { var checkArgs = checkSetup(''); assert.isFalse(checks['has-alt'].evaluate.apply(null, checkArgs));