Chromium Code Reviews| Index: Source/core/html/HTMLTextFormControlElement.cpp |
| diff --git a/Source/core/html/HTMLTextFormControlElement.cpp b/Source/core/html/HTMLTextFormControlElement.cpp |
| index 0434728f24a557dcb6aa4a94af75ce08ceaccc60..eb3ef54c466a7b70b6383b21cd449046dd8631ab 100644 |
| --- a/Source/core/html/HTMLTextFormControlElement.cpp |
| +++ b/Source/core/html/HTMLTextFormControlElement.cpp |
| @@ -163,12 +163,18 @@ void HTMLTextFormControlElement::updatePlaceholderVisibility(bool placeholderVal |
| void HTMLTextFormControlElement::setSelectionStart(int start) |
| { |
| - setSelectionRange(start, std::max(start, selectionEnd()), selectionDirection()); |
| + if (!innerEditorValue().isEmpty()) |
| + setSelectionRange(start, std::max(start, selectionEnd()), selectionDirection()); |
| + else |
| + setSelectionRange(start, std::max(start, m_cachedSelectionEnd), selectionDirection()); |
| } |
| void HTMLTextFormControlElement::setSelectionEnd(int end) |
| { |
| - setSelectionRange(std::min(end, selectionStart()), end, selectionDirection()); |
| + if (!innerEditorValue().isEmpty()) |
| + setSelectionRange(std::min(end, selectionStart()), end, selectionDirection()); |
| + else |
| + setSelectionRange(std::min(end, m_cachedSelectionStart), end, selectionDirection()); |
| } |
| void HTMLTextFormControlElement::setSelectionDirection(const String& direction) |
| @@ -284,11 +290,21 @@ void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField |
| end = std::max(end, 0); |
| start = std::min(std::max(start, 0), end); |
| + int textLength = innerEditorValue().length(); |
| + if (textLength) { |
| + if (textLength < start) { |
| + start = textLength; |
| + end = textLength; |
| + } else if (textLength < end) { |
| + end = textLength; |
| + } |
| + } |
| + cacheSelection(start, end, direction); |
| + bool isCaretSelection = (start == end); |
| + bool shouldSetSelection = (document().focusedElement() == this) || (!isCaretSelection && start < textLength); |
| - if (!hasVisibleTextArea(renderer(), innerEditorElement())) { |
| - cacheSelection(start, end, direction); |
| + if (!hasVisibleTextArea(renderer(), innerEditorElement())) |
| return; |
| - } |
| VisiblePosition startPosition = visiblePositionForIndex(start); |
| VisiblePosition endPosition; |
| if (start == end) |
| @@ -309,7 +325,8 @@ void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField |
| newSelection = VisibleSelection(startPosition, endPosition); |
| newSelection.setIsDirectional(direction != SelectionHasNoDirection); |
| - if (LocalFrame* frame = document().frame()) |
| + LocalFrame* frame = document().frame(); |
| + if (frame && shouldSetSelection) |
| frame->selection().setSelection(newSelection); |
| } |
| @@ -338,11 +355,10 @@ int HTMLTextFormControlElement::indexForVisiblePosition(const VisiblePosition& p |
| int HTMLTextFormControlElement::selectionStart() const |
| { |
| - if (!isTextFormControl()) |
| + if (!isTextFormControl() || innerEditorValue().isEmpty()) |
|
harpreet.sk
2014/07/02 10:13:05
We require check in selectionStart() and selection
|
| return 0; |
| if (document().focusedElement() != this) |
| return m_cachedSelectionStart; |
| - |
| return computeSelectionStart(); |
| } |
| @@ -358,7 +374,7 @@ int HTMLTextFormControlElement::computeSelectionStart() const |
| int HTMLTextFormControlElement::selectionEnd() const |
| { |
| - if (!isTextFormControl()) |
| + if (!isTextFormControl() || innerEditorValue().isEmpty()) |
| return 0; |
| if (document().focusedElement() != this) |
| return m_cachedSelectionEnd; |
| @@ -477,8 +493,8 @@ void HTMLTextFormControlElement::selectionChanged(bool userTriggered) |
| if (!renderer() || !isTextFormControl()) |
| return; |
| - // selectionStart() or selectionEnd() will return cached selection when this node doesn't have focus |
| - cacheSelection(computeSelectionStart(), computeSelectionEnd(), computeSelectionDirection()); |
| + if (document().focusedElement() == this) |
| + cacheSelection(computeSelectionStart(), computeSelectionEnd(), computeSelectionDirection()); |
| if (LocalFrame* frame = document().frame()) { |
| if (frame->selection().isRange() && userTriggered) |