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

Unified Diff: Source/core/html/HTMLTextFormControlElement.cpp

Issue 258063005: Blink does not respect input.selectionStart and input.selectionEnd for some cases (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: New patch fixing existing bug Created 6 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLTextFormControlElement.cpp
diff --git a/Source/core/html/HTMLTextFormControlElement.cpp b/Source/core/html/HTMLTextFormControlElement.cpp
index bf3597a17159f2e0cad756e6e8be0ffc25b2bc4c..3d25bf7228369eb2cdd3c092c0c8a8d4a83d0238 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,21 @@ 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) {
Inactive 2014/06/02 15:32:45 nit: The "textLength >= start && " part seems unne
harpreet.sk 2014/07/02 10:13:05 Done.
+ end = textLength;
+ }
+ }
+ cacheSelection(start, end, direction);
+ bool isCaretSelection = (start == end);
+ bool shouldSetSelection = (document().focusedElement() == this) || (!isCaretSelection && start < textLength);
tkent 2014/06/01 23:40:55 Why is !isCaretSelection necessary?
harpreet.sk 2014/07/02 10:13:05 This check is necessary because when input is not
- if (!hasVisibleTextArea(renderer(), innerTextElement())) {
- cacheSelection(start, end, direction);
+ if (!hasVisibleTextArea(renderer(), innerTextElement()))
return;
- }
VisiblePosition startPosition = visiblePositionForIndex(start);
VisiblePosition endPosition;
if (start == end)
@@ -310,7 +320,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);
}
@@ -341,12 +352,23 @@ int HTMLTextFormControlElement::selectionStart() const
{
if (!isTextFormControl())
return 0;
- if (document().focusedElement() != this)
+ if (document().focusedElement() != this) {
+ if (!innerTextValue().length())
tkent 2014/06/01 23:40:55 As I wrote again and again, adjusting value on sel
harpreet.sk 2014/07/02 10:13:05 We require a check in selectionStart() and selecti
+ return 0;
return m_cachedSelectionStart;
+ }
return computeSelectionStart();
}
+int HTMLTextFormControlElement::selectionStartForJavascriptBindings() const
+{
+ if (!isTextFormControl())
+ return 0;
+
+ return m_cachedSelectionStart;
+}
+
int HTMLTextFormControlElement::computeSelectionStart() const
{
ASSERT(isTextFormControl());
@@ -361,11 +383,22 @@ int HTMLTextFormControlElement::selectionEnd() const
{
if (!isTextFormControl())
return 0;
- if (document().focusedElement() != this)
+ if (document().focusedElement() != this) {
+ if (!innerTextValue().length())
Inactive 2014/06/02 15:32:45 if (innerTextValue().isEmpty())
harpreet.sk 2014/07/02 10:13:05 Done.
+ return 0;
return m_cachedSelectionEnd;
+ }
return computeSelectionEnd();
}
+int HTMLTextFormControlElement::selectionEndForJavascriptBindings() const
+{
+ if (!isTextFormControl())
+ return 0;
+
+ return m_cachedSelectionEnd;
+}
+
int HTMLTextFormControlElement::computeSelectionEnd() const
{
ASSERT(isTextFormControl());
@@ -478,8 +511,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)

Powered by Google App Engine
This is Rietveld 408576698