axe.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. type BaseSelector = string;
  39. type CrossTreeSelector = BaseSelector | BaseSelector[];
  40. type CrossFrameSelector = CrossTreeSelector[];
  41. type ContextObject = {
  42. include?: Node | BaseSelector | Array<Node | BaseSelector | BaseSelector[]>;
  43. exclude?: Node | BaseSelector | Array<Node | BaseSelector | BaseSelector[]>;
  44. };
  45. type SerialContextObject = {
  46. include?: BaseSelector | Array<BaseSelector | BaseSelector[]>;
  47. exclude?: BaseSelector | Array<BaseSelector | BaseSelector[]>;
  48. };
  49. type RunCallback = (error: Error, results: AxeResults) => void;
  50. type ElementContext = Node | NodeList | string | ContextObject;
  51. interface TestEngine {
  52. name: string;
  53. version: string;
  54. }
  55. interface TestRunner {
  56. name: string;
  57. }
  58. interface TestEnvironment {
  59. userAgent: string;
  60. windowWidth: number;
  61. windowHeight: number;
  62. orientationAngle?: number;
  63. orientationType?: string;
  64. }
  65. interface RunOnly {
  66. type: RunOnlyType;
  67. values: TagValue[] | string[];
  68. }
  69. interface RuleObject {
  70. [key: string]: {
  71. enabled: boolean;
  72. };
  73. }
  74. interface RunOptions {
  75. runOnly?: RunOnly | TagValue[] | string[] | string;
  76. rules?: RuleObject;
  77. reporter?: ReporterVersion;
  78. resultTypes?: resultGroups[];
  79. selectors?: boolean;
  80. ancestry?: boolean;
  81. xpath?: boolean;
  82. absolutePaths?: boolean;
  83. iframes?: boolean;
  84. elementRef?: boolean;
  85. frameWaitTime?: number;
  86. preload?: boolean;
  87. performanceTimer?: boolean;
  88. pingWaitTime?: number;
  89. }
  90. interface AxeResults extends EnvironmentData {
  91. toolOptions: RunOptions;
  92. passes: Result[];
  93. violations: Result[];
  94. incomplete: Result[];
  95. inapplicable: Result[];
  96. }
  97. interface Result {
  98. description: string;
  99. help: string;
  100. helpUrl: string;
  101. id: string;
  102. impact?: ImpactValue;
  103. tags: TagValue[];
  104. nodes: NodeResult[];
  105. }
  106. interface NodeResult {
  107. html: string;
  108. impact?: ImpactValue;
  109. target: string[];
  110. xpath?: string[];
  111. ancestry?: string[];
  112. any: CheckResult[];
  113. all: CheckResult[];
  114. none: CheckResult[];
  115. failureSummary?: string;
  116. element?: HTMLElement;
  117. }
  118. interface CheckResult {
  119. id: string;
  120. impact: string;
  121. message: string;
  122. data: any;
  123. relatedNodes?: RelatedNode[];
  124. }
  125. interface RelatedNode {
  126. target: string[];
  127. html: string;
  128. }
  129. interface RuleLocale {
  130. [key: string]: {
  131. description: string;
  132. help: string;
  133. };
  134. }
  135. interface CheckLocale {
  136. [key: string]: {
  137. pass: string | { [key: string]: string };
  138. fail: string | { [key: string]: string };
  139. incomplete: string | { [key: string]: string };
  140. };
  141. }
  142. interface Locale {
  143. lang?: string;
  144. rules?: RuleLocale;
  145. checks?: CheckLocale;
  146. }
  147. interface AriaAttrs {
  148. type: AriaAttrsType;
  149. values?: string[];
  150. allowEmpty?: boolean;
  151. global?: boolean;
  152. unsupported?: boolean;
  153. }
  154. interface AriaRoles {
  155. type: AriaRolesType | DpubRolesType;
  156. requiredContext?: string[];
  157. requiredOwned?: string[];
  158. requiredAttrs?: string[];
  159. allowedAttrs?: string[];
  160. nameFromContent?: boolean;
  161. unsupported?: boolean;
  162. }
  163. interface HtmlElmsVariant {
  164. contentTypes?: HtmlContentTypes[];
  165. allowedRoles: boolean | string[];
  166. noAriaAttrs?: boolean;
  167. shadowRoot?: boolean;
  168. implicitAttrs?: { [key: string]: string };
  169. namingMethods?: string[];
  170. }
  171. interface HtmlElms extends HtmlElmsVariant {
  172. variant?: { [key: string]: HtmlElmsVariant };
  173. }
  174. interface Standards {
  175. ariaAttrs?: { [key: string]: AriaAttrs };
  176. ariaRoles?: { [key: string]: AriaRoles };
  177. htmlElms?: { [key: string]: HtmlElms };
  178. cssColors?: { [key: string]: number[] };
  179. }
  180. interface Spec {
  181. branding?: string | Branding;
  182. reporter?: ReporterVersion;
  183. checks?: Check[];
  184. rules?: Rule[];
  185. standards?: Standards;
  186. locale?: Locale;
  187. disableOtherRules?: boolean;
  188. axeVersion?: string;
  189. noHtml?: boolean;
  190. allowedOrigins?: string[];
  191. // Deprecated - do not use.
  192. ver?: string;
  193. }
  194. /**
  195. * @deprecated Use branding: string instead to set the application key in help URLs
  196. */
  197. interface Branding {
  198. brand?: string;
  199. application?: string;
  200. }
  201. interface Check {
  202. id: string;
  203. evaluate?: Function | string;
  204. after?: Function | string;
  205. options?: any;
  206. matches?: string;
  207. enabled?: boolean;
  208. }
  209. interface Rule {
  210. id: string;
  211. selector?: string;
  212. impact?: ImpactValue;
  213. excludeHidden?: boolean;
  214. enabled?: boolean;
  215. pageLevel?: boolean;
  216. any?: string[];
  217. all?: string[];
  218. none?: string[];
  219. tags?: string[];
  220. matches?: string;
  221. reviewOnFail?: boolean;
  222. }
  223. interface AxePlugin {
  224. id: string;
  225. run(...args: any[]): any;
  226. commands: {
  227. id: string;
  228. callback(...args: any[]): void;
  229. }[];
  230. cleanup?(callback: Function): void;
  231. }
  232. interface RuleMetadata {
  233. ruleId: string;
  234. description: string;
  235. help: string;
  236. helpUrl: string;
  237. tags: string[];
  238. }
  239. interface SerialDqElement {
  240. source: string;
  241. nodeIndexes: number[];
  242. selector: CrossFrameSelector;
  243. xpath: string[];
  244. ancestry: CrossFrameSelector;
  245. }
  246. interface PartialRuleResult {
  247. id: string;
  248. result: 'inapplicable';
  249. pageLevel: boolean;
  250. impact: null;
  251. nodes: Array<Record<string, unknown>>;
  252. }
  253. interface PartialResult {
  254. frames: SerialDqElement[];
  255. results: PartialRuleResult[];
  256. environmentData?: EnvironmentData;
  257. }
  258. type PartialResults = Array<PartialResult | null>;
  259. interface FrameContext {
  260. frameSelector: CrossTreeSelector;
  261. frameContext: SerialContextObject;
  262. }
  263. interface Utils {
  264. getFrameContexts: (
  265. context?: ElementContext,
  266. options?: RunOptions
  267. ) => FrameContext[];
  268. shadowSelect: (selector: CrossTreeSelector) => Element | null;
  269. }
  270. interface EnvironmentData {
  271. testEngine: TestEngine;
  272. testRunner: TestRunner;
  273. testEnvironment: TestEnvironment;
  274. url: string;
  275. timestamp: string;
  276. }
  277. let version: string;
  278. let plugins: any;
  279. let utils: Utils;
  280. /**
  281. * Source string to use as an injected script in Selenium
  282. */
  283. let source: string;
  284. /**
  285. * Object for axe Results
  286. */
  287. var AxeResults: AxeResults;
  288. /**
  289. * Runs a number of rules against the provided HTML page and returns the resulting issue list
  290. *
  291. * @param {ElementContext} context Optional The `Context` specification object @see Context
  292. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  293. * @param {RunCallback} callback Optional The function to invoke when analysis is complete.
  294. * @returns {Promise<AxeResults>|void} If the callback was not defined, axe will return a Promise.
  295. */
  296. function run(context?: ElementContext): Promise<AxeResults>;
  297. function run(options: RunOptions): Promise<AxeResults>;
  298. function run(callback: (error: Error, results: AxeResults) => void): void;
  299. function run(context: ElementContext, callback: RunCallback): void;
  300. function run(options: RunOptions, callback: RunCallback): void;
  301. function run(
  302. context: ElementContext,
  303. options: RunOptions
  304. ): Promise<AxeResults>;
  305. function run(
  306. context: ElementContext,
  307. options: RunOptions,
  308. callback: RunCallback
  309. ): void;
  310. /**
  311. * Method for configuring the data format used by axe. Helpful for adding new
  312. * rules, which must be registered with the library to execute.
  313. * @param {Spec} Spec Object with valid `branding`, `reporter`, `checks` and `rules` data
  314. */
  315. function configure(spec: Spec): void;
  316. /**
  317. * Run axe in the current window only
  318. * @param {ElementContext} context Optional The `Context` specification object @see Context
  319. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  320. * @returns {Promise<PartialResult>} Partial result, for use in axe.finishRun.
  321. */
  322. function runPartial(
  323. context: ElementContext,
  324. options: RunOptions
  325. ): Promise<PartialResult>;
  326. /**
  327. * Create a report from axe.runPartial results
  328. * @param {PartialResult[]} partialResults Results from axe.runPartial, calls in different frames on the page.
  329. * @param {RunOptions} options Optional Options passed into rules or checks, temporarily modifying them.
  330. */
  331. function finishRun(
  332. partialResults: PartialResults,
  333. options: RunOptions
  334. ): Promise<AxeResults>;
  335. /**
  336. * Searches and returns rules that contain a tag in the list of tags.
  337. * @param {Array} tags Optional array of tags
  338. * @return {Array} Array of rules
  339. */
  340. function getRules(tags?: string[]): RuleMetadata[];
  341. /**
  342. * Restores the default axe configuration
  343. */
  344. function reset(): void;
  345. /**
  346. * Function to register a plugin configuration in document and its subframes
  347. * @param {Object} plugin A plugin configuration object
  348. */
  349. function registerPlugin(plugin: AxePlugin): void;
  350. /**
  351. * Function to clean up plugin configuration in document and its subframes
  352. */
  353. function cleanup(): void;
  354. /**
  355. * Set up alternative frame communication
  356. */
  357. function frameMessenger(frameMessenger: FrameMessenger): void;
  358. // axe.frameMessenger
  359. type FrameMessenger = {
  360. open: (topicHandler: TopicHandler) => Close | void;
  361. post: (
  362. frameWindow: Window,
  363. data: TopicData,
  364. replyHandler: ReplyHandler
  365. ) => boolean | void;
  366. };
  367. type Close = Function;
  368. type TopicHandler = (data: TopicData, responder: Responder) => void;
  369. type ReplyHandler = (
  370. message: any | Error,
  371. keepalive: boolean,
  372. responder: Responder
  373. ) => void;
  374. type Responder = (
  375. message: any | Error,
  376. keepalive?: boolean,
  377. replyHandler?: ReplyHandler
  378. ) => void;
  379. type TopicData = { topic: string } & ReplyData;
  380. type ReplyData = { channelId: string; message: any; keepalive: boolean };
  381. }
  382. export = axe;