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;
}
nsIFormControlFrame* formControlFrame = GetFormControlFrame(true);
nsTextEditorState* state = GetEditorState();
if (!state) {
aRv.Throw(NS_ERROR_FAILURE);
MOZ_ASSERT(state, "SupportsTextSelection came back true!");
nsITextControlFrame::SelectionDirection dir =
state->GetSelectionDirection(aRv);
if (aRv.Failed()) {
return;
}
nsresult rv = NS_ERROR_FAILURE;
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);
DirectionToName(dir, aDirection);
}
void

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

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

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

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

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

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