// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UI_ACCESSIBILITY_AX_NODE_H_ #define UI_ACCESSIBILITY_AX_NODE_H_ #include #include #include #include #include #include #include "base/strings/char_traits.h" #include "build/build_config.h" #include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/skia/include/core/SkColor.h" #include "ui/accessibility/ax_export.h" #include "ui/accessibility/ax_hypertext.h" #include "ui/accessibility/ax_node_data.h" #include "ui/accessibility/ax_tree_id.h" namespace ui { class AXComputedNodeData; class AXTableInfo; struct AXLanguageInfo; struct AXTreeData; // This class is used to represent a node in an accessibility tree (`AXTree`). class AX_EXPORT AXNode final { public: // Replacement character used to represent an embedded (or, additionally for // text navigation, an empty) object. Encoded in UTF16 format. Part of the // Unicode Standard. // // On some platforms, most objects are represented in the text of their // parents with a special "embedded object character" and not with their // actual text contents. Also on the same platforms, if a node has only // ignored descendants, i.e., it appears to be empty to assistive software, we // need to treat it as a character and a word boundary. static constexpr char16_t kEmbeddedCharacter[] = u"\uFFFC"; // We compute the embedded character's length instead of manually typing it in // order to avoid the two variables getting out of sync in a future update. static constexpr int kEmbeddedCharacterLength = base::CharTraits::length(kEmbeddedCharacter); // Interface to the tree class that owns an AXNode. We use this instead // of letting AXNode have a pointer to its AXTree directly so that we're // forced to think twice before calling an AXTree interface that might not // be necessary. class OwnerTree { public: struct Selection { bool is_backward; AXNodeID anchor_object_id; int anchor_offset; ax::mojom::TextAffinity anchor_affinity; AXNodeID focus_object_id; int focus_offset; ax::mojom::TextAffinity focus_affinity; }; // See AXTree::GetAXTreeID. virtual AXTreeID GetAXTreeID() const = 0; // See `AXTree::GetTableInfo`. virtual AXTableInfo* GetTableInfo(const AXNode* table_node) const = 0; // See AXTree::GetFromId. virtual AXNode* GetFromId(AXNodeID id) const = 0; // See AXTree::data. virtual const AXTreeData& data() const = 0; virtual absl::optional GetPosInSet(const AXNode& node) = 0; virtual absl::optional GetSetSize(const AXNode& node) = 0; virtual Selection GetUnignoredSelection() const = 0; virtual bool GetTreeUpdateInProgressState() const = 0; virtual bool HasPaginationSupport() const = 0; }; template class ChildIteratorBase { public: using iterator_category = std::bidirectional_iterator_tag; using difference_type = int; using value_type = NodeType; using pointer = NodeType*; using reference = NodeType&; ChildIteratorBase(const NodeType* parent, NodeType* child); ChildIteratorBase(const ChildIteratorBase& it); ~ChildIteratorBase() {} bool operator==(const ChildIteratorBase& rhs) const; bool operator!=(const ChildIteratorBase& rhs) const; ChildIteratorBase& operator++(); ChildIteratorBase& operator--(); NodeType* get() const; NodeType& operator*() const; NodeType* operator->() const; protected: const NodeType* parent_; NodeType* child_; }; // The constructor requires a parent, id, and index in parent, but // the data is not required. After initialization, only index_in_parent // and unignored_index_in_parent is allowed to change, the others are // guaranteed to never change. AXNode(OwnerTree* tree, AXNode* parent, AXNodeID id, size_t index_in_parent, size_t unignored_index_in_parent = 0); virtual ~AXNode(); // Accessors. OwnerTree* tree() const { return tree_; } AXNodeID id() const { return data_.id; } const AXNodeData& data() const { return data_; } // Returns ownership of |data_| to the caller; effectively clearing |data_|. AXNodeData&& TakeData(); // // Methods for walking the tree. // // These come in four flavors: Methods that walk all the nodes, methods that // walk only the unignored nodes (effectively re-structuring the tree to // remove all ignored nodes), and another two variants that do the above plus // cross tree boundaries, effectively stiching together all accessibility // trees that are part of the same webpage, PDF or window into a large global // tree. const std::vector& GetAllChildren() const; size_t GetChildCount() const; size_t GetChildCountCrossingTreeBoundary() const; size_t GetUnignoredChildCount() const; size_t GetUnignoredChildCountCrossingTreeBoundary() const; AXNode* GetChildAtIndex(size_t index) const; AXNode* GetChildAtIndexCrossingTreeBoundary(size_t index) const; AXNode* GetUnignoredChildAtIndex(size_t index) const; AXNode* GetUnignoredChildAtIndexCrossingTreeBoundary(size_t index) const; AXNode* GetParent() const; AXNode* GetParentCrossingTreeBoundary() const; AXNode* GetUnignoredParent() const; AXNode* GetUnignoredParentCrossingTreeBoundary() const; size_t GetIndexInParent() const; size_t GetUnignoredIndexInParent() const; AXNode* GetFirstChild() const; AXNode* GetFirstChildCrossingTreeBoundary() const; AXNode* GetFirstUnignoredChild() const; AXNode* GetFirstUnignoredChildCrossingTreeBoundary() const; AXNode* GetLastChild() const; AXNode* GetLastChildCrossingTreeBoundary() const; AXNode* GetLastUnignoredChild() const; AXNode* GetLastUnignoredChildCrossingTreeBoundary() const; AXNode* GetDeepestFirstChild() const; AXNode* GetDeepestFirstUnignoredChild() const; AXNode* GetDeepestLastChild() const; AXNode* GetDeepestLastUnignoredChild() const; AXNode* GetNextSibling() const; AXNode* GetNextUnignoredSibling() const; AXNode* GetPreviousSibling() const; AXNode* GetPreviousUnignoredSibling() const; // Traverse the tree in depth-first pre-order. AXNode* GetNextUnignoredInTreeOrder() const; AXNode* GetPreviousUnignoredInTreeOrder() const; // // Deprecated methods for walking the tree. // const std::vector& children() const { return children_; } AXNode* parent() const { return parent_; } size_t index_in_parent() const { return index_in_parent_; } // // Iterators for walking the tree in depth-first pre-order. // using AllChildIterator = ChildIteratorBase; AllChildIterator AllChildrenBegin() const; AllChildIterator AllChildrenEnd() const; using AllChildCrossingTreeBoundaryIterator = ChildIteratorBase; AllChildCrossingTreeBoundaryIterator AllChildrenCrossingTreeBoundaryBegin() const; AllChildCrossingTreeBoundaryIterator AllChildrenCrossingTreeBoundaryEnd() const; using UnignoredChildIterator = ChildIteratorBase; UnignoredChildIterator UnignoredChildrenBegin() const; UnignoredChildIterator UnignoredChildrenEnd() const; using UnignoredChildCrossingTreeBoundaryIterator = ChildIteratorBase; UnignoredChildCrossingTreeBoundaryIterator UnignoredChildrenCrossingTreeBoundaryBegin() const; UnignoredChildCrossingTreeBoundaryIterator UnignoredChildrenCrossingTreeBoundaryEnd() const; // Returns true if the node has any of the text related roles, including // kStaticText, kInlineTextBox and kListMarker (for Legacy Layout). Does not // include any text field roles. bool IsText() const; // Returns true if the node has any line break related roles or is the child // of a node with line break related roles. bool IsLineBreak() const; // Set the node's accessibility data. This may be done during initialization // or later when the node data changes. void SetData(const AXNodeData& src); // Update this node's location. This is separate from |SetData| just because // changing only the location is common and should be more efficient than // re-copying all of the data. // // The node's location is stored as a relative bounding box, the ID of // the element it's relative to, and an optional transformation matrix. // See ax_node_data.h for details. void SetLocation(AXNodeID offset_container_id, const gfx::RectF& location, gfx::Transform* transform); // Set the index in parent, for example if siblings were inserted or deleted. void SetIndexInParent(size_t index_in_parent); // When the node's `IsIgnored()` value changes, updates the cached values for // the unignored index in parent and the unignored child count. void UpdateUnignoredCachedValues(); // Swap the internal children vector with |children|. This instance // now owns all of the passed children. void SwapChildren(std::vector* children); // This is called when the AXTree no longer includes this node in the // tree. Reference counting is used on some platforms because the // operating system may hold onto a reference to an AXNode // object even after we're through with it, so this may decrement the // reference count and clear out the object's data. void Destroy(); // Returns true if this node is equal to or a descendant of |ancestor|. bool IsDescendantOf(const AXNode* ancestor) const; // Gets the text offsets where new lines start either from the node's data or // by computing them and caching the result. std::vector GetOrComputeLineStartOffsets(); // If the color is transparent, blends with the ancestor's color. // Note that this is imperfect; it won't work if a node is absolute- // positioned outside of its ancestor. However, it handles the most // common cases. SkColor ComputeColor() const; SkColor ComputeBackgroundColor() const; // // Methods for accessing accessibility attributes including attributes that // are computed on the browser side. (See `AXNodeData` and // `AXComputedNodeData` for more information.) // // Please prefer using the methods in this file for retrieving attributes, as // computed attributes would be automatically returned if available. // ax::mojom::Role GetRole() const { return data().role; } bool HasBoolAttribute(ax::mojom::BoolAttribute attribute) const { return data().HasBoolAttribute(attribute); } bool GetBoolAttribute(ax::mojom::BoolAttribute attribute) const { return data().GetBoolAttribute(attribute); } bool GetBoolAttribute(ax::mojom::BoolAttribute attribute, bool* value) const { return data().GetBoolAttribute(attribute, value); } bool HasFloatAttribute(ax::mojom::FloatAttribute attribute) const { return data().HasFloatAttribute(attribute); } float GetFloatAttribute(ax::mojom::FloatAttribute attribute) const { return data().GetFloatAttribute(attribute); } bool GetFloatAttribute(ax::mojom::FloatAttribute attribute, float* value) const { return data().GetFloatAttribute(attribute, value); } bool HasIntAttribute(ax::mojom::IntAttribute attribute) const { return data().HasIntAttribute(attribute); } int GetIntAttribute(ax::mojom::IntAttribute attribute) const { return data().GetIntAttribute(attribute); } bool GetIntAttribute(ax::mojom::IntAttribute attribute, int* value) const { return data().GetIntAttribute(attribute, value); } bool HasStringAttribute(ax::mojom::StringAttribute attribute) const; const std::string& GetStringAttribute( ax::mojom::StringAttribute attribute) const; bool GetStringAttribute(ax::mojom::StringAttribute attribute, std::string* value) const; std::u16string GetString16Attribute( ax::mojom::StringAttribute attribute) const; bool GetString16Attribute(ax::mojom::StringAttribute attribute, std::u16string* value) const; const std::string& GetInheritedStringAttribute( ax::mojom::StringAttribute attribute) const; std::u16string GetInheritedString16Attribute( ax::mojom::StringAttribute attribute) const; bool HasIntListAttribute(ax::mojom::IntListAttribute attribute) const { return data().HasIntListAttribute(attribute); } const std::vector& GetIntListAttribute( ax::mojom::IntListAttribute attribute) const { return data().GetIntListAttribute(attribute); } bool GetIntListAttribute(ax::mojom::IntListAttribute attribute, std::vector* value) const { return data().GetIntListAttribute(attribute, value); } bool HasStringListAttribute(ax::mojom::StringListAttribute attribute) const { return data().HasStringListAttribute(attribute); } const std::vector& GetStringListAttribute( ax::mojom::StringListAttribute attribute) const { return data().GetStringListAttribute(attribute); } bool GetStringListAttribute(ax::mojom::StringListAttribute attribute, std::vector* value) const { return data().GetStringListAttribute(attribute, value); } bool GetHtmlAttribute(const char* attribute, std::string* value) const { return data().GetHtmlAttribute(attribute, value); } bool GetHtmlAttribute(const char* attribute, std::u16string* value) const { return data().GetHtmlAttribute(attribute, value); } bool HasState(ax::mojom::State state) const { return data().HasState(state); } // Return the hierarchical level if supported. absl::optional GetHierarchicalLevel() const; // PosInSet and SetSize public methods. bool IsOrderedSetItem() const; bool IsOrderedSet() const; absl::optional GetPosInSet(); absl::optional GetSetSize(); // Helpers for GetPosInSet and GetSetSize. // Returns true if the role of ordered set matches the role of item. // Returns false otherwise. bool SetRoleMatchesItemRole(const AXNode* ordered_set) const; // Container objects that should be ignored for computing PosInSet and SetSize // for ordered sets. bool IsIgnoredContainerForOrderedSet() const; // If this node is a leaf, returns the inner text of this node. This is // equivalent to its visible accessible name. Otherwise, if this node is not a // leaf, represents every non-textual child node with a special "embedded // object character", and every textual child node with its inner text. // Textual nodes include e.g. static text and white space. // // This is how displayed text and embedded objects are represented in // ATK and IAccessible2 APIs. // // TODO(nektar): Consider changing the return value to std::string. const std::u16string& GetHypertext() const; // Temporary method that marks `hypertext_` dirty. This will eventually be // handled by the AX tree in a followup patch. void SetNeedsToUpdateHypertext(); // Temporary accessor methods until hypertext is fully migrated to this class. // Hypertext won't eventually need to be accessed outside this class. const std::map& GetHypertextOffsetToHyperlinkChildIndex() const; const AXHypertext& GetOldHypertext() const; // Returns the text that is found inside this node and all its descendants; // including text found in embedded objects. // // Only text displayed on screen is included. Text from ARIA and HTML // attributes that is either not displayed on screen, or outside this node, is // not returned. const std::string& GetInnerText() const; const std::u16string& GetInnerTextUTF16() const; // Returns the length of the text (in UTF16 code units) that is found inside // this node and all its descendants; including text found in embedded // objects. // // Only text displayed on screen is counted. Text from ARIA and HTML // attributes that is either not displayed on screen, or outside this node, is // not included. // // The length of the text is in UTF8 code units, not in grapheme clusters. int GetInnerTextLength() const; int GetInnerTextLengthUTF16() const; // Returns a string representing the language code. // // This will consider the language declared in the DOM, and may eventually // attempt to automatically detect the language from the text. // // This language code will be BCP 47. // // Returns empty string if no appropriate language was found. std::string GetLanguage() const; // Returns the value of a control such as an atomic text field ( or //