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

feat(d.ts): improve axe.d.ts types #4081

Merged
merged 7 commits into from
Jul 19, 2023
Merged
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
78 changes: 70 additions & 8 deletions axe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ declare namespace axe {
interface NodeResult {
html: string;
impact?: ImpactValue;
target: string[];
target: UnlabelledFrameSelector;
xpath?: string[];
ancestry?: string[];
ancestry?: UnlabelledFrameSelector;
any: CheckResult[];
all: CheckResult[];
none: CheckResult[];
Expand All @@ -181,8 +181,11 @@ declare namespace axe {
relatedNodes?: RelatedNode[];
}
interface RelatedNode {
target: string[];
html: string;
target: UnlabelledFrameSelector;
xpath?: string[];
ancestry?: UnlabelledFrameSelector;
element?: HTMLElement;
}
interface RuleLocale {
[key: string]: {
Expand All @@ -193,7 +196,7 @@ declare namespace axe {
interface CheckMessages {
pass: string | { [key: string]: string };
fail: string | { [key: string]: string };
incomplete: string | { [key: string]: string };
incomplete?: string | { [key: string]: string };
}
interface CheckLocale {
[key: string]: CheckMessages;
Expand Down Expand Up @@ -257,10 +260,31 @@ declare namespace axe {
brand?: string;
application?: string;
}
interface CheckHelper {
async: () => (result: boolean | undefined | Error) => void;
data: (data: unknown) => void;
relatedNodes: (nodes: Element[]) => void;
}
interface AfterResult {
id: string;
data?: unknown;
relatedNodes: SerialDqElement[];
result: boolean | undefined;
node: SerialDqElement;
}
interface Check {
id: string;
evaluate?: Function | string;
after?: Function | string;
evaluate?:
| string
| ((
this: CheckHelper,
node: Element,
options: unknown,
virtualNode: VirtualNode
) => boolean | undefined | void);
after?:
| string
| ((results: AfterResult[], options: unknown) => AfterResult[]);
options?: any;
matches?: string;
enabled?: boolean;
Expand All @@ -280,9 +304,10 @@ declare namespace axe {
all?: string[];
none?: string[];
tags?: string[];
matches?: string;
matches?: string | ((node: Element, virtualNode: VirtualNode) => boolean);
reviewOnFail?: boolean;
metadata?: Omit<RuleMetadata, 'ruleId'>;
actIds?: string[];
metadata?: Omit<RuleMetadata, 'ruleId' | 'tags' | 'actIds'>;
}
interface AxePlugin {
id: string;
Expand Down Expand Up @@ -367,6 +392,42 @@ declare namespace axe {
shadowSelect: (selector: CrossTreeSelector) => Element | null;
shadowSelectAll: (selector: CrossTreeSelector) => Element[];
getStandards(): Required<Standards>;
DqElement: new (
elm: Element,
options?: { absolutePaths?: boolean }
) => SerialDqElement;
uuid: (
options?: { random?: Uint8Array | Array<number> },
buf?: Uint8Array | Array<number>,
offset?: number
) => string | Uint8Array | Array<number>;
}

interface Aria {
getRoleType: (role: string | Element | VirtualNode | null) => string | null;
}

interface Dom {
isFocusable: (node: Element | VirtualNode) => boolean;
isNativelyFocusable: (node: Element | VirtualNode) => boolean;
}

type AccessibleTextOptions = {
inControlContext?: boolean;
inLabelledByContext?: boolean;
};

interface Text {
accessibleText: (
element: Element,
options?: AccessibleTextOptions
) => string;
}

interface Commons {
aria: Aria;
dom: Dom;
text: Text;
}

interface EnvironmentData {
Expand All @@ -380,6 +441,7 @@ declare namespace axe {
let version: string;
let plugins: any;
let utils: Utils;
let commons: Commons;

/**
* Source string to use as an injected script in Selenium
Expand Down
38 changes: 35 additions & 3 deletions typings/axe-core/axe-core-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,15 @@ var spec: axe.Spec = {
checks: [
{
id: 'custom-check',
evaluate: function () {
evaluate: function (node) {
this.relatedNodes([node]);
this.data('some data');
return true;
},
after: function (results) {
const id = results[0].id;
return results;
},
metadata: {
impact: 'minor',
messages: {
Expand All @@ -235,6 +241,13 @@ var spec: axe.Spec = {
}
}
}
},
{
id: 'async-check',
evaluate: function (node) {
const done = this.async();
done(true);
}
}
],
standards: {
Expand Down Expand Up @@ -264,11 +277,15 @@ var spec: axe.Spec = {
{
id: 'custom-rule',
any: ['custom-check'],
matches: function (node) {
return node.tagName === 'BODY';
},
tags: ['a'],
actIds: ['b'],
metadata: {
description: 'custom rule',
help: 'different help',
helpUrl: 'https://example.com',
tags: ['custom']
helpUrl: 'https://example.com'
}
}
]
Expand Down Expand Up @@ -318,6 +335,10 @@ axe.configure({
incomplete: {
foo: 'bar'
}
},
bar: {
pass: 'pass',
fail: 'fail'
}
}
}
Expand Down Expand Up @@ -360,3 +381,14 @@ var pluginSrc: axe.AxePlugin = {
};
axe.registerPlugin(pluginSrc);
axe.cleanup();

// Utils
const dqElement = new axe.utils.DqElement(document.body);
const element = axe.utils.shadowSelect(dqElement.selector[0]);
const uuid = axe.utils.uuid() as string;

// Commons
axe.commons.aria.getRoleType('img');
axe.commons.dom.isFocusable(document.body);
axe.commons.dom.isNativelyFocusable(document.body);
axe.commons.text.accessibleText(document.body);
Loading