Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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)
22 {
23 }
25 nsIFrame*
26 NS_NewColorControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
27 {
28 return new (aPresShell) nsColorControlFrame(aContext);
29 }
31 NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
33 NS_QUERYFRAME_HEAD(nsColorControlFrame)
34 NS_QUERYFRAME_ENTRY(nsColorControlFrame)
35 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
36 NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper)
39 void nsColorControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
40 {
41 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
42 nsContentUtils::DestroyAnonymousContent(&mColorContent);
43 nsColorControlFrameSuper::DestroyFrom(aDestructRoot);
44 }
46 nsIAtom*
47 nsColorControlFrame::GetType() const
48 {
49 return nsGkAtoms::colorControlFrame;
50 }
52 #ifdef DEBUG_FRAME_DUMP
53 nsresult
54 nsColorControlFrame::GetFrameName(nsAString& aResult) const
55 {
56 return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult);
57 }
58 #endif
60 // Create the color area for the button.
61 // The frame will be generated by the frame constructor.
62 nsresult
63 nsColorControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
64 {
65 nsCOMPtr<nsIDocument> doc = mContent->GetCurrentDoc();
66 mColorContent = doc->CreateHTMLElement(nsGkAtoms::div);
68 // Mark the element to be native anonymous before setting any attributes.
69 mColorContent->SetIsNativeAnonymousRoot();
71 nsresult rv = UpdateColor();
72 NS_ENSURE_SUCCESS(rv, rv);
74 nsCSSPseudoElements::Type pseudoType = nsCSSPseudoElements::ePseudo_mozColorSwatch;
75 nsRefPtr<nsStyleContext> newStyleContext = PresContext()->StyleSet()->
76 ResolvePseudoElementStyle(mContent->AsElement(), pseudoType,
77 StyleContext(), mColorContent->AsElement());
78 if (!aElements.AppendElement(ContentInfo(mColorContent, newStyleContext))) {
79 return NS_ERROR_OUT_OF_MEMORY;
80 }
82 return NS_OK;
83 }
85 void
86 nsColorControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
87 uint32_t aFilter)
88 {
89 aElements.MaybeAppendElement(mColorContent);
90 }
92 nsresult
93 nsColorControlFrame::UpdateColor()
94 {
95 // Get the color from the "value" property of our content; it will return the
96 // default color (through the sanitization algorithm) if there is none.
97 nsAutoString color;
98 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
99 elt->GetValue(color);
100 MOZ_ASSERT(!color.IsEmpty(),
101 "Content node's GetValue() should return a valid color string "
102 "(the default color, in case no valid color is set)");
104 // Set the background-color style property of the swatch element to this color
105 return mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
106 NS_LITERAL_STRING("background-color:") + color, true);
107 }
109 nsresult
110 nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
111 nsIAtom* aAttribute,
112 int32_t aModType)
113 {
114 NS_ASSERTION(mColorContent, "The color div must exist");
116 // If the value attribute is set, update the color box, but only if we're
117 // still a color control, which might not be the case if the type attribute
118 // was removed/changed.
119 nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
120 if (fctrl->GetType() == NS_FORM_INPUT_COLOR &&
121 aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
122 UpdateColor();
123 }
124 return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID, aAttribute,
125 aModType);
126 }
128 nsIFrame*
129 nsColorControlFrame::GetContentInsertionFrame()
130 {
131 return this;
132 }
134 Element*
135 nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType)
136 {
137 if (aType == nsCSSPseudoElements::ePseudo_mozColorSwatch) {
138 return mColorContent;
139 }
141 return nsContainerFrame::GetPseudoElement(aType);
142 }