From f4f6df61625c454b2dd76d26007240c549b1cbff Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Wed, 22 Aug 2018 11:17:50 +0200 Subject: [PATCH] fix: Allow divs as groups in dl (#1076) Allow div elements to group content inside of dl elements. Closes #262 ## Reviewer checks **Required fields, to be filled out by PR reviewer(s)** - [x] Follows the commit message policy, appropriate for next version - [x] Has documentation updated, a DU ticket, or requires no documentation change - [x] Includes new tests, or was unnecessary - [x] Code is reviewed for security by: Jey --- lib/checks/lists/only-dlitems.js | 21 +++++++++++++++++---- test/checks/lists/only-dlitems.js | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/checks/lists/only-dlitems.js b/lib/checks/lists/only-dlitems.js index 5227fb60de..72dabdaf24 100644 --- a/lib/checks/lists/only-dlitems.js +++ b/lib/checks/lists/only-dlitems.js @@ -10,20 +10,33 @@ const ALLOWED_TAGS = [ ]; const ALLOWED_ROLES = ['definition', 'term', 'list']; +const { getRole } = axe.commons.aria; let base = { badNodes: [], hasNonEmptyTextNode: false }; -const result = virtualNode.children.reduce((out, childNode) => { +const content = virtualNode.children.reduce((content, child) => { + const { actualNode } = child; + if ( + actualNode.nodeName.toUpperCase() === 'DIV' && + getRole(actualNode) === null + ) { + return content.concat(child.children); + } + return content.concat(child); +}, []); + +const result = content.reduce((out, childNode) => { const { actualNode } = childNode; const tagName = actualNode.nodeName.toUpperCase(); if (actualNode.nodeType === 1 && !ALLOWED_TAGS.includes(tagName)) { - const role = (actualNode.getAttribute('role') || '').toLowerCase(); - if ((tagName !== 'DT' && tagName !== 'DD') || role) { - if (!ALLOWED_ROLES.includes(role)) { + const explicitRole = getRole(actualNode, { noImplicit: true }); + + if ((tagName !== 'DT' && tagName !== 'DD') || explicitRole) { + if (!ALLOWED_ROLES.includes(explicitRole)) { // handle comment - https://github.com/dequelabs/axe-core/pull/518/files#r139284668 out.badNodes.push(actualNode); } diff --git a/test/checks/lists/only-dlitems.js b/test/checks/lists/only-dlitems.js index 2cb1fd062a..bc6e701367 100644 --- a/test/checks/lists/only-dlitems.js +++ b/test/checks/lists/only-dlitems.js @@ -176,6 +176,24 @@ describe('only-dlitems', function() { ); }); + it('should return false if the list has dt and dd inside a div group', function() { + var checkArgs = checkSetup( + '
An item
A list
' + ); + assert.isFalse( + checks['only-dlitems'].evaluate.apply(checkContext, checkArgs) + ); + }); + + it('should return true if the list has dt and dd inside a div group with a role', function() { + var checkArgs = checkSetup( + '
An item
A list
' + ); + assert.isTrue( + checks['only-dlitems'].evaluate.apply(checkContext, checkArgs) + ); + }); + (shadowSupport.v1 ? it : xit)( 'should return false in a shadow DOM pass', function() {