Skip to content

Commit b85f75a

Browse files
committed
fix(material/datepicker): calendar aria-descriptions start/end date
For date ranges, add aria-descriptions to the cell of the current start date and also for end date. Popuplate aria descriptions with the expected value of the ARIA accessible name of the `matStartDate` and `matEndDate` inputs. Introduces `_computeAriaAccessibleName` function to implement ARIA acc-name-1.2 specificiation. Fixes #23442 and #23445
1 parent 7891e19 commit b85f75a

16 files changed

+523
-17
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import {_computeAriaAccessibleName} from './aria-accessible-name';
2+
3+
describe('_computeAriaAccessibleName', () => {
4+
let rootElement: HTMLSpanElement;
5+
6+
beforeEach(() => {
7+
rootElement = document.createElement('span');
8+
document.body.appendChild(rootElement);
9+
});
10+
11+
afterEach(() => {
12+
rootElement.remove();
13+
});
14+
15+
it('uses aria-labelledby over aria-label', () => {
16+
rootElement.innerHTML = `
17+
<label id='test-label'>Aria Labelledby</label>
18+
<input id='test-el' aria-labelledby='test-label' aria-label='Aria Label'/>
19+
`;
20+
21+
const input = rootElement.querySelector('#test-el')!;
22+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Labelledby');
23+
});
24+
25+
it('uses aria-label over for/id', () => {
26+
rootElement.innerHTML = `
27+
<label for='test-el'>For</label>
28+
<input id='test-el' aria-label='Aria Label'/>
29+
`;
30+
31+
const input = rootElement.querySelector('#test-el')!;
32+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Label');
33+
});
34+
35+
it('uses a label with for/id over a title attribute', () => {
36+
rootElement.innerHTML = `
37+
<label for='test-el'>For</label>
38+
<input id='test-el' title='Title'/>
39+
`;
40+
41+
const input = rootElement.querySelector('#test-el')!;
42+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('For');
43+
});
44+
45+
it('returns title when argument has a specified title', () => {
46+
rootElement.innerHTML = `<input id="test-el" title='Title'/>`;
47+
48+
const input = rootElement.querySelector('#test-el')!;
49+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Title');
50+
});
51+
52+
// match browser behavior of giving placeholder attribute preference over title attribute
53+
it('uses placeholder over title', () => {
54+
rootElement.innerHTML = `<input id="test-el" title='Title' placeholder='Placeholder'/>`;
55+
56+
const input = rootElement.querySelector('#test-el')!;
57+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Placeholder');
58+
});
59+
60+
it('uses aria-label over title and placeholder', () => {
61+
rootElement.innerHTML = `<input id="test-el" title='Title' placeholder='Placeholder'
62+
aria-label="Aria Label"/>`;
63+
64+
const input = rootElement.querySelector('#test-el')!;
65+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Label');
66+
});
67+
68+
it('includes both textnode and element children of label with for/id', () => {
69+
rootElement.innerHTML = `
70+
<label for="test-el">
71+
Hello
72+
<span>
73+
Wo
74+
<span><span>r</span></span>
75+
<span> ld </span>
76+
</span>
77+
!
78+
</label>
79+
<input id='test-el'/>
80+
`;
81+
82+
const input = rootElement.querySelector('#test-el')!;
83+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Hello Wo r ld !');
84+
});
85+
86+
it('return computed name of hidden label which has for/id', () => {
87+
rootElement.innerHTML = `
88+
<label for="test-el" aria-hidden="true" style="display: none;">For</label>
89+
<input id='test-el'/>
90+
`;
91+
92+
const input = rootElement.querySelector('#test-el')!;
93+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('For');
94+
});
95+
96+
it('returns computed names of existing elements when 2 of 3 targets of aria-labelledby exist', () => {
97+
rootElement.innerHTML = `
98+
<label id="label-1-of-2" aria-hidden="true" style="display: none;">Label1</label>
99+
<label id="label-2-of-2" aria-hidden="true" style="display: none;">Label2</label>
100+
<input id="test-el" aria-labelledby="label-1-of-2 label-2-of-2 non-existant-label"/>
101+
`;
102+
103+
const input = rootElement.querySelector('#test-el')!;
104+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label1 Label2');
105+
});
106+
107+
it('returns repeated label when there are duplicate ids in aria-labelledby', () => {
108+
rootElement.innerHTML = `
109+
<label id="label-1-of-1" aria-hidden="true" style="display: none;">Label1</label>
110+
<input id="test-el" aria-labelledby="label-1-of-1 label-1-of-1"/>
111+
`;
112+
113+
const input = rootElement.querySelector('#test-el')!;
114+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label1 Label1');
115+
});
116+
117+
it('returns empty string when passed `<input id="test-el"/>`', () => {
118+
rootElement.innerHTML = `<input id="test-el"/>`;
119+
120+
const input = rootElement.querySelector('#test-el')!;
121+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('');
122+
});
123+
124+
it('ignores the aria-labelledby of an aria-labelledby', () => {
125+
rootElement.innerHTML = `
126+
<label id="label" aria-labelledby="transitive-label">Label</label>
127+
<label id="transitive-label" aria-labelled-by="transitive-label">Transitive Label</div>
128+
<input id="test-el" aria-labelledby="label"/>
129+
`;
130+
131+
const input = rootElement.querySelector('#test-el')!;
132+
const label = rootElement.querySelector('#label')!;
133+
expect(_computeAriaAccessibleName(label as any)).toBe('Transitive Label');
134+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label');
135+
});
136+
137+
it('ignores the aria-labelledby on a label with for/id', () => {
138+
rootElement.innerHTML = `
139+
<label for="transitive2-label" aria-labelledby="transitive2-div"></label>
140+
<div id="transitive2-div">Div</div>
141+
<input id="test-el" aria-labelled-by="transitive2-label"/>
142+
`;
143+
144+
const input = rootElement.querySelector('#test-el')!;
145+
expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('');
146+
});
147+
148+
it('returns empty string when argument input is aria-labelledby itself', () => {
149+
rootElement.innerHTML = `
150+
<input id="test-el" aria-labelled-by="test-el"/>
151+
`;
152+
153+
const input = rootElement.querySelector('#test-el')!;
154+
const computedName = _computeAriaAccessibleName(input as HTMLInputElement);
155+
expect(typeof computedName)
156+
.withContext('should return value of type string')
157+
.toBe('string');
158+
});
159+
});
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// This file contains the `_computeAriaAccessibleName` function, which computes what the *expected*
10+
// ARIA accessible name would be for a given element. Implements a subset of ARIA specification
11+
// [Accessible Name and Description Computation 1.2](https://www.w3.org/TR/accname-1.2/).
12+
//
13+
// Specification accname-1.2 can be summarized by returning the result of the first method
14+
// available.
15+
//
16+
// 1. `aria-labelledby` attribute
17+
// ```
18+
// <!-- example using aria-labelledby-->
19+
// <label id='label-id'>Start Date</label>
20+
// <input aria-labelledby='label-id'/>
21+
// ```
22+
// 2. `aria-label` attribute (e.g. `<input aria-label="Departure"/>`)
23+
// 3. Label with `for`/`id`
24+
// ```
25+
// <!-- example using for/id -->
26+
// <label for="current-node">Label</label>
27+
// <input id="current-node"/>
28+
// ```
29+
// 4. `placeholder` attribute (e.g. `<input placeholder="06/03/1990"/>`)
30+
// 5. `title` attribute (e.g. `<input title="Check-In"/>`)
31+
// 6. text content
32+
// ```
33+
// <!-- example using text content -->
34+
// <label for="current-node"><span>Departure</span> Date</label>
35+
// <input id="current-node"/>
36+
// ```
37+
38+
/**
39+
* Computes the *expected* ARIA accessible name for argument element based on [accname-1.2
40+
* specification](https://www.w3.org/TR/accname-1.2/). Implements a subset of accname-1.2,
41+
* and should only be used for the Datepicker's specific use case.
42+
*
43+
* Intended use:
44+
* This is not a general use implementation. Only implements the parts of accname-1.2 that are
45+
* required for the Datepicker's specific use case. This function is not intended for any other
46+
* use.
47+
*
48+
* Limitations:
49+
* - Only covers the needs of `matStartDate` and `matEndDate`. Does not support other use cases.
50+
* - See NOTES's in implementation for specific details on what parts of the accname-1.2
51+
* specification are not implemented.
52+
*
53+
* @param element {HTMLInputElement} native &lt;input/&gt; element of `matStartDate` or
54+
* `matEndDate` component. Corresponds to the 'Root Element' from accname-1.2
55+
*
56+
* @return expected ARIA accessible name of argument &lt;input/&gt;
57+
*/
58+
export function _computeAriaAccessibleName(
59+
element: HTMLInputElement | HTMLTextAreaElement,
60+
): string {
61+
return _computeAriaAccessibleNameInternal(element, true);
62+
}
63+
64+
/**
65+
* Determine if argument node is an Element based on `nodeType` property. This function is safe to
66+
* use with server-side rendering.
67+
*/
68+
function ssrSafeIsElement(node: Node): node is Element {
69+
return node.nodeType === Node.ELEMENT_NODE;
70+
}
71+
72+
/**
73+
* Determine if argument node is an HTMLInputElement based on `nodeName` property. This funciton is
74+
* safe to use with server-side rendering.
75+
*/
76+
function ssrSafeIsHTMLInputElement(node: Node): node is HTMLInputElement {
77+
return node.nodeName === 'INPUT';
78+
}
79+
80+
/**
81+
* Determine if argument node is an HTMLTextAreaElement based on `nodeName` property. This
82+
* funciton is safe to use with server-side rendering.
83+
*/
84+
function ssrSafeIsHTMLTextAreaElement(node: Node): node is HTMLTextAreaElement {
85+
return node.nodeName === 'TEXTAREA';
86+
}
87+
88+
/**
89+
* Calculate the expected ARIA accessible name for given DOM Node. Given DOM Node may be either the
90+
* "Root node" passed to `_computeAriaAccessibleName` or "Current node" as result of recursion.
91+
*
92+
* @return the accessible name of argument DOM Node
93+
*
94+
* @param currentNode node to determine accessible name of
95+
* @param isDirectlyReferenced true if `currentNode` is the root node to calculate ARIA accessible
96+
* name of. False if it is a result of recursion.
97+
*/
98+
function _computeAriaAccessibleNameInternal(
99+
currentNode: Node,
100+
isDirectlyReferenced: boolean,
101+
): string {
102+
// NOTE: this differs from accname-1.2 specification.
103+
// - Does not implement Step 1. of accname-1.2: '''If `currentNode`'s role prohibits naming,
104+
// return the empty string ("")'''.
105+
// - Does not implement Step 2.A. of accname-1.2: '''if current node is hidden and not directly
106+
// referenced by aria-labelledby... return the empty string.'''
107+
108+
// acc-name-1.2 Step 2.B.: aria-labelledby
109+
if (ssrSafeIsElement(currentNode) && isDirectlyReferenced) {
110+
const labelledbyIds: string[] =
111+
currentNode.getAttribute?.('aria-labelledby')?.split(/\s+/g) || [];
112+
const validIdRefs: HTMLElement[] = labelledbyIds.reduce((validIds, id) => {
113+
const elem = document.getElementById(id);
114+
if (elem) {
115+
validIds.push(elem);
116+
}
117+
return validIds;
118+
}, [] as HTMLElement[]);
119+
120+
if (validIdRefs.length) {
121+
return validIdRefs
122+
.map(idRef => {
123+
return _computeAriaAccessibleNameInternal(idRef, false);
124+
})
125+
.join(' ');
126+
}
127+
}
128+
129+
// acc-name-1.2 Step 2.C.: aria-label
130+
if (ssrSafeIsElement(currentNode)) {
131+
const ariaLabel = currentNode.getAttribute('aria-label')?.trim();
132+
133+
if (ariaLabel) {
134+
return ariaLabel;
135+
}
136+
}
137+
138+
// acc-name-1.2 Step 2.D. attribute or element that defines a text alternative
139+
//
140+
// NOTE: this differs from accname-1.2 specification.
141+
// Only implements Step 2.D. for `<label>`,`<input/>`, and `<textarea/>` element. Does not
142+
// implement other elements that have an attribute or element that defines a text alternative.
143+
if (ssrSafeIsHTMLInputElement(currentNode) || ssrSafeIsHTMLTextAreaElement(currentNode)) {
144+
// use label with a `for` attribute referencing the current node
145+
if (currentNode.labels?.length) {
146+
return Array.from(currentNode.labels)
147+
.map(x => _computeAriaAccessibleNameInternal(x, false))
148+
.join(' ');
149+
}
150+
151+
// use placeholder if available
152+
const placeholder = currentNode.getAttribute('placeholder')?.trim();
153+
if (placeholder) {
154+
return placeholder;
155+
}
156+
157+
// use title if available
158+
const title = currentNode.getAttribute('title')?.trim();
159+
if (title) {
160+
return title;
161+
}
162+
}
163+
164+
// NOTE: this differs from accname-1.2 specification.
165+
// - does not implement acc-name-1.2 Step 2.E.: '''if the current node is a control embedded
166+
// within the label... then include the embedded control as part of the text alternative in
167+
// the following manner...'''. Step 2E applies to embedded controls such as textbox, listbox,
168+
// range, etc.
169+
// - does not implement acc-name-1.2 step 2.F.: check that '''role allows name from content''',
170+
// which applies to `currentNode` and its children.
171+
// - does not implement acc-name-1.2 Step 2.F.ii.: '''Check for CSS generated textual content'''
172+
// (e.g. :before and :after).
173+
// - does not implement acc-name-1.2 Step 2.I.: '''if the current node has a Tooltip attribute,
174+
// return its value'''
175+
176+
// Return text content with whitespace collapsed into a single space character. Accomplish
177+
// acc-name-1.2 steps 2F, 2G, and 2H.
178+
return (currentNode.textContent || '').replace(/\s+/g, ' ').trim();
179+
}

src/material/datepicker/calendar-body.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
[attr.aria-disabled]="!item.enabled || null"
6464
[attr.aria-pressed]="_isSelected(item.compareValue)"
6565
[attr.aria-current]="todayValue === item.compareValue ? 'date' : null"
66+
[attr.aria-describedby]="_getDescribedby(item.compareValue)"
6667
(click)="_cellClicked(item, $event)"
6768
(focus)="_emitActiveDateChange(item, $event)">
6869
<div class="mat-calendar-body-cell-content mat-focus-indicator"
@@ -75,3 +76,10 @@
7576
</button>
7677
</td>
7778
</tr>
79+
80+
<label [id]="_startDateLabelId" class="mat-calendar-body-hidden-label">
81+
{{startDateAccessibleName}}
82+
</label>
83+
<label [id]="_endDateLabelId" class="mat-calendar-body-hidden-label">
84+
{{endDateAccessibleName}}
85+
</label>

src/material/datepicker/calendar-body.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ $calendar-range-end-body-cell-size:
3333
padding-right: $calendar-body-label-side-padding;
3434
}
3535

36+
// Label that is not rendered and removed from the accessibility tree.
37+
.mat-calendar-body-hidden-label {
38+
display: none;
39+
}
40+
3641
.mat-calendar-body-cell-container {
3742
position: relative;
3843
height: 0;

0 commit comments

Comments
 (0)