Skip to content

Commit d5af41d

Browse files
authored
fix: Ignore title in 2E (#893)
* fix: Ignore `title` in 2E * Type-safety * Lint * Ensure empty 2F text is ignored * Update .changeset/clever-masks-film.md
1 parent 8088f6d commit d5af41d

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

.changeset/clever-masks-film.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"dom-accessibility-api": patch
3+
---
4+
5+
Don't consider `title` in 2E
6+
7+
Effectively ensures that `title` will not have precedence over name from content.
8+
For example, the `option` in `<option title="Title">Content</option>` will now have `"Content"` has its accessible name instead of `"Title"`.

sources/__tests__/accessible-name.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ describe("to upstream", () => {
108108
`<select><optgroup data-test label="foo"><option value="1">baz</option></optgroup></select>`,
109109
"foo",
110110
],
111+
[
112+
"option",
113+
`<select><option data-test title="Title">Content</option></select>`,
114+
"Content",
115+
],
111116
[
112117
"radio",
113118
`<div data-test role="radio"><em>greek</em> kappa</div>`,
@@ -401,6 +406,7 @@ test.each([
401406
`<img data-test alt="" aria-label="a logo" role="presentation" /> />`,
402407
"a logo",
403408
],
409+
[` <input type="radio" data-test title="crazy"/>`, "crazy"],
404410
])(`misc #%#`, (markup, expectedAccessibleName) => {
405411
expect(markup).toRenderIntoDocumentAccessibleName(expectedAccessibleName);
406412
});

sources/accessible-name-and-description.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,6 @@ function isDescendantOfNativeHostLanguageTextAlternativeElement(
207207
return false;
208208
}
209209

210-
/**
211-
* TODO https://github.com/eps1lon/dom-accessibility-api/issues/101
212-
*/
213-
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- not implemented yet
214-
function computeTooltipAttributeValue(node: Node): string | null {
215-
return null;
216-
}
217-
218210
function getValueOfTextbox(element: Element): string {
219211
if (isHTMLInputElement(element) || isHTMLTextAreaElement(element)) {
220212
return element.value;
@@ -401,30 +393,38 @@ export function computeTextAlternative(
401393
return accumulatedText.trim();
402394
}
403395

404-
function computeElementTextAlternative(node: Node): string | null {
396+
/**
397+
*
398+
* @param element
399+
* @param attributeName
400+
* @returns A string non-empty string or `null`
401+
*/
402+
function useAttribute(
403+
element: Element,
404+
attributeName: string
405+
): string | null {
406+
const attribute = element.getAttributeNode(attributeName);
407+
if (
408+
attribute !== null &&
409+
!consultedNodes.has(attribute) &&
410+
attribute.value.trim() !== ""
411+
) {
412+
consultedNodes.add(attribute);
413+
return attribute.value;
414+
}
415+
return null;
416+
}
417+
418+
function computeTooltipAttributeValue(node: Node): string | null {
405419
if (!isElement(node)) {
406420
return null;
407421
}
408422

409-
/**
410-
*
411-
* @param element
412-
* @param attributeName
413-
* @returns A string non-empty string or `null`
414-
*/
415-
function useAttribute(
416-
element: Element,
417-
attributeName: string
418-
): string | null {
419-
const attribute = element.getAttributeNode(attributeName);
420-
if (
421-
attribute !== null &&
422-
!consultedNodes.has(attribute) &&
423-
attribute.value.trim() !== ""
424-
) {
425-
consultedNodes.add(attribute);
426-
return attribute.value;
427-
}
423+
return useAttribute(node, "title");
424+
}
425+
426+
function computeElementTextAlternative(node: Node): string | null {
427+
if (!isElement(node)) {
428428
return null;
429429
}
430430

@@ -547,10 +547,9 @@ export function computeTextAlternative(
547547
if (nameFromSubTree !== "") {
548548
return nameFromSubTree;
549549
}
550-
return useAttribute(node, "title");
551550
}
552551

553-
return useAttribute(node, "title");
552+
return null;
554553
}
555554

556555
function computeTextAlternative(
@@ -673,11 +672,14 @@ export function computeTextAlternative(
673672
isNativeHostLanguageTextAlternativeElement(current) ||
674673
isDescendantOfNativeHostLanguageTextAlternativeElement(current)
675674
) {
676-
consultedNodes.add(current);
677-
return computeMiscTextAlternative(current, {
675+
const accumulatedText2F = computeMiscTextAlternative(current, {
678676
isEmbeddedInLabel: context.isEmbeddedInLabel,
679677
isReferenced: false,
680678
});
679+
if (accumulatedText2F !== "") {
680+
consultedNodes.add(current);
681+
return accumulatedText2F;
682+
}
681683
}
682684

683685
if (current.nodeType === current.TEXT_NODE) {

0 commit comments

Comments
 (0)