| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_ACCESSIBILITY_AX_POSITION_H_ | 5 #ifndef UI_ACCESSIBILITY_AX_POSITION_H_ |
| 6 #define UI_ACCESSIBILITY_AX_POSITION_H_ | 6 #define UI_ACCESSIBILITY_AX_POSITION_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 copy->text_offset_ >= copy->MaxTextOffset()) { | 271 copy->text_offset_ >= copy->MaxTextOffset()) { |
| 272 copy->child_index_ = copy->AnchorChildCount(); | 272 copy->child_index_ = copy->AnchorChildCount(); |
| 273 } else { | 273 } else { |
| 274 DCHECK_GE(copy->text_offset_, 0); | 274 DCHECK_GE(copy->text_offset_, 0); |
| 275 copy->child_index_ = BEFORE_TEXT; | 275 copy->child_index_ = BEFORE_TEXT; |
| 276 | 276 |
| 277 int current_offset = 0; | 277 int current_offset = 0; |
| 278 for (int i = 0; i < copy->AnchorChildCount(); ++i) { | 278 for (int i = 0; i < copy->AnchorChildCount(); ++i) { |
| 279 AXPositionInstance child = copy->CreateChildPositionAt(i); | 279 AXPositionInstance child = copy->CreateChildPositionAt(i); |
| 280 DCHECK(child); | 280 DCHECK(child); |
| 281 int child_length = child->MaxTextOffsetInParent(); |
| 281 if (copy->text_offset_ >= current_offset && | 282 if (copy->text_offset_ >= current_offset && |
| 282 copy->text_offset_ < (current_offset + child->MaxTextOffset())) { | 283 copy->text_offset_ < (current_offset + child_length)) { |
| 283 copy->child_index_ = i; | 284 copy->child_index_ = i; |
| 284 break; | 285 break; |
| 285 } | 286 } |
| 286 | 287 |
| 287 current_offset += child->MaxTextOffset(); | 288 current_offset += child_length; |
| 288 } | 289 } |
| 289 } | 290 } |
| 290 | 291 |
| 291 copy->kind_ = AXPositionKind::TREE_POSITION; | 292 copy->kind_ = AXPositionKind::TREE_POSITION; |
| 292 return copy; | 293 return copy; |
| 293 } | 294 } |
| 294 | 295 |
| 295 AXPositionInstance AsTextPosition() const { | 296 AXPositionInstance AsTextPosition() const { |
| 296 if (IsNullPosition() || IsTextPosition()) | 297 if (IsNullPosition() || IsTextPosition()) |
| 297 return Clone(); | 298 return Clone(); |
| 298 | 299 |
| 299 AXPositionInstance copy = Clone(); | 300 AXPositionInstance copy = Clone(); |
| 300 DCHECK(copy); | 301 DCHECK(copy); |
| 301 // Check if it is a "before text" position. | 302 // Check if it is a "before text" position. |
| 302 if (copy->child_index_ == BEFORE_TEXT) { | 303 if (copy->child_index_ == BEFORE_TEXT) { |
| 303 // "Before text" positions can only appear on leaf nodes. | 304 // "Before text" positions can only appear on leaf nodes. |
| 304 DCHECK(!copy->AnchorChildCount()); | 305 DCHECK(!copy->AnchorChildCount()); |
| 305 // If the current text offset is valid, we don't touch it. | 306 // If the current text offset is valid, we don't touch it. |
| 306 if (copy->text_offset_ < 0 || copy->text_offset_ >= copy->MaxTextOffset()) | 307 if (copy->text_offset_ < 0 || copy->text_offset_ >= copy->MaxTextOffset()) |
| 307 copy->text_offset_ = 0; | 308 copy->text_offset_ = 0; |
| 308 } else if (copy->child_index_ == copy->AnchorChildCount()) { | 309 } else if (copy->child_index_ == copy->AnchorChildCount()) { |
| 309 copy->text_offset_ = copy->MaxTextOffset(); | 310 copy->text_offset_ = copy->MaxTextOffset(); |
| 310 } else { | 311 } else { |
| 311 DCHECK_GE(copy->child_index_, 0); | 312 DCHECK_GE(copy->child_index_, 0); |
| 312 DCHECK_LT(copy->child_index_, copy->AnchorChildCount()); | 313 DCHECK_LT(copy->child_index_, copy->AnchorChildCount()); |
| 313 int new_offset = 0; | 314 int new_offset = 0; |
| 314 for (int i = 0; i <= child_index_; ++i) { | 315 for (int i = 0; i <= child_index_; ++i) { |
| 315 AXPositionInstance child = copy->CreateChildPositionAt(i); | 316 AXPositionInstance child = copy->CreateChildPositionAt(i); |
| 316 DCHECK(child); | 317 DCHECK(child); |
| 318 int child_length = child->MaxTextOffsetInParent(); |
| 317 | 319 |
| 318 // If the current text offset is valid, we don't touch it. | 320 // If the current text offset is valid, we don't touch it. |
| 319 // Otherwise, we reset it to the beginning of the current child node. | 321 // Otherwise, we reset it to the beginning of the current child node. |
| 320 if (i == child_index_ && | 322 if (i == child_index_ && |
| 321 (copy->text_offset_ < new_offset || | 323 (copy->text_offset_ < new_offset || |
| 322 copy->text_offset_ > (new_offset + child->MaxTextOffset()) || | 324 copy->text_offset_ > (new_offset + child_length) || |
| 323 // When the text offset is equal to the text's length but this is | 325 // When the text offset is equal to the text's length but this is |
| 324 // not an "after text" position. | 326 // not an "after text" position. |
| 325 (!copy->AtEndOfAnchor() && | 327 (!copy->AtEndOfAnchor() && |
| 326 copy->text_offset_ == (new_offset + child->MaxTextOffset())))) { | 328 copy->text_offset_ == (new_offset + child_length)))) { |
| 327 copy->text_offset_ = new_offset; | 329 copy->text_offset_ = new_offset; |
| 328 break; | 330 break; |
| 329 } | 331 } |
| 330 | 332 |
| 331 new_offset += child->MaxTextOffset(); | 333 new_offset += child_length; |
| 332 } | 334 } |
| 333 } | 335 } |
| 334 | 336 |
| 335 copy->kind_ = AXPositionKind::TEXT_POSITION; | 337 copy->kind_ = AXPositionKind::TEXT_POSITION; |
| 336 return copy; | 338 return copy; |
| 337 } | 339 } |
| 338 | 340 |
| 339 AXPositionInstance AsLeafTextPosition() const { | 341 AXPositionInstance AsLeafTextPosition() const { |
| 340 if (IsNullPosition() || !AnchorChildCount()) | 342 if (IsNullPosition() || !AnchorChildCount()) |
| 341 return AsTextPosition(); | 343 return AsTextPosition(); |
| (...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1035 | 1037 |
| 1036 // Abstract methods. | 1038 // Abstract methods. |
| 1037 virtual void AnchorChild(int child_index, | 1039 virtual void AnchorChild(int child_index, |
| 1038 int* tree_id, | 1040 int* tree_id, |
| 1039 int32_t* child_id) const = 0; | 1041 int32_t* child_id) const = 0; |
| 1040 virtual int AnchorChildCount() const = 0; | 1042 virtual int AnchorChildCount() const = 0; |
| 1041 virtual int AnchorIndexInParent() const = 0; | 1043 virtual int AnchorIndexInParent() const = 0; |
| 1042 virtual void AnchorParent(int* tree_id, int32_t* parent_id) const = 0; | 1044 virtual void AnchorParent(int* tree_id, int32_t* parent_id) const = 0; |
| 1043 virtual AXNodeType* GetNodeInTree(int tree_id, int32_t node_id) const = 0; | 1045 virtual AXNodeType* GetNodeInTree(int tree_id, int32_t node_id) const = 0; |
| 1044 // Returns the length of the text that is present inside the anchor node, | 1046 // Returns the length of the text that is present inside the anchor node, |
| 1045 // including any text found in descendant nodes. | 1047 // including any text found in descendant text nodes. |
| 1046 virtual int MaxTextOffset() const = 0; | 1048 virtual int MaxTextOffset() const = 0; |
| 1049 // Returns the length of text that this anchor node takes up in its parent. |
| 1050 // On some platforms, embedded objects are represented in their parent with a |
| 1051 // single embedded object character. |
| 1052 virtual int MaxTextOffsetInParent() const { return MaxTextOffset(); } |
| 1047 virtual bool IsInLineBreak() const = 0; | 1053 virtual bool IsInLineBreak() const = 0; |
| 1048 virtual std::vector<int32_t> GetWordStartOffsets() const = 0; | 1054 virtual std::vector<int32_t> GetWordStartOffsets() const = 0; |
| 1049 virtual std::vector<int32_t> GetWordEndOffsets() const = 0; | 1055 virtual std::vector<int32_t> GetWordEndOffsets() const = 0; |
| 1050 virtual int32_t GetNextOnLineID(int32_t node_id) const = 0; | 1056 virtual int32_t GetNextOnLineID(int32_t node_id) const = 0; |
| 1051 virtual int32_t GetPreviousOnLineID(int32_t node_id) const = 0; | 1057 virtual int32_t GetPreviousOnLineID(int32_t node_id) const = 0; |
| 1052 | 1058 |
| 1053 private: | 1059 private: |
| 1054 AXPositionKind kind_; | 1060 AXPositionKind kind_; |
| 1055 int tree_id_; | 1061 int tree_id_; |
| 1056 int32_t anchor_id_; | 1062 int32_t anchor_id_; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 | 1140 |
| 1135 template <class AXPositionType, class AXNodeType> | 1141 template <class AXPositionType, class AXNodeType> |
| 1136 bool operator>=(const AXPosition<AXPositionType, AXNodeType>& first, | 1142 bool operator>=(const AXPosition<AXPositionType, AXNodeType>& first, |
| 1137 const AXPosition<AXPositionType, AXNodeType>& second) { | 1143 const AXPosition<AXPositionType, AXNodeType>& second) { |
| 1138 return first == second || first > second; | 1144 return first == second || first > second; |
| 1139 } | 1145 } |
| 1140 | 1146 |
| 1141 } // namespace ui | 1147 } // namespace ui |
| 1142 | 1148 |
| 1143 #endif // UI_ACCESSIBILITY_AX_POSITION_H_ | 1149 #endif // UI_ACCESSIBILITY_AX_POSITION_H_ |
| OLD | NEW |