-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(new-rule): summary elements must have an accessible name #4511
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { hasContentVirtual } from '../commons/dom'; | ||
|
||
export default function summaryIsInteractiveMatches(_, virtualNode) { | ||
// Summary only interactive if its real DOM parent is a details element | ||
const parent = virtualNode.parent; | ||
if ( | ||
parent.props.nodeName !== 'details' || | ||
virtualNode.shadowId !== parent.shadowId | ||
) { | ||
return false; | ||
} | ||
if (!hasDetailsContent(parent)) { | ||
return false; | ||
} | ||
// Only the first summary element is interactive | ||
const firstSummary = parent.children.find( | ||
child => child.props.nodeName === 'summary' | ||
); | ||
if (firstSummary !== virtualNode) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function hasDetailsContent(vDetails) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why ignore summary elements of details that don't have content? They are still interactive and screen readers read them out. They don't seem to be treated any differently if the details has text content. <details>
<summary></summary>
</details>
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not less sure what's going on when there's nothing in the details element to expand. Its just an empty tab stop at that point. Those aren't great, but they're not an WCAG violation IMO. |
||
let summary = false; | ||
for (const vNode of vDetails.children) { | ||
const { nodeType, nodeValue } = vNode.props; | ||
// Skip the first summary elm | ||
if (!summary && vNode.props.nodeName === 'summary') { | ||
summary = true; | ||
continue; | ||
} | ||
// True for elements with content | ||
if (nodeType === 1 && hasContentVirtual(vNode)) { | ||
return true; | ||
} | ||
// True for text nodes | ||
if (nodeType === 3 && nodeValue.trim()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"id": "summary-name", | ||
"impact": "serious", | ||
"selector": "summary", | ||
"matches": "summary-interactive-matches", | ||
"tags": [ | ||
"cat.name-role-value", | ||
"wcag2a", | ||
"wcag412", | ||
"section508", | ||
"section508.22.a", | ||
"TTv5", | ||
"TT6.a", | ||
"EN-301-549", | ||
"EN-9.4.1.2" | ||
], | ||
"metadata": { | ||
"description": "Ensures summary elements have discernible text", | ||
"help": "Summary elements must have discernible text" | ||
}, | ||
"all": [], | ||
"any": [ | ||
"has-visible-text", | ||
"aria-label", | ||
"aria-labelledby", | ||
"non-empty-title" | ||
], | ||
"none": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<details> | ||
<summary id="empty-fail"></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="text-pass">name</summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="aria-label-pass" aria-label="Name"></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="aria-label-fail" aria-label=""></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="aria-labelledby-pass" aria-labelledby="labeldiv"></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="aria-labelledby-fail" aria-labelledby="nonexistent"></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="aria-labelledby-empty-fail" aria-labelledby="emptydiv"></summary> | ||
Hello world | ||
</details> | ||
<div id="labeldiv">summary label</div> | ||
<div id="emptydiv"></div> | ||
|
||
<details> | ||
<summary id="combo-pass" aria-label="Aria Name">Name</summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="title-pass" title="Title"></summary> | ||
Hello world | ||
</details> | ||
|
||
<details> | ||
<summary id="presentation-role-fail" role="presentation"></summary> | ||
Conflict resolution gets this to be ignored | ||
</details> | ||
|
||
<details> | ||
<summary id="none-role-fail" role="none"></summary> | ||
Conflict resolution gets this to be ignored | ||
</details> | ||
|
||
<details> | ||
<summary id="heading-role-fail" role="heading"></summary> | ||
Conflict resolution gets this to be ignored | ||
</details> | ||
|
||
<!-- Invalid naming methods --> | ||
|
||
<details> | ||
<summary id="value-attr-fail" value="Button Name"></summary> | ||
Not a valid method for giving a name | ||
</details> | ||
|
||
<details> | ||
<summary id="alt-attr-fail" alt="Button Name"></summary> | ||
Not a valid method for giving a name | ||
</details> | ||
|
||
<label> | ||
<details> | ||
<summary id="label-elm-fail"></summary> | ||
Text here | ||
</details> | ||
Not a valid method for giving a name | ||
</label> | ||
|
||
<!-- inapplicable cases --> | ||
|
||
<details> | ||
<summary id="no-siblings-inapplicable"></summary> | ||
</details> | ||
|
||
<details> | ||
<summary id="empty-siblings-inapplicable"></summary> | ||
<div><span> </span></div> | ||
<!-- and a comment --> | ||
<script> | ||
const hello = 'world'; | ||
</script> | ||
<style> | ||
.hello { | ||
world: blue; | ||
} | ||
</style> | ||
<template> Skip me </template> | ||
<noscript>And skip me</noscript> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"description": "summary-name test", | ||
"rule": "summary-name", | ||
"violations": [ | ||
["#empty-fail"], | ||
["#aria-label-fail"], | ||
["#aria-labelledby-fail"], | ||
["#aria-labelledby-empty-fail"], | ||
["#presentation-role-fail"], | ||
["#none-role-fail"], | ||
["#heading-role-fail"], | ||
["#value-attr-fail"], | ||
["#alt-attr-fail"], | ||
["#label-elm-fail"] | ||
], | ||
"passes": [ | ||
["#text-pass"], | ||
["#aria-label-pass"], | ||
["#aria-labelledby-pass"], | ||
["#combo-pass"], | ||
["#title-pass"] | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
function appendSerialChild(parent, child) { | ||
if (child instanceof axe.SerialVirtualNode === false) { | ||
child = new axe.SerialVirtualNode(child); | ||
} | ||
child.parent = parent; | ||
parent.children ??= []; | ||
parent.children.push(child); | ||
return child; | ||
} | ||
|
||
describe('summary-name virtual-rule', () => { | ||
let vDetails; | ||
beforeEach(() => { | ||
vDetails = new axe.SerialVirtualNode({ | ||
nodeName: 'details', | ||
attributes: {} | ||
}); | ||
appendSerialChild(vDetails, { nodeName: '#text', nodeValue: 'text' }); | ||
}); | ||
|
||
it('fails without children', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: {} | ||
}); | ||
vSummary.children = []; | ||
appendSerialChild(vDetails, vSummary); | ||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
console.log(results); | ||
assert.lengthOf(results.passes, 0); | ||
assert.lengthOf(results.violations, 1); | ||
assert.lengthOf(results.incomplete, 0); | ||
}); | ||
|
||
it('passes with text content', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: {} | ||
}); | ||
appendSerialChild(vSummary, { nodeName: '#text', nodeValue: 'text' }); | ||
appendSerialChild(vDetails, vSummary); | ||
|
||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
assert.lengthOf(results.passes, 1); | ||
assert.lengthOf(results.violations, 0); | ||
assert.lengthOf(results.incomplete, 0); | ||
}); | ||
|
||
it('is inapplicable without siblings to summary', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: {} | ||
}); | ||
appendSerialChild(vSummary, { nodeName: '#text', nodeValue: 'text' }); | ||
vSummary.parent = vDetails; | ||
vDetails.children = [vSummary]; | ||
|
||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
assert.lengthOf(results.passes, 0); | ||
assert.lengthOf(results.violations, 0); | ||
assert.lengthOf(results.incomplete, 0); | ||
assert.lengthOf(results.inapplicable, 1); | ||
}); | ||
|
||
it('passes with aria-label', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: { 'aria-label': 'foobar' } | ||
}); | ||
appendSerialChild(vDetails, vSummary); | ||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
assert.lengthOf(results.passes, 1); | ||
assert.lengthOf(results.violations, 0); | ||
assert.lengthOf(results.incomplete, 0); | ||
}); | ||
|
||
it('passes with title', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: { title: 'foobar' } | ||
}); | ||
appendSerialChild(vDetails, vSummary); | ||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
assert.lengthOf(results.passes, 1); | ||
assert.lengthOf(results.violations, 0); | ||
assert.lengthOf(results.incomplete, 0); | ||
}); | ||
|
||
it('incompletes with aria-labelledby', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: { 'aria-labelledby': 'foobar' } | ||
}); | ||
appendSerialChild(vDetails, vSummary); | ||
const results = axe.runVirtualRule('summary-name', vSummary); | ||
assert.lengthOf(results.passes, 0); | ||
assert.lengthOf(results.violations, 0); | ||
assert.lengthOf(results.incomplete, 1); | ||
}); | ||
|
||
it('throws without a parent', () => { | ||
const vSummary = new axe.SerialVirtualNode({ | ||
nodeName: 'summary', | ||
attributes: { 'aria-labelledby': 'foobar' } | ||
}); | ||
vSummary.children = []; | ||
assert.throws(() => { | ||
axe.runVirtualRule('summary-name', vSummary); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
details
andsummary
every not share the sameshadowId
?details
cannot have a shadow root attached to it and if you put a summary as slotted content to the details they'll both have the sameshadowId
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gd catch. Do have a way to see in virtual tree that two nodes are in the same tree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we have a way to distinguish between light DOM tree and shadow DOM tree.