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: Use placeholder attribute as accessible name #1046

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ test.each([
`<button title="You should really click this" data-test>click me</button>`,
"click me",
],
// https://www.w3.org/TR/html-aam-1.0/#input-type-text-input-type-password-input-type-number-input-type-search-input-type-tel-input-type-email-input-type-url-and-textarea-element-accessible-name-computation
[
`<input type="text" data-test title="address" placeholder="Type here" />`,
"address",
],
[`<input type="text" data-test placeholder="Type here" />`, "Type here"],
[`<textarea data-test title="address" placeholder="Type here" />`, "address"],
[`<textarea data-test placeholder="Type here" />`, "Type here"],
// https://w3c.github.io/html-aam/#input-type-button-input-type-submit-and-input-type-reset-accessible-name-computation
[`<input data-test value="Submit form" type="submit" />`, "Submit form"],
// https://w3c.github.io/html-aam/#input-type-image
Expand Down
24 changes: 23 additions & 1 deletion sources/accessible-name-and-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,35 @@ export function computeTextAlternative(
}
}

if (
(isHTMLInputElement(node) &&
(node.type === "text" ||
node.type === "password" ||
node.type === "number" ||
node.type === "search" ||
node.type === "tel" ||
node.type === "email" ||
node.type === "url")) ||
isHTMLTextAreaElement(node)
) {
// https://www.w3.org/TR/html-aam-1.0/#input-type-text-input-type-password-input-type-number-input-type-search-input-type-tel-input-type-email-input-type-url-and-textarea-element-accessible-name-computation
const nameFromTitle = useAttribute(node, "title");
if (nameFromTitle !== null) {
return nameFromTitle;
}
const nameFromPlaceholder = useAttribute(node, "placeholder");
if (nameFromPlaceholder !== null) {
return nameFromPlaceholder;
}
}

if (
isHTMLInputElement(node) &&
(node.type === "button" ||
node.type === "submit" ||
node.type === "reset")
) {
// https://w3c.github.io/html-aam/#input-type-text-input-type-password-input-type-search-input-type-tel-input-type-email-input-type-url-and-textarea-element-accessible-description-computation
// https://www.w3.org/TR/html-aam-1.0/#input-type-button-input-type-submit-and-input-type-reset-accessible-name-computation
const nameFromValue = useAttribute(node, "value");
if (nameFromValue !== null) {
return nameFromValue;
Expand Down