-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
408 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const fluentImageComponents: string[]; | ||
export const imageDomNodes: string[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use strict"; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
const fluentImageComponents = ["Image"]; | ||
const imageDomNodes = ["img"]; | ||
module.exports = { | ||
fluentImageComponents, | ||
imageDomNodes | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const linkBasedComponents: string[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
const linkBasedComponents = ["Link", "a"]; // , "BreadcrumbButton" | ||
module.exports = { | ||
linkBasedComponents | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare const _exports: import("eslint").Rule.RuleModule; | ||
export = _exports; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
"use strict"; | ||
const { elementType } = require("jsx-ast-utils"); | ||
const { hasNonEmptyProp } = require("../util/hasNonEmptyProp"); | ||
const { hasTextContentChild } = require("../util/hasTextContentChild"); | ||
const { hasLabelledChildImage } = require("../util/hasLabelledChildImage"); | ||
const { linkBasedComponents } = require("../applicableComponents/linkBasedComponents"); | ||
const { hasAssociatedLabelViaAriaLabelledBy } = require("../util/labelUtils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Accessibility: Image links must have an accessible name. Add either text content, labelling to the image or labelling to the link itself.", | ||
recommended: true, | ||
url: "https://www.w3.org/WAI/standards-guidelines/act/rules/c487ae/" // URL to the documentation page for this rule | ||
}, | ||
messages: { | ||
missingAriaLabel: "Accessibility Rule: Image links must have an accessible name. Link can have a title attribute or text content, or Image can have an aria-label, aria-labelledby, or title attribute.", | ||
missingHref: "Links must have an href" | ||
}, | ||
fixable: "code", | ||
schema: [] // Add a schema if the rule has options | ||
}, | ||
// create (function) returns an object with methods that ESLint calls to “visit” nodes while traversing the abstract syntax tree | ||
create(context) { | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
// visitor functions for different types of nodes | ||
JSXElement(node) { | ||
const openingElement = node.openingElement; | ||
// if it's not a link based component, return | ||
if (!linkBasedComponents.includes(elementType(openingElement))) { | ||
return; | ||
} | ||
const hasHref = hasNonEmptyProp(openingElement.attributes, "href"); | ||
// check if the link has an href | ||
if (!hasHref) { | ||
context.report({ | ||
node, | ||
messageId: `missingHref` | ||
}); | ||
} | ||
// if it has text content, return | ||
if (hasTextContentChild(node)) { | ||
return; | ||
} | ||
// if there is a containing image and it is labelled correctly, return | ||
const hasAccessibleImage = hasLabelledChildImage(node); | ||
if (hasAccessibleImage) { | ||
return; | ||
} | ||
// Check if there is an accessible link | ||
const linkHasAccessibleLabel = hasNonEmptyProp(openingElement.attributes, "title") || | ||
hasNonEmptyProp(openingElement.attributes, "aria-label") || | ||
hasAssociatedLabelViaAriaLabelledBy(openingElement, context); | ||
if (linkHasAccessibleLabel) { | ||
return; | ||
} | ||
// Report if there is no text content, accessible link or image | ||
if (!linkHasAccessibleLabel || !hasAccessibleImage) { | ||
context.report({ | ||
node, | ||
messageId: `missingAriaLabel` | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* hasLabelledChildImage - determines if a component has text content as a child e.g. <Link href="https://www.bing.com" {...props}><Image src="https://www.w3schools.com/images/img_girl.jpg" alt="abc"></Image></Link> | ||
* @param {*} node JSXElement | ||
* @returns boolean | ||
*/ | ||
export function hasLabelledChildImage(node: any): boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use strict"; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
const { flattenChildren } = require("./flattenChildren"); | ||
const { hasProp, getPropValue } = require("jsx-ast-utils"); | ||
const { hasNonEmptyProp } = require("./hasNonEmptyProp"); | ||
const { fluentImageComponents, imageDomNodes } = require("../applicableComponents/imageBasedComponents"); | ||
const mergedImageComponents = [...fluentImageComponents, ...imageDomNodes]; | ||
/** | ||
* hasLabelledChildImage - determines if a component has text content as a child e.g. <Link href="https://www.bing.com" {...props}><Image src="https://www.w3schools.com/images/img_girl.jpg" alt="abc"></Image></Link> | ||
* @param {*} node JSXElement | ||
* @returns boolean | ||
*/ | ||
function hasLabelledChildImage(node) { | ||
// no children | ||
if (node.children == null || node.children == undefined || node.children.length === 0) { | ||
return false; | ||
} | ||
// Check if there is an accessible image | ||
const hasAccessibleImage = flattenChildren(node).some(child => { | ||
console.log("mergedImageComponents.includes(child.openingElement.name.name)::: ", mergedImageComponents.includes(child.openingElement.name.name)); | ||
if (child.type === "JSXElement" && mergedImageComponents.includes(child.openingElement.name.name)) { | ||
console.log("here 3"); | ||
return hasProp(child.openingElement.attributes, "aria-hidden") || getPropValue(child.openingElement.attributes, "alt") | ||
? false | ||
: hasNonEmptyProp(child.openingElement.attributes, "title") || | ||
hasNonEmptyProp(child.openingElement.attributes, "alt") || | ||
hasNonEmptyProp(child.openingElement.attributes, "aria-label") || | ||
hasNonEmptyProp(child.openingElement.attributes, "aria-labelledby"); | ||
} | ||
return false; | ||
}); | ||
return hasAccessibleImage; | ||
} | ||
module.exports = { | ||
hasLabelledChildImage | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const fluentImageComponents = ["Image"]; | ||
const imageDomNodes = ["img"]; | ||
|
||
module.exports = { | ||
fluentImageComponents, | ||
imageDomNodes | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const linkBasedComponents = ["Link", "a"]; // , "BreadcrumbButton" | ||
|
||
module.exports = { | ||
linkBasedComponents | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.