1/*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21#include "config.h"
22#include "StepRange.h"
23
24#include "HTMLNames.h"
25#include "HTMLParserIdioms.h"
26#include <wtf/MathExtras.h>
27
28namespace WebCore {
29
30using namespace HTMLNames;
31
32StepRange::StepRange()
33    : m_maximum(100)
34    , m_minimum(0)
35    , m_step(1)
36    , m_stepBase(0)
37    , m_hasStep(false)
38{
39}
40
41StepRange::StepRange(const StepRange& stepRange)
42    : m_maximum(stepRange.m_maximum)
43    , m_minimum(stepRange.m_minimum)
44    , m_step(stepRange.m_step)
45    , m_stepBase(stepRange.m_stepBase)
46    , m_stepDescription(stepRange.m_stepDescription)
47    , m_hasStep(stepRange.m_hasStep)
48{
49}
50
51StepRange::StepRange(const Decimal& stepBase, const Decimal& minimum, const Decimal& maximum, const Decimal& step, const StepDescription& stepDescription)
52    : m_maximum(maximum)
53    , m_minimum(minimum)
54    , m_step(step.isFinite() ? step : 1)
55    , m_stepBase(stepBase.isFinite() ? stepBase : 1)
56    , m_stepDescription(stepDescription)
57    , m_hasStep(step.isFinite())
58{
59    ASSERT(m_maximum.isFinite());
60    ASSERT(m_minimum.isFinite());
61    ASSERT(m_step.isFinite());
62    ASSERT(m_stepBase.isFinite());
63}
64
65Decimal StepRange::acceptableError() const
66{
67    // FIXME: We should use DBL_MANT_DIG instead of FLT_MANT_DIG regarding to HTML5 specification.
68    DEPRECATED_DEFINE_STATIC_LOCAL(const Decimal, twoPowerOfFloatMantissaBits, (Decimal::Positive, 0, UINT64_C(1) << FLT_MANT_DIG));
69    return m_stepDescription.stepValueShouldBe == StepValueShouldBeReal ? m_step / twoPowerOfFloatMantissaBits : Decimal(0);
70}
71
72Decimal StepRange::alignValueForStep(const Decimal& currentValue, const Decimal& newValue) const
73{
74    DEPRECATED_DEFINE_STATIC_LOCAL(const Decimal, tenPowerOf21, (Decimal::Positive, 21, 1));
75    if (newValue >= tenPowerOf21)
76        return newValue;
77
78    return stepMismatch(currentValue) ? newValue : roundByStep(newValue, m_stepBase);
79}
80
81Decimal StepRange::clampValue(const Decimal& value) const
82{
83    const Decimal inRangeValue = std::max(m_minimum, std::min(value, m_maximum));
84    if (!m_hasStep)
85        return inRangeValue;
86    // Rounds inRangeValue to minimum + N * step.
87    const Decimal roundedValue = roundByStep(inRangeValue, m_minimum);
88    const Decimal clampedValue = roundedValue > m_maximum ? roundedValue - m_step : roundedValue;
89    ASSERT(clampedValue >= m_minimum);
90    ASSERT(clampedValue <= m_maximum);
91    return clampedValue;
92}
93
94Decimal StepRange::parseStep(AnyStepHandling anyStepHandling, const StepDescription& stepDescription, const String& stepString)
95{
96    if (stepString.isEmpty())
97        return stepDescription.defaultValue();
98
99    if (equalIgnoringCase(stepString, "any")) {
100        switch (anyStepHandling) {
101        case RejectAny:
102            return Decimal::nan();
103        case AnyIsDefaultStep:
104            return stepDescription.defaultValue();
105        default:
106            ASSERT_NOT_REACHED();
107        }
108    }
109
110    Decimal step = parseToDecimalForNumberType(stepString);
111    if (!step.isFinite() || step <= 0)
112        return stepDescription.defaultValue();
113
114    switch (stepDescription.stepValueShouldBe) {
115    case StepValueShouldBeReal:
116        step *= stepDescription.stepScaleFactor;
117        break;
118    case ParsedStepValueShouldBeInteger:
119        // For date, month, and week, the parsed value should be an integer for some types.
120        step = std::max(step.round(), Decimal(1));
121        step *= stepDescription.stepScaleFactor;
122        break;
123    case ScaledStepValueShouldBeInteger:
124        // For datetime, datetime-local, time, the result should be an integer.
125        step *= stepDescription.stepScaleFactor;
126        step = std::max(step.round(), Decimal(1));
127        break;
128    default:
129        ASSERT_NOT_REACHED();
130    }
131
132    ASSERT(step > 0);
133    return step;
134}
135
136Decimal StepRange::roundByStep(const Decimal& value, const Decimal& base) const
137{
138    return base + ((value - base) / m_step).round() * m_step;
139}
140
141bool StepRange::stepMismatch(const Decimal& valueForCheck) const
142{
143    if (!m_hasStep)
144        return false;
145    if (!valueForCheck.isFinite())
146        return false;
147    const Decimal value = (valueForCheck - m_stepBase).abs();
148    if (!value.isFinite())
149        return false;
150    // Decimal's fractional part size is DBL_MAN_DIG-bit. If the current value
151    // is greater than step*2^DBL_MANT_DIG, the following computation for
152    // remainder makes no sense.
153    DEPRECATED_DEFINE_STATIC_LOCAL(const Decimal, twoPowerOfDoubleMantissaBits, (Decimal::Positive, 0, UINT64_C(1) << DBL_MANT_DIG));
154    if (value / twoPowerOfDoubleMantissaBits > m_step)
155        return false;
156    // The computation follows HTML5 4.10.7.2.10 `The step attribute' :
157    // ... that number subtracted from the step base is not an integral multiple
158    // of the allowed value step, the element is suffering from a step mismatch.
159    const Decimal remainder = (value - m_step * (value / m_step).round()).abs();
160    // Accepts erros in lower fractional part which IEEE 754 single-precision
161    // can't represent.
162    const Decimal computedAcceptableError = acceptableError();
163    return computedAcceptableError < remainder && remainder < (m_step - computedAcceptableError);
164}
165
166} // namespace WebCore
167