Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: third_party/WebKit/Source/core/editing/DOMSelection.cpp

Issue 2877793003: Merge "Selection API: collapseToStart() and collapseToEnd() should work for text fields." to M59 (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/editing/selection/collapseto_in_text_fields.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return; 287 return;
288 288
289 // The method must throw InvalidStateError exception if the context object is 289 // The method must throw InvalidStateError exception if the context object is
290 // empty. 290 // empty.
291 if (rangeCount() == 0) { 291 if (rangeCount() == 0) {
292 exception_state.ThrowDOMException(kInvalidStateError, 292 exception_state.ThrowDOMException(kInvalidStateError,
293 "there is no selection."); 293 "there is no selection.");
294 return; 294 return;
295 } 295 }
296 296
297 // Otherwise, it must create a new range, set both its start and end to the 297 if (Range* current_range = DocumentCachedRange()) {
298 // end of the context object's range, 298 // Otherwise, it must create a new range, set both its start and end to the
299 Range* new_range = getRangeAt(0, ASSERT_NO_EXCEPTION)->cloneRange(); 299 // end of the context object's range,
300 new_range->collapse(false); 300 Range* new_range = current_range->cloneRange();
301 new_range->collapse(false);
301 302
302 // and then set the context object's range to the newly-created range. 303 // and then set the context object's range to the newly-created range.
303 SelectionInDOMTree::Builder builder; 304 SelectionInDOMTree::Builder builder;
304 builder.Collapse(new_range->EndPosition()); 305 builder.Collapse(new_range->EndPosition());
305 UpdateFrameSelection(builder.Build(), new_range); 306 UpdateFrameSelection(builder.Build(), new_range);
307 } else {
308 // TODO(tkent): The Selection API doesn't define this behavior. We should
309 // discuss this on https://github.com/w3c/selection-api/issues/83.
310 SelectionInDOMTree::Builder builder;
311 builder.Collapse(
312 GetFrame()->Selection().GetSelectionInDOMTree().ComputeEndPosition());
313 UpdateFrameSelection(builder.Build(), nullptr);
314 }
306 } 315 }
307 316
308 // https://www.w3.org/TR/selection-api/#dom-selection-collapsetostart 317 // https://www.w3.org/TR/selection-api/#dom-selection-collapsetostart
309 void DOMSelection::collapseToStart(ExceptionState& exception_state) { 318 void DOMSelection::collapseToStart(ExceptionState& exception_state) {
310 if (!IsAvailable()) 319 if (!IsAvailable())
311 return; 320 return;
312 321
313 // The method must throw InvalidStateError ([DOM4]) exception if the context 322 // The method must throw InvalidStateError ([DOM4]) exception if the context
314 // object is empty. 323 // object is empty.
315 if (rangeCount() == 0) { 324 if (rangeCount() == 0) {
316 exception_state.ThrowDOMException(kInvalidStateError, 325 exception_state.ThrowDOMException(kInvalidStateError,
317 "there is no selection."); 326 "there is no selection.");
318 return; 327 return;
319 } 328 }
320 329
321 // Otherwise, it must create a new range, set both its start and end to the 330 if (Range* current_range = DocumentCachedRange()) {
322 // start of the context object's range, 331 // Otherwise, it must create a new range, set both its start and end to the
323 Range* new_range = getRangeAt(0, ASSERT_NO_EXCEPTION)->cloneRange(); 332 // start of the context object's range,
324 new_range->collapse(true); 333 Range* new_range = current_range->cloneRange();
334 new_range->collapse(true);
325 335
326 // and then set the context object's range to the newly-created range. 336 // and then set the context object's range to the newly-created range.
327 SelectionInDOMTree::Builder builder; 337 SelectionInDOMTree::Builder builder;
328 builder.Collapse(new_range->StartPosition()); 338 builder.Collapse(new_range->StartPosition());
329 UpdateFrameSelection(builder.Build(), new_range); 339 UpdateFrameSelection(builder.Build(), new_range);
340 } else {
341 // TODO(tkent): The Selection API doesn't define this behavior. We should
342 // discuss this on https://github.com/w3c/selection-api/issues/83.
343 SelectionInDOMTree::Builder builder;
344 builder.Collapse(
345 GetFrame()->Selection().GetSelectionInDOMTree().ComputeStartPosition());
346 UpdateFrameSelection(builder.Build(), nullptr);
347 }
330 } 348 }
331 349
332 void DOMSelection::empty() { 350 void DOMSelection::empty() {
333 if (!IsAvailable()) 351 if (!IsAvailable())
334 return; 352 return;
335 GetFrame()->Selection().Clear(); 353 GetFrame()->Selection().Clear();
336 } 354 }
337 355
338 void DOMSelection::setBaseAndExtent(Node* base_node, 356 void DOMSelection::setBaseAndExtent(Node* base_node,
339 unsigned base_offset, 357 unsigned base_offset,
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 tree_scope_->GetDocument().AddConsoleMessage( 844 tree_scope_->GetDocument().AddConsoleMessage(
827 ConsoleMessage::Create(kJSMessageSource, kErrorMessageLevel, message)); 845 ConsoleMessage::Create(kJSMessageSource, kErrorMessageLevel, message));
828 } 846 }
829 847
830 DEFINE_TRACE(DOMSelection) { 848 DEFINE_TRACE(DOMSelection) {
831 visitor->Trace(tree_scope_); 849 visitor->Trace(tree_scope_);
832 ContextClient::Trace(visitor); 850 ContextClient::Trace(visitor);
833 } 851 }
834 852
835 } // namespace blink 853 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/editing/selection/collapseto_in_text_fields.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698