replaced by .editorconfig Differential Revision: https://phabricator.services.mozilla.com/D287852
115 lines
4.5 KiB
C++
115 lines
4.5 KiB
C++
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "nsColorControlFrame.h"
|
|
|
|
#include "mozilla/PresShell.h"
|
|
#include "mozilla/dom/Document.h"
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
|
#include "nsContentCreatorFunctions.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsGkAtoms.h"
|
|
#include "nsIFormControl.h"
|
|
|
|
#ifdef ACCESSIBILITY
|
|
# include "nsAccessibilityService.h"
|
|
#endif
|
|
|
|
using namespace mozilla;
|
|
using mozilla::dom::CallerType;
|
|
using mozilla::dom::HTMLInputElement;
|
|
|
|
nsColorControlFrame::nsColorControlFrame(ComputedStyle* aStyle,
|
|
nsPresContext* aPresContext)
|
|
: ButtonControlFrame(aStyle, aPresContext, kClassID) {}
|
|
|
|
nsIFrame* NS_NewColorControlFrame(PresShell* aPresShell,
|
|
ComputedStyle* aStyle) {
|
|
return new (aPresShell)
|
|
nsColorControlFrame(aStyle, aPresShell->GetPresContext());
|
|
}
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
|
|
|
|
NS_QUERYFRAME_HEAD(nsColorControlFrame)
|
|
NS_QUERYFRAME_ENTRY(nsColorControlFrame)
|
|
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
|
|
NS_QUERYFRAME_TAIL_INHERITING(ButtonControlFrame)
|
|
|
|
void nsColorControlFrame::Destroy(DestroyContext& aContext) {
|
|
aContext.AddAnonymousContent(mColorContent.forget());
|
|
ButtonControlFrame::Destroy(aContext);
|
|
}
|
|
|
|
// Create the color area for the button.
|
|
// The frame will be generated by the frame constructor.
|
|
nsresult nsColorControlFrame::CreateAnonymousContent(
|
|
nsTArray<ContentInfo>& aElements) {
|
|
mColorContent = mContent->OwnerDoc()->CreateHTMLElement(nsGkAtoms::div);
|
|
mColorContent->SetPseudoElementType(PseudoStyleType::MozColorSwatch);
|
|
// Mark the element to be native anonymous before setting any attributes.
|
|
mColorContent->SetIsNativeAnonymousRoot();
|
|
UpdateColor();
|
|
aElements.AppendElement(mColorContent);
|
|
return NS_OK;
|
|
}
|
|
|
|
void nsColorControlFrame::AppendAnonymousContentTo(
|
|
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
|
|
if (mColorContent) {
|
|
aElements.AppendElement(mColorContent);
|
|
}
|
|
}
|
|
|
|
void nsColorControlFrame::UpdateColor() {
|
|
// Get the color from the "value" property of our content; it will return the
|
|
// default color (through the sanitization algorithm) if the value is empty.
|
|
nsAutoString color;
|
|
auto* elt = HTMLInputElement::FromNode(mContent);
|
|
elt->GetValue(color, CallerType::System);
|
|
|
|
if (color.IsEmpty()) {
|
|
// OK, there is one case the color string might be empty -- if our content
|
|
// is still being created, i.e. if it has mDoneCreating==false. In that
|
|
// case, we simply do nothing, because we'll be called again with a complete
|
|
// content node before we ever reflow or paint. Specifically: we can expect
|
|
// that HTMLInputElement::DoneCreatingElement() will set mDoneCreating to
|
|
// true (which enables sanitization) and then it'll call SetValueInternal(),
|
|
// which produces a nonempty color (via sanitization), and then it'll call
|
|
// this function here, and we'll get the nonempty default color.
|
|
MOZ_ASSERT(HasAnyStateBits(NS_FRAME_FIRST_REFLOW),
|
|
"Content node's GetValue() should return a valid color string "
|
|
"by the time we've been reflowed (the default color, in case "
|
|
"no valid color is set)");
|
|
return;
|
|
}
|
|
|
|
// Set the color CSS property of the swatch element to this color.
|
|
mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
|
u"color:"_ns + color,
|
|
/* aNotify */ true);
|
|
}
|
|
|
|
nsresult nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
|
|
nsAtom* aAttribute,
|
|
AttrModType aModType) {
|
|
NS_ASSERTION(mColorContent, "The color div must exist");
|
|
|
|
// If the value attribute is set, update the color box, but only if we're
|
|
// still a color control, which might not be the case if the type attribute
|
|
// was removed/changed.
|
|
if (nsIFormControl::FromNode(GetContent())->ControlType() ==
|
|
FormControlType::InputColor &&
|
|
aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
|
|
UpdateColor();
|
|
#ifdef ACCESSIBILITY
|
|
if (nsAccessibilityService* accService = GetAccService()) {
|
|
accService->ColorValueChanged(PresShell(), mContent);
|
|
}
|
|
#endif
|
|
}
|
|
return ButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute,
|
|
aModType);
|
|
}
|