web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
23 KiB

  1. # Axe Developer Guide
  2. Axe runs a series of tests to check for accessibility of content and functionality on a website. A test is made up of a series of Rules which are, themselves, made up of Checks. Axe executes these Rules asynchronously and, when the Rules are finished running, runs a callback function which is passed a Result structure. Since some Rules run on the page level while others do not, tests will also run in one of two ways. If a document is specified, the page level rules will run, otherwise they will not.
  3. Axe 3.0 supports open Shadow DOM: see our virtual DOM APIs and test utilities for developing axe-core moving forward. Note: we do not and cannot support closed Shadow DOM.
  4. 1. [Getting Started](#getting-started)
  5. 1. [Environment Pre-requisites](#environment-pre-requisites)
  6. 1. [Building axe.js](#building-axejs)
  7. 1. [Running Tests](#running-tests)
  8. 1. [API Reference](#api-reference)
  9. 1. [Supported CSS Selectors](#supported-css-selectors)
  10. 1. [Architecture Overview](#architecture-overview)
  11. 1. [Rules](#rules)
  12. 1. [Checks](#checks)
  13. 1. [Common Functions](#common-functions)
  14. 1. [Virtual Nodes](#virtual-nodes)
  15. 1. [Core Utilities](#core-utilities)
  16. 1. [Virtual DOM APIs](#virtual-dom-apis)
  17. 1. [API Name: axe.utils.getFlattenedTree](#api-name-axeutilsgetflattenedtree)
  18. 1. [API Name: axe.utils.getNodeFromTree](#api-name-axeutilsgetnodefromtree)
  19. 1. [Test Utilities](#test-utilities)
  20. 1. [Test Util Name: axe.testUtils.MockCheckContext](#test-util-name-mockcheckcontext)
  21. 1. [Test Util Name: axe.testUtils.shadowSupport](#test-util-name-shadowsupport)
  22. 1. [Test Util Name: axe.testUtils.fixtureSetup](#test-util-name-fixturesetup)
  23. 1. [Test Util Name: axe.testUtils.checkSetup](#test-util-name-checksetup)
  24. 1. [Using Rule Generation CLI](#using-rule-generation-cli)
  25. ## Getting Started
  26. ### Environment Pre-requisites
  27. 1. You must have NodeJS installed.
  28. 1. Install npm development dependencies. In the root folder of your axe-core repository, run `npm install`
  29. ### Building axe.js
  30. To build axe.js, simply run `npm run build` in the root folder of the axe-core repository. axe.js and axe.min.js are placed into the root folder.
  31. ### Running Tests
  32. To run all tests from the command line you can run `npm test`, which will run all unit and integration tests using headless chrome and Selenium Webdriver.
  33. You can also load tests in any supported browser, which is helpful for debugging. Tests require a local server to run, you must first start a local server to serve files. You can use Grunt to start one by running `npm start`. Once your local server is running you can load the following pages in any browser to run tests:
  34. 1. [Core Tests](../test/core/)
  35. 2. [Commons Tests](../test/commons/)
  36. 3. [Check Tests](../test/checks/)
  37. 4. [Rule Matches](../test/rule-matches/)
  38. 5. [Integration Tests](../test/integration/rules/)
  39. 6. There are additional tests located in [test/integration/full/](../test/integration/full/) for tests that need to be run against their own document.
  40. ### API Reference
  41. [See API exposed on axe](./API.md#section-2-api-reference)
  42. ### Supported CSS Selectors
  43. Axe supports the following CSS selectors:
  44. - Type, Class, ID, and Universal selectors. E.g `div.main, #main`
  45. - Pseudo selector `not`. E.g `th:not([scope])`
  46. - Descendant and Child combinators. E.g. `table td`, `ul > li`
  47. - Attribute selectors `=`, `^=`, `$=`, `*=`. E.g `a[href^="#"]`
  48. ## Architecture Overview
  49. Axe tests for accessibility using objects called Rules. Each Rule tests for a high-level aspect of accessibility, such as color contrast, button labels, and alternate text for images. Each rule is made up of a series of Checks. Depending on the rule; all, some, or none of these checks must pass in order for the rule to pass.
  50. Upon execution, a Rule runs each of its Checks against all relevant nodes. Which nodes are relevant is determined by the Rule's `selector` property and `matches` function. If a Rule has no Checks that apply to a given node, the Rule will result in an inapplicable result.
  51. After execution, a Check will return `true`, `false`, or `undefined` depending on whether or not the tested condition was satisfied. The result, as well as more information on what caused the Check to pass or fail, will be stored in either the `passes`, `violations`, or `incomplete` arrays.
  52. ### Rules
  53. Rules are defined by JSON files in the [lib/rules directory](../lib/rules). The JSON object is used to seed the [Rule object](../lib/core/base/rule.js#L30). A valid Rule JSON consists of the following:
  54. - `id` - `String` A unique name of the Rule.
  55. - `selector` - **optional** `String` which is a [CSS selector](#supported-css-selectors) that specifies the elements of the page on which the Rule runs. axe-core will look inside of the light DOM and _open_ [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM) trees for elements matching the provided selector. If omitted, the rule will run against every node.
  56. - `excludeHidden` - **optional** `Boolean` Whether the rule should exclude hidden elements. Defaults to `true`.
  57. - `enabled` - **optional** `Boolean` Whether the rule is enabled by default. Defaults to `true`.
  58. - `pageLevel` - **optional** `Boolean` Whether the rule is page level. Page level rules will only run if given an entire `document` as context.
  59. - `matches` - **optional** `String` The ID of the filtering function that will exclude elements that match the `selector` property. See the [`metadata-function-map`](../lib/core/base/metadata-function-map.js) file for all defined IDs.
  60. - `impact` - **optional** `String` (one of `minor`, `moderate`, `serious`, or `critical`). Override the impact defined by checks.
  61. - `tags` - **optional** `Array` Strings of the accessibility guidelines of which the Rule applies.
  62. - `metadata` - `Object` Consisting of:
  63. - `description` - `String` Text string that describes what the rule does.
  64. - `helpUrl` - `String` **optional** URL that provides more information about the specifics of the violation. Links to a page on the Deque University site.
  65. - `help` - `String` Help text that describes the test that was performed.
  66. - `any` - `Array` Checks that make up this Rule; one of these checks must return `true` for a Rule to pass.
  67. - `all` - `Array` Checks that make up this Rule; all these checks must return `true` for a Rule to pass.
  68. - `none` - `Array` Checks that make up this Rule; none of these checks must return `true` for a Rule to pass.
  69. The `any`, `all` and `none` arrays must contain either a `String` which references the `id` of the Check; or an object of the following format:
  70. - `id` - `String` The unique ID of the Check.
  71. - `options` - `Mixed` Any options the Check requires that are specific to the Rule.
  72. There is a Grunt target which will ensure each Rule has a valid format, which can be run with `npx grunt validate`.
  73. #### Matches Function
  74. A Rule's `matches` function is executed against each node which matches the Rule's `selector` and receive two parameters:
  75. - `node` – node, the DOM Node to test
  76. - `virtualNode`– object, the virtual DOM representation of the node. See [virtualNode documentation](#virtual-nodes) for more.
  77. The matches function must return either `true` or `false`. Common functions are provided as `commons`. [See the data-table matches function for an example.](../lib/rules/data-table-matches.js)
  78. ### Checks
  79. Similar to Rules, Checks are defined by JSON files in the [lib/checks directory](../lib/checks). The JSON object is used to seed the [Check object](../lib/core/base/check.js). A valid Check JSON consists of the following:
  80. - `id` - `String` A unique name of the Check
  81. - `evaluate` - `String` The ID of the function that implements the check's functionality. See the [`metadata-function-map`](../lib/core/base/metadata-function-map.js) file for all defined IDs.
  82. - `after` - **optional** `String` The ID of the function that gets called for checks that operate on a page-level basis, to process the results from the iframes.
  83. - `options` - **optional** `Object` Any information the Check needs that you might need to customize and/or is locale specific. Options can be overridden at runtime (with the options parameter) or config-time. For example, the [valid-lang](../lib/checks/language/valid-lang.json) Check defines what ISO 639-1 language codes it should accept as valid. Options do not need to follow any specific format or type; it is up to the author of a Check to determine the most appropriate format.
  84. - `metadata` - `Object` Consisting of:
  85. - `impact` - `String` (one of `minor`, `moderate`, `serious`, or `critical`)
  86. - `messages` - `Object` These messages are displayed when the Check passes or fails
  87. - `pass` - `String` [doT.js](http://olado.github.io/doT/) template string displayed when the Check passes
  88. - `fail` - `String` [doT.js](http://olado.github.io/doT/) template string displayed when the Check fails
  89. - `incomplete``String|Object` – [doT.js](http://olado.github.io/doT/) template string displayed when the Check is incomplete OR an object with `missingData` on why it returned incomplete. Refer to [rules.md](./rule-development.md).
  90. #### Check `evaluate`
  91. A Check's evaluate function is run a special context in order to give access to APIs which provide more information. Checks will run against a single node and do not have access to other frames. A Check must either return `true` or `false`.
  92. The following variables are defined for `Check#evaluate`:
  93. - `node` - `HTMLElement` The element that the Check is run against
  94. - `options` - `Mixed` Any options specific to this Check that may be necessary. If not specified by the user at run-time or configure-time; it will use `options` as defined by the Check's JSON file.
  95. - `virtualNode``Object` The virtualNode object for use with Shadow DOM. See [virtualNode documentation](#virtual-nodes).
  96. - `this.data()` - `Function` Free-form data that either the Check message requires or is presented as `data` in the CheckResult object. Subsequent calls to `this.data()` will overwrite previous. See [aria-valid-attr](../lib/checks/aria/aria-valid-attr-value-evaluate.js) for example usage.
  97. - `this.relatedNodes()` - `Function` Array or NodeList of elements that are related to this Check. For example the [duplicate-id](../lib/checks/parsing/duplicate-id-evaluate.js) Check will add all Elements which share the same ID.
  98. #### Check `after`
  99. You can use the `after` function to evaluate nodes that might be in other frames or to filter the number of violations or passes produced. The `after` function runs once for each Rule in the top-most (or originating) frame. Due to this, you should not perform DOM operations in after functions, but instead operate on `data` defined by the Check.
  100. For example, the [duplicate-id](../lib/checks/parsing/duplicate-id.json) Check include an [after function](../lib/checks/parsing/duplicate-id-after.js) which reduces the number of violations so that only one violation per instance of a duplicate ID is found.
  101. The following variables are defined for `Check#after`:
  102. - `results` - `Array` Contains [CheckResults](#checkresult) for every matching node.
  103. The after function must return an `Array` of CheckResults, due to this, it is a very common pattern to just use `Array#filter` to filter results:
  104. ```js
  105. var uniqueIds = [];
  106. return results.filter(function(r) {
  107. if (uniqueIds.indexOf(r.data) === -1) {
  108. uniqueIds.push(r.data);
  109. return true;
  110. }
  111. return false;
  112. });
  113. ```
  114. #### Pass, Fail and Incomplete Templates
  115. Occasionally, you may want to add additional information about why a Check passed, failed or returned undefined into its message. For example, the [aria-valid-attr](../lib/checks/aria/valid-attr.json) will add information about any invalid ARIA attributes to its fail message. The message uses a [custom message format](./check-message-template.md). In the Check message, you have access to the `data` object as `data`.
  116. ```js
  117. // aria-valid-attr check
  118. "messages": {
  119. "pass": "ARIA attributes are used correctly for the defined role",
  120. "fail": {
  121. "singular": "ARIA attribute is not allowed: ${data.values}",
  122. "plural": "ARIA attributes are not allowed: ${data.values}"
  123. },
  124. "incomplete": "axe-core couldn't tell because of ${data.missingData}"
  125. }
  126. ```
  127. See [Developing Axe-core Rules](./rule-development.md) for more information
  128. on writing rules and checks, including incomplete results.
  129. #### CheckResult
  130. When a Check is executed, its result is then added to a [CheckResult object](../lib/core/base/check-result.js). Much like the RuleResult object, the CheckResult object only contains information that is required to determine whether a Check, and its parent Rule passed or failed. `metadata` from the originating Check is combined later and will not be available until axe reaches the reporting stage.
  131. A CheckResult has the following properties:
  132. - `id` - `String` The ID of the Check this CheckResult belongs to.
  133. - `data` - `Mixed` Any data the Check's evaluate function added with `this.data()`. Typically used to insert data from analysis into a message or to perform further tests in the post-processing function.
  134. - `relatedNodes` - `Array` Nodes that are related to the current Check as defined by [check.evaluate](#check-evaluate).
  135. - `result` - `Boolean` The return value of [check.evaluate](#check-evaluate).
  136. ### Common Functions
  137. Common functions are an internal library used by the rules and checks. If you have code repeated across rules and checks, you can use these functions and contribute to them. Documentation is available in [source code](../lib/commons/).
  138. #### Commons and Shadow DOM
  139. To support open Shadow DOM while maintaining backwards compatibility, commons functions that query DOM nodes must operate on an in-memory representation of the DOM using axe-core’s built-in [API methods and utility functions](./API.md#virtual-dom-utilities).
  140. Commons functions should do the virtual tree lookup and call a `virtual` function including the rest of the commons code. The naming of this special function should contain the original commons function name with `Virtual` added to signify it expects to operate on a virtual DOM tree.
  141. Let’s look at an example:
  142. ```js
  143. // lib/commons/text/accessible-text.js
  144. import { getNodeFromTree } from '../../core/utils';
  145. import accessibleTextVirtual from './accessible-text-virtual';
  146. function accessibleText(element, inLabelledbyContext) {
  147. let virtualNode = getNodeFromTree(axe._tree[0], element); // throws an exception on purpose if axe._tree not correct
  148. return accessibleTextVirtual(virtualNode, inLabelledbyContext);
  149. }
  150. export default accessibleText;
  151. // lib/commons/text/accessible-text-virtual.js
  152. function accessibleTextVirtual(element, inLabelledbyContext) {
  153. // rest of the commons code minus the virtual tree lookup, since it’s passed in
  154. }
  155. ```
  156. `accessibleTextVirtual` would only be called directly if you’ve got a virtual node you can use. If you don’t already have one, call the `accessibleText` lookup function, which passes on a virtual DOM node with both the light DOM and Shadow DOM (if applicable).
  157. ### Virtual Nodes
  158. To support open Shadow DOM, axe-core has the ability to handle virtual nodes in [rule matches](#matches-function) and [check evaluate](#check-evaluate) functions. The full set of API methods for Shadow DOM can be found in the [API documentation](./API.md#virtual-dom-utilities), but the general structure for a virtualNode is as follows:
  159. ```js
  160. {
  161. actualNode: <HTMLElement>,
  162. children: <Array>,
  163. parent: <VirtualNode>,
  164. shadowId: <String>,
  165. attr: <Function>,
  166. hasAttr: <Function>,
  167. props: <Object>,
  168. }
  169. ```
  170. - A virtualNode is an object containing an HTML DOM element (`actualNode`).
  171. - Children contains an array of child VirtualNodes.
  172. - Parent is the VirtualNode parent
  173. - The shadowID indicates whether the node is in an open shadow root and if it is, which one it is inside the boundary.
  174. - Attr is a function which returns the value of the passed in attribute, similar to `node.getAttribute()` (e.g. `vNode.attr('aria-label')`)
  175. - HasAttr is a function which returns true if the VirtualNode has the attribute, similar to `node.hasAttribute()` (e.g. `vNode.hasAttr('aria-label')`)
  176. - Props is an object of HTML DOM element properties. The general structure is as follows:
  177. ```js
  178. {
  179. nodeName: <String>,
  180. nodeType: <Number>,
  181. id: <String>,
  182. nodeValue: <String>
  183. }
  184. ```
  185. ### Core Utilities
  186. Core Utilities are an internal library that provides axe with functionality used throughout its core processes. Most notably among these are the queue function and the DqElement constructor.
  187. #### Common Utility Functions
  188. In addition to the ARIA lookupTable, there are also utility functions on the axe.commons.aria and axe.commons.dom namespaces:
  189. - `axe.commons.aria.implicitRole` - Get the implicit role for a given node
  190. - `axe.commons.aria.label` - Gets the accessible ARIA label text of a given element
  191. - `axe.commons.dom.isVisible` - Determine whether an element is visible
  192. #### Queue Function
  193. The queue function creates an asynchronous "queue", list of functions to be invoked in parallel, but not necessarily returned in order. The queue function returns an object with the following methods:
  194. - `defer(func)` Defer a function that may or may not run asynchronously
  195. - `then(callback)` The callback to execute once all "deferred" functions have completed. Will only be invoked once.
  196. - `abort()` Abort the "queue" and prevent `then` function from firing
  197. #### DqElement Class
  198. The DqElement is a "serialized" `HTMLElement`. It will calculate the CSS selector, grab the source outerHTML and offer an array for storing frame paths. The DqElement class takes the following parameters:
  199. - `Element` - `HTMLElement` The element to serialize
  200. - `Spec` - `Object` Properties to use in place of the element when instantiated on Elements from other frames
  201. ```js
  202. var firstH1 = document.getElementByTagName('h1')[0];
  203. var dqH1 = new axe.utils.DqElement(firstH1);
  204. ```
  205. Elements returned by the DqElement class have the following methods and properties:
  206. - `selector` - `string` A unique CSS selector for the element
  207. - `source` - `string` The generated HTML source code of the element
  208. - `element` - `DOMNode` The element which this object is based off or the containing frame, used for sorting.
  209. - `toJSON()` - Returns an object containing the selector and source properties
  210. ## Virtual DOM APIs
  211. Note: You shouldn’t need the Shadow DOM APIs below unless you’re working on the axe-core
  212. engine, as rules and checks already have `virtualNode` objects passed in. However, these APIs
  213. will make it easier to work with the virtual DOM.
  214. ### API Name: axe.utils.getFlattenedTree
  215. #### Description
  216. Recursively return an array containing the virtual DOM tree for the node specified, excluding comment nodes
  217. and shadow DOM nodes `<content>` and `<slot>`. This method will return a flattened tree containing both
  218. light and shadow DOM, if applicable.
  219. #### Synopsis
  220. ```js
  221. var element = document.body;
  222. axe.utils.getFlattenedTree(element, shadowId);
  223. ```
  224. #### Parameters
  225. - `node` – HTMLElement. The current HTML node for which you want a flattened DOM tree.
  226. - `shadowId` – string(optional). ID of the shadow DOM that is the closest shadow ancestor of the node
  227. #### Returns
  228. An array of objects, where each object is a virtualNode:
  229. ```js
  230. [
  231. {
  232. actualNode: body,
  233. children: [virtualNodes],
  234. shadowId: undefined
  235. }
  236. ];
  237. ```
  238. ### API Name: axe.utils.getNodeFromTree
  239. #### Description
  240. Recursively return a single node from a virtual DOM tree. This is commonly used in rules and checks where the node is readily available without querying the DOM.
  241. #### Synopsis
  242. ```js
  243. axe.utils.getNodeFromTree(axe._tree[0], node);
  244. ```
  245. #### Parameters
  246. - `vNode` – object. The flattened DOM tree to fetch a virtual node from
  247. - `node` – HTMLElement. The HTML DOM node for which you need a virtual representation
  248. #### Returns
  249. A virtualNode object:
  250. ```js
  251. {
  252. actualNode: div,
  253. children: [virtualNodes],
  254. shadowId: undefined
  255. }
  256. ```
  257. ## Test Utilities
  258. All tests must support open Shadow DOM, so we created some test utilities to make this easier.
  259. ### Test Util Name: MockCheckContext
  260. Create a check context for mocking and resetting data and relatedNodes in tests.
  261. #### Synopsis
  262. ```js
  263. describe('region', function() {
  264. var fixture = document.getElementById('fixture');
  265. var checkContext = new axe.testUtils.MockCheckContext();
  266. afterEach(function() {
  267. fixture.innerHTML = '';
  268. checkContext.reset();
  269. });
  270. it('should return true when all content is inside the region', function() {
  271. assert.isTrue(checks.region.evaluate.apply(checkContext, checkArgs));
  272. assert.equal(checkContext._relatedNodes.length, 0);
  273. });
  274. });
  275. ```
  276. #### Parameters
  277. None
  278. #### Returns
  279. An object containing the data, relatedNodes, and a way to reset them.
  280. ```js
  281. {
  282. data: (){},
  283. relatedNodes: (){},
  284. reset: (){}
  285. }
  286. ```
  287. ### Test Util Name: shadowSupport
  288. Provides an API for determining Shadow DOM v0 and v1 support in tests. For example: PhantomJS doesn't have Shadow DOM support, while some browsers do.
  289. #### Synopsis
  290. ```js
  291. (axe.testUtils.shadowSupport.v1 ? it : xit)(
  292. 'should test Shadow tree content',
  293. function() {
  294. // The rest of the shadow DOM test
  295. }
  296. );
  297. ```
  298. #### Parameters
  299. None
  300. #### Returns
  301. An object containing booleans for the following Shadow DOM supports: `v0`, `v1`, or `undefined`.
  302. ### Test Util Name: fixtureSetup
  303. Method for injecting content into a fixture and caching the flattened DOM tree (light and Shadow DOM together).
  304. #### Synopsis
  305. ```js
  306. it(
  307. 'should return true if there is only one ' +
  308. type +
  309. ' element with the same name',
  310. function() {
  311. axe.testUtils.fixtureSetup(
  312. '<input type="' +
  313. type +
  314. '" id="target" name="uniqueyname">' +
  315. '<input type="' +
  316. type +
  317. '" name="differentname">'
  318. );
  319. var node = fixture.querySelector('#target');
  320. assert.isTrue(check.evaluate.call(checkContext, node));
  321. }
  322. );
  323. ```
  324. #### Parameters
  325. - `content` – Node|String. Stuff to go into the fixture (html or DOM node)
  326. #### Returns
  327. An HTML Element for the fixture
  328. ### Test Util Name: checkSetup
  329. Create check arguments.
  330. #### Synopsis
  331. ```js
  332. it('should return true when all content is inside the region', function() {
  333. var checkArgs = checkSetup(
  334. '<div id="target"><div role="main"><a href="a.html#mainheader">Click Here</a><div><h1 id="mainheader" tabindex="0">Introduction</h1></div></div></div>'
  335. );
  336. assert.isTrue(checks.region.evaluate.apply(checkContext, checkArgs));
  337. assert.equal(checkContext._relatedNodes.length, 0);
  338. });
  339. ```
  340. #### Parameters
  341. - `content` – String|Node. Stuff to go into the fixture (html or node)
  342. - `options` – Object. Options argument for the check (optional, default: {})
  343. - `target` – String. Target for the check, CSS selector (default: '#target')
  344. #### Returns
  345. An array with the DOM Node, options and virtualNode
  346. ```js
  347. [node, options, virtualNode];
  348. ```
  349. ## Using Rule Generation CLI
  350. Axe provides a CLI for generating the necessary files and configuration assets for authoring a rule.
  351. To invoke the rule generator, run:
  352. ```sh
  353. npm run rule-gen
  354. ```
  355. The CLI acts a wizard, by asking a series of questions related to generation of the rule, for example:
  356. ```sh
  357. - What is the name of the RULE? (Eg: aria-valid): sample-rule
  358. - Does the RULE need a MATCHES file to be created?: Yes
  359. - Would you like to create a CHECK for the RULE?: No
  360. - Would you like to create UNIT test files? Yes
  361. - Would you like to create INTEGRATION test files? Yes
  362. ```
  363. Upon answering of which the assets are created in the respective directories.