Bug 1343037 part 9. Simplify the setup around the editor state's GetSelectionDirection 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.

MozReview-Commit-ID: AEW9D1zG6sM
This commit is contained in:
Boris Zbarsky 2017-03-09 14:44:05 -05:00
Родитель 8a06255585
Коммит cced97dfc3
4 изменённых файлов: 40 добавлений и 60 удалений

Просмотреть файл

@ -6571,29 +6571,15 @@ HTMLInputElement::GetSelectionDirection(nsAString& aDirection, ErrorResult& aRv)
return; return;
} }
nsIFormControlFrame* formControlFrame = GetFormControlFrame(true);
nsTextEditorState* state = GetEditorState(); nsTextEditorState* state = GetEditorState();
if (!state) { MOZ_ASSERT(state, "SupportsTextSelection came back true!");
aRv.Throw(NS_ERROR_FAILURE); nsITextControlFrame::SelectionDirection dir =
state->GetSelectionDirection(aRv);
if (aRv.Failed()) {
return; return;
} }
nsresult rv = NS_ERROR_FAILURE; DirectionToName(dir, aDirection);
if (formControlFrame) {
nsITextControlFrame::SelectionDirection dir;
rv = state->GetSelectionDirection(&dir);
if (NS_SUCCEEDED(rv)) {
DirectionToName(dir, aDirection);
return;
}
}
if (state->IsSelectionCached()) {
DirectionToName(state->GetSelectionProperties().GetDirection(), aDirection);
return;
}
aRv.Throw(rv);
} }
void void

Просмотреть файл

@ -807,23 +807,12 @@ DirectionToName(nsITextControlFrame::SelectionDirection dir, nsAString& aDirecti
void void
HTMLTextAreaElement::GetSelectionDirection(nsAString& aDirection, ErrorResult& aError) HTMLTextAreaElement::GetSelectionDirection(nsAString& aDirection, ErrorResult& aError)
{ {
nsresult rv = NS_ERROR_FAILURE; nsITextControlFrame::SelectionDirection dir =
nsIFormControlFrame* formControlFrame = GetFormControlFrame(true); mState.GetSelectionDirection(aError);
if (formControlFrame) { if (aError.Failed()) {
nsITextControlFrame::SelectionDirection dir;
rv = mState.GetSelectionDirection(&dir);
if (NS_SUCCEEDED(rv)) {
DirectionToName(dir, aDirection);
return;
}
}
if (mState.IsSelectionCached()) {
DirectionToName(mState.GetSelectionProperties().GetDirection(), aDirection);
return; return;
} }
DirectionToName(dir, aDirection);
aError.Throw(rv);
} }
void void

Просмотреть файл

@ -1608,39 +1608,41 @@ nsTextEditorState::GetSelectionRange(int32_t* aSelectionStart,
*aSelectionStart, *aSelectionEnd); *aSelectionStart, *aSelectionEnd);
} }
nsresult nsITextControlFrame::SelectionDirection
nsTextEditorState::GetSelectionDirection(nsITextControlFrame::SelectionDirection* aDirection) nsTextEditorState::GetSelectionDirection(ErrorResult& aRv)
{ {
MOZ_ASSERT(mBoundFrame, MOZ_ASSERT(IsSelectionCached() || GetSelectionController(),
"Caller didn't flush out frames and check for a frame?"); "How can we not have a cached selection if we have no selection "
MOZ_ASSERT(aDirection); "controller?");
// It's not clear that all the checks here are needed, but the previous // Note that we may have both IsSelectionCached() _and_
// version of this code in nsTextControlFrame was doing them, so we keep them // GetSelectionController() if we haven't initialized our editor yet.
// for now. if (IsSelectionCached()) {
return GetSelectionProperties().GetDirection();
nsresult rv = mBoundFrame->EnsureEditorInitialized(); }
NS_ENSURE_SUCCESS(rv, rv);
nsISelectionController* selCon = GetSelectionController(); nsISelectionController* selCon = GetSelectionController();
NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
nsCOMPtr<nsISelection> selection; nsCOMPtr<nsISelection> selection;
rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, nsresult rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
getter_AddRefs(selection)); getter_AddRefs(selection));
NS_ENSURE_SUCCESS(rv, rv); if (NS_WARN_IF(NS_FAILED(rv))) {
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE); aRv.Throw(rv);
return nsITextControlFrame::eForward; // Doesn't really matter
}
if (NS_WARN_IF(!selection)) {
aRv.Throw(NS_ERROR_FAILURE);
return nsITextControlFrame::eForward; // Doesn't really matter
}
dom::Selection* sel = selection->AsSelection(); dom::Selection* sel = selection->AsSelection();
nsDirection direction = sel->GetSelectionDirection(); nsDirection direction = sel->GetSelectionDirection();
if (direction == eDirNext) { if (direction == eDirNext) {
*aDirection = nsITextControlFrame::eForward; return nsITextControlFrame::eForward;
} else if (direction == eDirPrevious) {
*aDirection = nsITextControlFrame::eBackward;
} else {
NS_NOTREACHED("Invalid nsDirection enum value");
} }
return NS_OK;
MOZ_ASSERT(direction == eDirPrevious);
return nsITextControlFrame::eBackward;
} }
HTMLInputElement* HTMLInputElement*
@ -1721,11 +1723,13 @@ nsTextEditorState::UnbindFromFrame(nsTextControlFrame* aFrame)
if (!IsSelectionCached()) { if (!IsSelectionCached()) {
// Go ahead and cache it now. // Go ahead and cache it now.
int32_t start = 0, end = 0; int32_t start = 0, end = 0;
nsITextControlFrame::SelectionDirection direction =
nsITextControlFrame::eForward;
IgnoredErrorResult rangeRv; IgnoredErrorResult rangeRv;
GetSelectionRange(&start, &end, rangeRv); GetSelectionRange(&start, &end, rangeRv);
GetSelectionDirection(&direction);
IgnoredErrorResult dirRv;
nsITextControlFrame::SelectionDirection direction =
GetSelectionDirection(dirRv);
MOZ_ASSERT(aFrame == mBoundFrame); MOZ_ASSERT(aFrame == mBoundFrame);
SelectionProperties& props = GetSelectionProperties(); SelectionProperties& props = GetSelectionProperties();
props.SetStart(start); props.SetStart(start);

Просмотреть файл

@ -278,7 +278,8 @@ public:
mozilla::ErrorResult& aRv); mozilla::ErrorResult& aRv);
// Get the selection direction // Get the selection direction
nsresult GetSelectionDirection(nsITextControlFrame::SelectionDirection* aDirection); nsITextControlFrame::SelectionDirection
GetSelectionDirection(mozilla::ErrorResult& aRv);
void UpdateEditableState(bool aNotify) { void UpdateEditableState(bool aNotify) {
if (mRootNode) { if (mRootNode) {