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.

534 lines
19KB

  1. "use strict";
  2. exports.__esModule = true;
  3. exports.computeTextAlternative = computeTextAlternative;
  4. var _array = _interopRequireDefault(require("./polyfills/array.from"));
  5. var _SetLike = _interopRequireDefault(require("./polyfills/SetLike"));
  6. var _util = require("./util");
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. * implements https://w3c.github.io/accname/
  10. */
  11. /**
  12. *
  13. * @param {string} string -
  14. * @returns {FlatString} -
  15. */
  16. function asFlatString(s) {
  17. return s.trim().replace(/\s\s+/g, " ");
  18. }
  19. /**
  20. *
  21. * @param node -
  22. * @param options - These are not optional to prevent accidentally calling it without options in `computeAccessibleName`
  23. * @returns {boolean} -
  24. */
  25. function isHidden(node, getComputedStyleImplementation) {
  26. if (!(0, _util.isElement)(node)) {
  27. return false;
  28. }
  29. if (node.hasAttribute("hidden") || node.getAttribute("aria-hidden") === "true") {
  30. return true;
  31. }
  32. var style = getComputedStyleImplementation(node);
  33. return style.getPropertyValue("display") === "none" || style.getPropertyValue("visibility") === "hidden";
  34. }
  35. /**
  36. * @param {Node} node -
  37. * @returns {boolean} - As defined in step 2E of https://w3c.github.io/accname/#mapping_additional_nd_te
  38. */
  39. function isControl(node) {
  40. return (0, _util.hasAnyConcreteRoles)(node, ["button", "combobox", "listbox", "textbox"]) || hasAbstractRole(node, "range");
  41. }
  42. function hasAbstractRole(node, role) {
  43. if (!(0, _util.isElement)(node)) {
  44. return false;
  45. }
  46. switch (role) {
  47. case "range":
  48. return (0, _util.hasAnyConcreteRoles)(node, ["meter", "progressbar", "scrollbar", "slider", "spinbutton"]);
  49. default:
  50. throw new TypeError("No knowledge about abstract role '".concat(role, "'. This is likely a bug :("));
  51. }
  52. }
  53. /**
  54. * element.querySelectorAll but also considers owned tree
  55. * @param element
  56. * @param selectors
  57. */
  58. function querySelectorAllSubtree(element, selectors) {
  59. var elements = (0, _array.default)(element.querySelectorAll(selectors));
  60. (0, _util.queryIdRefs)(element, "aria-owns").forEach(function (root) {
  61. // babel transpiles this assuming an iterator
  62. elements.push.apply(elements, (0, _array.default)(root.querySelectorAll(selectors)));
  63. });
  64. return elements;
  65. }
  66. function querySelectedOptions(listbox) {
  67. if ((0, _util.isHTMLSelectElement)(listbox)) {
  68. // IE11 polyfill
  69. return listbox.selectedOptions || querySelectorAllSubtree(listbox, "[selected]");
  70. }
  71. return querySelectorAllSubtree(listbox, '[aria-selected="true"]');
  72. }
  73. function isMarkedPresentational(node) {
  74. return (0, _util.hasAnyConcreteRoles)(node, ["none", "presentation"]);
  75. }
  76. /**
  77. * Elements specifically listed in html-aam
  78. *
  79. * We don't need this for `label` or `legend` elements.
  80. * Their implicit roles already allow "naming from content".
  81. *
  82. * sources:
  83. *
  84. * - https://w3c.github.io/html-aam/#table-element
  85. */
  86. function isNativeHostLanguageTextAlternativeElement(node) {
  87. return (0, _util.isHTMLTableCaptionElement)(node);
  88. }
  89. /**
  90. * https://w3c.github.io/aria/#namefromcontent
  91. */
  92. function allowsNameFromContent(node) {
  93. return (0, _util.hasAnyConcreteRoles)(node, ["button", "cell", "checkbox", "columnheader", "gridcell", "heading", "label", "legend", "link", "menuitem", "menuitemcheckbox", "menuitemradio", "option", "radio", "row", "rowheader", "switch", "tab", "tooltip", "treeitem"]);
  94. }
  95. /**
  96. * TODO https://github.com/eps1lon/dom-accessibility-api/issues/100
  97. */
  98. function isDescendantOfNativeHostLanguageTextAlternativeElement(
  99. // eslint-disable-next-line @typescript-eslint/no-unused-vars -- not implemented yet
  100. node) {
  101. return false;
  102. }
  103. function getValueOfTextbox(element) {
  104. if ((0, _util.isHTMLInputElement)(element) || (0, _util.isHTMLTextAreaElement)(element)) {
  105. return element.value;
  106. }
  107. // https://github.com/eps1lon/dom-accessibility-api/issues/4
  108. return element.textContent || "";
  109. }
  110. function getTextualContent(declaration) {
  111. var content = declaration.getPropertyValue("content");
  112. if (/^["'].*["']$/.test(content)) {
  113. return content.slice(1, -1);
  114. }
  115. return "";
  116. }
  117. /**
  118. * https://html.spec.whatwg.org/multipage/forms.html#category-label
  119. * TODO: form-associated custom elements
  120. * @param element
  121. */
  122. function isLabelableElement(element) {
  123. var localName = (0, _util.getLocalName)(element);
  124. return localName === "button" || localName === "input" && element.getAttribute("type") !== "hidden" || localName === "meter" || localName === "output" || localName === "progress" || localName === "select" || localName === "textarea";
  125. }
  126. /**
  127. * > [...], then the first such descendant in tree order is the label element's labeled control.
  128. * -- https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  129. * @param element
  130. */
  131. function findLabelableElement(element) {
  132. if (isLabelableElement(element)) {
  133. return element;
  134. }
  135. var labelableElement = null;
  136. element.childNodes.forEach(function (childNode) {
  137. if (labelableElement === null && (0, _util.isElement)(childNode)) {
  138. var descendantLabelableElement = findLabelableElement(childNode);
  139. if (descendantLabelableElement !== null) {
  140. labelableElement = descendantLabelableElement;
  141. }
  142. }
  143. });
  144. return labelableElement;
  145. }
  146. /**
  147. * Polyfill of HTMLLabelElement.control
  148. * https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  149. * @param label
  150. */
  151. function getControlOfLabel(label) {
  152. if (label.control !== undefined) {
  153. return label.control;
  154. }
  155. var htmlFor = label.getAttribute("for");
  156. if (htmlFor !== null) {
  157. return label.ownerDocument.getElementById(htmlFor);
  158. }
  159. return findLabelableElement(label);
  160. }
  161. /**
  162. * Polyfill of HTMLInputElement.labels
  163. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/labels
  164. * @param element
  165. */
  166. function getLabels(element) {
  167. var labelsProperty = element.labels;
  168. if (labelsProperty === null) {
  169. return labelsProperty;
  170. }
  171. if (labelsProperty !== undefined) {
  172. return (0, _array.default)(labelsProperty);
  173. }
  174. // polyfill
  175. if (!isLabelableElement(element)) {
  176. return null;
  177. }
  178. var document = element.ownerDocument;
  179. return (0, _array.default)(document.querySelectorAll("label")).filter(function (label) {
  180. return getControlOfLabel(label) === element;
  181. });
  182. }
  183. /**
  184. * Gets the contents of a slot used for computing the accname
  185. * @param slot
  186. */
  187. function getSlotContents(slot) {
  188. // Computing the accessible name for elements containing slots is not
  189. // currently defined in the spec. This implementation reflects the
  190. // behavior of NVDA 2020.2/Firefox 81 and iOS VoiceOver/Safari 13.6.
  191. var assignedNodes = slot.assignedNodes();
  192. if (assignedNodes.length === 0) {
  193. // if no nodes are assigned to the slot, it displays the default content
  194. return (0, _array.default)(slot.childNodes);
  195. }
  196. return assignedNodes;
  197. }
  198. /**
  199. * implements https://w3c.github.io/accname/#mapping_additional_nd_te
  200. * @param root
  201. * @param options
  202. * @returns
  203. */
  204. function computeTextAlternative(root) {
  205. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  206. var consultedNodes = new _SetLike.default();
  207. var window = (0, _util.safeWindow)(root);
  208. var _options$compute = options.compute,
  209. compute = _options$compute === void 0 ? "name" : _options$compute,
  210. _options$computedStyl = options.computedStyleSupportsPseudoElements,
  211. computedStyleSupportsPseudoElements = _options$computedStyl === void 0 ? options.getComputedStyle !== undefined : _options$computedStyl,
  212. _options$getComputedS = options.getComputedStyle,
  213. getComputedStyle = _options$getComputedS === void 0 ? window.getComputedStyle.bind(window) : _options$getComputedS,
  214. _options$hidden = options.hidden,
  215. hidden = _options$hidden === void 0 ? false : _options$hidden;
  216. // 2F.i
  217. function computeMiscTextAlternative(node, context) {
  218. var accumulatedText = "";
  219. if ((0, _util.isElement)(node) && computedStyleSupportsPseudoElements) {
  220. var pseudoBefore = getComputedStyle(node, "::before");
  221. var beforeContent = getTextualContent(pseudoBefore);
  222. accumulatedText = "".concat(beforeContent, " ").concat(accumulatedText);
  223. }
  224. // FIXME: Including aria-owns is not defined in the spec
  225. // But it is required in the web-platform-test
  226. var childNodes = (0, _util.isHTMLSlotElement)(node) ? getSlotContents(node) : (0, _array.default)(node.childNodes).concat((0, _util.queryIdRefs)(node, "aria-owns"));
  227. childNodes.forEach(function (child) {
  228. var result = computeTextAlternative(child, {
  229. isEmbeddedInLabel: context.isEmbeddedInLabel,
  230. isReferenced: false,
  231. recursion: true
  232. });
  233. // TODO: Unclear why display affects delimiter
  234. // see https://github.com/w3c/accname/issues/3
  235. var display = (0, _util.isElement)(child) ? getComputedStyle(child).getPropertyValue("display") : "inline";
  236. var separator = display !== "inline" ? " " : "";
  237. // trailing separator for wpt tests
  238. accumulatedText += "".concat(separator).concat(result).concat(separator);
  239. });
  240. if ((0, _util.isElement)(node) && computedStyleSupportsPseudoElements) {
  241. var pseudoAfter = getComputedStyle(node, "::after");
  242. var afterContent = getTextualContent(pseudoAfter);
  243. accumulatedText = "".concat(accumulatedText, " ").concat(afterContent);
  244. }
  245. return accumulatedText.trim();
  246. }
  247. /**
  248. *
  249. * @param element
  250. * @param attributeName
  251. * @returns A string non-empty string or `null`
  252. */
  253. function useAttribute(element, attributeName) {
  254. var attribute = element.getAttributeNode(attributeName);
  255. if (attribute !== null && !consultedNodes.has(attribute) && attribute.value.trim() !== "") {
  256. consultedNodes.add(attribute);
  257. return attribute.value;
  258. }
  259. return null;
  260. }
  261. function computeTooltipAttributeValue(node) {
  262. if (!(0, _util.isElement)(node)) {
  263. return null;
  264. }
  265. return useAttribute(node, "title");
  266. }
  267. function computeElementTextAlternative(node) {
  268. if (!(0, _util.isElement)(node)) {
  269. return null;
  270. }
  271. // https://w3c.github.io/html-aam/#fieldset-and-legend-elements
  272. if ((0, _util.isHTMLFieldSetElement)(node)) {
  273. consultedNodes.add(node);
  274. var children = (0, _array.default)(node.childNodes);
  275. for (var i = 0; i < children.length; i += 1) {
  276. var child = children[i];
  277. if ((0, _util.isHTMLLegendElement)(child)) {
  278. return computeTextAlternative(child, {
  279. isEmbeddedInLabel: false,
  280. isReferenced: false,
  281. recursion: false
  282. });
  283. }
  284. }
  285. } else if ((0, _util.isHTMLTableElement)(node)) {
  286. // https://w3c.github.io/html-aam/#table-element
  287. consultedNodes.add(node);
  288. var _children = (0, _array.default)(node.childNodes);
  289. for (var _i = 0; _i < _children.length; _i += 1) {
  290. var _child = _children[_i];
  291. if ((0, _util.isHTMLTableCaptionElement)(_child)) {
  292. return computeTextAlternative(_child, {
  293. isEmbeddedInLabel: false,
  294. isReferenced: false,
  295. recursion: false
  296. });
  297. }
  298. }
  299. } else if ((0, _util.isSVGSVGElement)(node)) {
  300. // https://www.w3.org/TR/svg-aam-1.0/
  301. consultedNodes.add(node);
  302. var _children2 = (0, _array.default)(node.childNodes);
  303. for (var _i2 = 0; _i2 < _children2.length; _i2 += 1) {
  304. var _child2 = _children2[_i2];
  305. if ((0, _util.isSVGTitleElement)(_child2)) {
  306. return _child2.textContent;
  307. }
  308. }
  309. return null;
  310. } else if ((0, _util.getLocalName)(node) === "img" || (0, _util.getLocalName)(node) === "area") {
  311. // https://w3c.github.io/html-aam/#area-element
  312. // https://w3c.github.io/html-aam/#img-element
  313. var nameFromAlt = useAttribute(node, "alt");
  314. if (nameFromAlt !== null) {
  315. return nameFromAlt;
  316. }
  317. } else if ((0, _util.isHTMLOptGroupElement)(node)) {
  318. var nameFromLabel = useAttribute(node, "label");
  319. if (nameFromLabel !== null) {
  320. return nameFromLabel;
  321. }
  322. }
  323. if ((0, _util.isHTMLInputElement)(node) && (node.type === "button" || node.type === "submit" || node.type === "reset")) {
  324. // https://w3c.github.io/html-aam/#input-type-text-input-type-password-input-type-search-input-type-tel-input-type-email-input-type-url-and-textarea-element-accessible-description-computation
  325. var nameFromValue = useAttribute(node, "value");
  326. if (nameFromValue !== null) {
  327. return nameFromValue;
  328. }
  329. // TODO: l10n
  330. if (node.type === "submit") {
  331. return "Submit";
  332. }
  333. // TODO: l10n
  334. if (node.type === "reset") {
  335. return "Reset";
  336. }
  337. }
  338. var labels = getLabels(node);
  339. if (labels !== null && labels.length !== 0) {
  340. consultedNodes.add(node);
  341. return (0, _array.default)(labels).map(function (element) {
  342. return computeTextAlternative(element, {
  343. isEmbeddedInLabel: true,
  344. isReferenced: false,
  345. recursion: true
  346. });
  347. }).filter(function (label) {
  348. return label.length > 0;
  349. }).join(" ");
  350. }
  351. // https://w3c.github.io/html-aam/#input-type-image-accessible-name-computation
  352. // TODO: wpt test consider label elements but html-aam does not mention them
  353. // We follow existing implementations over spec
  354. if ((0, _util.isHTMLInputElement)(node) && node.type === "image") {
  355. var _nameFromAlt = useAttribute(node, "alt");
  356. if (_nameFromAlt !== null) {
  357. return _nameFromAlt;
  358. }
  359. var nameFromTitle = useAttribute(node, "title");
  360. if (nameFromTitle !== null) {
  361. return nameFromTitle;
  362. }
  363. // TODO: l10n
  364. return "Submit Query";
  365. }
  366. if ((0, _util.hasAnyConcreteRoles)(node, ["button"])) {
  367. // https://www.w3.org/TR/html-aam-1.0/#button-element
  368. var nameFromSubTree = computeMiscTextAlternative(node, {
  369. isEmbeddedInLabel: false,
  370. isReferenced: false
  371. });
  372. if (nameFromSubTree !== "") {
  373. return nameFromSubTree;
  374. }
  375. }
  376. return null;
  377. }
  378. function computeTextAlternative(current, context) {
  379. if (consultedNodes.has(current)) {
  380. return "";
  381. }
  382. // 2A
  383. if (!hidden && isHidden(current, getComputedStyle) && !context.isReferenced) {
  384. consultedNodes.add(current);
  385. return "";
  386. }
  387. // 2B
  388. var labelAttributeNode = (0, _util.isElement)(current) ? current.getAttributeNode("aria-labelledby") : null;
  389. // TODO: Do we generally need to block query IdRefs of attributes we have already consulted?
  390. var labelElements = labelAttributeNode !== null && !consultedNodes.has(labelAttributeNode) ? (0, _util.queryIdRefs)(current, "aria-labelledby") : [];
  391. if (compute === "name" && !context.isReferenced && labelElements.length > 0) {
  392. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Can't be null here otherwise labelElements would be empty
  393. consultedNodes.add(labelAttributeNode);
  394. return labelElements.map(function (element) {
  395. // TODO: Chrome will consider repeated values i.e. use a node multiple times while we'll bail out in computeTextAlternative.
  396. return computeTextAlternative(element, {
  397. isEmbeddedInLabel: context.isEmbeddedInLabel,
  398. isReferenced: true,
  399. // this isn't recursion as specified, otherwise we would skip
  400. // `aria-label` in
  401. // <input id="myself" aria-label="foo" aria-labelledby="myself"
  402. recursion: false
  403. });
  404. }).join(" ");
  405. }
  406. // 2C
  407. // Changed from the spec in anticipation of https://github.com/w3c/accname/issues/64
  408. // spec says we should only consider skipping if we have a non-empty label
  409. var skipToStep2E = context.recursion && isControl(current) && compute === "name";
  410. if (!skipToStep2E) {
  411. var ariaLabel = ((0, _util.isElement)(current) && current.getAttribute("aria-label") || "").trim();
  412. if (ariaLabel !== "" && compute === "name") {
  413. consultedNodes.add(current);
  414. return ariaLabel;
  415. }
  416. // 2D
  417. if (!isMarkedPresentational(current)) {
  418. var elementTextAlternative = computeElementTextAlternative(current);
  419. if (elementTextAlternative !== null) {
  420. consultedNodes.add(current);
  421. return elementTextAlternative;
  422. }
  423. }
  424. }
  425. // special casing, cheating to make tests pass
  426. // https://github.com/w3c/accname/issues/67
  427. if ((0, _util.hasAnyConcreteRoles)(current, ["menu"])) {
  428. consultedNodes.add(current);
  429. return "";
  430. }
  431. // 2E
  432. if (skipToStep2E || context.isEmbeddedInLabel || context.isReferenced) {
  433. if ((0, _util.hasAnyConcreteRoles)(current, ["combobox", "listbox"])) {
  434. consultedNodes.add(current);
  435. var selectedOptions = querySelectedOptions(current);
  436. if (selectedOptions.length === 0) {
  437. // defined per test `name_heading_combobox`
  438. return (0, _util.isHTMLInputElement)(current) ? current.value : "";
  439. }
  440. return (0, _array.default)(selectedOptions).map(function (selectedOption) {
  441. return computeTextAlternative(selectedOption, {
  442. isEmbeddedInLabel: context.isEmbeddedInLabel,
  443. isReferenced: false,
  444. recursion: true
  445. });
  446. }).join(" ");
  447. }
  448. if (hasAbstractRole(current, "range")) {
  449. consultedNodes.add(current);
  450. if (current.hasAttribute("aria-valuetext")) {
  451. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute guard
  452. return current.getAttribute("aria-valuetext");
  453. }
  454. if (current.hasAttribute("aria-valuenow")) {
  455. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute guard
  456. return current.getAttribute("aria-valuenow");
  457. }
  458. // Otherwise, use the value as specified by a host language attribute.
  459. return current.getAttribute("value") || "";
  460. }
  461. if ((0, _util.hasAnyConcreteRoles)(current, ["textbox"])) {
  462. consultedNodes.add(current);
  463. return getValueOfTextbox(current);
  464. }
  465. }
  466. // 2F: https://w3c.github.io/accname/#step2F
  467. if (allowsNameFromContent(current) || (0, _util.isElement)(current) && context.isReferenced || isNativeHostLanguageTextAlternativeElement(current) || isDescendantOfNativeHostLanguageTextAlternativeElement(current)) {
  468. var accumulatedText2F = computeMiscTextAlternative(current, {
  469. isEmbeddedInLabel: context.isEmbeddedInLabel,
  470. isReferenced: false
  471. });
  472. if (accumulatedText2F !== "") {
  473. consultedNodes.add(current);
  474. return accumulatedText2F;
  475. }
  476. }
  477. if (current.nodeType === current.TEXT_NODE) {
  478. consultedNodes.add(current);
  479. return current.textContent || "";
  480. }
  481. if (context.recursion) {
  482. consultedNodes.add(current);
  483. return computeMiscTextAlternative(current, {
  484. isEmbeddedInLabel: context.isEmbeddedInLabel,
  485. isReferenced: false
  486. });
  487. }
  488. var tooltipAttributeValue = computeTooltipAttributeValue(current);
  489. if (tooltipAttributeValue !== null) {
  490. consultedNodes.add(current);
  491. return tooltipAttributeValue;
  492. }
  493. // TODO should this be reachable?
  494. consultedNodes.add(current);
  495. return "";
  496. }
  497. return asFlatString(computeTextAlternative(root, {
  498. isEmbeddedInLabel: false,
  499. // by spec computeAccessibleDescription starts with the referenced elements as roots
  500. isReferenced: compute === "description",
  501. recursion: false
  502. }));
  503. }
  504. //# sourceMappingURL=accessible-name-and-description.js.map