Bug 1343037 part 6. Simplify the setup around the editor state's GetSelectionRange function. r=ehsan
Really, there are only two cases we need to worry about. Either IsSelectionCached(), and then our SelectionProperties has the data we want, or not and then we have a non-null mSelCon which has the data we want. Since we are now using cached selection state a lot more (instead of initializing the editor whenever someone asks for selection state), we need to actually update it more correctly when .value is set. And since we now update the cached selection state for the case when .value has been set (to point to the end of the text), we need to change HTMLInputElement::HasCachedSelection to return false for that case. Otherwise we will always do eager editor init on value set. We handle that by not doing eager init if the cached selection is collapsed. The web platform test changes test the "update on .value set" behavior. They fail without this patch, pass with it. MozReview-Commit-ID: DDU8U4MGb23
This commit is contained in:
@@ -2010,7 +2010,8 @@ HTMLInputElement::SetValue(const nsAString& aValue, CallerType aCallerType,
|
||||
|
||||
nsresult rv =
|
||||
SetValueInternal(aValue, nsTextEditorState::eSetValue_ByContent |
|
||||
nsTextEditorState::eSetValue_Notify);
|
||||
nsTextEditorState::eSetValue_Notify |
|
||||
nsTextEditorState::eSetValue_MoveCursorToEnd);
|
||||
if (NS_FAILED(rv)) {
|
||||
aRv.Throw(rv);
|
||||
return;
|
||||
@@ -2022,7 +2023,8 @@ HTMLInputElement::SetValue(const nsAString& aValue, CallerType aCallerType,
|
||||
} else {
|
||||
nsresult rv =
|
||||
SetValueInternal(aValue, nsTextEditorState::eSetValue_ByContent |
|
||||
nsTextEditorState::eSetValue_Notify);
|
||||
nsTextEditorState::eSetValue_Notify |
|
||||
nsTextEditorState::eSetValue_MoveCursorToEnd);
|
||||
if (NS_FAILED(rv)) {
|
||||
aRv.Throw(rv);
|
||||
return;
|
||||
@@ -2866,8 +2868,10 @@ HTMLInputElement::SetUserInput(const nsAString& aValue)
|
||||
return rv.StealNSResult();
|
||||
} else {
|
||||
nsresult rv =
|
||||
SetValueInternal(aValue, nsTextEditorState::eSetValue_BySetUserInput |
|
||||
nsTextEditorState::eSetValue_Notify);
|
||||
SetValueInternal(aValue,
|
||||
nsTextEditorState::eSetValue_BySetUserInput |
|
||||
nsTextEditorState::eSetValue_Notify|
|
||||
nsTextEditorState::eSetValue_MoveCursorToEnd);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
@@ -6321,14 +6325,9 @@ HTMLInputElement::SetRangeText(const nsAString& aReplacement, ErrorResult& aRv)
|
||||
}
|
||||
|
||||
int32_t start, end;
|
||||
aRv = GetSelectionRange(&start, &end);
|
||||
GetSelectionRange(&start, &end, aRv);
|
||||
if (aRv.Failed()) {
|
||||
nsTextEditorState* state = GetEditorState();
|
||||
if (state && state->IsSelectionCached()) {
|
||||
start = state->GetSelectionProperties().GetStart();
|
||||
end = state->GetSelectionProperties().GetEnd();
|
||||
aRv = NS_OK;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
SetRangeText(aReplacement, start, end, mozilla::dom::SelectionMode::Preserve,
|
||||
@@ -6364,14 +6363,9 @@ HTMLInputElement::SetRangeText(const nsAString& aReplacement, uint32_t aStart,
|
||||
}
|
||||
|
||||
if (aSelectionStart == -1 && aSelectionEnd == -1) {
|
||||
aRv = GetSelectionRange(&aSelectionStart, &aSelectionEnd);
|
||||
GetSelectionRange(&aSelectionStart, &aSelectionEnd, aRv);
|
||||
if (aRv.Failed()) {
|
||||
nsTextEditorState* state = GetEditorState();
|
||||
if (state && state->IsSelectionCached()) {
|
||||
aSelectionStart = state->GetSelectionProperties().GetStart();
|
||||
aSelectionEnd = state->GetSelectionProperties().GetEnd();
|
||||
aRv = NS_OK;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6447,17 +6441,7 @@ int32_t
|
||||
HTMLInputElement::GetSelectionStartIgnoringType(ErrorResult& aRv)
|
||||
{
|
||||
int32_t selEnd, selStart;
|
||||
nsresult rv = GetSelectionRange(&selStart, &selEnd);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
nsTextEditorState* state = GetEditorState();
|
||||
if (state && state->IsSelectionCached()) {
|
||||
return state->GetSelectionProperties().GetStart();
|
||||
}
|
||||
aRv.Throw(rv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
GetSelectionRange(&selStart, &selEnd, aRv);
|
||||
return selStart;
|
||||
}
|
||||
|
||||
@@ -6488,7 +6472,7 @@ HTMLInputElement::SetSelectionStart(const Nullable<int32_t>& aSelectionStart,
|
||||
}
|
||||
|
||||
int32_t start, end;
|
||||
aRv = GetSelectionRange(&start, &end);
|
||||
GetSelectionRange(&start, &end, aRv);
|
||||
if (aRv.Failed()) {
|
||||
return;
|
||||
}
|
||||
@@ -6520,17 +6504,7 @@ int32_t
|
||||
HTMLInputElement::GetSelectionEndIgnoringType(ErrorResult& aRv)
|
||||
{
|
||||
int32_t selEnd, selStart;
|
||||
nsresult rv = GetSelectionRange(&selStart, &selEnd);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
nsTextEditorState* state = GetEditorState();
|
||||
if (state && state->IsSelectionCached()) {
|
||||
return state->GetSelectionProperties().GetEnd();
|
||||
}
|
||||
aRv.Throw(rv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
GetSelectionRange(&selStart, &selEnd, aRv);
|
||||
return selEnd;
|
||||
}
|
||||
|
||||
@@ -6561,7 +6535,7 @@ HTMLInputElement::SetSelectionEnd(const Nullable<int32_t>& aSelectionEnd,
|
||||
}
|
||||
|
||||
int32_t start, end;
|
||||
aRv = GetSelectionRange(&start, &end);
|
||||
GetSelectionRange(&start, &end, aRv);
|
||||
if (aRv.Failed()) {
|
||||
return;
|
||||
}
|
||||
@@ -6582,22 +6556,19 @@ HTMLInputElement::GetFiles(nsIDOMFileList** aFileList)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
void
|
||||
HTMLInputElement::GetSelectionRange(int32_t* aSelectionStart,
|
||||
int32_t* aSelectionEnd)
|
||||
int32_t* aSelectionEnd,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
// Flush frames, because our editor state will want to work with the frame.
|
||||
if (IsInComposedDoc()) {
|
||||
GetComposedDoc()->FlushPendingNotifications(FlushType::Frames);
|
||||
}
|
||||
|
||||
nsTextEditorState* state = GetEditorState();
|
||||
if (!state) {
|
||||
// Not a text control.
|
||||
return NS_ERROR_FAILURE;
|
||||
aRv.Throw(NS_ERROR_UNEXPECTED);
|
||||
return;
|
||||
}
|
||||
|
||||
return state->GetSelectionRange(aSelectionStart, aSelectionEnd);
|
||||
state->GetSelectionRange(aSelectionStart, aSelectionEnd, aRv);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -6676,7 +6647,7 @@ HTMLInputElement::SetSelectionDirection(const nsAString& aDirection, ErrorResult
|
||||
}
|
||||
|
||||
int32_t start, end;
|
||||
aRv = GetSelectionRange(&start, &end);
|
||||
GetSelectionRange(&start, &end, aRv);
|
||||
if (!aRv.Failed()) {
|
||||
aRv = SetSelectionRange(start, end, aDirection);
|
||||
}
|
||||
@@ -8540,7 +8511,8 @@ HTMLInputElement::HasCachedSelection()
|
||||
if (state) {
|
||||
isCached = state->IsSelectionCached() &&
|
||||
state->HasNeverInitializedBefore() &&
|
||||
!state->GetSelectionProperties().IsDefault();
|
||||
state->GetSelectionProperties().GetStart() !=
|
||||
state->GetSelectionProperties().GetEnd();
|
||||
if (isCached) {
|
||||
state->WillInitEagerly();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user