diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index b69e73bfabdf..eb234c061bdf 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -6539,22 +6539,8 @@ HTMLInputElement::SetSelectionDirection(const nsAString& aDirection, ErrorResult } nsTextEditorState* state = GetEditorState(); - if (state && state->IsSelectionCached()) { - nsITextControlFrame::SelectionDirection dir = nsITextControlFrame::eNone; - if (aDirection.EqualsLiteral("forward")) { - dir = nsITextControlFrame::eForward; - } else if (aDirection.EqualsLiteral("backward")) { - dir = nsITextControlFrame::eBackward; - } - state->GetSelectionProperties().SetDirection(dir); - return; - } - - int32_t start, end; - GetSelectionRange(&start, &end, aRv); - if (!aRv.Failed()) { - aRv = SetSelectionRange(start, end, aDirection); - } + MOZ_ASSERT(state, "SupportsTextSelection came back true!"); + state->SetSelectionDirection(aDirection, aRv); } NS_IMETHODIMP diff --git a/dom/html/HTMLTextAreaElement.cpp b/dom/html/HTMLTextAreaElement.cpp index 8584c5355139..c00d3507f63d 100644 --- a/dom/html/HTMLTextAreaElement.cpp +++ b/dom/html/HTMLTextAreaElement.cpp @@ -765,27 +765,7 @@ void HTMLTextAreaElement::SetSelectionDirection(const nsAString& aDirection, ErrorResult& aError) { - if (mState.IsSelectionCached()) { - nsITextControlFrame::SelectionDirection dir = nsITextControlFrame::eNone; - if (aDirection.EqualsLiteral("forward")) { - dir = nsITextControlFrame::eForward; - } else if (aDirection.EqualsLiteral("backward")) { - dir = nsITextControlFrame::eBackward; - } - mState.GetSelectionProperties().SetDirection(dir); - return; - } - - int32_t start, end; - GetSelectionRange(&start, &end, aError); - if (aError.Failed()) { - return; - } - - nsresult rv = SetSelectionRange(start, end, aDirection); - if (NS_FAILED(rv)) { - aError.Throw(rv); - } + mState.SetSelectionDirection(aDirection, aError); } NS_IMETHODIMP diff --git a/dom/html/nsTextEditorState.cpp b/dom/html/nsTextEditorState.cpp index 8bad91001b57..6bbd2e0c12bd 100644 --- a/dom/html/nsTextEditorState.cpp +++ b/dom/html/nsTextEditorState.cpp @@ -1762,6 +1762,38 @@ nsTextEditorState::SetSelectionEnd(const mozilla::dom::Nullable& aEnd, SetSelectionRange(start, end, dir, aRv); } +static nsITextControlFrame::SelectionDirection +DirectionStringToSelectionDirection(const nsAString& aDirection) +{ + if (aDirection.EqualsLiteral("backward")) { + return nsITextControlFrame::eBackward; + } + + // We don't support directionless selections. + return nsITextControlFrame::eForward; +} + +void +nsTextEditorState::SetSelectionDirection(const nsAString& aDirection, + ErrorResult& aRv) +{ + nsITextControlFrame::SelectionDirection dir = + DirectionStringToSelectionDirection(aDirection); + + if (IsSelectionCached()) { + GetSelectionProperties().SetDirection(dir); + return; + } + + int32_t start, end; + GetSelectionRange(&start, &end, aRv); + if (aRv.Failed()) { + return; + } + + SetSelectionRange(start, end, dir, aRv); +} + HTMLInputElement* nsTextEditorState::GetParentNumberControl(nsFrame* aFrame) const { diff --git a/dom/html/nsTextEditorState.h b/dom/html/nsTextEditorState.h index dbbc8494f945..0dd36458ff16 100644 --- a/dom/html/nsTextEditorState.h +++ b/dom/html/nsTextEditorState.h @@ -310,6 +310,12 @@ public: void SetSelectionEnd(const mozilla::dom::Nullable& aEnd, mozilla::ErrorResult& aRv); + // Set the selection direction. This basically implements the + // https://html.spec.whatwg.org/multipage/forms.html#dom-textarea/input-selectiondirection + // setter. + void SetSelectionDirection(const nsAString& aDirection, + mozilla::ErrorResult& aRv); + void UpdateEditableState(bool aNotify) { if (mRootNode) { mRootNode->UpdateEditableState(aNotify);