accessibility.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Accessibility = void 0;
  6. /**
  7. * Copyright 2018 Google Inc. All rights reserved.
  8. * Modifications copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class Accessibility {
  23. constructor(getAXTree) {
  24. this._getAXTree = void 0;
  25. this._getAXTree = getAXTree;
  26. }
  27. async snapshot(options = {}) {
  28. const {
  29. interestingOnly = true,
  30. root = null
  31. } = options;
  32. const {
  33. tree,
  34. needle
  35. } = await this._getAXTree(root || undefined);
  36. if (!interestingOnly) {
  37. if (root) return needle && serializeTree(needle)[0];
  38. return serializeTree(tree)[0];
  39. }
  40. const interestingNodes = new Set();
  41. collectInterestingNodes(interestingNodes, tree, false);
  42. if (root && (!needle || !interestingNodes.has(needle))) return null;
  43. return serializeTree(needle || tree, interestingNodes)[0];
  44. }
  45. }
  46. exports.Accessibility = Accessibility;
  47. function collectInterestingNodes(collection, node, insideControl) {
  48. if (node.isInteresting(insideControl)) collection.add(node);
  49. if (node.isLeafNode()) return;
  50. insideControl = insideControl || node.isControl();
  51. for (const child of node.children()) collectInterestingNodes(collection, child, insideControl);
  52. }
  53. function serializeTree(node, whitelistedNodes) {
  54. const children = [];
  55. for (const child of node.children()) children.push(...serializeTree(child, whitelistedNodes));
  56. if (whitelistedNodes && !whitelistedNodes.has(node)) return children;
  57. const serializedNode = node.serialize();
  58. if (children.length) serializedNode.children = children;
  59. return [serializedNode];
  60. }