Bumping manifests a=b2g-bump
[gecko.git] / layout / forms / nsColorControlFrame.cpp
blobbf35fbd137fc56231b7ead0b35025b859aefb202
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsColorControlFrame.h"
8 #include "nsContentCreatorFunctions.h"
9 #include "nsContentList.h"
10 #include "nsContentUtils.h"
11 #include "nsFormControlFrame.h"
12 #include "nsGkAtoms.h"
13 #include "nsIDOMHTMLInputElement.h"
14 #include "nsIDOMNode.h"
15 #include "nsIFormControl.h"
16 #include "nsStyleSet.h"
17 #include "nsIDocument.h"
19 using mozilla::dom::Element;
21 nsColorControlFrame::nsColorControlFrame(nsStyleContext* aContext):
22 nsColorControlFrameSuper(aContext)
26 nsIFrame*
27 NS_NewColorControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
29 return new (aPresShell) nsColorControlFrame(aContext);
32 NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
34 NS_QUERYFRAME_HEAD(nsColorControlFrame)
35 NS_QUERYFRAME_ENTRY(nsColorControlFrame)
36 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
37 NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper)
40 void nsColorControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
42 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
43 nsContentUtils::DestroyAnonymousContent(&mColorContent);
44 nsColorControlFrameSuper::DestroyFrom(aDestructRoot);
47 nsIAtom*
48 nsColorControlFrame::GetType() const
50 return nsGkAtoms::colorControlFrame;
53 #ifdef DEBUG_FRAME_DUMP
54 nsresult
55 nsColorControlFrame::GetFrameName(nsAString& aResult) const
57 return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult);
59 #endif
61 // Create the color area for the button.
62 // The frame will be generated by the frame constructor.
63 nsresult
64 nsColorControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
66 nsCOMPtr<nsIDocument> doc = mContent->GetComposedDoc();
67 mColorContent = doc->CreateHTMLElement(nsGkAtoms::div);
69 // Mark the element to be native anonymous before setting any attributes.
70 mColorContent->SetIsNativeAnonymousRoot();
72 nsresult rv = UpdateColor();
73 NS_ENSURE_SUCCESS(rv, rv);
75 nsCSSPseudoElements::Type pseudoType = nsCSSPseudoElements::ePseudo_mozColorSwatch;
76 nsRefPtr<nsStyleContext> newStyleContext = PresContext()->StyleSet()->
77 ResolvePseudoElementStyle(mContent->AsElement(), pseudoType,
78 StyleContext(), mColorContent->AsElement());
79 if (!aElements.AppendElement(ContentInfo(mColorContent, newStyleContext))) {
80 return NS_ERROR_OUT_OF_MEMORY;
83 return NS_OK;
86 void
87 nsColorControlFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
88 uint32_t aFilter)
90 if (mColorContent) {
91 aElements.AppendElement(mColorContent);
95 nsresult
96 nsColorControlFrame::UpdateColor()
98 // Get the color from the "value" property of our content; it will return the
99 // default color (through the sanitization algorithm) if there is none.
100 nsAutoString color;
101 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
102 elt->GetValue(color);
103 MOZ_ASSERT(!color.IsEmpty(),
104 "Content node's GetValue() should return a valid color string "
105 "(the default color, in case no valid color is set)");
107 // Set the background-color style property of the swatch element to this color
108 return mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
109 NS_LITERAL_STRING("background-color:") + color, true);
112 nsresult
113 nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
114 nsIAtom* aAttribute,
115 int32_t aModType)
117 NS_ASSERTION(mColorContent, "The color div must exist");
119 // If the value attribute is set, update the color box, but only if we're
120 // still a color control, which might not be the case if the type attribute
121 // was removed/changed.
122 nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
123 if (fctrl->GetType() == NS_FORM_INPUT_COLOR &&
124 aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
125 UpdateColor();
127 return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID, aAttribute,
128 aModType);
131 nsContainerFrame*
132 nsColorControlFrame::GetContentInsertionFrame()
134 return this;
137 Element*
138 nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType)
140 if (aType == nsCSSPseudoElements::ePseudo_mozColorSwatch) {
141 return mColorContent;
144 return nsContainerFrame::GetPseudoElement(aType);