File: ax_property_node.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 4,539 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_PROPERTY_NODE_H_
#define UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_PROPERTY_NODE_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/component_export.h"

namespace ui {

struct AXPropertyFilter;

// Property node is a tree-like structure, representing a property or collection
// of properties and its invocation arguments. A collection of properties is
// specified by putting a wildcard into a property name, for exampe, AXRole*
// will match both AXRole and AXRoleDescription properties. Parameters of a
// property are given in parentheses like a conventional function call, for
// example, AXCellForColumnAndRow([0, 0]) will call AXCellForColumnAndRow
// parameterized property for column/row 0 indexes.
class COMPONENT_EXPORT(AX_PLATFORM) AXPropertyNode final {
 public:
  // Parses a property node from a string or a property filter.
  static AXPropertyNode From(const std::string& property,
                             const std::vector<std::string>& line_indexes = {});
  static AXPropertyNode From(const AXPropertyFilter& filter);

  AXPropertyNode();
  AXPropertyNode(AXPropertyNode&&);
  ~AXPropertyNode();

  AXPropertyNode& operator=(AXPropertyNode&& other);
  explicit operator bool() const;

  // Key name in case of { key: value } dictionary.
  std::string key;

  // Value or a property(method) name, for example 3 or AXLineForIndex
  std::string name_or_value;

  // Arguments if it's a method, for example, it is a vector of a single
  // value 3 in case of AXLineForIndex(3)
  std::vector<AXPropertyNode> arguments;

  // Next property node in a chain if any.
  std::unique_ptr<AXPropertyNode> next;

  // Rvalue if any.
  std::unique_ptr<AXPropertyNode> rvalue;

  // Used to store the original unparsed property including invocation
  // arguments if any.
  std::string original_property;

  // The list of line indexes of accessible objects the property is allowed to
  // be called for, used if no property target is provided.
  std::vector<std::string> line_indexes;

  template <class... Args>
  AXPropertyNode* ConnectTo(bool chained, Args&&... args) {
    return chained ? ChainToLastArgument(std::forward<Args>(args)...)
                   : AppendToArguments(std::forward<Args>(args)...);
  }

  template <class... Args>
  AXPropertyNode* AppendToArguments(Args&&... args) {
    arguments.emplace_back(std::forward<Args>(args)...);
    return &arguments.back();
  }
  template <class... Args>
  AXPropertyNode* ChainToLastArgument(Args&&... args) {
    auto* last = &arguments.back();
    while (last->next) {
      last = last->next.get();
    }
    last->next = std::make_unique<AXPropertyNode>(
        AXPropertyNode(std::forward<Args>(args)...));
    return last->next.get();
  }

  bool IsMatching(const std::string& pattern) const;

  // Argument conversion methods.
  bool IsTarget() const { return !!next; }
  bool IsArray() const;
  bool IsDict() const;
  std::optional<int> AsInt() const;
  std::string AsString() const;
  const AXPropertyNode* FindKey(const char* refkey) const;
  std::optional<std::string> FindStringKey(const char* refkey) const;
  std::optional<int> FindIntKey(const char* key) const;

  // Returns a string representation of the node.
  std::string ToString() const;

  // Returns a flat, single line string representing the node tree.
  std::string ToFlatString() const;

  // Returns a tree-like string representing the node tree.
  std::string ToTreeString(const std::string& indent = "") const;

  using iterator = std::string::const_iterator;

  explicit AXPropertyNode(iterator key_begin,
                          iterator key_end,
                          const std::string&);
  AXPropertyNode(iterator begin, iterator end);
  AXPropertyNode(iterator key_begin,
                 iterator key_end,
                 iterator value_begin,
                 iterator value_end);

 private:
  // Used by Parse to indicate a state the parser currently has.
  enum ParseState {
    kArgument,
    kChain,
  };

  // Builds a property node struct for a string of NAME(ARG1, ..., ARGN) format,
  // where each ARG is a scalar value or a string of the same format.
  static iterator Parse(AXPropertyNode* node, iterator begin, iterator end);
};

}  // namespace ui

#endif  // UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_PROPERTY_NODE_H_