-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Michiel Pauw <[email protected]> Co-authored-by: onkar75 <[email protected]>
- Loading branch information
1 parent
4a6cbaf
commit ff6c1ce
Showing
3 changed files
with
107 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { AccessibilityError } from "../scanner"; | ||
import { labelledByIsValid, querySelectorAll } from "../utils"; | ||
|
||
// Metadata | ||
const id = "aria-tooltip-name"; | ||
const text = "ARIA tooltip must have an accessible name"; | ||
const url = `https://dequeuniversity.com/rules/axe/4.4/${id}?application=RuleDescription`; | ||
|
||
/** | ||
* Make sure that a elements text is "visible" to a screenreader user. | ||
* | ||
* - Inner text that is discernible to screen reader users. | ||
* - Non-empty aria-label attribute. | ||
* - aria-labelledby pointing to element with text which is discernible to screen reader users. | ||
*/ | ||
function hasAccessibleText(el: Element): boolean { | ||
if (el.hasAttribute("aria-label")) { | ||
return el.getAttribute("aria-label")!.trim() !== ""; | ||
} | ||
|
||
if (!labelledByIsValid(el)) return false; | ||
|
||
if (el.getAttribute("title")) { | ||
return el.getAttribute("title")!.trim() !== ""; | ||
} | ||
|
||
if (el.textContent) { | ||
return el.textContent.trim() !== ""; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export function ariaTooltipName(el: Element): AccessibilityError[] { | ||
const errors = []; | ||
const tooltips = querySelectorAll("[role=tooltip]", el); | ||
if (el.matches("[role=tooltip]")) tooltips.push(el); | ||
for (const tooltip of tooltips) { | ||
if (!hasAccessibleText(tooltip)) { | ||
errors.push({ | ||
element: tooltip, | ||
url, | ||
text, | ||
}); | ||
} | ||
} | ||
return errors; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { fixture, html, expect } from "@open-wc/testing"; | ||
import { Scanner } from "../src/scanner"; | ||
import { ariaTooltipName } from "../src/rules/aria-tooltip-name"; | ||
|
||
const scanner = new Scanner([ariaTooltipName]); | ||
|
||
const passes = [ | ||
`<div role="tooltip" id="al" aria-label="Name"></div>`, | ||
`<div> | ||
<div role="tooltip" id="alb" aria-labelledby="labeldiv"></div> | ||
<div id="labeldiv">Hello world!</div> | ||
</div>`, | ||
`<div role="tooltip" id="combo" aria-label="Aria Name">Name</div>`, | ||
`<div role="tooltip" id="title" title="Title"></div>`, | ||
]; | ||
|
||
const violations = [ | ||
`<div role="tooltip" id="empty"></div>`, | ||
`<div role="tooltip" id="alempty" aria-label=""></div>`, | ||
`<div | ||
role="tooltip" | ||
id="albmissing" | ||
aria-labelledby="nonexistent" | ||
></div>`, | ||
`<div> | ||
<div role="tooltip" id="albempty" aria-labelledby="emptydiv"></div> | ||
<div id="emptydiv"></div> | ||
</div>`, | ||
]; | ||
|
||
describe("aria-tooltip-name", async function () { | ||
for (const markup of passes) { | ||
const el = await fixture(html`${markup}`); | ||
it(el.outerHTML, async () => { | ||
const results = (await scanner.scan(el)).map(({ text, url }) => { | ||
return { text, url }; | ||
}); | ||
|
||
expect(results).to.be.empty; | ||
}); | ||
} | ||
|
||
for await (const markup of violations) { | ||
const el = await fixture(html`${markup}`); | ||
it(el.outerHTML, async () => { | ||
const results = (await scanner.scan(el)).map(({ text, url }) => { | ||
return { text, url }; | ||
}); | ||
|
||
expect(results).to.eql([ | ||
{ | ||
text: "ARIA tooltip must have an accessible name", | ||
url: "https://dequeuniversity.com/rules/axe/4.4/aria-tooltip-name?application=RuleDescription", | ||
}, | ||
]); | ||
}); | ||
} | ||
}); |