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】: alt-text string null determination #1013

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 4 additions & 4 deletions src/rules/alt-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const ariaLabelHasValue = (prop) => {
if (value === undefined) {
return false;
}
if (typeof value === 'string' && value.length === 0) {
if (typeof value === 'string' && value.length === 0 && value === '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value === '' and value.length === 0, when it's already a string, are the same condition.

Suggested change
if (typeof value === 'string' && value.length === 0 && value === '') {
if (typeof value === 'string' && value.length === 0) {

return false;
}
return true;
Expand All @@ -62,7 +62,7 @@ const ruleByElement = {
// Don't create an error if the attribute is used correctly. But if it
// isn't, suggest that the developer use `alt` instead.
const ariaLabelProp = getProp(node.attributes, 'aria-label');
if (ariaLabelProp !== undefined) {
if (ariaLabelProp !== undefined || ariaLabelProp !== '') {
if (!ariaLabelHasValue(ariaLabelProp)) {
context.report({
node,
Expand Down Expand Up @@ -134,7 +134,7 @@ const ruleByElement = {
}

const altProp = getProp(node.attributes, 'alt');
if (altProp === undefined) {
if (altProp === undefined || altProp === '') {
context.report({
node,
message: 'Each area of an image map must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.',
Expand Down Expand Up @@ -170,7 +170,7 @@ const ruleByElement = {
}

const altProp = getProp(node.attributes, 'alt');
if (altProp === undefined) {
if (altProp === undefined || altProp === '') {
context.report({
node,
message: '<input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.',
Expand Down
Loading