| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 /** | 5 /** |
| 6 * @fileoverview Classes related to cursors that point to and select parts of | 6 * @fileoverview Classes related to cursors that point to and select parts of |
| 7 * the automation tree. | 7 * the automation tree. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 goog.provide('cursors.Cursor'); | 10 goog.provide('cursors.Cursor'); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 var adjustedIndex = this.index_; | 158 var adjustedIndex = this.index_; |
| 159 if (this.node.role == RoleType.inlineTextBox) { | 159 if (this.node.role == RoleType.inlineTextBox) { |
| 160 if (adjustedIndex == cursors.NODE_INDEX) | 160 if (adjustedIndex == cursors.NODE_INDEX) |
| 161 adjustedIndex = 0; | 161 adjustedIndex = 0; |
| 162 | 162 |
| 163 var sibling = this.node.previousSibling; | 163 var sibling = this.node.previousSibling; |
| 164 while (sibling) { | 164 while (sibling) { |
| 165 adjustedIndex += sibling.name.length; | 165 adjustedIndex += sibling.name.length; |
| 166 sibling = sibling.previousSibling; | 166 sibling = sibling.previousSibling; |
| 167 } | 167 } |
| 168 } | |
| 169 | 168 |
| 170 if (this.selectionNode_ && AutomationPredicate.text(this.selectionNode_)) { | |
| 171 // Work around Blink's somewhat unexpected offset calculation which | 169 // Work around Blink's somewhat unexpected offset calculation which |
| 172 // requires us to consider all previous siblings of the parenting node of | 170 // requires us to consider all previous siblings of the parenting static |
| 173 // the static text. | 171 // text. |
| 174 | 172 var parent = this.node.parent; |
| 175 var parent = this.selectionNode_.parent; | 173 if (parent.role == RoleType.staticText) { |
| 176 if (parent) { | 174 sibling = parent.previousSibling; |
| 177 // Grab all of the text nodes and accumulate their lengths up to | 175 while (sibling) { |
| 178 // but not including the current static text node. | 176 if (sibling.name) |
| 179 var texts = []; | 177 adjustedIndex += sibling.name.length; |
| 180 AutomationUtil.findNodePre(parent, Dir.FORWARD, function(node) { | 178 sibling = sibling.previousSibling; |
| 181 if (AutomationPredicate.text(node)) | |
| 182 texts.push(node); | |
| 183 return false; | |
| 184 }); | |
| 185 | |
| 186 for (var i = 0; i < texts.length; i++) { | |
| 187 if (texts[i] == this.selectionNode_) | |
| 188 break; | |
| 189 | |
| 190 adjustedIndex += texts[i].name.length; | |
| 191 } | 179 } |
| 192 } | 180 } |
| 193 } else if (this.index_ == cursors.NODE_INDEX) { | 181 } else if (this.index_ == cursors.NODE_INDEX) { |
| 194 if (this.index_ == cursors.NODE_INDEX) { | 182 if (this.index_ == cursors.NODE_INDEX) { |
| 195 // Translate the index into a selection on the parent. | 183 // Translate the index into a selection on the parent. |
| 196 if (this.node.parent) | 184 if (this.node.parent) |
| 197 adjustedIndex = this.node.indexInParent; | 185 adjustedIndex = this.node.indexInParent; |
| 198 } | 186 } |
| 199 } | 187 } |
| 200 return adjustedIndex; | 188 return adjustedIndex; |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 /** | 582 /** |
| 595 * Select the text contained within this range. | 583 * Select the text contained within this range. |
| 596 */ | 584 */ |
| 597 select: function() { | 585 select: function() { |
| 598 var startNode = this.start.selectionNode_; | 586 var startNode = this.start.selectionNode_; |
| 599 var endNode = this.end.selectionNode_; | 587 var endNode = this.end.selectionNode_; |
| 600 | 588 |
| 601 if (!startNode || !endNode) | 589 if (!startNode || !endNode) |
| 602 return; | 590 return; |
| 603 | 591 |
| 604 // Only allow selections inside of the same web tree. | 592 // Find the most common root. |
| 605 if (startNode.root && | 593 var uniqueAncestors = AutomationUtil.getUniqueAncestors(startNode, endNode); |
| 606 startNode.root.role == RoleType.rootWebArea && | 594 var mcr = startNode.root; |
| 607 startNode.root === endNode.root) { | 595 if (uniqueAncestors) { |
| 596 var common = uniqueAncestors.pop().parent; |
| 597 if (common) |
| 598 mcr = common.root; |
| 599 } |
| 600 |
| 601 if (!mcr || mcr.role == RoleType.desktop) |
| 602 return; |
| 603 |
| 604 if (mcr === startNode.root && mcr === endNode.root) { |
| 608 var startIndex = this.start.selectionIndex_; | 605 var startIndex = this.start.selectionIndex_; |
| 609 | 606 |
| 610 // We want to adjust to select the entire node for node offsets; | 607 // We want to adjust to select the entire node for node offsets; |
| 611 // otherwise, use the plain character offset. | 608 // otherwise, use the plain character offset. |
| 612 var endIndex = this.end.index == cursors.NODE_INDEX ? | 609 var endIndex = this.end.index == cursors.NODE_INDEX ? |
| 613 this.end.selectionIndex_ + 1 : this.end.selectionIndex_; | 610 this.end.selectionIndex_ + 1 : this.end.selectionIndex_; |
| 614 | 611 |
| 615 chrome.automation.setDocumentSelection( | 612 chrome.automation.setDocumentSelection( |
| 616 { anchorObject: startNode, | 613 { anchorObject: startNode, |
| 617 anchorOffset: startIndex, | 614 anchorOffset: startIndex, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 634 /** | 631 /** |
| 635 * Returns whether this range has valid start and end cursors. | 632 * Returns whether this range has valid start and end cursors. |
| 636 * @return {boolean} | 633 * @return {boolean} |
| 637 */ | 634 */ |
| 638 isValid: function() { | 635 isValid: function() { |
| 639 return this.start.isValid() && this.end.isValid(); | 636 return this.start.isValid() && this.end.isValid(); |
| 640 } | 637 } |
| 641 }; | 638 }; |
| 642 | 639 |
| 643 }); // goog.scope | 640 }); // goog.scope |
| OLD | NEW |