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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 * not both. An index of |cursors.NODE_INDEX| means the node as a whole is | 65 * not both. An index of |cursors.NODE_INDEX| means the node as a whole is |
66 * pointed to and covers the case where the accessible text is empty. | 66 * pointed to and covers the case where the accessible text is empty. |
67 */ | 67 */ |
68 cursors.Cursor = function(node, index) { | 68 cursors.Cursor = function(node, index) { |
69 /** @type {!AutomationNode} */ | 69 /** @type {!AutomationNode} */ |
70 this.node = node; | 70 this.node = node; |
71 /** @type {number} */ | 71 /** @type {number} */ |
72 this.index = index; | 72 this.index = index; |
73 }; | 73 }; |
74 | 74 |
75 /** | |
76 * Convenience method to construct a Cursor from a node. | |
77 * @param {!AutomationNode} node | |
78 * @return {!cursors.Cursor} | |
79 */ | |
80 cursors.Cursor.fromNode = function(node) { | |
81 return new cursors.Cursor(node, cursors.NODE_INDEX); | |
82 }; | |
83 | |
75 cursors.Cursor.prototype = { | 84 cursors.Cursor.prototype = { |
76 /** | 85 /** |
77 * Returns a copy of this cursor. | 86 * Returns a copy of this cursor. |
78 * @return {cursors.Cursor} | 87 * @return {!cursors.Cursor} |
79 */ | 88 */ |
80 clone: function() { | 89 clone: function() { |
81 return new cursors.Cursor(this.node, this.index); | 90 return new cursors.Cursor(this.node, this.index); |
82 }, | 91 }, |
83 | 92 |
84 /** | 93 /** |
94 * Returns true if |rhs| is equal to this cursor. | |
95 * @param {!cursors.Cursor} rhs | |
96 * @return {boolean} | |
97 */ | |
98 equals: function(rhs) { | |
99 return this.node === rhs.node && | |
100 this.index === rhs.index; | |
101 }, | |
102 | |
103 /** | |
85 * Gets the accessible text of the node associated with this cursor. | 104 * Gets the accessible text of the node associated with this cursor. |
86 * | 105 * |
87 * Note that only one of |name| or |value| attribute is ever nonempty on an | 106 * Note that only one of |name| or |value| attribute is ever nonempty on an |
88 * automation node. If either contains whitespace, we still treat it as we do | 107 * automation node. If either contains whitespace, we still treat it as we do |
89 * for a nonempty string. | 108 * for a nonempty string. |
90 * @param {!AutomationNode=} opt_node Use this node rather than this cursor's | 109 * @param {!AutomationNode=} opt_node Use this node rather than this cursor's |
91 * node. | 110 * node. |
92 * @return {string} | 111 * @return {string} |
93 */ | 112 */ |
94 getText: function(opt_node) { | 113 getText: function(opt_node) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 AutomationPredicate.linebreak, {before: true}) || node; | 226 AutomationPredicate.linebreak, {before: true}) || node; |
208 } | 227 } |
209 this.node = node || this.node; | 228 this.node = node || this.node; |
210 break; | 229 break; |
211 } | 230 } |
212 break; | 231 break; |
213 } | 232 } |
214 } | 233 } |
215 }; | 234 }; |
216 | 235 |
236 /** | |
237 * Represents a range in the automation tree. There is no visible selection on | |
238 * the page caused by usage of this object. | |
239 * @param {!cursors.Cursor} start | |
240 * @param {!cursors.Cursor} end | |
241 * @constructor | |
242 */ | |
243 cursors.Range = function(start, end) { | |
244 this.start = start; | |
245 this.end = end; | |
Peter Lundblad
2014/10/23 13:35:07
Are these members meant to be public?
| |
246 this.normalize(); | |
247 }; | |
248 | |
249 /** | |
250 * Convenience method to construct a Range surrounding one node. | |
251 * @param {!AutomationNode} node | |
252 * @return {!cursors.Range} | |
253 */ | |
254 cursors.Range.fromNode = function(node) { | |
255 return new cursors.Range(cursors.Cursor.fromNode(node), | |
256 cursors.Cursor.fromNode(node)); | |
257 }; | |
258 | |
259 cursors.Range.prototype = { | |
260 /** @return {!cursors.Range} */ | |
261 clone: function() { | |
262 return new cursors.Range(this.start.clone(), this.end.clone()); | |
263 }, | |
264 | |
265 /** | |
266 * Returns true if |rhs| is equal to this range. | |
267 * @param {!cursors.Range} rhs | |
268 * @return {boolean} | |
269 */ | |
270 equals: function(rhs) { | |
271 return this.start.equals(rhs.start) && | |
272 this.end.equals(rhs.end); | |
273 }, | |
274 | |
275 /** | |
276 * Gets a cursor bounding this range. | |
277 * @param {Dir} dir Which endpoint cursor to return; Dir.FORWARD for end, | |
278 * Dir.BACKWARD for start. | |
279 * @return {cursors.Cursor} | |
280 */ | |
281 getBound: function(dir) { | |
282 return dir == Dir.FORWARD ? this.end : this.start; | |
283 }, | |
284 | |
285 /** | |
286 * Collapses this range to an endpoint. | |
287 * @param {Dir} dir Dir.FORWARD collapses to end; Dir.BACKWARD collapses to | |
288 * start. | |
289 * @return {cursors.Range} | |
290 */ | |
291 collapse: function(dir) { | |
292 this.start = this.getBound(dir); | |
293 this.end = this.start; | |
294 return this; | |
295 }, | |
296 | |
297 /** | |
298 * Moves this Range by the unit in the given direction. | |
299 * @param {Unit} unit | |
300 * @param {Dir} dir | |
301 */ | |
302 move: function(unit, dir) { | |
303 var newStart = this.getBound(dir); | |
304 var newEnd = newStart.clone(); | |
305 switch (unit) { | |
306 case Unit.CHARACTER: | |
307 newStart.move(unit, Movement.BOUND, dir); | |
308 this.normalize(); | |
309 break; | |
310 case Unit.WORD: | |
311 case Unit.LINE: | |
312 case Unit.NODE: | |
313 newEnd.move(unit, Movement.DIRECTIONAL, dir); | |
314 newStart = newEnd.clone(); | |
315 newEnd.move(unit, Movement.BOUND, Dir.FORWARD); | |
316 break; | |
317 } | |
318 this.start = newStart; | |
319 this.end = newEnd; | |
320 }, | |
321 | |
322 /** | |
323 * Normalizes this range to point forward. | |
dmazzoni
2014/10/17 16:46:08
I'm confused - the description makes it sound like
| |
324 */ | |
325 normalize: function() { | |
326 if (this.start.node == this.end.node) | |
dmazzoni
2014/10/17 16:46:08
nit: ===?
Peter Lundblad
2014/10/23 13:35:07
Do we want to be consistent and use === when compa
| |
327 return this.start.index <= this.end.index; | |
328 return AutomationUtil.isBefore(this.start.node, this.end.node); | |
329 } | |
Peter Lundblad
2014/10/23 13:35:07
Is this the right implementation for this function
| |
330 }; | |
331 | |
217 }); // goog.scope | 332 }); // goog.scope |
OLD | NEW |