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 Simple class to represent a cursor selection. | 6 * @fileoverview Simple class to represent a cursor selection. |
7 * A cursor selection is just two cursors; one for the start and one for | 7 * A cursor selection is just two cursors; one for the start and one for |
8 * the end of some interval in the document. | 8 * the end of some interval in the document. |
9 */ | 9 */ |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 this.end = end.clone(); | 33 this.end = end.clone(); |
34 | 34 |
35 if (opt_reverse == undefined) { | 35 if (opt_reverse == undefined) { |
36 opt_reverse = false; | 36 opt_reverse = false; |
37 } | 37 } |
38 /** @private */ | 38 /** @private */ |
39 this.isReversed_ = opt_reverse; | 39 this.isReversed_ = opt_reverse; |
40 | 40 |
41 if ((this.isReversed_ && | 41 if ((this.isReversed_ && |
42 this.start.node.compareDocumentPosition(this.end.node) == | 42 this.start.node.compareDocumentPosition(this.end.node) == |
43 cvox.CursorSelection.BEFORE) || | 43 cvox.CursorSelection.BEFORE) || |
44 (!this.isReversed_ && | 44 (!this.isReversed_ && |
45 this.end.node.compareDocumentPosition(this.start.node) == | 45 this.end.node.compareDocumentPosition(this.start.node) == |
46 cvox.CursorSelection.BEFORE)) { | 46 cvox.CursorSelection.BEFORE)) { |
47 var oldStart = this.start; | 47 var oldStart = this.start; |
48 this.start = this.end; | 48 this.start = this.end; |
49 this.end = oldStart; | 49 this.end = oldStart; |
50 } | 50 } |
51 }; | 51 }; |
52 | 52 |
53 | 53 |
54 /** | 54 /** |
55 * From http://www.w3schools.com/jsref/met_node_comparedocumentposition.asp | 55 * From http://www.w3schools.com/jsref/met_node_comparedocumentposition.asp |
56 */ | 56 */ |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 * @param {Node} node The node. | 127 * @param {Node} node The node. |
128 * @return {cvox.CursorSelection} The selection. | 128 * @return {cvox.CursorSelection} The selection. |
129 */ | 129 */ |
130 cvox.CursorSelection.fromNode = function(node) { | 130 cvox.CursorSelection.fromNode = function(node) { |
131 if (!node) { | 131 if (!node) { |
132 return null; | 132 return null; |
133 } | 133 } |
134 var text = cvox.TraverseUtil.getNodeText(node); | 134 var text = cvox.TraverseUtil.getNodeText(node); |
135 | 135 |
136 return new cvox.CursorSelection( | 136 return new cvox.CursorSelection( |
137 new cvox.Cursor(node, 0, text), | 137 new cvox.Cursor(node, 0, text), new cvox.Cursor(node, 0, text)); |
138 new cvox.Cursor(node, 0, text)); | |
139 }; | 138 }; |
140 | 139 |
141 | 140 |
142 /** | 141 /** |
143 * Creates a new cursor selection that starts and ends at document.body. | 142 * Creates a new cursor selection that starts and ends at document.body. |
144 * @return {!cvox.CursorSelection} The selection. | 143 * @return {!cvox.CursorSelection} The selection. |
145 */ | 144 */ |
146 cvox.CursorSelection.fromBody = function() { | 145 cvox.CursorSelection.fromBody = function() { |
147 return /** @type {!cvox.CursorSelection} */ ( | 146 return /** @type {!cvox.CursorSelection} */ ( |
148 cvox.CursorSelection.fromNode(document.body)); | 147 cvox.CursorSelection.fromNode(document.body)); |
149 }; | 148 }; |
150 | 149 |
151 /** | 150 /** |
152 * Returns the text that the selection spans. | 151 * Returns the text that the selection spans. |
153 * @return {string} Text within the selection. '' if it is a node selection. | 152 * @return {string} Text within the selection. '' if it is a node selection. |
154 */ | 153 */ |
155 cvox.CursorSelection.prototype.getText = function() { | 154 cvox.CursorSelection.prototype.getText = function() { |
156 if (this.start.equals(this.end)) { | 155 if (this.start.equals(this.end)) { |
157 return cvox.TraverseUtil.getNodeText(this.start.node); | 156 return cvox.TraverseUtil.getNodeText(this.start.node); |
158 } | 157 } |
(...skipping 24 matching lines...) Expand all Loading... |
183 cvox.CursorSelection.prototype.equals = function(rhs) { | 182 cvox.CursorSelection.prototype.equals = function(rhs) { |
184 return this.start.equals(rhs.start) && this.end.equals(rhs.end); | 183 return this.start.equals(rhs.start) && this.end.equals(rhs.end); |
185 }; | 184 }; |
186 | 185 |
187 /** | 186 /** |
188 * Check for equality regardless of direction. | 187 * Check for equality regardless of direction. |
189 * @param {!cvox.CursorSelection} rhs The CursorSelection to compare against. | 188 * @param {!cvox.CursorSelection} rhs The CursorSelection to compare against. |
190 * @return {boolean} True if equal. | 189 * @return {boolean} True if equal. |
191 */ | 190 */ |
192 cvox.CursorSelection.prototype.absEquals = function(rhs) { | 191 cvox.CursorSelection.prototype.absEquals = function(rhs) { |
193 return ((this.start.equals(rhs.start) && this.end.equals(rhs.end)) || | 192 return ( |
| 193 (this.start.equals(rhs.start) && this.end.equals(rhs.end)) || |
194 (this.end.equals(rhs.start) && this.start.equals(rhs.end))); | 194 (this.end.equals(rhs.start) && this.start.equals(rhs.end))); |
195 }; | 195 }; |
196 | 196 |
197 /** | 197 /** |
198 * Determines if this starts before another CursorSelection in document order. | 198 * Determines if this starts before another CursorSelection in document order. |
199 * If this is reversed, then a reversed document order is checked. | 199 * If this is reversed, then a reversed document order is checked. |
200 * In the case that this and rhs start at the same position, we return true. | 200 * In the case that this and rhs start at the same position, we return true. |
201 * @param {!cvox.CursorSelection} rhs The selection to compare. | 201 * @param {!cvox.CursorSelection} rhs The selection to compare. |
202 * @return {boolean} True if this is before rhs. | 202 * @return {boolean} True if this is before rhs. |
203 */ | 203 */ |
(...skipping 10 matching lines...) Expand all Loading... |
214 * to its endpoints. | 214 * to its endpoints. |
215 * The result is to surround the node with this cursor. | 215 * The result is to surround the node with this cursor. |
216 * @return {!cvox.CursorSelection} The normalized selection. | 216 * @return {!cvox.CursorSelection} The normalized selection. |
217 */ | 217 */ |
218 cvox.CursorSelection.prototype.normalize = function() { | 218 cvox.CursorSelection.prototype.normalize = function() { |
219 if (this.absEnd().index == 0 && this.absEnd().node) { | 219 if (this.absEnd().index == 0 && this.absEnd().node) { |
220 var node = this.absEnd().node; | 220 var node = this.absEnd().node; |
221 | 221 |
222 // DOM ranges use different conventions when surrounding a node. For | 222 // DOM ranges use different conventions when surrounding a node. For |
223 // instance, input nodes endOffset is always 0 while h1's endOffset is 1 | 223 // instance, input nodes endOffset is always 0 while h1's endOffset is 1 |
224 //with both having no children. Use a range to compute the endOffset. | 224 // with both having no children. Use a range to compute the endOffset. |
225 var testRange = document.createRange(); | 225 var testRange = document.createRange(); |
226 testRange.selectNodeContents(node); | 226 testRange.selectNodeContents(node); |
227 this.absEnd().index = testRange.endOffset; | 227 this.absEnd().index = testRange.endOffset; |
228 } | 228 } |
229 return this; | 229 return this; |
230 }; | 230 }; |
231 | 231 |
232 /** | 232 /** |
233 * Collapses to the directed start of the selection. | 233 * Collapses to the directed start of the selection. |
234 * @return {!cvox.CursorSelection} For chaining. | 234 * @return {!cvox.CursorSelection} For chaining. |
(...skipping 11 matching lines...) Expand all Loading... |
246 if (this.end.index > 0) { | 246 if (this.end.index > 0) { |
247 this.end.index--; | 247 this.end.index--; |
248 } | 248 } |
249 } else { | 249 } else { |
250 if (this.end.index < this.end.text.length) { | 250 if (this.end.index < this.end.text.length) { |
251 this.end.index++; | 251 this.end.index++; |
252 } | 252 } |
253 } | 253 } |
254 return this; | 254 return this; |
255 }; | 255 }; |
OLD | NEW |