Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 */
#include "nsNumberControlFrame.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/EventStates.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/PresShell.h"
#include "HTMLInputElement.h"
#include "nsGkAtoms.h"
#include "nsNameSpaceManager.h"
#include "nsStyleConsts.h"
#include "nsContentUtils.h"
#include "nsContentCreatorFunctions.h"
#include "nsCSSPseudoElements.h"
# include "mozilla/a11y/AccTypes.h"
using namespace mozilla;
using namespace mozilla::dom;
nsIFrame* NS_NewNumberControlFrame(PresShell* aPresShell,
ComputedStyle* aStyle) {
return new (aPresShell)
nsNumberControlFrame(aStyle, aPresShell->GetPresContext());
nsNumberControlFrame::nsNumberControlFrame(ComputedStyle* aStyle,
nsPresContext* aPresContext)
: nsTextControlFrame(aStyle, aPresContext, kClassID) {}
void nsNumberControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
PostDestroyData& aPostDestroyData) {
nsTextControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
nsresult nsNumberControlFrame::CreateAnonymousContent(
nsTArray<ContentInfo>& aElements) {
// We create an anonymous tree for our input element that is structured as
// follows:
// input
// div - spin box wrapping up/down arrow buttons
// div - spin up (up arrow button)
// div - spin down (down arrow button)
// div - editor root
// div - placeholder
// div - preview div
// If you change this, be careful to change the destruction order in
// nsNumberControlFrame::DestroyFrom.
// The author has elected to hide the spinner by setting this
// -moz-appearance. We will reframe if it changes.
if (StyleDisplay()->EffectiveAppearance() != StyleAppearance::Textfield) {
// Create the ::-moz-number-spin-box pseudo-element:
mSpinBox = MakeAnonElement(PseudoStyleType::mozNumberSpinBox);
// Create the ::-moz-number-spin-up pseudo-element:
mSpinUp = MakeAnonElement(PseudoStyleType::mozNumberSpinUp, mSpinBox);
// Create the ::-moz-number-spin-down pseudo-element:
mSpinDown = MakeAnonElement(PseudoStyleType::mozNumberSpinDown, mSpinBox);
// It's important that this goes first, so that reflow can know our size for
// the rest of the children.
return NS_OK;
/* static */
nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForSpinButton(
nsIFrame* aFrame) {
// If aFrame is a spin button for an <input type=number> then we expect the
// frame of the NAC root parent 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();
auto* nacHost = content->GetClosestNativeAnonymousSubtreeRootParent();
if (!nacHost) {
return nullptr;
auto* input = HTMLInputElement::FromNode(nacHost);
if (!input || input->ControlType() != NS_FORM_INPUT_NUMBER) {
return nullptr;
return do_QueryFrame(input->GetPrimaryFrame());
int32_t nsNumberControlFrame::GetSpinButtonForPointerEvent(
WidgetGUIEvent* aEvent) const {
MOZ_ASSERT(aEvent->mClass == eMouseEventClass, "Unexpected event type");
if (!mSpinBox) {
// we don't have a spinner
return eSpinButtonNone;
if (aEvent->mOriginalTarget == mSpinUp) {
return eSpinButtonUp;
if (aEvent->mOriginalTarget == mSpinDown) {
return eSpinButtonDown;
if (aEvent->mOriginalTarget == mSpinBox) {
// 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.
LayoutDeviceIntPoint absPoint = aEvent->mRefPoint;
nsPoint point = nsLayoutUtils::GetEventCoordinatesRelativeTo(
aEvent, absPoint, RelativeTo{mSpinBox->GetPrimaryFrame()});
if (point.y < mSpinBox->GetPrimaryFrame()->GetSize().height / 2) {
return eSpinButtonUp;
return eSpinButtonDown;
return eSpinButtonNone;
void nsNumberControlFrame::SpinnerStateChanged() const {
MOZ_ASSERT(mSpinUp && mSpinDown,
"We should not be called when we have no spinner");
nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
if (spinUpFrame && spinUpFrame->IsThemed()) {
nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
if (spinDownFrame && spinDownFrame->IsThemed()) {
bool nsNumberControlFrame::SpinnerUpButtonIsDepressed() const {
return HTMLInputElement::FromNode(mContent)
bool nsNumberControlFrame::SpinnerDownButtonIsDepressed() const {
return HTMLInputElement::FromNode(mContent)
void nsNumberControlFrame::AppendAnonymousContentTo(
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
if (mSpinBox) {
nsTextControlFrame::AppendAnonymousContentTo(aElements, aFilter);
a11y::AccType nsNumberControlFrame::AccessibleType() {
return a11y::eHTMLSpinnerType;