|
| 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 <input/> 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 <input/> |
| 57 | + */ |
| 58 | +export function _computeAriaAccessibleName(element: HTMLInputElement): string { |
| 59 | + return _computeAriaAccessibleNameInternal(element, true); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Determine if argument node is an Element based on `nodeType` property. This function is safe to |
| 64 | + * use with server-side rendering. |
| 65 | + */ |
| 66 | +function ssrSafeIsElement(node: Node): node is Element { |
| 67 | + return node.nodeType === Node.ELEMENT_NODE; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Determine if argument node is an HTMLInputElement based on `nodeName` property. This funciton is |
| 72 | + * safe to use with server-side rendering. |
| 73 | + */ |
| 74 | +function ssrSafeIsHTMLInputElement(node: Node): node is HTMLInputElement { |
| 75 | + return node.nodeName === 'INPUT'; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Calculate the expected ARIA accessible name for given DOM Node. Given DOM Node may be either the |
| 80 | + * "Root node" passed to `_computeAriaAccessibleName` or "Current node" as result of recursion. |
| 81 | + * |
| 82 | + * @return the accessible name of argument DOM Node |
| 83 | + * |
| 84 | + * @param currentNode node to determine accessible name of |
| 85 | + * @param isDirectlyReferenced true if `currentNode` is the root node to calculate ARIA accessible |
| 86 | + * name of. False if it is a result of recursion. |
| 87 | + */ |
| 88 | +function _computeAriaAccessibleNameInternal( |
| 89 | + currentNode: Node, |
| 90 | + isDirectlyReferenced: boolean, |
| 91 | +): string { |
| 92 | + // NOTE: this differs from accname-1.2 specification. |
| 93 | + // - Does not implement Step 1. of accname-1.2: '''If `currentNode`'s role prohibits naming, |
| 94 | + // return the empty string ("")'''. |
| 95 | + // - Does not implement Step 2.A. of accname-1.2: '''if current node is hidden and not directly |
| 96 | + // referenced by aria-labelledby... return the empty string.''' |
| 97 | + |
| 98 | + // acc-name-1.2 Step 2.B.: aria-labelledby |
| 99 | + if (ssrSafeIsElement(currentNode) && isDirectlyReferenced) { |
| 100 | + const labelledbyIds: string[] = |
| 101 | + currentNode.getAttribute?.('aria-labelledby')?.split(/\s+/g) || []; |
| 102 | + const validIdRefs: HTMLElement[] = labelledbyIds.reduce((validIds, id) => { |
| 103 | + const elem = document.getElementById(id); |
| 104 | + if (elem) { |
| 105 | + validIds.push(elem); |
| 106 | + } |
| 107 | + return validIds; |
| 108 | + }, [] as HTMLElement[]); |
| 109 | + |
| 110 | + if (validIdRefs.length) { |
| 111 | + return validIdRefs |
| 112 | + .map(idRef => { |
| 113 | + return _computeAriaAccessibleNameInternal(idRef, false); |
| 114 | + }) |
| 115 | + .join(' '); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // acc-name-1.2 Step 2.C.: aria-label |
| 120 | + if (ssrSafeIsElement(currentNode)) { |
| 121 | + const ariaLabel = currentNode.getAttribute('aria-label')?.trim(); |
| 122 | + |
| 123 | + if (ariaLabel) { |
| 124 | + return ariaLabel; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // acc-name-1.2 Step 2.D. attribute or element that defines a text alternative |
| 129 | + // |
| 130 | + // NOTE: this differs from accname-1.2 specification. |
| 131 | + // Only implements acc-name-1.2 for `<label>` and `<input/>` element. Does not implement all |
| 132 | + // other elements that have an attribute or element that defines a text alternative. |
| 133 | + if (ssrSafeIsHTMLInputElement(currentNode)) { |
| 134 | + // use label with a `for` attribute referencing the current node |
| 135 | + if (currentNode.labels?.length) { |
| 136 | + return Array.from(currentNode.labels) |
| 137 | + .map(x => _computeAriaAccessibleNameInternal(x, false)) |
| 138 | + .join(' '); |
| 139 | + } |
| 140 | + |
| 141 | + // use placeholder if available |
| 142 | + const placeholder = currentNode.getAttribute('placeholder')?.trim(); |
| 143 | + if (placeholder) { |
| 144 | + return placeholder; |
| 145 | + } |
| 146 | + |
| 147 | + // use title if available |
| 148 | + const title = currentNode.getAttribute('title')?.trim(); |
| 149 | + if (title) { |
| 150 | + return title; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // NOTE: this differs from accname-1.2 specification. |
| 155 | + // - does not implement acc-name-1.2 Step 2.E.: '''if the current node is a control embedded |
| 156 | + // within the label... then include the embedded control as part of the text alternative in |
| 157 | + // the following manner...'''. Step 2E applies to embedded controls such as textbox, listbox, |
| 158 | + // range, etc. |
| 159 | + // - does not implement acc-name-1.2 step 2.F.: check that '''role allows name from content''', |
| 160 | + // which applies to `currentNode` and its children. |
| 161 | + // - does not implement acc-name-1.2 Step 2.F.ii.: '''Check for CSS generated textual content''' |
| 162 | + // (e.g. :before and :after). |
| 163 | + // - does not implement acc-name-1.2 Step 2.I.: '''if the current node has a Tooltip attribute, |
| 164 | + // return its value''' |
| 165 | + |
| 166 | + // Return text content with whitespace collapsed into a single space character. Accomplish |
| 167 | + // acc-name-1.2 steps 2F, 2G, and 2H. |
| 168 | + return (currentNode.textContent || '').replace(/\s+/g, ' ').trim(); |
| 169 | +} |
0 commit comments