Skip to content
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

fix: Ignore title in 2E #893

Merged
merged 5 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/clever-masks-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"dom-accessibility-api": patch
---

Don't consider `title` in 2E

Effectively ensures that `title` will not have precedence over name from content.
For example, the `option` in `<option title="Title">Content</option>` will now have `"Content"` has its accessible name.
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ describe("to upstream", () => {
`<select><optgroup data-test label="foo"><option value="1">baz</option></optgroup></select>`,
"foo",
],
[
"option",
`<select><option data-test title="Title">Content</option></select>`,
"Content",
],
[
"radio",
`<div data-test role="radio"><em>greek</em> kappa</div>`,
Expand Down Expand Up @@ -401,6 +406,7 @@ test.each([
`<img data-test alt="" aria-label="a logo" role="presentation" /> />`,
"a logo",
],
[` <input type="radio" data-test title="crazy"/>`, "crazy"],
])(`misc #%#`, (markup, expectedAccessibleName) => {
expect(markup).toRenderIntoDocumentAccessibleName(expectedAccessibleName);
});
Expand Down
66 changes: 34 additions & 32 deletions sources/accessible-name-and-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,6 @@ function isDescendantOfNativeHostLanguageTextAlternativeElement(
return false;
}

/**
* TODO https://github.com/eps1lon/dom-accessibility-api/issues/101
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- not implemented yet
function computeTooltipAttributeValue(node: Node): string | null {
return null;
}

function getValueOfTextbox(element: Element): string {
if (isHTMLInputElement(element) || isHTMLTextAreaElement(element)) {
return element.value;
Expand Down Expand Up @@ -401,30 +393,38 @@ export function computeTextAlternative(
return accumulatedText.trim();
}

function computeElementTextAlternative(node: Node): string | null {
/**
*
* @param element
* @param attributeName
* @returns A string non-empty string or `null`
*/
function useAttribute(
element: Element,
attributeName: string
): string | null {
const attribute = element.getAttributeNode(attributeName);
if (
attribute !== null &&
!consultedNodes.has(attribute) &&
attribute.value.trim() !== ""
) {
consultedNodes.add(attribute);
return attribute.value;
}
return null;
}

function computeTooltipAttributeValue(node: Node): string | null {
if (!isElement(node)) {
return null;
}

/**
*
* @param element
* @param attributeName
* @returns A string non-empty string or `null`
*/
function useAttribute(
element: Element,
attributeName: string
): string | null {
const attribute = element.getAttributeNode(attributeName);
if (
attribute !== null &&
!consultedNodes.has(attribute) &&
attribute.value.trim() !== ""
) {
consultedNodes.add(attribute);
return attribute.value;
}
return useAttribute(node, "title");
}

function computeElementTextAlternative(node: Node): string | null {
if (!isElement(node)) {
return null;
}

Expand Down Expand Up @@ -547,10 +547,9 @@ export function computeTextAlternative(
if (nameFromSubTree !== "") {
return nameFromSubTree;
}
return useAttribute(node, "title");
}

return useAttribute(node, "title");
return null;
}

function computeTextAlternative(
Expand Down Expand Up @@ -673,11 +672,14 @@ export function computeTextAlternative(
isNativeHostLanguageTextAlternativeElement(current) ||
isDescendantOfNativeHostLanguageTextAlternativeElement(current)
) {
consultedNodes.add(current);
return computeMiscTextAlternative(current, {
const accumulatedText2F = computeMiscTextAlternative(current, {
isEmbeddedInLabel: context.isEmbeddedInLabel,
isReferenced: false,
});
if (accumulatedText2F !== "") {
consultedNodes.add(current);
return accumulatedText2F;
}
}

if (current.nodeType === current.TEXT_NODE) {
Expand Down