-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(image-redundant-alt): prevent redundant issues of image tree (#1616)
* fix(image-redundant-alt): prevent redundant issues of image tree * use aria.getRole, rename variable
- Loading branch information
Showing
6 changed files
with
55 additions
and
57 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
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,19 +1,17 @@ | ||
const text = axe.commons.text.visibleVirtual(virtualNode, true).toLowerCase(); | ||
if (text === '') { | ||
const { aria, text, dom } = axe.commons; | ||
|
||
if (['none', 'presentation'].includes(aria.getRole(node))) { | ||
return false; | ||
} | ||
|
||
// Get all visible images in the composed tree of the current node | ||
const images = axe.utils | ||
.querySelectorAll(virtualNode, 'img') | ||
// Ignore hidden or role=none/presentation images | ||
.filter( | ||
({ actualNode }) => | ||
axe.commons.dom.isVisible(actualNode) && | ||
!['none', 'presentation'].includes(actualNode.getAttribute('role')) | ||
); | ||
|
||
// See if any of the images duplicate the node's text | ||
return images.some( | ||
img => text === axe.commons.text.accessibleTextVirtual(img).toLowerCase() | ||
const parent = dom.findUpVirtual( | ||
virtualNode, | ||
'button, [role="button"], a[href], p, li, td, th' | ||
); | ||
const parentVNode = axe.utils.getNodeFromTree(parent); | ||
const visibleText = text.visibleVirtual(parentVNode, true).toLowerCase(); | ||
if (visibleText === '') { | ||
return false; | ||
} | ||
|
||
return visibleText === text.accessibleTextVirtual(virtualNode).toLowerCase(); |
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
22 changes: 12 additions & 10 deletions
22
test/integration/rules/image-redundant-alt/image-redundant-alt.html
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,18 +1,20 @@ | ||
<button id="pass1"><img alt="Img alt text" />Link text</button> | ||
<a href="#" id="pass2"><img alt="Img alt text" />Link text</a> | ||
<a href="#" id="pass3"><img alt="Link text" role="presentation" />Link text</a> | ||
<a href="#" id="pass4"><img alt="Link text" aria-hidden="true" />Link text</a> | ||
<button><img id="pass1" alt="Img alt text" />Link text</button> | ||
<a href="#"><img id="pass2" alt="Img alt text" />Link text</a> | ||
<a href="#"><img id="pass3" alt="Link text" role="presentation" />Link text</a> | ||
|
||
<button id="fail1"><img alt="button text" />Button text</button> | ||
<a href="#" id="fail2"><img alt="Link text" />Link text</a> | ||
<p id="fail3"><img alt="paragraph text" />paragraph text</p> | ||
<button><img id="fail1" alt="button text" />Button text</button> | ||
<a href="#"><img id="fail2" alt="Link text" />Link text</a> | ||
<p><img id="fail3" alt="paragraph text" />paragraph text</p> | ||
<table> | ||
<tr> | ||
<th id="fail4"><img alt="header cell text" />header cell text</th> | ||
<td id="fail5"><img alt="data cell text" />data cell text</td> | ||
<th><img id="fail4" alt="header cell text" />header cell text</th> | ||
<td><img id="fail5" alt="data cell text" />data cell text</td> | ||
</tr> | ||
</table> | ||
|
||
<ul> | ||
<li id="fail6"><img alt="list text" />list text</li> | ||
<li><img id="fail6" alt="list text" />list text</li> | ||
</ul> | ||
|
||
<!-- ignore --> | ||
<a href="#"><img alt="Link text" aria-hidden="true" />Link text</a> |
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