Bug 807613 - Add a pref to make the placeholder disappear when a text field is focused. r=bz
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 09 Nov 2012 10:32:58 +0000
changeset 112815 1fdb059f5ae6ea6ce52d71ed50efba936c06ad36
parent 112814 61bd7f8b54deaca3d571771ba8e18c5b97c44e2a
child 112816 c39596b468638ecac8903847717b14200cf5ec9f
child 112817 c95b9413e66ef360e1a111136280dd79793bb295
push id23838
push usereakhgari@mozilla.com
push dateFri, 09 Nov 2012 15:21:51 +0000
treeherdermozilla-central@c39596b46863 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs807613
milestone19.0a1
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 807613 - Add a pref to make the placeholder disappear when a text field is focused. r=bz
content/html/content/src/nsTextEditorState.cpp
layout/forms/nsTextControlFrame.cpp
layout/reftests/forms/placeholder/placeholder-focus-pref.html
layout/reftests/forms/placeholder/reftest.list
modules/libpref/src/init/all.js
--- a/content/html/content/src/nsTextEditorState.cpp
+++ b/content/html/content/src/nsTextEditorState.cpp
@@ -33,16 +33,17 @@
 #include "nsISelectionPrivate.h"
 #include "nsPIDOMWindow.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIEditor.h"
 #include "nsTextEditRules.h"
 #include "mozilla/Selection.h"
 #include "nsEventListenerManager.h"
 #include "nsContentUtils.h"
+#include "mozilla/Preferences.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 static NS_DEFINE_CID(kTextEditorCID, NS_TEXTEDITOR_CID);
 
 static nsINativeKeyBindings *sNativeInputBindings = nullptr;
 static nsINativeKeyBindings *sNativeTextAreaBindings = nullptr;
@@ -1997,16 +1998,22 @@ nsTextEditorState::UpdatePlaceholderVisi
   NS_ASSERTION(mPlaceholderDiv, "This function should not be called if "
                                 "mPlaceholderDiv isn't set");
 
   nsAutoString value;
   GetValue(value, true);
 
   mPlaceholderVisibility = value.IsEmpty();
 
+  if (mPlaceholderVisibility &&
+      !Preferences::GetBool("dom.placeholder.show_on_focus", true)) {
+    nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
+    mPlaceholderVisibility = !nsContentUtils::IsFocusedContent(content);
+  }
+
   if (mBoundFrame && aNotify) {
     mBoundFrame->InvalidateFrame();
   }
 }
 
 void
 nsTextEditorState::HideSelectionIfBlurred()
 {
--- a/layout/forms/nsTextControlFrame.cpp
+++ b/layout/forms/nsTextControlFrame.cpp
@@ -643,16 +643,22 @@ nsTextControlFrame::ScrollOnFocusEvent::
 void nsTextControlFrame::SetFocus(bool aOn, bool aRepaint)
 {
   nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(GetContent());
   NS_ASSERTION(txtCtrl, "Content not a text control element");
 
   // Revoke the previous scroll event if one exists
   mScrollEvent.Revoke();
 
+  // If 'dom.placeholeder.show_on_focus' preference is 'false', focusing or
+  // blurring the frame can have an impact on the placeholder visibility.
+  if (mUsePlaceholder) {
+    txtCtrl->UpdatePlaceholderVisibility(true);
+  }
+
   if (!aOn) {
     return;
   }
 
   nsISelectionController* selCon = txtCtrl->GetSelectionController();
   if (!selCon)
     return;
 
new file mode 100644
--- /dev/null
+++ b/layout/reftests/forms/placeholder/placeholder-focus-pref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+  <link rel='stylesheet' type='text/css' href='placeholder-style.css'>
+  <!-- Test: when focused, element should not placeholder when
+       'dom.placeholder.show_on_focus' pref is false. -->
+  <script type="text/javascript">
+    function focusPlaceholder()
+    {
+      document.getElementById('p1').focus();
+    }
+    function disableReftestWait()
+    {
+      document.documentElement.className = '';
+    }
+  </script>
+
+  <body onload="focusPlaceholder();">
+    <input type="text" id="p1" value="" placeholder="my placeholder" onfocus="disableReftestWait();">
+  </body>
+</html>
\ No newline at end of file
--- a/layout/reftests/forms/placeholder/reftest.list
+++ b/layout/reftests/forms/placeholder/reftest.list
@@ -27,8 +27,9 @@ needs-focus == placeholder-10.html place
 == placeholder-13.html placeholder-visible-ref.html
 == placeholder-14.html placeholder-visible-ref.html
 == placeholder-18.html placeholder-overridden-ref.html
 random-if(winWidget) == placeholder-19.xul  placeholder-overridden-ref.xul
 # needs-focus == placeholder-20.html placeholder-focus-ref.html
 needs-focus == placeholder-21.html placeholder-blank-ref.html
 needs-focus == placeholder-22.html placeholder-blank-ref.html
 == placeholder-rtl.html placeholder-rtl-ref.html
+pref(dom.placeholder.show_on_focus,false) needs-focus == placeholder-focus-pref.html placeholder-blank-ref.html
\ No newline at end of file
--- a/modules/libpref/src/init/all.js
+++ b/modules/libpref/src/init/all.js
@@ -3829,8 +3829,11 @@ pref("network.activity.blipIntervalMilli
 // (JS components, JSMs, etc).  This saves memory, but makes it possible for
 // the scripts to interfere with each other.  A restart is required for this
 // to take effect.
 pref("jsloader.reuseGlobal", false);
 
 // When we're asked to take a screenshot, don't wait more than 2000ms for the
 // event loop to become idle before actually taking the screenshot.
 pref("dom.browserElement.maxScreenshotDelayMS", 2000);
+
+// Whether we should show the placeholder when the element is focused but empty.
+pref("dom.placeholder.show_on_focus", true);
\ No newline at end of file