axe.d.ts 14 KB

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