Index: Source/core/html/HTMLTextFormControlElement.cpp |
diff --git a/Source/core/html/HTMLTextFormControlElement.cpp b/Source/core/html/HTMLTextFormControlElement.cpp |
index bf3597a17159f2e0cad756e6e8be0ffc25b2bc4c..bec1bf2764b00840dad0f6d61373599237fba964 100644 |
--- a/Source/core/html/HTMLTextFormControlElement.cpp |
+++ b/Source/core/html/HTMLTextFormControlElement.cpp |
@@ -164,12 +164,12 @@ void HTMLTextFormControlElement::updatePlaceholderVisibility(bool placeholderVal |
void HTMLTextFormControlElement::setSelectionStart(int start) |
{ |
- setSelectionRange(start, max(start, selectionEnd()), selectionDirection()); |
+ setSelectionRange(start, max(start, selectionEndForJavascriptBindings()), selectionDirection()); |
} |
void HTMLTextFormControlElement::setSelectionEnd(int end) |
{ |
- setSelectionRange(min(end, selectionStart()), end, selectionDirection()); |
+ setSelectionRange(min(end, selectionStartForJavascriptBindings()), end, selectionDirection()); |
} |
void HTMLTextFormControlElement::setSelectionDirection(const String& direction) |
@@ -285,11 +285,19 @@ void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField |
end = max(end, 0); |
start = min(max(start, 0), end); |
+ int textLength = innerTextValue().length(); |
+ if (textLength) { |
+ if (textLength < start) { |
+ start = textLength; |
+ end = textLength; |
+ } else if (textLength >= start && textLength < end) { |
+ end = textLength; |
+ } |
+ } |
+ cacheSelection(start, end, direction); |
- if (!hasVisibleTextArea(renderer(), innerTextElement())) { |
- cacheSelection(start, end, direction); |
+ if (!hasVisibleTextArea(renderer(), innerTextElement())) |
return; |
- } |
VisiblePosition startPosition = visiblePositionForIndex(start); |
VisiblePosition endPosition; |
if (start == end) |
@@ -341,6 +349,19 @@ int HTMLTextFormControlElement::selectionStart() const |
{ |
if (!isTextFormControl()) |
return 0; |
+ if (document().focusedElement() != this) { |
+ if (!innerTextValue().length()) |
+ return 0; |
+ return m_cachedSelectionStart; |
+ } |
+ |
+ return computeSelectionStart(); |
+} |
+ |
+int HTMLTextFormControlElement::selectionStartForJavascriptBindings() const |
+{ |
+ if (!isTextFormControl()) |
+ return 0; |
if (document().focusedElement() != this) |
return m_cachedSelectionStart; |
@@ -361,8 +382,21 @@ int HTMLTextFormControlElement::selectionEnd() const |
{ |
if (!isTextFormControl()) |
return 0; |
+ if (document().focusedElement() != this) { |
+ if (!innerTextValue().length()) |
+ return 0; |
+ return m_cachedSelectionEnd; |
+ } |
+ return computeSelectionEnd(); |
+} |
+ |
+int HTMLTextFormControlElement::selectionEndForJavascriptBindings() const |
+{ |
+ if (!isTextFormControl()) |
+ return 0; |
if (document().focusedElement() != this) |
return m_cachedSelectionEnd; |
+ |
return computeSelectionEnd(); |
} |
@@ -478,9 +512,6 @@ 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 (LocalFrame* frame = document().frame()) { |
if (frame->selection().isRange() && userTriggered) |
dispatchEvent(Event::createBubble(EventTypeNames::select)); |