| 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 133 |
| 134 /** | 134 /** |
| 135 * A node appropriate for making selections. | 135 * A node appropriate for making selections. |
| 136 * @return {AutomationNode} | 136 * @return {AutomationNode} |
| 137 * @private | 137 * @private |
| 138 */ | 138 */ |
| 139 get selectionNode_() { | 139 get selectionNode_() { |
| 140 if (!this.node) | 140 if (!this.node) |
| 141 return null; | 141 return null; |
| 142 | 142 |
| 143 if (this.node.role == RoleType.inlineTextBox || | 143 if (this.node.role == RoleType.inlineTextBox) |
| 144 this.index == cursors.NODE_INDEX) | |
| 145 return this.node.parent; | 144 return this.node.parent; |
| 146 | 145 |
| 147 return this.node; | 146 return this.node; |
| 148 }, | 147 }, |
| 149 | 148 |
| 150 /** | 149 /** |
| 151 * An index appropriate for making selections. If this cursor has a | 150 * An index appropriate for making selections. If this cursor has a |
| 152 * cursors.NODE_INDEX index, the selection index is a node offset e.g. the | 151 * cursors.NODE_INDEX index, the selection index is a node offset e.g. the |
| 153 * index in parent. If not, the index is a character offset. | 152 * index in parent. If not, the index is a character offset. |
| 154 * @return {number} | 153 * @return {number} |
| 155 * @private | 154 * @private |
| 156 */ | 155 */ |
| 157 get selectionIndex_() { | 156 get selectionIndex_() { |
| 158 var adjustedIndex = this.index_; | 157 var adjustedIndex = this.index_; |
| 159 if (this.node.role == RoleType.inlineTextBox) { | 158 if (this.node.role == RoleType.inlineTextBox) { |
| 160 if (adjustedIndex == cursors.NODE_INDEX) | 159 if (adjustedIndex == cursors.NODE_INDEX) |
| 161 adjustedIndex = 0; | 160 adjustedIndex = 0; |
| 162 | 161 |
| 163 var sibling = this.node.previousSibling; | 162 var sibling = this.node.previousSibling; |
| 164 while (sibling) { | 163 while (sibling) { |
| 165 adjustedIndex += sibling.name.length; | 164 adjustedIndex += sibling.name.length; |
| 166 sibling = sibling.previousSibling; | 165 sibling = sibling.previousSibling; |
| 167 } | 166 } |
| 168 | |
| 169 // Work around Blink's somewhat unexpected offset calculation which | |
| 170 // requires us to consider all previous siblings of the parenting static | |
| 171 // text. | |
| 172 var parent = this.node.parent; | |
| 173 if (parent.role == RoleType.staticText) { | |
| 174 sibling = parent.previousSibling; | |
| 175 while (sibling) { | |
| 176 if (sibling.name) | |
| 177 adjustedIndex += sibling.name.length; | |
| 178 sibling = sibling.previousSibling; | |
| 179 } | |
| 180 } | |
| 181 } else if (this.index_ == cursors.NODE_INDEX) { | 167 } else if (this.index_ == cursors.NODE_INDEX) { |
| 182 if (this.index_ == cursors.NODE_INDEX) { | 168 // Indicies of this kind are buggy. Set it to 0 (different than the DOM |
| 183 // Translate the index into a selection on the parent. | 169 // index in parent convention). |
| 184 if (this.node.parent) | 170 adjustedIndex = 0; |
| 185 adjustedIndex = this.node.indexInParent; | |
| 186 } | |
| 187 } | 171 } |
| 188 return adjustedIndex; | 172 return adjustedIndex; |
| 189 }, | 173 }, |
| 190 | 174 |
| 191 /** | 175 /** |
| 192 * Gets the accessible text of the node associated with this cursor. | 176 * Gets the accessible text of the node associated with this cursor. |
| 193 * | 177 * |
| 194 * @param {!AutomationNode=} opt_node Use this node rather than this cursor's | 178 * @param {!AutomationNode=} opt_node Use this node rather than this cursor's |
| 195 * node. | 179 * node. |
| 196 * @return {string} | 180 * @return {string} |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 /** | 566 /** |
| 583 * Select the text contained within this range. | 567 * Select the text contained within this range. |
| 584 */ | 568 */ |
| 585 select: function() { | 569 select: function() { |
| 586 var startNode = this.start.selectionNode_; | 570 var startNode = this.start.selectionNode_; |
| 587 var endNode = this.end.selectionNode_; | 571 var endNode = this.end.selectionNode_; |
| 588 | 572 |
| 589 if (!startNode || !endNode) | 573 if (!startNode || !endNode) |
| 590 return; | 574 return; |
| 591 | 575 |
| 592 // Find the most common root. | 576 // Only allow selections within the same web tree. |
| 593 var uniqueAncestors = AutomationUtil.getUniqueAncestors(startNode, endNode); | 577 if (startNode.root && |
| 594 var mcr = startNode.root; | 578 startNode.root.role == RoleType.rootWebArea && |
| 595 if (uniqueAncestors) { | 579 startNode.root == endNode.root) { |
| 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) { | |
| 605 var startIndex = this.start.selectionIndex_; | |
| 606 | |
| 607 // We want to adjust to select the entire node for node offsets; | 580 // We want to adjust to select the entire node for node offsets; |
| 608 // otherwise, use the plain character offset. | 581 // otherwise, use the plain character offset. |
| 582 var startIndex = this.start.selectionIndex_; |
| 609 var endIndex = this.end.index == cursors.NODE_INDEX ? | 583 var endIndex = this.end.index == cursors.NODE_INDEX ? |
| 610 this.end.selectionIndex_ + 1 : this.end.selectionIndex_; | 584 this.end.selectionIndex_ + 1 : this.end.selectionIndex_; |
| 611 | 585 |
| 612 chrome.automation.setDocumentSelection( | 586 chrome.automation.setDocumentSelection( |
| 613 { anchorObject: startNode, | 587 { anchorObject: startNode, |
| 614 anchorOffset: startIndex, | 588 anchorOffset: startIndex, |
| 615 focusObject: endNode, | 589 focusObject: endNode, |
| 616 focusOffset: endIndex } | 590 focusOffset: endIndex } |
| 617 ); | 591 ); |
| 618 } | 592 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 631 /** | 605 /** |
| 632 * Returns whether this range has valid start and end cursors. | 606 * Returns whether this range has valid start and end cursors. |
| 633 * @return {boolean} | 607 * @return {boolean} |
| 634 */ | 608 */ |
| 635 isValid: function() { | 609 isValid: function() { |
| 636 return this.start.isValid() && this.end.isValid(); | 610 return this.start.isValid() && this.end.isValid(); |
| 637 } | 611 } |
| 638 }; | 612 }; |
| 639 | 613 |
| 640 }); // goog.scope | 614 }); // goog.scope |
| OLD | NEW |