2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-09-04 14:30:36 +04:00
|
|
|
/* 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 "nsNumberControlFrame.h"
|
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
#include "mozilla/BasicEvents.h"
|
|
|
|
#include "mozilla/EventStates.h"
|
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/PresShell.h"
|
|
|
|
#include "mozilla/dom/MutationEventBinding.h"
|
2011-06-21 18:13:12 +04:00
|
|
|
#include "HTMLInputElement.h"
|
2014-01-23 19:43:12 +04:00
|
|
|
#include "ICUUtils.h"
|
2011-06-21 18:13:12 +04:00
|
|
|
#include "nsFocusManager.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
#include "nsFontMetrics.h"
|
2017-09-27 15:12:00 +03:00
|
|
|
#include "nsCheckboxRadioFrame.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2018-07-25 16:03:45 +03:00
|
|
|
#include "nsStyleConsts.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
2016-02-17 23:37:00 +03:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2014-08-01 04:25:43 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2013-12-11 07:19:26 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
# include "mozilla/a11y/AccTypes.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-04 14:30:36 +04:00
|
|
|
using namespace mozilla;
|
2011-06-21 18:13:12 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
nsIFrame* NS_NewNumberControlFrame(PresShell* aPresShell,
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aStyle) {
|
2019-02-05 19:45:54 +03:00
|
|
|
return new (aPresShell)
|
|
|
|
nsNumberControlFrame(aStyle, aPresShell->GetPresContext());
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsNumberControlFrame)
|
|
|
|
|
|
|
|
NS_QUERYFRAME_HEAD(nsNumberControlFrame)
|
2018-12-07 23:00:18 +03:00
|
|
|
NS_QUERYFRAME_ENTRY(nsNumberControlFrame)
|
2020-01-14 18:05:22 +03:00
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsTextControlFrame)
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2019-02-05 19:45:54 +03:00
|
|
|
nsNumberControlFrame::nsNumberControlFrame(ComputedStyle* aStyle,
|
|
|
|
nsPresContext* aPresContext)
|
2020-01-14 18:05:22 +03:00
|
|
|
: nsTextControlFrame(aStyle, aPresContext, kClassID) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
void nsNumberControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
|
|
|
|
PostDestroyData& aPostDestroyData) {
|
|
|
|
aPostDestroyData.AddAnonymousContent(mOuterWrapper.forget());
|
2020-01-14 18:05:22 +03:00
|
|
|
nsTextControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:55 +03:00
|
|
|
already_AddRefed<Element> nsNumberControlFrame::MakeAnonymousElement(
|
2019-02-19 16:44:33 +03:00
|
|
|
Element* aParent, nsAtom* aTagName, PseudoStyleType aPseudoType) {
|
2013-09-04 14:30:36 +04:00
|
|
|
// Get the NodeInfoManager and tag necessary to create the anonymous divs.
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = mContent->GetComposedDoc();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Element> resultElement = doc->CreateHTMLElement(aTagName);
|
2017-01-13 06:21:11 +03:00
|
|
|
resultElement->SetPseudoElementType(aPseudoType);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2019-02-19 16:44:33 +03:00
|
|
|
if (aPseudoType == PseudoStyleType::mozNumberSpinDown ||
|
|
|
|
aPseudoType == PseudoStyleType::mozNumberSpinUp) {
|
2013-12-11 07:19:26 +04:00
|
|
|
resultElement->SetAttr(kNameSpaceID_None, nsGkAtoms::role,
|
|
|
|
NS_LITERAL_STRING("button"), false);
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:55 +03:00
|
|
|
if (aParent) {
|
|
|
|
aParent->AppendChildTo(resultElement, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultElement.forget();
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsNumberControlFrame::CreateAnonymousContent(
|
|
|
|
nsTArray<ContentInfo>& aElements) {
|
|
|
|
// We create an anonymous tree for our input element that is structured as
|
|
|
|
// follows:
|
|
|
|
//
|
|
|
|
// input
|
|
|
|
// div - outer wrapper with "display:flex" by default
|
2020-01-14 18:05:22 +03:00
|
|
|
// div - editor root
|
2013-09-04 14:30:36 +04:00
|
|
|
// div - spin box wrapping up/down arrow buttons
|
|
|
|
// div - spin up (up arrow button)
|
|
|
|
// div - spin down (down arrow button)
|
2020-01-14 18:05:22 +03:00
|
|
|
// div - placeholder
|
|
|
|
// div - preview div
|
2013-09-04 14:30:36 +04:00
|
|
|
//
|
|
|
|
// If you change this, be careful to change the destruction order in
|
|
|
|
// nsNumberControlFrame::DestroyFrom.
|
|
|
|
|
|
|
|
// Create the anonymous outer wrapper:
|
2018-07-24 21:11:55 +03:00
|
|
|
mOuterWrapper = MakeAnonymousElement(nullptr, nsGkAtoms::div,
|
2019-02-19 16:44:33 +03:00
|
|
|
PseudoStyleType::mozNumberWrapper);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2018-07-24 21:11:55 +03:00
|
|
|
aElements.AppendElement(mOuterWrapper);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2020-01-14 18:05:22 +03:00
|
|
|
nsTArray<ContentInfo> nestedContent;
|
|
|
|
nsTextControlFrame::CreateAnonymousContent(nestedContent);
|
|
|
|
for (auto& content : nestedContent) {
|
|
|
|
// The root goes inside the container.
|
|
|
|
if (content.mContent == mRootNode) {
|
|
|
|
mOuterWrapper->AppendChildTo(content.mContent, false);
|
|
|
|
} else {
|
|
|
|
// The rest (placeholder and preview), directly under us.
|
|
|
|
aElements.AppendElement(std::move(content));
|
|
|
|
}
|
2011-06-21 18:13:12 +04:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:45 +03:00
|
|
|
if (StyleDisplay()->mAppearance == StyleAppearance::Textfield) {
|
2013-12-09 03:26:07 +04:00
|
|
|
// The author has elected to hide the spinner by setting this
|
|
|
|
// -moz-appearance. We will reframe if it changes.
|
2018-07-24 21:11:55 +03:00
|
|
|
return NS_OK;
|
2013-12-09 03:26:07 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 14:30:36 +04:00
|
|
|
// Create the ::-moz-number-spin-box pseudo-element:
|
2018-07-24 21:11:55 +03:00
|
|
|
mSpinBox = MakeAnonymousElement(mOuterWrapper, nsGkAtoms::div,
|
2019-02-19 16:44:33 +03:00
|
|
|
PseudoStyleType::mozNumberSpinBox);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
|
|
|
// Create the ::-moz-number-spin-up pseudo-element:
|
2018-07-24 21:11:55 +03:00
|
|
|
mSpinUp = MakeAnonymousElement(mSpinBox, nsGkAtoms::div,
|
2019-02-19 16:44:33 +03:00
|
|
|
PseudoStyleType::mozNumberSpinUp);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
|
|
|
// Create the ::-moz-number-spin-down pseudo-element:
|
2018-07-24 21:11:55 +03:00
|
|
|
mSpinDown = MakeAnonymousElement(mSpinBox, nsGkAtoms::div,
|
2019-02-19 16:44:33 +03:00
|
|
|
PseudoStyleType::mozNumberSpinDown);
|
2013-12-11 06:13:06 +04:00
|
|
|
|
2018-07-24 21:11:55 +03:00
|
|
|
return NS_OK;
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForTextField(
|
|
|
|
nsIFrame* aFrame) {
|
2013-12-05 20:20:34 +04:00
|
|
|
// If aFrame is the anon text field for an <input type=number> then we expect
|
|
|
|
// the frame of its mContent's grandparent to be that input's frame. We
|
|
|
|
// have to check for this via the content tree because we don't know whether
|
|
|
|
// extra frames will be wrapped around any of the elements between aFrame and
|
|
|
|
// the nsNumberControlFrame that we're looking for (e.g. flex wrappers).
|
|
|
|
nsIContent* content = aFrame->GetContent();
|
|
|
|
if (content->IsInNativeAnonymousSubtree() && content->GetParent() &&
|
|
|
|
content->GetParent()->GetParent()) {
|
|
|
|
nsIContent* grandparent = content->GetParent()->GetParent();
|
2015-03-03 14:08:59 +03:00
|
|
|
if (grandparent->IsHTMLElement(nsGkAtoms::input) &&
|
2017-12-07 21:13:50 +03:00
|
|
|
grandparent->AsElement()->AttrValueIs(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::type, nsGkAtoms::number,
|
|
|
|
eCaseMatters)) {
|
2013-12-05 20:20:34 +04:00
|
|
|
return do_QueryFrame(grandparent->GetPrimaryFrame());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForSpinButton(
|
|
|
|
nsIFrame* aFrame) {
|
2013-12-05 20:20:34 +04:00
|
|
|
// If aFrame is a spin button for an <input type=number> then we expect the
|
|
|
|
// frame of its mContent's great-grandparent to be that input's frame. We
|
|
|
|
// have to check for this via the content tree because we don't know whether
|
|
|
|
// extra frames will be wrapped around any of the elements between aFrame and
|
|
|
|
// the nsNumberControlFrame that we're looking for (e.g. flex wrappers).
|
|
|
|
nsIContent* content = aFrame->GetContent();
|
|
|
|
if (content->IsInNativeAnonymousSubtree() && content->GetParent() &&
|
|
|
|
content->GetParent()->GetParent() &&
|
|
|
|
content->GetParent()->GetParent()->GetParent()) {
|
|
|
|
nsIContent* greatgrandparent =
|
|
|
|
content->GetParent()->GetParent()->GetParent();
|
2015-03-03 14:08:59 +03:00
|
|
|
if (greatgrandparent->IsHTMLElement(nsGkAtoms::input) &&
|
2017-12-07 21:13:50 +03:00
|
|
|
greatgrandparent->AsElement()->AttrValueIs(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::type, nsGkAtoms::number,
|
|
|
|
eCaseMatters)) {
|
2013-12-05 20:20:34 +04:00
|
|
|
return do_QueryFrame(greatgrandparent->GetPrimaryFrame());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-01 17:49:10 +04:00
|
|
|
int32_t nsNumberControlFrame::GetSpinButtonForPointerEvent(
|
|
|
|
WidgetGUIEvent* aEvent) const {
|
2014-08-04 09:28:50 +04:00
|
|
|
MOZ_ASSERT(aEvent->mClass == eMouseEventClass, "Unexpected event type");
|
2013-12-01 17:49:10 +04:00
|
|
|
|
2013-12-09 03:26:07 +04:00
|
|
|
if (!mSpinBox) {
|
|
|
|
// we don't have a spinner
|
|
|
|
return eSpinButtonNone;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinUp) {
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonUp;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinDown) {
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonDown;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinBox) {
|
2013-12-05 20:20:34 +04:00
|
|
|
// In the case that the up/down buttons are hidden (display:none) we use
|
|
|
|
// just the spin box element, spinning up if the pointer is over the top
|
|
|
|
// half of the element, or down if it's over the bottom half. This is
|
|
|
|
// important to handle since this is the state things are in for the
|
|
|
|
// default UA style sheet. See the comment in forms.css for why.
|
2016-04-18 17:09:02 +03:00
|
|
|
LayoutDeviceIntPoint absPoint = aEvent->mRefPoint;
|
2013-12-05 20:20:34 +04:00
|
|
|
nsPoint point = nsLayoutUtils::GetEventCoordinatesRelativeTo(
|
2015-02-02 01:27:31 +03:00
|
|
|
aEvent, absPoint, mSpinBox->GetPrimaryFrame());
|
2013-12-05 20:20:34 +04:00
|
|
|
if (point != nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) {
|
|
|
|
if (point.y < mSpinBox->GetPrimaryFrame()->GetSize().height / 2) {
|
|
|
|
return eSpinButtonUp;
|
|
|
|
}
|
|
|
|
return eSpinButtonDown;
|
|
|
|
}
|
|
|
|
}
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonNone;
|
|
|
|
}
|
|
|
|
|
2013-12-05 20:20:34 +04:00
|
|
|
void nsNumberControlFrame::SpinnerStateChanged() const {
|
2013-12-09 03:26:07 +04:00
|
|
|
MOZ_ASSERT(mSpinUp && mSpinDown,
|
|
|
|
"We should not be called when we have no spinner");
|
|
|
|
|
2013-12-05 20:20:34 +04:00
|
|
|
nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
|
|
|
|
if (spinUpFrame && spinUpFrame->IsThemed()) {
|
|
|
|
spinUpFrame->InvalidateFrame();
|
|
|
|
}
|
|
|
|
nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
|
|
|
|
if (spinDownFrame && spinDownFrame->IsThemed()) {
|
|
|
|
spinDownFrame->InvalidateFrame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsNumberControlFrame::SpinnerUpButtonIsDepressed() const {
|
2018-03-22 00:39:04 +03:00
|
|
|
return HTMLInputElement::FromNode(mContent)
|
2013-12-05 20:20:34 +04:00
|
|
|
->NumberSpinnerUpButtonIsDepressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsNumberControlFrame::SpinnerDownButtonIsDepressed() const {
|
2018-03-22 00:39:04 +03:00
|
|
|
return HTMLInputElement::FromNode(mContent)
|
2013-12-05 20:20:34 +04:00
|
|
|
->NumberSpinnerDownButtonIsDepressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STYLES_DISABLING_NATIVE_THEMING \
|
|
|
|
NS_AUTHOR_SPECIFIED_BACKGROUND | NS_AUTHOR_SPECIFIED_PADDING | \
|
|
|
|
NS_AUTHOR_SPECIFIED_BORDER
|
|
|
|
|
|
|
|
bool nsNumberControlFrame::ShouldUseNativeStyleForSpinner() const {
|
2013-12-09 03:26:07 +04:00
|
|
|
MOZ_ASSERT(mSpinUp && mSpinDown,
|
|
|
|
"We should not be called when we have no spinner");
|
|
|
|
|
2013-12-05 20:20:34 +04:00
|
|
|
nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
|
|
|
|
nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
|
|
|
|
|
|
|
|
return spinUpFrame &&
|
2018-07-25 16:03:45 +03:00
|
|
|
spinUpFrame->StyleDisplay()->mAppearance ==
|
|
|
|
StyleAppearance::SpinnerUpbutton &&
|
2013-12-05 20:20:34 +04:00
|
|
|
!PresContext()->HasAuthorSpecifiedRules(
|
|
|
|
spinUpFrame, STYLES_DISABLING_NATIVE_THEMING) &&
|
|
|
|
spinDownFrame &&
|
2018-07-25 16:03:45 +03:00
|
|
|
spinDownFrame->StyleDisplay()->mAppearance ==
|
|
|
|
StyleAppearance::SpinnerDownbutton &&
|
2013-12-05 20:20:34 +04:00
|
|
|
!PresContext()->HasAuthorSpecifiedRules(
|
|
|
|
spinDownFrame, STYLES_DISABLING_NATIVE_THEMING);
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
void nsNumberControlFrame::AppendAnonymousContentTo(
|
|
|
|
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
|
|
|
|
if (mOuterWrapper) {
|
|
|
|
aElements.AppendElement(mOuterWrapper);
|
|
|
|
}
|
2020-01-14 18:05:22 +03:00
|
|
|
if (mPlaceholderDiv) {
|
|
|
|
aElements.AppendElement(mPlaceholderDiv);
|
2015-09-18 00:17:35 +03:00
|
|
|
}
|
2020-01-14 18:05:22 +03:00
|
|
|
if (mPreviewDiv) {
|
|
|
|
aElements.AppendElement(mPreviewDiv);
|
2014-01-30 16:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 07:19:26 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
a11y::AccType nsNumberControlFrame::AccessibleType() {
|
|
|
|
return a11y::eHTMLSpinnerType;
|
|
|
|
}
|
|
|
|
#endif
|