Bug 665528 - Add support for placeholders to <input type=number>. r=mounir
authorJonathan Watt <jwatt@jwatt.org>
Fri, 22 Nov 2013 13:24:54 +0000
changeset 157387 e4a69852984c52b431d721f2ad35a0f7c1d28ffd
parent 157386 764f96661644ef67e85950101c27d6194a50127e
child 157388 0605fd2da3472146a671d4644505915cb87a2192
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmounir
bugs665528
milestone28.0a1
Bug 665528 - Add support for placeholders to <input type=number>. r=mounir
layout/forms/nsNumberControlFrame.cpp
layout/forms/nsNumberControlFrame.h
--- a/layout/forms/nsNumberControlFrame.cpp
+++ b/layout/forms/nsNumberControlFrame.cpp
@@ -142,16 +142,39 @@ nsNumberControlFrame::
   NS_ENSURE_SUCCESS(rv, rv);
   MOZ_ASSERT(NS_FRAME_IS_FULLY_COMPLETE(childStatus),
              "We gave our child unconstrained height, so it should be complete");
   return FinishReflowChild(aOuterWrapperFrame, aPresContext,
                            &wrapperReflowState, aWrappersDesiredSize,
                            xoffset, yoffset, 0);
 }
 
+NS_IMETHODIMP
+nsNumberControlFrame::AttributeChanged(int32_t  aNameSpaceID,
+                                       nsIAtom* aAttribute,
+                                       int32_t  aModType)
+{
+  if (aNameSpaceID == kNameSpaceID_None) {
+    if (aAttribute == nsGkAtoms::placeholder) {
+      if (aModType == nsIDOMMutationEvent::REMOVAL) {
+        mTextField->UnsetAttr(aNameSpaceID, aAttribute, true);
+      } else {
+        MOZ_ASSERT(aModType == nsIDOMMutationEvent::ADDITION ||
+                   aModType == nsIDOMMutationEvent::MODIFICATION);
+        nsAutoString value;
+        mContent->GetAttr(aNameSpaceID, aAttribute, value);
+        mTextField->SetAttr(aNameSpaceID, aAttribute, value, true);
+      }
+    }
+  }
+
+  return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute,
+                                            aModType);
+}
+
 nsresult
 nsNumberControlFrame::MakeAnonymousElement(nsIContent** aResult,
                                            nsTArray<ContentInfo>& aElements,
                                            nsIAtom* aTagName,
                                            nsCSSPseudoElements::Type aPseudoType,
                                            nsStyleContext* aParentContext)
 {
   // Get the NodeInfoManager and tag necessary to create the anonymous divs.
@@ -224,16 +247,22 @@ nsNumberControlFrame::CreateAnonymousCon
   mTextField->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
                       NS_LITERAL_STRING("text"), PR_FALSE);
 
   // Initialize the text field value:
   nsAutoString value;
   HTMLInputElement::FromContent(mContent)->GetValue(value);
   mTextField->SetAttr(kNameSpaceID_None, nsGkAtoms::value, value, false);
 
+  // Initialize the text field's placeholder, if ours is set:
+  nsAutoString placeholder;
+  if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::placeholder, placeholder)) {
+    mTextField->SetAttr(kNameSpaceID_None, nsGkAtoms::placeholder, placeholder, false);
+  }
+
   if (mContent->AsElement()->State().HasState(NS_EVENT_STATE_FOCUS)) {
     // We don't want to focus the frame but the text field.
     nsIFocusManager* fm = nsFocusManager::GetFocusManager();
     nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mTextField);
     NS_ASSERTION(element, "Really, this should be a nsIDOMElement!");
     fm->SetFocus(element, 0);
   }
 
--- a/layout/forms/nsNumberControlFrame.h
+++ b/layout/forms/nsNumberControlFrame.h
@@ -41,16 +41,20 @@ public:
 
   virtual bool IsLeaf() const MOZ_OVERRIDE { return true; }
 
   NS_IMETHOD Reflow(nsPresContext*           aPresContext,
                     nsHTMLReflowMetrics&     aDesiredSize,
                     const nsHTMLReflowState& aReflowState,
                     nsReflowStatus&          aStatus) MOZ_OVERRIDE;
 
+  NS_IMETHOD AttributeChanged(int32_t  aNameSpaceID,
+                              nsIAtom* aAttribute,
+                              int32_t  aModType) MOZ_OVERRIDE;
+
   // nsIAnonymousContentCreator
   virtual nsresult CreateAnonymousContent(nsTArray<ContentInfo>& aElements) MOZ_OVERRIDE;
   virtual void AppendAnonymousContentTo(nsBaseContentList& aElements,
                                         uint32_t aFilter) MOZ_OVERRIDE;
 
 #ifdef NS_DEBUG
   NS_IMETHOD GetFrameName(nsAString& aResult) const MOZ_OVERRIDE {
     return MakeFrameName(NS_LITERAL_STRING("NumberControl"), aResult);