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

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: Call setSelectionRange inside setValueCommon 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
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 if (isFocusable()) {
189 RefPtrWillBeRawPtr<HTMLTextFormControlElement> protector(this);
190 document().page()->focusController().setFocusedElement(this, document(). frame());
191 }
192 setSelectionRange(0, std::numeric_limits<int>::max(), SelectionHasNoDirectio n, ChangeSelection);
184 } 193 }
185 194
186 bool HTMLTextFormControlElement::shouldDispatchFormControlChangeEvent(String& ol dValue, String& newValue) 195 bool HTMLTextFormControlElement::shouldDispatchFormControlChangeEvent(String& ol dValue, String& newValue)
187 { 196 {
188 return !equalIgnoringNullity(oldValue, newValue); 197 return !equalIgnoringNullity(oldValue, newValue);
189 } 198 }
190 199
191 void HTMLTextFormControlElement::dispatchFormControlChangeEvent() 200 void HTMLTextFormControlElement::dispatchFormControlChangeEvent()
192 { 201 {
193 String newValue = value(); 202 String newValue = value();
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 } 276 }
268 277
269 void HTMLTextFormControlElement::setSelectionRange(int start, int end, const Str ing& directionString) 278 void HTMLTextFormControlElement::setSelectionRange(int start, int end, const Str ing& directionString)
270 { 279 {
271 TextFieldSelectionDirection direction = SelectionHasNoDirection; 280 TextFieldSelectionDirection direction = SelectionHasNoDirection;
272 if (directionString == "forward") 281 if (directionString == "forward")
273 direction = SelectionHasForwardDirection; 282 direction = SelectionHasForwardDirection;
274 else if (directionString == "backward") 283 else if (directionString == "backward")
275 direction = SelectionHasBackwardDirection; 284 direction = SelectionHasBackwardDirection;
276 285
286 if (direction == SelectionHasNoDirection && document().frame() && document() .frame()->editor().behavior().shouldConsiderSelectionAsDirectional())
287 direction = SelectionHasForwardDirection;
288
277 return setSelectionRange(start, end, direction); 289 return setSelectionRange(start, end, direction);
278 } 290 }
279 291
280 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField SelectionDirection direction) 292 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField SelectionDirection direction, SelectionOption selectionOption)
281 { 293 {
282 document().updateLayoutIgnorePendingStylesheets(); 294 document().updateLayoutIgnorePendingStylesheets();
283 295
284 if (!renderer() || !renderer()->isTextControl()) 296 if (!renderer() || !renderer()->isTextControl())
285 return; 297 return;
286 298
287 end = std::max(end, 0); 299 end = std::max(end, 0);
288 start = std::min(std::max(start, 0), end); 300 start = std::min(std::max(start, 0), end);
301 cacheSelection(start, end, direction);
302 if (!hasVisibleTextArea(renderer(), innerEditorElement()))
303 return;
289 304
290 if (!hasVisibleTextArea(renderer(), innerEditorElement())) { 305 LocalFrame* frame = document().frame();
291 cacheSelection(start, end, direction); 306
307 if (!frame)
292 return; 308 return;
293 } 309
310 if (selectionOption == NotChangeSelection && document().focusedElement() != this)
311 return;
312
294 VisiblePosition startPosition = visiblePositionForIndex(start); 313 VisiblePosition startPosition = visiblePositionForIndex(start);
295 VisiblePosition endPosition; 314 VisiblePosition endPosition;
296 if (start == end) 315 if (start == end)
297 endPosition = startPosition; 316 endPosition = startPosition;
298 else 317 else
299 endPosition = visiblePositionForIndex(end); 318 endPosition = visiblePositionForIndex(end);
300 319
301 // startPosition and endPosition can be null position for example when 320 // startPosition and endPosition can be null position for example when
302 // "-webkit-user-select: none" style attribute is specified. 321 // "-webkit-user-select: none" style attribute is specified.
303 if (startPosition.isNotNull() && endPosition.isNotNull()) { 322 if (startPosition.isNotNull() && endPosition.isNotNull()) {
304 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() == this 323 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() == this
305 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th is); 324 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th is);
306 } 325 }
307 VisibleSelection newSelection; 326 VisibleSelection newSelection;
308 if (direction == SelectionHasBackwardDirection) 327 if (direction == SelectionHasBackwardDirection)
309 newSelection = VisibleSelection(endPosition, startPosition); 328 newSelection = VisibleSelection(endPosition, startPosition);
310 else 329 else
311 newSelection = VisibleSelection(startPosition, endPosition); 330 newSelection = VisibleSelection(startPosition, endPosition);
312 newSelection.setIsDirectional(direction != SelectionHasNoDirection); 331 newSelection.setIsDirectional(direction != SelectionHasNoDirection);
313 332
314 if (LocalFrame* frame = document().frame()) 333 frame->selection().setSelection(newSelection, FrameSelection::CloseTyping | FrameSelection::ClearTypingStyle | FrameSelection::DoNotSetFocus);
315 frame->selection().setSelection(newSelection);
316 } 334 }
317 335
318 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c onst 336 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c onst
319 { 337 {
320 if (index <= 0) 338 if (index <= 0)
321 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST REAM); 339 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST REAM);
322 RefPtrWillBeRawPtr<Range> range = Range::create(document()); 340 RefPtrWillBeRawPtr<Range> range = Range::create(document());
323 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION); 341 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION);
324 CharacterIterator it(range.get()); 342 CharacterIterator it(range.get());
325 it.advance(index - 1); 343 it.advance(index - 1);
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 Text* textNode = toText(node); 912 Text* textNode = toText(node);
895 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi votPosition.offsetInContainerNode() : 0); 913 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi votPosition.offsetInContainerNode() : 0);
896 if (firstLineBreak != kNotFound) 914 if (firstLineBreak != kNotFound)
897 return Position(textNode, firstLineBreak + 1); 915 return Position(textNode, firstLineBreak + 1);
898 } 916 }
899 } 917 }
900 return endOfInnerText(textFormControl); 918 return endOfInnerText(textFormControl);
901 } 919 }
902 920
903 } // namespace Webcore 921 } // namespace Webcore
OLDNEW
« Source/core/html/HTMLTextAreaElement.cpp ('K') | « Source/core/html/HTMLTextFormControlElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698