OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/accessibility/ax_node.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <queue> |
| 9 |
| 10 namespace ui { |
| 11 |
| 12 AXPosition::AXPosition(AXNode* anchor, int offset, AXPositionType type) : anchor
_(anchor), offset_(offset), type_(type) { |
| 13 switch (type_) { |
| 14 case AXPositionType::TreePosition: |
| 15 offset_ = 0; |
| 16 break; |
| 17 case AXPositionType::TextPosition: |
| 18 if (!anchor_ || offset_ < 0 || offset_ > MaxOffset()) { |
| 19 // Reset to the null position. |
| 20 type_ = AXPositionType::TreePosition; |
| 21 anchor_ = nullptr; |
| 22 offset_ = 0; |
| 23 } |
| 24 break; |
| 25 } |
| 26 } |
| 27 |
| 28 AXPosition::~AXPosition() { |
| 29 } |
| 30 |
| 31 AXPosition AXPosition::CreateNullPosition() { |
| 32 return AXPosition(nullptr, 0, AXPositionType::TreePosition); |
| 33 } |
| 34 |
| 35 AXPosition AXPosition::CreateTreePosition(AXNode* anchor) { |
| 36 return AXPosition(anchor, 0, AXPositionType::TreePosition); |
| 37 } |
| 38 |
| 39 AXPosition AXPosition::CreateTextPosition(AXNode* anchor, int offset = 0) { |
| 40 return AXPosition(anchor, offset, AXPositionType::TextPosition); |
| 41 } |
| 42 |
| 43 // static |
| 44 AXPosition AXPosition::CommonAncestor(const AXPosition& first, const AXPosition&
second) { |
| 45 std::queue<AXPosition> ancestors1; |
| 46 ancestors1.push(first); |
| 47 while (!ancestors1.back().IsNullPosition()) |
| 48 ancestors1.push(ancestors1.back().GetParentPosition()); |
| 49 ancestors1.pop(); |
| 50 if (ancestors1.empty()) |
| 51 return CreateNullPosition(); |
| 52 |
| 53 std::queue<AXPosition> ancestors2; |
| 54 ancestors2.push(second); |
| 55 while (!ancestors2.back().IsNullPosition()) |
| 56 ancestors2.push(ancestors2.back().GetParentPosition()); |
| 57 ancestors2.pop(); |
| 58 if (ancestors2.empty()) |
| 59 return CreateNullPosition(); |
| 60 |
| 61 AXPosition commonAncestor = CreateNullPosition(); |
| 62 do { |
| 63 if (ancestors1.front() == ancestors2.front()) { |
| 64 commonAncestor = ancestors1.pop(), ancestors2.pop(); |
| 65 } else { |
| 66 break; |
| 67 } |
| 68 } while (!ancestors1.empty() && !ancestors2.empty()); |
| 69 return commonAncestor; |
| 70 } |
| 71 |
| 72 bool AXPosition::AtStartOfAnchor() const { |
| 73 if (type_ != AXPositionType::TextPosition) |
| 74 return true; |
| 75 return offset_ <= 0; |
| 76 } |
| 77 |
| 78 bool AXPosition::AtEndOfAnchor() const { |
| 79 if (type_ != AXPositionType::TextPosition) |
| 80 return true; |
| 81 return offset_ >= MaxOffset(); |
| 82 } |
| 83 |
| 84 bool AXPosition::operator<(const AXPosition& position) const { |
| 85 return false; |
| 86 } |
| 87 |
| 88 bool AXPosition::operator<=(const AXPosition& position) const { |
| 89 return false; |
| 90 } |
| 91 |
| 92 bool AXPosition::operator>(const AXPosition& position) const { |
| 93 return false; |
| 94 } |
| 95 |
| 96 bool AXPosition::operator>=(const AXPosition& position) const { |
| 97 return false; |
| 98 } |
| 99 |
| 100 AXPosition AXPosition::ToLeafTextPosition() const { |
| 101 } |
| 102 |
| 103 AXPosition AXPosition::ToTextPositionInAnchor(AXNode* anchor) const { |
| 104 } |
| 105 |
| 106 AXPosition AXPosition::GetPositionAtStartOfAnchor() const { |
| 107 if (type_ == AXPositionType::TextPosition) |
| 108 offset_ = 0; |
| 109 } |
| 110 |
| 111 AXPosition AXPosition::GetPositionAtEndOfAnchor() const { |
| 112 if (type_ == AXPositionType::TextPosition) |
| 113 offset_ = MaxOffset(); |
| 114 } |
| 115 |
| 116 AXPosition AXPosition::GetNextCharacterPosition() const { |
| 117 return CreateTextPosition(anchor_, offset_ + 1); |
| 118 } |
| 119 |
| 120 AXPosition AXPosition::GetPreviousCharacterPosition() const { |
| 121 return CreateTextPosition(anchor_, offset_ - 1); |
| 122 } |
| 123 |
| 124 AXPosition AXPosition::GetNextWordStartPosition() const { |
| 125 int newOffset = std::upper_bound(GetWordStartOffsets(), get_offset()); |
| 126 return CreateTextPosition(get_anchor(), newOffset); |
| 127 } |
| 128 |
| 129 AXPosition AXPosition::GetPreviousWordStartPosition() const { |
| 130 int newOffset = std::upper_bound(GetWordStartOffsets(), get_offset() - 1); |
| 131 return CreateTextPosition(get_anchor(), newOffset); |
| 132 } |
| 133 |
| 134 AXPosition AXPosition::GetNextWordEndPosition() const { |
| 135 int newOffset = std::upper_bound(GetWordEndOffsets(), get_offset()); |
| 136 return CreateTextPosition(get_anchor(), newOffset); |
| 137 } |
| 138 |
| 139 AXPosition AXPosition::GetPreviousWordEndPosition() const { |
| 140 int newOffset = std::upper_bound(GetWordEndOffsets(), get_offset() - 1); |
| 141 return CreateTextPosition(get_anchor(), newOffset); |
| 142 } |
| 143 |
| 144 AXPosition AXPosition::GetNextSentenceStartPosition() const { |
| 145 return CreateNullPosition(); |
| 146 } |
| 147 |
| 148 AXPosition AXPosition::GetPreviousSentenceStartPosition() const { |
| 149 return CreateNullPosition(); |
| 150 } |
| 151 |
| 152 AXPosition AXPosition::GetNextSentenceEndPosition() const { |
| 153 return CreateNullPosition(); |
| 154 } |
| 155 |
| 156 AXPosition AXPosition::GetPreviousSentenceEndPosition() const { |
| 157 return CreateNullPosition(); |
| 158 } |
| 159 |
| 160 AXPosition AXPosition::GetNextParagraphStartPosition() const { |
| 161 return CreateNullPosition(); |
| 162 } |
| 163 |
| 164 AXPosition AXPosition::GetPreviousParagraphStartPosition() const { |
| 165 return CreateNullPosition(); |
| 166 } |
| 167 |
| 168 AXPosition AXPosition::GetNextParagraphEndPosition() const { |
| 169 return CreateNullPosition(); |
| 170 } |
| 171 |
| 172 AXPosition AXPosition::GetPreviousParagraphEndPosition() const { |
| 173 return CreateNullPosition(); |
| 174 } |
| 175 |
| 176 AXPosition AXPosition::GetNextAnchorPosition() const { |
| 177 if (AnchorChildCount()) |
| 178 return GetChildPositionAt(0); |
| 179 |
| 180 for (AXPosition position = *this; !position.IsNullPosition(); position = posit
ion.GetParentPosition()) { |
| 181 // Get the next sibling if it exists. |
| 182 AXPosition parentPosition = position.GetParentPosition(); |
| 183 int indexInParent = position.AnchorIndexInParent(); |
| 184 if (indexInParent < parentPosition.AnchorChildCount() - 1) |
| 185 return parentPosition.GetChildPositionAt(indexInParent + 1); |
| 186 } |
| 187 return CreateNullPosition(); |
| 188 } |
| 189 |
| 190 bool operator==(const AXPosition& first, const AXPosition& second) { |
| 191 return false; |
| 192 } |
| 193 |
| 194 bool operator!=(const AXPosition& first, const AXPosition& second) { |
| 195 return false; |
| 196 } |
| 197 |
| 198 } // namespace ui |
OLD | NEW |