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
)
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
);
48 nsColorControlFrame::GetType() const
50 return nsGkAtoms::colorControlFrame
;
53 #ifdef DEBUG_FRAME_DUMP
55 nsColorControlFrame::GetFrameName(nsAString
& aResult
) const
57 return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult
);
61 // Create the color area for the button.
62 // The frame will be generated by the frame constructor.
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
;
87 nsColorControlFrame::AppendAnonymousContentTo(nsTArray
<nsIContent
*>& aElements
,
91 aElements
.AppendElement(mColorContent
);
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.
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);
113 nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID
,
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
) {
127 return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID
, aAttribute
,
132 nsColorControlFrame::GetContentInsertionFrame()
138 nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType
)
140 if (aType
== nsCSSPseudoElements::ePseudo_mozColorSwatch
) {
141 return mColorContent
;
144 return nsContainerFrame::GetPseudoElement(aType
);