Bug 637671 - Don't display the selection for a non-focused text element when restoring the selection state after a reframe. r=bzbarsky
☠☠ backed out by aa0b6404ec25 ☠ ☠
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 11 Apr 2011 23:18:44 -0700
changeset 67990 b48ebf9695bbc24d8c8429de7ecac7cb4a7ae26e
parent 67989 5f6f0204b6827bd2263ebc5d6c6c4c19f29ea3fc
child 67991 8a1136b1b6249b797107ce5e753ad25cd3b4407c
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs637671
milestone2.2a1pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 637671 - Don't display the selection for a non-focused text element when restoring the selection state after a reframe. r=bzbarsky
content/html/content/src/nsTextEditorState.cpp
content/html/content/src/nsTextEditorState.h
layout/reftests/editor/reftest.list
layout/reftests/editor/selection_visibility_after_reframe-2.html
layout/reftests/editor/selection_visibility_after_reframe-ref.html
layout/reftests/editor/selection_visibility_after_reframe.html
--- a/content/html/content/src/nsTextEditorState.cpp
+++ b/content/html/content/src/nsTextEditorState.cpp
@@ -77,39 +77,43 @@ static nsINativeKeyBindings *sNativeText
 
 struct SelectionState {
   PRInt32 mStart;
   PRInt32 mEnd;
 };
 
 class RestoreSelectionState : public nsRunnable {
 public:
-  RestoreSelectionState(nsTextControlFrame *aFrame, PRInt32 aStart, PRInt32 aEnd)
+  RestoreSelectionState(nsTextEditorState *aState, nsTextControlFrame *aFrame,
+                        PRInt32 aStart, PRInt32 aEnd)
     : mFrame(aFrame),
       mWeakFrame(aFrame),
       mStart(aStart),
-      mEnd(aEnd)
+      mEnd(aEnd),
+      mTextEditorState(aState)
   {
   }
 
   NS_IMETHOD Run() {
     if (mWeakFrame.IsAlive()) {
       // SetSelectionRange leads to Selection::AddRange which flushes Layout -
       // need to block script to avoid nested PrepareEditor calls (bug 642800).
       nsAutoScriptBlocker scriptBlocker;
       mFrame->SetSelectionRange(mStart, mEnd);
+      mTextEditorState->HideSelectionIfBlurred();
     }
     return NS_OK;
   }
 
 private:
   nsTextControlFrame* mFrame;
   nsWeakFrame mWeakFrame;
   PRInt32 mStart;
   PRInt32 mEnd;
+  nsTextEditorState* mTextEditorState;
 };
 
 /*static*/
 PRBool
 nsITextControlElement::GetWrapPropertyEnum(nsIContent* aContent,
   nsITextControlElement::nsHTMLTextWrap& aWrapProp)
 {
   // soft is the default; "physical" defaults to soft as well because all other
@@ -1380,17 +1384,17 @@ nsTextEditorState::PrepareEditor(const n
     mEditorInitialized = PR_TRUE;
   }
 
   if (mTextListener)
     newEditor->AddEditorObserver(mTextListener);
 
   // Restore our selection after being bound to a new frame
   if (mSelState) {
-    nsContentUtils::AddScriptRunner(new RestoreSelectionState(mBoundFrame, mSelState->mStart, mSelState->mEnd));
+    nsContentUtils::AddScriptRunner(new RestoreSelectionState(this, mBoundFrame, mSelState->mStart, mSelState->mEnd));
     mSelState = nsnull;
   }
 
   return rv;
 }
 
 void
 nsTextEditorState::DestroyEditor()
@@ -2006,16 +2010,26 @@ nsTextEditorState::SetPlaceholderClass(P
 
   nsIContent* placeholderDiv = GetPlaceholderNode();
   NS_ENSURE_TRUE(placeholderDiv, );
 
   placeholderDiv->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
                           classValue, aNotify);
 }
 
+void
+nsTextEditorState::HideSelectionIfBlurred()
+{
+  NS_ABORT_IF_FALSE(mSelCon, "Should have a selection controller if we have a frame!");
+  nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
+  if (!nsContentUtils::IsFocusedContent(content)) {
+    mSelCon->SetDisplaySelection(nsISelectionController::SELECTION_OFF);
+  }
+}
+
 NS_IMPL_ISUPPORTS1(nsAnonDivObserver, nsIMutationObserver)
 
 void
 nsAnonDivObserver::CharacterDataChanged(nsIDocument*             aDocument,
                                         nsIContent*              aContent,
                                         CharacterDataChangeInfo* aInfo)
 {
   mTextEditorState->ClearValueCache();
--- a/content/html/content/src/nsTextEditorState.h
+++ b/content/html/content/src/nsTextEditorState.h
@@ -205,16 +205,18 @@ public:
    */
   PRBool GetMaxLength(PRInt32* aMaxLength);
 
   /* called to free up native keybinding services */
   static NS_HIDDEN_(void) ShutDown();
 
   void ClearValueCache() { mCachedValue.Truncate(); }
 
+  void HideSelectionIfBlurred();
+
 private:
   // not copy constructible
   nsTextEditorState(const nsTextEditorState&);
   // not assignable
   void operator= (const nsTextEditorState&);
 
   nsresult CreateRootNode();
 
--- a/layout/reftests/editor/reftest.list
+++ b/layout/reftests/editor/reftest.list
@@ -55,8 +55,10 @@ fails-if(Android) != spellcheck-hyphen-i
 == spellcheck-comma-valid.html spellcheck-comma-valid-ref.html
 == spellcheck-hyphen-multiple-valid.html spellcheck-hyphen-multiple-valid-ref.html
 fails-if(Android) != spellcheck-hyphen-multiple-invalid.html spellcheck-hyphen-multiple-invalid-ref.html
 == spellcheck-dotafterquote-valid.html spellcheck-dotafterquote-valid-ref.html
 == unneeded_scroll.html unneeded_scroll-ref.html
 == caret_on_presshell_reinit.html caret_on_presshell_reinit-ref.html
 == caret_on_presshell_reinit-2.html caret_on_presshell_reinit-ref.html
 == 642800.html 642800-ref.html
+== selection_visibility_after_reframe.html selection_visibility_after_reframe-ref.html
+!= selection_visibility_after_reframe-2.html selection_visibility_after_reframe-ref.html
new file mode 100644
--- /dev/null
+++ b/layout/reftests/editor/selection_visibility_after_reframe-2.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+  <body>
+    <input value="foo">
+    <script>
+      var i = document.querySelector("input");
+      i.selectionStart = 1;
+      i.selectionEnd = 2;
+    </script>
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/editor/selection_visibility_after_reframe-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+  <body>
+    <input value="foo">
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/editor/selection_visibility_after_reframe.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+  <body>
+    <input value="foo">
+    <script>
+      var i = document.querySelector("input");
+      i.selectionStart = 1;
+      i.selectionEnd = 2;
+      i.style.display = "none";
+      document.body.clientHeight;
+      i.style.display = "";
+    </script>
+  </body>
+</html>