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.

594 lines
18 KiB

  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( // eslint-disable-next-line @typescript-eslint/no-unused-vars -- not implemented yet
  99. node) {
  100. return false;
  101. }
  102. /**
  103. * TODO https://github.com/eps1lon/dom-accessibility-api/issues/101
  104. */
  105. // eslint-disable-next-line @typescript-eslint/no-unused-vars -- not implemented yet
  106. function computeTooltipAttributeValue(node) {
  107. return null;
  108. }
  109. function getValueOfTextbox(element) {
  110. if ((0, _util.isHTMLInputElement)(element) || (0, _util.isHTMLTextAreaElement)(element)) {
  111. return element.value;
  112. } // https://github.com/eps1lon/dom-accessibility-api/issues/4
  113. return element.textContent || "";
  114. }
  115. function getTextualContent(declaration) {
  116. var content = declaration.getPropertyValue("content");
  117. if (/^["'].*["']$/.test(content)) {
  118. return content.slice(1, -1);
  119. }
  120. return "";
  121. }
  122. /**
  123. * https://html.spec.whatwg.org/multipage/forms.html#category-label
  124. * TODO: form-associated custom elements
  125. * @param element
  126. */
  127. function isLabelableElement(element) {
  128. var localName = (0, _util.getLocalName)(element);
  129. return localName === "button" || localName === "input" && element.getAttribute("type") !== "hidden" || localName === "meter" || localName === "output" || localName === "progress" || localName === "select" || localName === "textarea";
  130. }
  131. /**
  132. * > [...], then the first such descendant in tree order is the label element's labeled control.
  133. * -- https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  134. * @param element
  135. */
  136. function findLabelableElement(element) {
  137. if (isLabelableElement(element)) {
  138. return element;
  139. }
  140. var labelableElement = null;
  141. element.childNodes.forEach(function (childNode) {
  142. if (labelableElement === null && (0, _util.isElement)(childNode)) {
  143. var descendantLabelableElement = findLabelableElement(childNode);
  144. if (descendantLabelableElement !== null) {
  145. labelableElement = descendantLabelableElement;
  146. }
  147. }
  148. });
  149. return labelableElement;
  150. }
  151. /**
  152. * Polyfill of HTMLLabelElement.control
  153. * https://html.spec.whatwg.org/multipage/forms.html#labeled-control
  154. * @param label
  155. */
  156. function getControlOfLabel(label) {
  157. if (label.control !== undefined) {
  158. return label.control;
  159. }
  160. var htmlFor = label.getAttribute("for");
  161. if (htmlFor !== null) {
  162. return label.ownerDocument.getElementById(htmlFor);
  163. }
  164. return findLabelableElement(label);
  165. }
  166. /**
  167. * Polyfill of HTMLInputElement.labels
  168. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/labels
  169. * @param element
  170. */
  171. function getLabels(element) {
  172. var labelsProperty = element.labels;
  173. if (labelsProperty === null) {
  174. return labelsProperty;
  175. }
  176. if (labelsProperty !== undefined) {
  177. return (0, _array.default)(labelsProperty);
  178. }
  179. if (!isLabelableElement(element)) {
  180. return null;
  181. }
  182. var document = element.ownerDocument;
  183. return (0, _array.default)(document.querySelectorAll("label")).filter(function (label) {
  184. return getControlOfLabel(label) === element;
  185. });
  186. }
  187. /**
  188. * Gets the contents of a slot used for computing the accname
  189. * @param slot
  190. */
  191. function getSlotContents(slot) {
  192. // Computing the accessible name for elements containing slots is not
  193. // currently defined in the spec. This implementation reflects the
  194. // behavior of NVDA 2020.2/Firefox 81 and iOS VoiceOver/Safari 13.6.
  195. var assignedNodes = slot.assignedNodes();
  196. if (assignedNodes.length === 0) {
  197. // if no nodes are assigned to the slot, it displays the default content
  198. return (0, _array.default)(slot.childNodes);
  199. }
  200. return assignedNodes;
  201. }
  202. /**
  203. * implements https://w3c.github.io/accname/#mapping_additional_nd_te
  204. * @param root
  205. * @param [options]
  206. * @param [options.getComputedStyle] - mock window.getComputedStyle. Needs `content`, `display` and `visibility`
  207. */
  208. function computeTextAlternative(root) {
  209. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  210. var consultedNodes = new _SetLike.default();
  211. var window = (0, _util.safeWindow)(root);
  212. var _options$compute = options.compute,
  213. compute = _options$compute === void 0 ? "name" : _options$compute,
  214. _options$computedStyl = options.computedStyleSupportsPseudoElements,
  215. computedStyleSupportsPseudoElements = _options$computedStyl === void 0 ? options.getComputedStyle !== undefined : _options$computedStyl,
  216. _options$getComputedS = options.getComputedStyle,
  217. getComputedStyle = _options$getComputedS === void 0 ? window.getComputedStyle.bind(window) : _options$getComputedS; // 2F.i
  218. function computeMiscTextAlternative(node, context) {
  219. var accumulatedText = "";
  220. if ((0, _util.isElement)(node) && computedStyleSupportsPseudoElements) {
  221. var pseudoBefore = getComputedStyle(node, "::before");
  222. var beforeContent = getTextualContent(pseudoBefore);
  223. accumulatedText = "".concat(beforeContent, " ").concat(accumulatedText);
  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. }); // TODO: Unclear why display affects delimiter
  233. // see https://github.com/w3c/accname/issues/3
  234. var display = (0, _util.isElement)(child) ? getComputedStyle(child).getPropertyValue("display") : "inline";
  235. var separator = display !== "inline" ? " " : ""; // trailing separator for wpt tests
  236. accumulatedText += "".concat(separator).concat(result).concat(separator);
  237. });
  238. if ((0, _util.isElement)(node) && computedStyleSupportsPseudoElements) {
  239. var pseudoAfter = getComputedStyle(node, "::after");
  240. var afterContent = getTextualContent(pseudoAfter);
  241. accumulatedText = "".concat(accumulatedText, " ").concat(afterContent);
  242. }
  243. return accumulatedText;
  244. }
  245. function computeElementTextAlternative(node) {
  246. if (!(0, _util.isElement)(node)) {
  247. return null;
  248. }
  249. /**
  250. *
  251. * @param element
  252. * @param attributeName
  253. * @returns A string non-empty string or `null`
  254. */
  255. function useAttribute(element, attributeName) {
  256. var attribute = element.getAttributeNode(attributeName);
  257. if (attribute !== null && !consultedNodes.has(attribute) && attribute.value.trim() !== "") {
  258. consultedNodes.add(attribute);
  259. return attribute.value;
  260. }
  261. return null;
  262. } // https://w3c.github.io/html-aam/#fieldset-and-legend-elements
  263. if ((0, _util.isHTMLFieldSetElement)(node)) {
  264. consultedNodes.add(node);
  265. var children = (0, _array.default)(node.childNodes);
  266. for (var i = 0; i < children.length; i += 1) {
  267. var child = children[i];
  268. if ((0, _util.isHTMLLegendElement)(child)) {
  269. return computeTextAlternative(child, {
  270. isEmbeddedInLabel: false,
  271. isReferenced: false,
  272. recursion: false
  273. });
  274. }
  275. }
  276. } else if ((0, _util.isHTMLTableElement)(node)) {
  277. // https://w3c.github.io/html-aam/#table-element
  278. consultedNodes.add(node);
  279. var _children = (0, _array.default)(node.childNodes);
  280. for (var _i = 0; _i < _children.length; _i += 1) {
  281. var _child = _children[_i];
  282. if ((0, _util.isHTMLTableCaptionElement)(_child)) {
  283. return computeTextAlternative(_child, {
  284. isEmbeddedInLabel: false,
  285. isReferenced: false,
  286. recursion: false
  287. });
  288. }
  289. }
  290. } else if ((0, _util.isSVGSVGElement)(node)) {
  291. // https://www.w3.org/TR/svg-aam-1.0/
  292. consultedNodes.add(node);
  293. var _children2 = (0, _array.default)(node.childNodes);
  294. for (var _i2 = 0; _i2 < _children2.length; _i2 += 1) {
  295. var _child2 = _children2[_i2];
  296. if ((0, _util.isSVGTitleElement)(_child2)) {
  297. return _child2.textContent;
  298. }
  299. }
  300. return null;
  301. } else if ((0, _util.getLocalName)(node) === "img" || (0, _util.getLocalName)(node) === "area") {
  302. // https://w3c.github.io/html-aam/#area-element
  303. // https://w3c.github.io/html-aam/#img-element
  304. var nameFromAlt = useAttribute(node, "alt");
  305. if (nameFromAlt !== null) {
  306. return nameFromAlt;
  307. }
  308. }
  309. if ((0, _util.isHTMLInputElement)(node) && (node.type === "button" || node.type === "submit" || node.type === "reset")) {
  310. // 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
  311. var nameFromValue = useAttribute(node, "value");
  312. if (nameFromValue !== null) {
  313. return nameFromValue;
  314. } // TODO: l10n
  315. if (node.type === "submit") {
  316. return "Submit";
  317. } // TODO: l10n
  318. if (node.type === "reset") {
  319. return "Reset";
  320. }
  321. }
  322. if ((0, _util.isHTMLInputElement)(node) || (0, _util.isHTMLSelectElement)(node) || (0, _util.isHTMLTextAreaElement)(node)) {
  323. var input = node;
  324. var labels = getLabels(input);
  325. if (labels !== null && labels.length !== 0) {
  326. consultedNodes.add(input);
  327. return (0, _array.default)(labels).map(function (element) {
  328. return computeTextAlternative(element, {
  329. isEmbeddedInLabel: true,
  330. isReferenced: false,
  331. recursion: true
  332. });
  333. }).filter(function (label) {
  334. return label.length > 0;
  335. }).join(" ");
  336. }
  337. } // https://w3c.github.io/html-aam/#input-type-image-accessible-name-computation
  338. // TODO: wpt test consider label elements but html-aam does not mention them
  339. // We follow existing implementations over spec
  340. if ((0, _util.isHTMLInputElement)(node) && node.type === "image") {
  341. var _nameFromAlt = useAttribute(node, "alt");
  342. if (_nameFromAlt !== null) {
  343. return _nameFromAlt;
  344. }
  345. var nameFromTitle = useAttribute(node, "title");
  346. if (nameFromTitle !== null) {
  347. return nameFromTitle;
  348. } // TODO: l10n
  349. return "Submit Query";
  350. }
  351. return useAttribute(node, "title");
  352. }
  353. function computeTextAlternative(current, context) {
  354. if (consultedNodes.has(current)) {
  355. return "";
  356. } // special casing, cheating to make tests pass
  357. // https://github.com/w3c/accname/issues/67
  358. if ((0, _util.hasAnyConcreteRoles)(current, ["menu"])) {
  359. consultedNodes.add(current);
  360. return "";
  361. } // 2A
  362. if (isHidden(current, getComputedStyle) && !context.isReferenced) {
  363. consultedNodes.add(current);
  364. return "";
  365. } // 2B
  366. var labelElements = (0, _util.queryIdRefs)(current, "aria-labelledby");
  367. if (compute === "name" && !context.isReferenced && labelElements.length > 0) {
  368. return labelElements.map(function (element) {
  369. return computeTextAlternative(element, {
  370. isEmbeddedInLabel: context.isEmbeddedInLabel,
  371. isReferenced: true,
  372. // thais isn't recursion as specified, otherwise we would skip
  373. // `aria-label` in
  374. // <input id="myself" aria-label="foo" aria-labelledby="myself"
  375. recursion: false
  376. });
  377. }).join(" ");
  378. } // 2C
  379. // Changed from the spec in anticipation of https://github.com/w3c/accname/issues/64
  380. // spec says we should only consider skipping if we have a non-empty label
  381. var skipToStep2E = context.recursion && isControl(current) && compute === "name";
  382. if (!skipToStep2E) {
  383. var ariaLabel = ((0, _util.isElement)(current) && current.getAttribute("aria-label") || "").trim();
  384. if (ariaLabel !== "" && compute === "name") {
  385. consultedNodes.add(current);
  386. return ariaLabel;
  387. } // 2D
  388. if (!isMarkedPresentational(current)) {
  389. var elementTextAlternative = computeElementTextAlternative(current);
  390. if (elementTextAlternative !== null) {
  391. consultedNodes.add(current);
  392. return elementTextAlternative;
  393. }
  394. }
  395. } // 2E
  396. if (skipToStep2E || context.isEmbeddedInLabel || context.isReferenced) {
  397. if ((0, _util.hasAnyConcreteRoles)(current, ["combobox", "listbox"])) {
  398. consultedNodes.add(current);
  399. var selectedOptions = querySelectedOptions(current);
  400. if (selectedOptions.length === 0) {
  401. // defined per test `name_heading_combobox`
  402. return (0, _util.isHTMLInputElement)(current) ? current.value : "";
  403. }
  404. return (0, _array.default)(selectedOptions).map(function (selectedOption) {
  405. return computeTextAlternative(selectedOption, {
  406. isEmbeddedInLabel: context.isEmbeddedInLabel,
  407. isReferenced: false,
  408. recursion: true
  409. });
  410. }).join(" ");
  411. }
  412. if (hasAbstractRole(current, "range")) {
  413. consultedNodes.add(current);
  414. if (current.hasAttribute("aria-valuetext")) {
  415. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute guard
  416. return current.getAttribute("aria-valuetext");
  417. }
  418. if (current.hasAttribute("aria-valuenow")) {
  419. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute guard
  420. return current.getAttribute("aria-valuenow");
  421. } // Otherwise, use the value as specified by a host language attribute.
  422. return current.getAttribute("value") || "";
  423. }
  424. if ((0, _util.hasAnyConcreteRoles)(current, ["textbox"])) {
  425. consultedNodes.add(current);
  426. return getValueOfTextbox(current);
  427. }
  428. } // 2F: https://w3c.github.io/accname/#step2F
  429. if (allowsNameFromContent(current) || (0, _util.isElement)(current) && context.isReferenced || isNativeHostLanguageTextAlternativeElement(current) || isDescendantOfNativeHostLanguageTextAlternativeElement(current)) {
  430. consultedNodes.add(current);
  431. return computeMiscTextAlternative(current, {
  432. isEmbeddedInLabel: context.isEmbeddedInLabel,
  433. isReferenced: false
  434. });
  435. }
  436. if (current.nodeType === current.TEXT_NODE) {
  437. consultedNodes.add(current);
  438. return current.textContent || "";
  439. }
  440. if (context.recursion) {
  441. consultedNodes.add(current);
  442. return computeMiscTextAlternative(current, {
  443. isEmbeddedInLabel: context.isEmbeddedInLabel,
  444. isReferenced: false
  445. });
  446. }
  447. var tooltipAttributeValue = computeTooltipAttributeValue(current);
  448. if (tooltipAttributeValue !== null) {
  449. consultedNodes.add(current);
  450. return tooltipAttributeValue;
  451. } // TODO should this be reachable?
  452. consultedNodes.add(current);
  453. return "";
  454. }
  455. return asFlatString(computeTextAlternative(root, {
  456. isEmbeddedInLabel: false,
  457. // by spec computeAccessibleDescription starts with the referenced elements as roots
  458. isReferenced: compute === "description",
  459. recursion: false
  460. }));
  461. }
  462. //# sourceMappingURL=accessible-name-and-description.js.map