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

Side by Side Diff: Source/core/html/HTMLTextFormControlElement.cpp

Issue 392573002: HTMLTextAreaElement.setSelectionRange should not change focus. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix tests Created 6 years, 5 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 | « Source/core/html/HTMLTextFormControlElement.h ('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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 16 matching lines...) Expand all
27 27
28 #include "bindings/core/v8/ExceptionState.h" 28 #include "bindings/core/v8/ExceptionState.h"
29 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 29 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
30 #include "core/HTMLNames.h" 30 #include "core/HTMLNames.h"
31 #include "core/accessibility/AXObjectCache.h" 31 #include "core/accessibility/AXObjectCache.h"
32 #include "core/dom/Document.h" 32 #include "core/dom/Document.h"
33 #include "core/dom/NodeList.h" 33 #include "core/dom/NodeList.h"
34 #include "core/dom/NodeTraversal.h" 34 #include "core/dom/NodeTraversal.h"
35 #include "core/dom/Text.h" 35 #include "core/dom/Text.h"
36 #include "core/dom/shadow/ShadowRoot.h" 36 #include "core/dom/shadow/ShadowRoot.h"
37 #include "core/editing/Editor.h"
37 #include "core/editing/FrameSelection.h" 38 #include "core/editing/FrameSelection.h"
38 #include "core/editing/TextIterator.h" 39 #include "core/editing/TextIterator.h"
39 #include "core/events/Event.h" 40 #include "core/events/Event.h"
40 #include "core/frame/LocalFrame.h" 41 #include "core/frame/LocalFrame.h"
41 #include "core/frame/UseCounter.h" 42 #include "core/frame/UseCounter.h"
42 #include "core/html/HTMLBRElement.h" 43 #include "core/html/HTMLBRElement.h"
43 #include "core/html/shadow/ShadowElementNames.h" 44 #include "core/html/shadow/ShadowElementNames.h"
45 #include "core/page/FocusController.h"
46 #include "core/page/Page.h"
47 #include "core/rendering/RenderBlock.h"
44 #include "core/rendering/RenderBlockFlow.h" 48 #include "core/rendering/RenderBlockFlow.h"
45 #include "core/rendering/RenderTheme.h" 49 #include "core/rendering/RenderTheme.h"
46 #include "platform/heap/Handle.h" 50 #include "platform/heap/Handle.h"
47 #include "platform/text/TextBoundaries.h" 51 #include "platform/text/TextBoundaries.h"
48 #include "wtf/text/StringBuilder.h" 52 #include "wtf/text/StringBuilder.h"
49 53
50 namespace blink { 54 namespace blink {
51 55
52 using namespace HTMLNames; 56 using namespace HTMLNames;
53 57
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 setSelectionRange(std::min(end, selectionStart()), end, selectionDirection() ); 177 setSelectionRange(std::min(end, selectionStart()), end, selectionDirection() );
174 } 178 }
175 179
176 void HTMLTextFormControlElement::setSelectionDirection(const String& direction) 180 void HTMLTextFormControlElement::setSelectionDirection(const String& direction)
177 { 181 {
178 setSelectionRange(selectionStart(), selectionEnd(), direction); 182 setSelectionRange(selectionStart(), selectionEnd(), direction);
179 } 183 }
180 184
181 void HTMLTextFormControlElement::select() 185 void HTMLTextFormControlElement::select()
182 { 186 {
183 setSelectionRange(0, std::numeric_limits<int>::max(), SelectionHasNoDirectio n); 187 document().updateLayoutIgnorePendingStylesheets();
188 RefPtrWillBeRawPtr<HTMLTextFormControlElement> protector(this);
189 if (isFocusable())
190 document().page()->focusController().setFocusedElement(this, document(). frame());
191 setSelectionRange(0, std::numeric_limits<int>::max(), SelectionHasNoDirectio n, ChangeSelection);
184 } 192 }
185 193
186 bool HTMLTextFormControlElement::shouldDispatchFormControlChangeEvent(String& ol dValue, String& newValue) 194 bool HTMLTextFormControlElement::shouldDispatchFormControlChangeEvent(String& ol dValue, String& newValue)
187 { 195 {
188 return !equalIgnoringNullity(oldValue, newValue); 196 return !equalIgnoringNullity(oldValue, newValue);
189 } 197 }
190 198
191 void HTMLTextFormControlElement::dispatchFormControlChangeEvent() 199 void HTMLTextFormControlElement::dispatchFormControlChangeEvent()
192 { 200 {
193 String newValue = value(); 201 String newValue = value();
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 } 275 }
268 276
269 void HTMLTextFormControlElement::setSelectionRange(int start, int end, const Str ing& directionString) 277 void HTMLTextFormControlElement::setSelectionRange(int start, int end, const Str ing& directionString)
270 { 278 {
271 TextFieldSelectionDirection direction = SelectionHasNoDirection; 279 TextFieldSelectionDirection direction = SelectionHasNoDirection;
272 if (directionString == "forward") 280 if (directionString == "forward")
273 direction = SelectionHasForwardDirection; 281 direction = SelectionHasForwardDirection;
274 else if (directionString == "backward") 282 else if (directionString == "backward")
275 direction = SelectionHasBackwardDirection; 283 direction = SelectionHasBackwardDirection;
276 284
285 if (direction == SelectionHasNoDirection && document().frame() && document() .frame()->editor().behavior().shouldConsiderSelectionAsDirectional())
286 direction = SelectionHasForwardDirection;
287
277 return setSelectionRange(start, end, direction); 288 return setSelectionRange(start, end, direction);
278 } 289 }
279 290
280 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField SelectionDirection direction) 291 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField SelectionDirection direction, SelectionOption selectionOption)
281 { 292 {
282 document().updateLayoutIgnorePendingStylesheets(); 293 document().updateLayoutIgnorePendingStylesheets();
283 294
284 if (!renderer() || !renderer()->isTextControl()) 295 if (!renderer() || !renderer()->isTextControl())
285 return; 296 return;
286 297
287 end = std::max(end, 0); 298 end = std::max(end, 0);
288 start = std::min(std::max(start, 0), end); 299 start = std::min(std::max(start, 0), end);
300 cacheSelection(start, end, direction);
301 if (!hasVisibleTextArea(renderer(), innerEditorElement()))
302 return;
289 303
290 if (!hasVisibleTextArea(renderer(), innerEditorElement())) { 304 LocalFrame* frame = document().frame();
291 cacheSelection(start, end, direction); 305
306 if (!frame)
292 return; 307 return;
293 } 308
309 if (selectionOption == NotChangeSelection && document().focusedElement() != this)
310 return;
311
294 VisiblePosition startPosition = visiblePositionForIndex(start); 312 VisiblePosition startPosition = visiblePositionForIndex(start);
295 VisiblePosition endPosition; 313 VisiblePosition endPosition;
296 if (start == end) 314 if (start == end)
297 endPosition = startPosition; 315 endPosition = startPosition;
298 else 316 else
299 endPosition = visiblePositionForIndex(end); 317 endPosition = visiblePositionForIndex(end);
300 318
301 // startPosition and endPosition can be null position for example when 319 // startPosition and endPosition can be null position for example when
302 // "-webkit-user-select: none" style attribute is specified. 320 // "-webkit-user-select: none" style attribute is specified.
303 if (startPosition.isNotNull() && endPosition.isNotNull()) { 321 if (startPosition.isNotNull() && endPosition.isNotNull()) {
304 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() == this 322 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() == this
305 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th is); 323 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th is);
306 } 324 }
307 VisibleSelection newSelection; 325 VisibleSelection newSelection;
308 if (direction == SelectionHasBackwardDirection) 326 if (direction == SelectionHasBackwardDirection)
309 newSelection = VisibleSelection(endPosition, startPosition); 327 newSelection = VisibleSelection(endPosition, startPosition);
310 else 328 else
311 newSelection = VisibleSelection(startPosition, endPosition); 329 newSelection = VisibleSelection(startPosition, endPosition);
312 newSelection.setIsDirectional(direction != SelectionHasNoDirection); 330 newSelection.setIsDirectional(direction != SelectionHasNoDirection);
313 331
314 if (LocalFrame* frame = document().frame()) 332 frame->selection().setSelection(newSelection, FrameSelection::CloseTyping | FrameSelection::ClearTypingStyle | FrameSelection::DoNotSetFocus);
315 frame->selection().setSelection(newSelection);
316 } 333 }
317 334
318 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c onst 335 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c onst
319 { 336 {
320 if (index <= 0) 337 if (index <= 0)
321 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST REAM); 338 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST REAM);
322 RefPtrWillBeRawPtr<Range> range = Range::create(document()); 339 RefPtrWillBeRawPtr<Range> range = Range::create(document());
323 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION); 340 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION);
324 CharacterIterator it(range.get()); 341 CharacterIterator it(range.get());
325 it.advance(index - 1); 342 it.advance(index - 1);
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 Text* textNode = toText(node); 911 Text* textNode = toText(node);
895 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi votPosition.offsetInContainerNode() : 0); 912 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi votPosition.offsetInContainerNode() : 0);
896 if (firstLineBreak != kNotFound) 913 if (firstLineBreak != kNotFound)
897 return Position(textNode, firstLineBreak + 1); 914 return Position(textNode, firstLineBreak + 1);
898 } 915 }
899 } 916 }
900 return endOfInnerText(textFormControl); 917 return endOfInnerText(textFormControl);
901 } 918 }
902 919
903 } // namespace Webcore 920 } // namespace Webcore
OLDNEW
« no previous file with comments | « Source/core/html/HTMLTextFormControlElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698