From 54fa5698f4ca6357ed20d13cb2361df45421b9f5 Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Thu, 21 Jun 2018 15:55:15 +0200 Subject: [PATCH] fix(accessible-name): Allow fallback labels when input has id (#951) --- lib/commons/text/accessible-text-virtual.js | 3 ++- test/commons/text/accessible-text.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/commons/text/accessible-text-virtual.js b/lib/commons/text/accessible-text-virtual.js index 527b417994..b7a4f721d9 100644 --- a/lib/commons/text/accessible-text-virtual.js +++ b/lib/commons/text/accessible-text-virtual.js @@ -25,7 +25,8 @@ function findLabel(virtualNode) { label = dom.findElmsInContext({ elm: 'label', attr: 'for', value: virtualNode.actualNode.id, context: virtualNode.actualNode })[0]; - } else { + } + if (!label) { label = dom.findUpVirtual(virtualNode, 'label'); } return axe.utils.getNodeFromTree(axe._tree[0], label); diff --git a/test/commons/text/accessible-text.js b/test/commons/text/accessible-text.js index dcb4360b38..920d41857d 100644 --- a/test/commons/text/accessible-text.js +++ b/test/commons/text/accessible-text.js @@ -778,6 +778,18 @@ describe('text.accessibleTextVirtual', function() { }); }); + it('should find implicit labels with id that does not match to a label', function () { + types.forEach(function(type) { + var t = type ? ' type="' + type + '"' : ''; + fixture.innerHTML = ''; + axe._tree = axe.utils.getFlattenedTree(fixture); + + var target = axe.utils.querySelectorAll(axe._tree, 'input')[0]; + assert.equal(axe.commons.text.accessibleTextVirtual(target), 'Hello World', type); + }); + }) + // not implemented yet, doesn't work accross ATs it.skip('should find a placeholder attribute', function() { types.forEach(function(type) {