axe.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // Type definitions for axe-core
  2. // Project: https://github.com/dequelabs/axe-core
  3. declare namespace axe {
  4. type ImpactValue = 'minor' | 'moderate' | 'serious' | 'critical' | null;
  5. type TagValue = string;
  6. type ReporterVersion = 'v1' | 'v2' | 'raw' | 'rawEnv' | 'no-passes';
  7. type RunOnlyType = 'rule' | 'rules' | 'tag' | 'tags';
  8. type resultGroups = 'inapplicable' | 'passes' | 'incomplete' | 'violations';
  9. type AriaAttrsType =
  10. | 'boolean'
  11. | 'nmtoken'
  12. | 'mntokens'
  13. | 'idref'
  14. | 'idrefs'
  15. | 'string'
  16. | 'decimal'
  17. | 'int';
  18. type AriaRolesType = 'abstract' | 'widget' | 'structure' | 'landmark';
  19. type DpubRolesType =
  20. | 'section'
  21. | 'landmark'
  22. | 'link'
  23. | 'listitem'
  24. | 'img'
  25. | 'navigation'
  26. | 'note'
  27. | 'separator'
  28. | 'none'
  29. | 'sectionhead';
  30. type HtmlContentTypes =
  31. | 'flow'
  32. | 'sectioning'
  33. | 'heading'
  34. | 'phrasing'
  35. | 'embedded'
  36. | 'interactive';
  37. // Array of length 2 or greater
  38. type MultiArray<T> = [T, T, ...T[]];
  39. // Selectors within a frame
  40. type BaseSelector = string;
  41. type ShadowDomSelector = MultiArray<BaseSelector>;
  42. type CrossTreeSelector = BaseSelector | ShadowDomSelector;
  43. type LabelledShadowDomSelector = { fromShadowDom: ShadowDomSelector };
  44. // Cross-frame selectors
  45. type FramesSelector = Array<CrossTreeSelector | LabelledShadowDomSelector>;
  46. type UnlabelledFrameSelector = CrossTreeSelector[];
  47. type LabelledFramesSelector = { fromFrames: MultiArray<FramesSelector[0]> };
  48. /**
  49. * @deprecated Use UnlabelledFrameSelector instead
  50. */
  51. type CrossFrameSelector = UnlabelledFrameSelector;
  52. // Context options
  53. type Selector =
  54. | Node
  55. | BaseSelector
  56. | LabelledShadowDomSelector
  57. | LabelledFramesSelector;
  58. type SelectorList = Array<Selector | FramesSelector> | NodeList;
  59. type ContextProp = Selector | SelectorList;
  60. type ContextObject =
  61. | {
  62. include: ContextProp;
  63. exclude?: ContextProp;
  64. }
  65. | {
  66. exclude: ContextProp;
  67. include?: ContextProp;
  68. };
  69. type ContextSpec = ContextProp | ContextObject;
  70. /** Synonym to ContextSpec */
  71. type ElementContext = ContextSpec;
  72. type SerialSelector =
  73. | BaseSelector
  74. | LabelledShadowDomSelector
  75. | LabelledFramesSelector;
  76. type SerialFrameSelector = SerialSelector | FramesSelector;
  77. type SerialSelectorList = Array<SerialFrameSelector>;
  78. type SerialContextObject =
  79. | {
  80. include: SerialSelector | SerialSelectorList;
  81. exclude?: SerialSelector | SerialSelectorList;
  82. }
  83. | {
  84. exclude: SerialSelector | SerialSelectorList;
  85. include?: SerialSelector | SerialSelectorList;
  86. };
  87. interface FrameContextObject {
  88. include: UnlabelledFrameSelector[];
  89. exclude: UnlabelledFrameSelector[];
  90. }
  91. type RunCallback<T = AxeResults> = (error: Error, results: T) => void;
  92. interface TestEngine {
  93. name: string;
  94. version: string;
  95. }
  96. interface TestRunner {
  97. name: string;
  98. }
  99. interface TestEnvironment {
  100. userAgent: string;
  101. windowWidth: number;
  102. windowHeight: number;
  103. orientationAngle?: number;
  104. orientationType?: string;
  105. }
  106. interface RunOnly {
  107. type: RunOnlyType;
  108. values: TagValue[] | string[];
  109. }
  110. interface RuleObject {
  111. [key: string]: {
  112. enabled: boolean;
  113. };
  114. }
  115. interface RunOptions {
  116. runOnly?: RunOnly | TagValue[] | string[] | string;
  117. rules?: RuleObject;
  118. reporter?: ReporterVersion | string;
  119. resultTypes?: resultGroups[];
  120. selectors?: boolean;
  121. ancestry?: boolean;
  122. xpath?: boolean;
  123. absolutePaths?: boolean;
  124. iframes?: boolean;
  125. elementRef?: boolean;
  126. frameWaitTime?: number;
  127. preload?: boolean | PreloadOptions;
  128. performanceTimer?: boolean;
  129. pingWaitTime?: number;
  130. }
  131. interface PreloadOptions {
  132. assets: string[];
  133. timeout?: number;
  134. }
  135. interface AxeResults extends EnvironmentData {
  136. toolOptions: RunOptions;
  137. passes: Result[];
  138. violations: Result[];
  139. incomplete: Result[];
  140. inapplicable: Result[];
  141. }
  142. interface Result {
  143. description: string;
  144. help: string;
  145. helpUrl: string;
  146. id: string;
  147. impact?: ImpactValue;
  148. tags: TagValue[];
  149. nodes: NodeResult[];
  150. }
  151. interface NodeResult {
  152. html: string;
  153. impact?: ImpactValue;
  154. target: UnlabelledFrameSelector;
  155. xpath?: string[];
  156. ancestry?: UnlabelledFrameSelector;
  157. any: CheckResult[];
  158. all: CheckResult[];
  159. none: CheckResult[];
  160. failureSummary?: string;
  161. element?: HTMLElement;
  162. }
  163. interface CheckResult {
  164. id: string;
  165. impact: string;
  166. message: string;
  167. data: any;
  168. relatedNodes?: RelatedNode[];
  169. }
  170. interface RelatedNode {
  171. html: string;
  172. target: UnlabelledFrameSelector;
  173. xpath?: string[];
  174. ancestry?: UnlabelledFrameSelector;
  175. element?: HTMLElement;
  176. }
  177. interface RuleLocale {
  178. [key: string]: {
  179. description: string;
  180. help: string;
  181. };
  182. }
  183. interface CheckMessages {
  184. pass: string | { [key: string]: string };
  185. fail: string | { [key: string]: string };
  186. incomplete?: string | { [key: string]: string };
  187. }
  188. interface CheckLocale {
  189. [key: string]: CheckMessages;
  190. }
  191. interface Locale {
  192. lang?: string;
  193. rules?: RuleLocale;
  194. checks?: CheckLocale;
  195. }
  196. interface AriaAttrs {
  197. type: AriaAttrsType;
  198. values?: string[];
  199. allowEmpty?: boolean;
  200. global?: boolean;
  201. unsupported?: boolean;
  202. }
  203. interface AriaRoles {
  204. type: AriaRolesType | DpubRolesType;
  205. requiredContext?: string[];
  206. requiredOwned?: string[];
  207. requiredAttrs?: string[];
  208. allowedAttrs?: string[];
  209. nameFromContent?: boolean;
  210. unsupported?: boolean;
  211. }
  212. interface HtmlElmsVariant {
  213. contentTypes?: HtmlContentTypes[];
  214. allowedRoles: boolean | string[];
  215. noAriaAttrs?: boolean;
  216. shadowRoot?: boolean;
  217. implicitAttrs?: { [key: string]: string };
  218. namingMethods?: string[];
  219. }
  220. interface HtmlElms extends HtmlElmsVariant {
  221. variant?: { [key: string]: HtmlElmsVariant };
  222. }
  223. interface Standards {
  224. ariaAttrs?: { [key: string]: AriaAttrs };
  225. ariaRoles?: { [key: string]: AriaRoles };
  226. htmlElms?: { [key: string]: HtmlElms };
  227. cssColors?: { [key: string]: number[] };
  228. }
  229. interface Spec {
  230. branding?: string | Branding;
  231. reporter?: ReporterVersion | string | AxeReporter;
  232. checks?: Check[];
  233. rules?: Rule[];
  234. standards?: Standards;
  235. locale?: Locale;
  236. disableOtherRules?: boolean;
  237. axeVersion?: string;
  238. noHtml?: boolean;
  239. allowedOrigins?: string[];
  240. // Deprecated - do not use.
  241. ver?: string;
  242. }
  243. /**
  244. * @deprecated Use branding: string instead to set the application key in help URLs
  245. */
  246. interface Branding {
  247. brand?: string;
  248. application?: string;
  249. }
  250. interface CheckHelper {
  251. async: () => (result: boolean | undefined | Error) => void;
  252. data: (data: unknown) => void;
  253. relatedNodes: (nodes: Element[]) => void;
  254. }
  255. interface AfterResult {
  256. id: string;
  257. data?: unknown;
  258. relatedNodes: SerialDqElement[];
  259. result: boolean | undefined;
  260. node: SerialDqElement;
  261. }
  262. interface Check {
  263. id: string;
  264. evaluate?:
  265. | string
  266. | ((
  267. this: CheckHelper,
  268. node: Element,
  269. options: unknown,
  270. virtualNode: VirtualNode
  271. ) => boolean | undefined | void);
  272. after?:
  273. | string
  274. | ((results: AfterResult[], options: unknown) => AfterResult[]);
  275. options?: any;
  276. matches?: string;
  277. enabled?: boolean;
  278. metadata?: {
  279. impact?: ImpactValue;
  280. messages?: CheckMessages;
  281. };
  282. }
  283. interface Rule {
  284. id: string;
  285. selector?: string;
  286. impact?: ImpactValue;
  287. excludeHidden?: boolean;
  288. enabled?: boolean;
  289. pageLevel?: boolean;
  290. any?: string[];
  291. all?: string[];
  292. none?: string[];
  293. tags?: string[];
  294. matches?: string | ((node: Element, virtualNode: VirtualNode) => boolean);
  295. reviewOnFail?: boolean;
  296. actIds?: string[];
  297. metadata?: Omit<RuleMetadata, 'ruleId' | 'tags' | 'actIds'>;
  298. }
  299. interface AxePlugin {
  300. id: string;
  301. run(...args: any[]): any;
  302. commands: {
  303. id: string;
  304. callback(...args: any[]): void;
  305. }[];
  306. cleanup?(callback: Function): void;
  307. }
  308. interface RuleMetadata {
  309. ruleId: string;
  310. description: string;
  311. help: string;
  312. helpUrl: string;
  313. tags: string[];
  314. actIds?: string[];
  315. }
  316. interface SerialDqElement {
  317. source: string;
  318. nodeIndexes: number[];
  319. selector: UnlabelledFrameSelector;
  320. xpath: string[];
  321. ancestry: UnlabelledFrameSelector;
  322. }
  323. interface DqElement extends SerialDqElement {
  324. element: Element;
  325. toJSON(): SerialDqElement;
  326. mergeSpecs(
  327. childSpec: SerialDqElement,
  328. parentSpec: SerialDqElement
  329. ): SerialDqElement;
  330. }
  331. interface PartialRuleResult {
  332. id: string;
  333. result: 'inapplicable';
  334. pageLevel: boolean;
  335. impact: null;
  336. nodes: Array<Record<string, unknown>>;
  337. }
  338. interface PartialResult {
  339. frames: SerialDqElement[];
  340. results: PartialRuleResult[];
  341. environmentData?: EnvironmentData;
  342. }
  343. type PartialResults = Array<PartialResult | null>;
  344. interface FrameContext {
  345. frameSelector: CrossTreeSelector;
  346. frameContext: FrameContextObject;
  347. }
  348. interface RawCheckResult extends Omit<CheckResult, 'relatedNodes'> {
  349. relatedNodes?: Array<SerialDqElement | DqElement>;
  350. }
  351. interface RawNodeResult<T extends 'passed' | 'failed' | 'incomplete'> {
  352. node: SerialDqElement | DqElement;
  353. any: RawCheckResult[];
  354. all: RawCheckResult[];
  355. none: RawCheckResult[];
  356. impact: ImpactValue | null;
  357. result: T;
  358. }
  359. interface RawResult extends Omit<Result, 'nodes'> {
  360. inapplicable: Array<never>;
  361. passes: RawNodeResult<'passed'>[];
  362. incomplete: RawNodeResult<'incomplete'>[];
  363. violations: RawNodeResult<'failed'>[];
  364. pageLevel: boolean;
  365. result: 'failed' | 'passed' | 'incomplete' | 'inapplicable';
  366. }
  367. type AxeReporter<T = unknown> = (
  368. rawResults: RawResult[],
  369. option: RunOptions,
  370. resolve: (report: T) => void,
  371. reject: (error: Error) => void
  372. ) => void;
  373. interface VirtualNode {
  374. actualNode?: Node;
  375. shadowId?: string;
  376. children?: VirtualNode[];
  377. parent?: VirtualNode;
  378. attr(attr: string): string | null;
  379. hasAttr(attr: string): boolean;
  380. props: { [key: string]: unknown };
  381. boundingClientRect: DOMRect;
  382. }
  383. interface Utils {
  384. getFrameContexts: (
  385. context?: ElementContext,
  386. options?: RunOptions
  387. ) => FrameContext[];
  388. shadowSelect: (selector: CrossTreeSelector) => Element | null;
  389. shadowSelectAll: (selector: CrossTreeSelector) => Element[];
  390. getStandards(): Required<Standards>;
  391. isContextSpec: (context: unknown) => context is ContextSpec;
  392. isContextObject: (context: unknown) => context is ContextObject;
  393. isContextProp: (context: unknown) => context is ContextProp;
  394. isLabelledFramesSelector: (
  395. selector: unknown
  396. ) => selector is LabelledFramesSelector;
  397. isLabelledShadowDomSelector: (
  398. selector: unknown
  399. ) => selector is LabelledShadowDomSelector;
  400. DqElement: new (
  401. elm: Element,
  402. options?: { absolutePaths?: boolean }
  403. ) => DqElement;
  404. uuid: (
  405. options?: { random?: Uint8Array | Array<number> },
  406. buf?: Uint8Array | Array<number>,
  407. offset?: number
  408. ) => string | Uint8Array | Array<number>;
  409. }
  410. interface Aria {
  411. getRoleType: (role: string | Element | VirtualNode | null) => string | null;
  412. }
  413. interface Dom {
  414. isFocusable: (node: Element | VirtualNode) => boolean;
  415. isNativelyFocusable: (node: Element | VirtualNode) => boolean;
  416. }
  417. type AccessibleTextOptions = {
  418. inControlContext?: boolean;
  419. inLabelledByContext?: boolean;
  420. };
  421. interface Text {
  422. accessibleText: (
  423. element: Element,
  424. options?: AccessibleTextOptions
  425. ) => string;
  426. }
  427. interface Commons {
  428. aria: Aria;
  429. dom: Dom;
  430. text: Text;
  431. }
  432. interface EnvironmentData {
  433. testEngine: TestEngine;
  434. testRunner: TestRunner;
  435. testEnvironment: TestEnvironment;
  436. url: string;
  437. timestamp: string;
  438. }
  439. let version: string;
  440. let plugins: any;
  441. let utils: Utils;
  442. let commons: Commons;
  443. /**
  444. * Source string to use as an injected script in Selenium
  445. */
  446. let source: string;
  447. /**
  448. * Object for axe Results
  449. */
  450. var AxeResults: AxeResults;
  451. /**
  452. * Runs a number of rules against the provided HTML page and returns the resulting issue list
  453. *
  454. * @param {ElementContext} context Optional The `Context` specification object @see Context
  455. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  456. * @param {RunCallback} callback Optional The function to invoke when analysis is complete.
  457. * @returns {Promise<AxeResults>|void} If the callback was not defined, axe will return a Promise.
  458. */
  459. function run<T = AxeResults>(context?: ElementContext): Promise<T>;
  460. function run<T = AxeResults>(options: RunOptions): Promise<T>;
  461. function run<T = AxeResults>(
  462. callback: (error: Error, results: T) => void
  463. ): void;
  464. function run<T = AxeResults>(
  465. context: ElementContext,
  466. callback: RunCallback<T>
  467. ): void;
  468. function run<T = AxeResults>(
  469. options: RunOptions,
  470. callback: RunCallback<T>
  471. ): void;
  472. function run<T = AxeResults>(
  473. context: ElementContext,
  474. options: RunOptions
  475. ): Promise<T>;
  476. function run<T = AxeResults>(
  477. context: ElementContext,
  478. options: RunOptions,
  479. callback: RunCallback<T>
  480. ): void;
  481. /**
  482. * Method for configuring the data format used by axe. Helpful for adding new
  483. * rules, which must be registered with the library to execute.
  484. * @param {Spec} Spec Object with valid `branding`, `reporter`, `checks` and `rules` data
  485. */
  486. function configure(spec: Spec): void;
  487. /**
  488. * Run axe in the current window only
  489. * @param {ElementContext} context Optional The `Context` specification object @see Context
  490. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  491. * @returns {Promise<PartialResult>} Partial result, for use in axe.finishRun.
  492. */
  493. function runPartial(
  494. context: ElementContext,
  495. options: RunOptions
  496. ): Promise<PartialResult>;
  497. /**
  498. * Create a report from axe.runPartial results
  499. * @param {PartialResult[]} partialResults Results from axe.runPartial, calls in different frames on the page.
  500. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  501. */
  502. function finishRun(
  503. partialResults: PartialResults,
  504. options: RunOptions
  505. ): Promise<AxeResults>;
  506. /**
  507. * Searches and returns rules that contain a tag in the list of tags.
  508. * @param {Array} tags Optional array of tags
  509. * @return {Array} Array of rules
  510. */
  511. function getRules(tags?: string[]): RuleMetadata[];
  512. /**
  513. * Restores the default axe configuration
  514. */
  515. function reset(): void;
  516. /**
  517. * Function to register a plugin configuration in document and its subframes
  518. * @param {Object} plugin A plugin configuration object
  519. */
  520. function registerPlugin(plugin: AxePlugin): void;
  521. /**
  522. * Function to clean up plugin configuration in document and its subframes
  523. */
  524. function cleanup(): void;
  525. /**
  526. * Set up alternative frame communication
  527. */
  528. function frameMessenger(frameMessenger: FrameMessenger): void;
  529. /**
  530. * Setup axe-core so axe.common functions can work properly.
  531. */
  532. function setup(node?: Element | Document): VirtualNode;
  533. /**
  534. * Clean up axe-core tree and caches. `axe.run` will call this function at the end of the run so there's no need to call it yourself afterwards.
  535. */
  536. function teardown(): void;
  537. /**
  538. * Check if a reporter is registered
  539. */
  540. function hasReporter(reporterName: string): boolean;
  541. /**
  542. * Get a reporter based the name it is registered with
  543. */
  544. function getReporter<T>(reporterName: string): AxeReporter<T>;
  545. /**
  546. * Register a new reporter, optionally setting it as the default
  547. */
  548. function addReporter<T>(
  549. reporterName: string,
  550. reporter: AxeReporter<T>,
  551. isDefault?: boolean
  552. ): void;
  553. // axe.frameMessenger
  554. type FrameMessenger = {
  555. open: (topicHandler: TopicHandler) => Close | void;
  556. post: (
  557. frameWindow: Window,
  558. data: TopicData,
  559. replyHandler: ReplyHandler
  560. ) => boolean | void;
  561. };
  562. type Close = Function;
  563. type TopicHandler = (data: TopicData, responder: Responder) => void;
  564. type ReplyHandler = (
  565. message: any | Error,
  566. keepalive: boolean,
  567. responder: Responder
  568. ) => void;
  569. type Responder = (
  570. message: any | Error,
  571. keepalive?: boolean,
  572. replyHandler?: ReplyHandler
  573. ) => void;
  574. type TopicData = { topic: string } & ReplyData;
  575. type ReplyData = { channelId: string; message: any; keepalive: boolean };
  576. }
  577. export = axe;