diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index 8d211759b32c..02f72f3393dd 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -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 diff --git a/dom/html/HTMLTextAreaElement.cpp b/dom/html/HTMLTextAreaElement.cpp index 5d55547abdd4..9ad6bf116a20 100644 --- a/dom/html/HTMLTextAreaElement.cpp +++ b/dom/html/HTMLTextAreaElement.cpp @@ -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 diff --git a/dom/html/nsTextEditorState.cpp b/dom/html/nsTextEditorState.cpp index cdd37c6b9362..8f9dda5beacd 100644 --- a/dom/html/nsTextEditorState.cpp +++ b/dom/html/nsTextEditorState.cpp @@ -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 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); diff --git a/dom/html/nsTextEditorState.h b/dom/html/nsTextEditorState.h index 19f2e44ddcde..4f8a00d46819 100644 --- a/dom/html/nsTextEditorState.h +++ b/dom/html/nsTextEditorState.h @@ -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) {