OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 return 0; | 192 return 0; |
193 return m_frame->selection()->isNone() ? 0 : 1; | 193 return m_frame->selection()->isNone() ? 0 : 1; |
194 } | 194 } |
195 | 195 |
196 void DOMSelection::collapse(Node* node, int offset, ExceptionState& es) | 196 void DOMSelection::collapse(Node* node, int offset, ExceptionState& es) |
197 { | 197 { |
198 if (!m_frame) | 198 if (!m_frame) |
199 return; | 199 return; |
200 | 200 |
201 if (offset < 0) { | 201 if (offset < 0) { |
202 es.throwDOMException(IndexSizeError); | 202 es.throwDOMException(IndexSizeError, "Failed to execute 'collapse' on a 'Selection' object: " + String::number(offset) + " is not a valid offset."); |
203 return; | 203 return; |
204 } | 204 } |
205 | 205 |
206 if (!isValidForPosition(node)) | 206 if (!isValidForPosition(node)) |
207 return; | 207 return; |
208 | 208 |
209 // FIXME: Eliminate legacy editing positions | 209 // FIXME: Eliminate legacy editing positions |
210 m_frame->selection()->moveTo(VisiblePosition(createLegacyEditingPosition(nod e, offset), DOWNSTREAM)); | 210 m_frame->selection()->moveTo(VisiblePosition(createLegacyEditingPosition(nod e, offset), DOWNSTREAM)); |
211 } | 211 } |
212 | 212 |
213 void DOMSelection::collapseToEnd(ExceptionState& es) | 213 void DOMSelection::collapseToEnd(ExceptionState& es) |
214 { | 214 { |
215 if (!m_frame) | 215 if (!m_frame) |
216 return; | 216 return; |
217 | 217 |
218 const VisibleSelection& selection = m_frame->selection()->selection(); | 218 const VisibleSelection& selection = m_frame->selection()->selection(); |
219 | 219 |
220 if (selection.isNone()) { | 220 if (selection.isNone()) { |
221 es.throwDOMException(InvalidStateError); | 221 es.throwDOMException(InvalidStateError, "Failed to execute 'collapseToEn d' on a 'Selection' object: there is no selection."); |
arv (Not doing code reviews)
2013/08/13 13:57:31
I feel like we are repeating ourselves a lot. Mayb
Mike West
2013/08/13 14:00:17
I can do that. I have vague performance reluctance
arv (Not doing code reviews)
2013/08/13 14:07:49
Yeah, I don't think we need to optimize for the ex
| |
222 return; | 222 return; |
223 } | 223 } |
224 | 224 |
225 m_frame->selection()->moveTo(VisiblePosition(selection.end(), DOWNSTREAM)); | 225 m_frame->selection()->moveTo(VisiblePosition(selection.end(), DOWNSTREAM)); |
226 } | 226 } |
227 | 227 |
228 void DOMSelection::collapseToStart(ExceptionState& es) | 228 void DOMSelection::collapseToStart(ExceptionState& es) |
229 { | 229 { |
230 if (!m_frame) | 230 if (!m_frame) |
231 return; | 231 return; |
232 | 232 |
233 const VisibleSelection& selection = m_frame->selection()->selection(); | 233 const VisibleSelection& selection = m_frame->selection()->selection(); |
234 | 234 |
235 if (selection.isNone()) { | 235 if (selection.isNone()) { |
236 es.throwDOMException(InvalidStateError); | 236 es.throwDOMException(InvalidStateError, "Failed to execute 'collapseToSt art' on a 'Selection' object: there is no selection."); |
237 return; | 237 return; |
238 } | 238 } |
239 | 239 |
240 m_frame->selection()->moveTo(VisiblePosition(selection.start(), DOWNSTREAM)) ; | 240 m_frame->selection()->moveTo(VisiblePosition(selection.start(), DOWNSTREAM)) ; |
241 } | 241 } |
242 | 242 |
243 void DOMSelection::empty() | 243 void DOMSelection::empty() |
244 { | 244 { |
245 if (!m_frame) | 245 if (!m_frame) |
246 return; | 246 return; |
247 m_frame->selection()->clear(); | 247 m_frame->selection()->clear(); |
248 } | 248 } |
249 | 249 |
250 void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extent Node, int extentOffset, ExceptionState& es) | 250 void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extent Node, int extentOffset, ExceptionState& es) |
251 { | 251 { |
252 if (!m_frame) | 252 if (!m_frame) |
253 return; | 253 return; |
254 | 254 |
255 if (baseOffset < 0 || extentOffset < 0) { | 255 if (baseOffset < 0) { |
256 es.throwDOMException(IndexSizeError); | 256 es.throwDOMException(IndexSizeError, "Failed to execute 'setBaseAndExten t' on a 'Selection' object: " + String::number(baseOffset) + " is not a valid ba se offset."); |
257 return; | 257 return; |
258 } | 258 } |
259 | 259 |
260 if (extentOffset < 0) { | |
261 es.throwDOMException(IndexSizeError, "Failed to execute 'setBaseAndExten t' on a 'Selection' object: " + String::number(extentOffset) + " is not a valid extent offset."); | |
262 return; | |
263 } | |
264 | |
260 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode)) | 265 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode)) |
261 return; | 266 return; |
262 | 267 |
263 // FIXME: Eliminate legacy editing positions | 268 // FIXME: Eliminate legacy editing positions |
264 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM); | 269 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM); |
265 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM); | 270 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM); |
266 | 271 |
267 m_frame->selection()->moveTo(visibleBase, visibleExtent); | 272 m_frame->selection()->moveTo(visibleBase, visibleExtent); |
268 } | 273 } |
269 | 274 |
270 void DOMSelection::setPosition(Node* node, int offset, ExceptionState& es) | 275 void DOMSelection::setPosition(Node* node, int offset, ExceptionState& es) |
271 { | 276 { |
272 if (!m_frame) | 277 if (!m_frame) |
273 return; | 278 return; |
274 if (offset < 0) { | 279 if (offset < 0) { |
275 es.throwDOMException(IndexSizeError); | 280 es.throwDOMException(IndexSizeError, "Failed to execute 'setPosition' on a 'Selection' object: " + String::number(offset) + " is not a valid offset."); |
276 return; | 281 return; |
277 } | 282 } |
278 | 283 |
279 if (!isValidForPosition(node)) | 284 if (!isValidForPosition(node)) |
280 return; | 285 return; |
281 | 286 |
282 // FIXME: Eliminate legacy editing positions | 287 // FIXME: Eliminate legacy editing positions |
283 m_frame->selection()->moveTo(VisiblePosition(createLegacyEditingPosition(nod e, offset), DOWNSTREAM)); | 288 m_frame->selection()->moveTo(VisiblePosition(createLegacyEditingPosition(nod e, offset), DOWNSTREAM)); |
284 } | 289 } |
285 | 290 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 | 337 |
333 m_frame->selection()->modify(alter, direction, granularity); | 338 m_frame->selection()->modify(alter, direction, granularity); |
334 } | 339 } |
335 | 340 |
336 void DOMSelection::extend(Node* node, int offset, ExceptionState& es) | 341 void DOMSelection::extend(Node* node, int offset, ExceptionState& es) |
337 { | 342 { |
338 if (!m_frame) | 343 if (!m_frame) |
339 return; | 344 return; |
340 | 345 |
341 if (!node) { | 346 if (!node) { |
342 es.throwDOMException(TypeMismatchError); | 347 es.throwDOMException(TypeMismatchError, "Failed to execute 'extend' on a 'Selection' object: The node provided is invalid."); |
343 return; | 348 return; |
344 } | 349 } |
345 | 350 |
346 if (offset < 0 || offset > (node->offsetInCharacters() ? caretMaxOffset(node ) : (int)node->childNodeCount())) { | 351 if (offset < 0) { |
347 es.throwDOMException(IndexSizeError); | 352 es.throwDOMException(IndexSizeError, "Failed to execute 'extend' on a 'S election' object: " + String::number(offset) + " is not a valid offset."); |
353 return; | |
354 } | |
355 if (offset > (node->offsetInCharacters() ? caretMaxOffset(node) : (int)node- >childNodeCount())) { | |
356 es.throwDOMException(IndexSizeError, "Failed to execute 'extend' on a 'S election' object: " + String::number(offset) + " is larger than the given node's length."); | |
348 return; | 357 return; |
349 } | 358 } |
350 | 359 |
351 if (!isValidForPosition(node)) | 360 if (!isValidForPosition(node)) |
352 return; | 361 return; |
353 | 362 |
354 // FIXME: Eliminate legacy editing positions | 363 // FIXME: Eliminate legacy editing positions |
355 m_frame->selection()->setExtent(VisiblePosition(createLegacyEditingPosition( node, offset), DOWNSTREAM)); | 364 m_frame->selection()->setExtent(VisiblePosition(createLegacyEditingPosition( node, offset), DOWNSTREAM)); |
356 } | 365 } |
357 | 366 |
358 PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionState& es) | 367 PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionState& es) |
359 { | 368 { |
360 if (!m_frame) | 369 if (!m_frame) |
361 return 0; | 370 return 0; |
362 | 371 |
363 if (index < 0 || index >= rangeCount()) { | 372 if (index < 0 || index >= rangeCount()) { |
364 es.throwDOMException(IndexSizeError); | 373 es.throwDOMException(IndexSizeError, "Failed to execute 'getRangeAt' on a 'Selection' object: " + String::number(index) + " is not a valid index."); |
365 return 0; | 374 return 0; |
366 } | 375 } |
367 | 376 |
368 // If you're hitting this, you've added broken multi-range selection support | 377 // If you're hitting this, you've added broken multi-range selection support |
369 ASSERT(rangeCount() == 1); | 378 ASSERT(rangeCount() == 1); |
370 | 379 |
371 if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) { | 380 if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) { |
372 ContainerNode* container = shadowAncestor->parentNodeGuaranteedHostFree( ); | 381 ContainerNode* container = shadowAncestor->parentNodeGuaranteedHostFree( ); |
373 int offset = shadowAncestor->nodeIndex(); | 382 int offset = shadowAncestor->nodeIndex(); |
374 return Range::create(shadowAncestor->document(), container, offset, cont ainer, offset); | 383 return Range::create(shadowAncestor->document(), container, offset, cont ainer, offset); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 | 541 |
533 bool DOMSelection::isValidForPosition(Node* node) const | 542 bool DOMSelection::isValidForPosition(Node* node) const |
534 { | 543 { |
535 ASSERT(m_frame); | 544 ASSERT(m_frame); |
536 if (!node) | 545 if (!node) |
537 return true; | 546 return true; |
538 return node->document() == m_frame->document(); | 547 return node->document() == m_frame->document(); |
539 } | 548 } |
540 | 549 |
541 } // namespace WebCore | 550 } // namespace WebCore |
OLD | NEW |