2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "core/html/forms/StepRange.h"
24 #include "core/HTMLNames.h"
25 #include "core/html/parser/HTMLParserIdioms.h"
26 #include "wtf/MathExtras.h"
27 #include "wtf/text/WTFString.h"
32 using namespace HTMLNames;
34 StepRange::StepRange()
43 StepRange::StepRange(const StepRange& stepRange)
44 : m_maximum(stepRange.m_maximum)
45 , m_minimum(stepRange.m_minimum)
46 , m_step(stepRange.m_step)
47 , m_stepBase(stepRange.m_stepBase)
48 , m_stepDescription(stepRange.m_stepDescription)
49 , m_hasStep(stepRange.m_hasStep)
53 StepRange::StepRange(const Decimal& stepBase, const Decimal& minimum, const Decimal& maximum, const Decimal& step, const StepDescription& stepDescription)
56 , m_step(step.isFinite() ? step : 1)
57 , m_stepBase(stepBase.isFinite() ? stepBase : 1)
58 , m_stepDescription(stepDescription)
59 , m_hasStep(step.isFinite())
61 ASSERT(m_maximum.isFinite());
62 ASSERT(m_minimum.isFinite());
63 ASSERT(m_step.isFinite());
64 ASSERT(m_stepBase.isFinite());
67 Decimal StepRange::acceptableError() const
69 // FIXME: We should use DBL_MANT_DIG instead of FLT_MANT_DIG regarding to HTML5 specification.
70 DEFINE_STATIC_LOCAL(const Decimal, twoPowerOfFloatMantissaBits, (Decimal::Positive, 0, UINT64_C(1) << FLT_MANT_DIG));
71 return m_stepDescription.stepValueShouldBe == StepValueShouldBeReal ? m_step / twoPowerOfFloatMantissaBits : Decimal(0);
74 Decimal StepRange::alignValueForStep(const Decimal& currentValue, const Decimal& newValue) const
76 DEFINE_STATIC_LOCAL(const Decimal, tenPowerOf21, (Decimal::Positive, 21, 1));
77 if (newValue >= tenPowerOf21)
80 return stepMismatch(currentValue) ? newValue : roundByStep(newValue, m_stepBase);
83 Decimal StepRange::clampValue(const Decimal& value) const
85 const Decimal inRangeValue = std::max(m_minimum, std::min(value, m_maximum));
88 // Rounds inRangeValue to stepBase + N * step.
89 const Decimal roundedValue = roundByStep(inRangeValue, m_stepBase);
90 const Decimal clampedValue = roundedValue > m_maximum ? roundedValue - m_step : (roundedValue < m_minimum ? roundedValue + m_step : roundedValue);
91 ASSERT(clampedValue >= m_minimum);
92 ASSERT(clampedValue <= m_maximum);
96 Decimal StepRange::parseStep(AnyStepHandling anyStepHandling, const StepDescription& stepDescription, const String& stepString)
98 if (stepString.isEmpty())
99 return stepDescription.defaultValue();
101 if (equalIgnoringCase(stepString, "any")) {
102 switch (anyStepHandling) {
104 return Decimal::nan();
105 case AnyIsDefaultStep:
106 return stepDescription.defaultValue();
108 ASSERT_NOT_REACHED();
112 Decimal step = parseToDecimalForNumberType(stepString);
113 if (!step.isFinite() || step <= 0)
114 return stepDescription.defaultValue();
116 switch (stepDescription.stepValueShouldBe) {
117 case StepValueShouldBeReal:
118 step *= stepDescription.stepScaleFactor;
120 case ParsedStepValueShouldBeInteger:
121 // For date, month, and week, the parsed value should be an integer for some types.
122 step = std::max(step.round(), Decimal(1));
123 step *= stepDescription.stepScaleFactor;
125 case ScaledStepValueShouldBeInteger:
126 // For datetime, datetime-local, time, the result should be an integer.
127 step *= stepDescription.stepScaleFactor;
128 step = std::max(step.round(), Decimal(1));
131 ASSERT_NOT_REACHED();
138 Decimal StepRange::roundByStep(const Decimal& value, const Decimal& base) const
140 return base + ((value - base) / m_step).round() * m_step;
143 bool StepRange::stepMismatch(const Decimal& valueForCheck) const
147 if (!valueForCheck.isFinite())
149 const Decimal value = (valueForCheck - m_stepBase).abs();
150 if (!value.isFinite())
152 // Decimal's fractional part size is DBL_MAN_DIG-bit. If the current value
153 // is greater than step*2^DBL_MANT_DIG, the following computation for
154 // remainder makes no sense.
155 DEFINE_STATIC_LOCAL(const Decimal, twoPowerOfDoubleMantissaBits, (Decimal::Positive, 0, UINT64_C(1) << DBL_MANT_DIG));
156 if (value / twoPowerOfDoubleMantissaBits > m_step)
158 // The computation follows HTML5 4.10.7.2.10 `The step attribute' :
159 // ... that number subtracted from the step base is not an integral multiple
160 // of the allowed value step, the element is suffering from a step mismatch.
161 const Decimal remainder = (value - m_step * (value / m_step).round()).abs();
162 // Accepts errors in lower fractional part which IEEE 754 single-precision
164 const Decimal computedAcceptableError = acceptableError();
165 return computedAcceptableError < remainder && remainder < (m_step - computedAcceptableError);