diff --git a/content/html/content/src/nsTextEditorState.cpp b/content/html/content/src/nsTextEditorState.cpp
index a4724acf25a..2df8a535a3d 100644
--- a/content/html/content/src/nsTextEditorState.cpp
+++ b/content/html/content/src/nsTextEditorState.cpp
@@ -73,6 +73,11 @@ static NS_DEFINE_CID(kFrameSelectionCID, NS_FRAMESELECTION_CID);
static nsINativeKeyBindings *sNativeInputBindings = nsnull;
static nsINativeKeyBindings *sNativeTextAreaBindings = nsnull;
+struct SelectionState {
+ PRInt32 mStart;
+ PRInt32 mEnd;
+};
+
/*static*/
PRBool
nsITextControlElement::GetWrapPropertyEnum(nsIContent* aContent,
@@ -1321,6 +1326,12 @@ nsTextEditorState::PrepareEditor(const nsAString *aValue)
if (mTextListener)
newEditor->AddEditorObserver(mTextListener);
+ // Restore our selection after being bound to a new frame
+ if (mSelState) {
+ mBoundFrame->SetSelectionRange(mSelState->mStart, mSelState->mEnd);
+ mSelState = nsnull;
+ }
+
return rv;
}
@@ -1351,6 +1362,20 @@ nsTextEditorState::UnbindFromFrame(nsTextControlFrame* aFrame)
nsAutoString value;
GetValue(value, PR_TRUE);
+ // Save our selection state if needed.
+ // Note that nsTextControlFrame::GetSelectionRange attempts to initialize the
+ // editor before grabbing the range, and because this is not an acceptable
+ // side effect for unbinding from a text control frame, we need to call
+ // GetSelectionRange before calling DestroyEditor, and only if
+ // mEditorInitialized indicates that we actually have an editor available.
+ if (mEditorInitialized) {
+ mSelState = new SelectionState();
+ nsresult rv = mBoundFrame->GetSelectionRange(&mSelState->mStart, &mSelState->mEnd);
+ if (NS_FAILED(rv)) {
+ mSelState = nsnull;
+ }
+ }
+
// Destroy our editor
DestroyEditor();
diff --git a/content/html/content/src/nsTextEditorState.h b/content/html/content/src/nsTextEditorState.h
index 1a52bb1217c..eac4abc3f0e 100644
--- a/content/html/content/src/nsTextEditorState.h
+++ b/content/html/content/src/nsTextEditorState.h
@@ -51,6 +51,7 @@ class nsISelectionController;
class nsFrameSelection;
class nsIEditor;
class nsITextControlElement;
+struct SelectionState;
/**
* nsTextEditorState is a class which is responsible for managing the state of
@@ -250,6 +251,7 @@ private:
nsITextControlElement* const mTextCtrlElement;
nsRefPtr mSelCon;
+ nsAutoPtr mSelState;
nsCOMPtr mEditor;
nsCOMPtr mRootNode;
nsCOMPtr mPlaceholderDiv;
diff --git a/editor/libeditor/text/tests/Makefile.in b/editor/libeditor/text/tests/Makefile.in
index 4588525478f..07e9a0cc6a8 100644
--- a/editor/libeditor/text/tests/Makefile.in
+++ b/editor/libeditor/text/tests/Makefile.in
@@ -51,6 +51,7 @@ _TEST_FILES = \
test_bug596001.html \
test_bug596333.html \
test_bug596506.html \
+ test_bug597331.html \
test_bug600570.html \
$(NULL)
diff --git a/editor/libeditor/text/tests/test_bug597331.html b/editor/libeditor/text/tests/test_bug597331.html
new file mode 100644
index 00000000000..915f3210e1e
--- /dev/null
+++ b/editor/libeditor/text/tests/test_bug597331.html
@@ -0,0 +1,64 @@
+
+
+
+
+ Test for Bug 597331
+
+
+
+
+
+
+
+Mozilla Bug 597331
+
+
+
+
+
+
+
+
+