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"
18 using mozilla::dom::Element
;
20 nsColorControlFrame::nsColorControlFrame(nsStyleContext
* aContext
):
21 nsColorControlFrameSuper(aContext
)
26 NS_NewColorControlFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
28 return new (aPresShell
) nsColorControlFrame(aContext
);
31 NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame
)
33 NS_QUERYFRAME_HEAD(nsColorControlFrame
)
34 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator
)
35 NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper
)
38 void nsColorControlFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
40 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame
*>(this), false);
41 nsContentUtils::DestroyAnonymousContent(&mColorContent
);
42 nsColorControlFrameSuper::DestroyFrom(aDestructRoot
);
46 nsColorControlFrame::GetType() const
48 return nsGkAtoms::colorControlFrame
;
51 #ifdef DEBUG_FRAME_DUMP
53 nsColorControlFrame::GetFrameName(nsAString
& aResult
) const
55 return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult
);
59 // Create the color area for the button.
60 // The frame will be generated by the frame constructor.
62 nsColorControlFrame::CreateAnonymousContent(nsTArray
<ContentInfo
>& aElements
)
64 nsCOMPtr
<nsIDocument
> doc
= mContent
->GetCurrentDoc();
65 mColorContent
= doc
->CreateHTMLElement(nsGkAtoms::div
);
67 // Mark the element to be native anonymous before setting any attributes.
68 mColorContent
->SetIsNativeAnonymousRoot();
70 nsresult rv
= UpdateColor();
71 NS_ENSURE_SUCCESS(rv
, rv
);
73 nsCSSPseudoElements::Type pseudoType
= nsCSSPseudoElements::ePseudo_mozColorSwatch
;
74 nsRefPtr
<nsStyleContext
> newStyleContext
= PresContext()->StyleSet()->
75 ResolvePseudoElementStyle(mContent
->AsElement(), pseudoType
,
76 StyleContext(), mColorContent
->AsElement());
77 if (!aElements
.AppendElement(ContentInfo(mColorContent
, newStyleContext
))) {
78 return NS_ERROR_OUT_OF_MEMORY
;
85 nsColorControlFrame::AppendAnonymousContentTo(nsBaseContentList
& aElements
,
88 aElements
.MaybeAppendElement(mColorContent
);
92 nsColorControlFrame::UpdateColor()
94 // Get the color from the "value" property of our content; it will return the
95 // default color (through the sanitization algorithm) if there is none.
97 nsCOMPtr
<nsIDOMHTMLInputElement
> elt
= do_QueryInterface(mContent
);
99 MOZ_ASSERT(!color
.IsEmpty(),
100 "Content node's GetValue() should return a valid color string "
101 "(the default color, in case no valid color is set)");
103 // Set the background-color style property of the swatch element to this color
104 return mColorContent
->SetAttr(kNameSpaceID_None
, nsGkAtoms::style
,
105 NS_LITERAL_STRING("background-color:") + color
, true);
109 nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID
,
113 NS_ASSERTION(mColorContent
, "The color div must exist");
115 // If the value attribute is set, update the color box, but only if we're
116 // still a color control, which might not be the case if the type attribute
117 // was removed/changed.
118 nsCOMPtr
<nsIFormControl
> fctrl
= do_QueryInterface(GetContent());
119 if (fctrl
->GetType() == NS_FORM_INPUT_COLOR
&&
120 aNameSpaceID
== kNameSpaceID_None
&& nsGkAtoms::value
== aAttribute
) {
123 return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID
, aAttribute
,
128 nsColorControlFrame::GetContentInsertionFrame()
134 nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType
)
136 if (aType
== nsCSSPseudoElements::ePseudo_mozColorSwatch
) {
137 return mColorContent
;
140 return nsContainerFrame::GetPseudoElement(aType
);