Home | History | Annotate | Download | only in forms
      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 "core/html/forms/StepRange.h"
     23 
     24 #include "core/HTMLNames.h"
     25 #include "core/html/parser/HTMLParserIdioms.h"
     26 #include "wtf/MathExtras.h"
     27 #include "wtf/text/WTFString.h"
     28 #include <float.h>
     29 
     30 namespace WebCore {
     31 
     32 using namespace HTMLNames;
     33 
     34 StepRange::StepRange()
     35     : m_maximum(100)
     36     , m_minimum(0)
     37     , m_step(1)
     38     , m_stepBase(0)
     39     , m_hasStep(false)
     40 {
     41 }
     42 
     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)
     50 {
     51 }
     52 
     53 StepRange::StepRange(const Decimal& stepBase, const Decimal& minimum, const Decimal& maximum, const Decimal& step, const StepDescription& stepDescription)
     54     : m_maximum(maximum)
     55     , m_minimum(minimum)
     56     , m_step(step.isFinite() ? step : 1)
     57     , m_stepBase(stepBase.isFinite() ? stepBase : 1)
     58     , m_stepDescription(stepDescription)
     59     , m_hasStep(step.isFinite())
     60 {
     61     ASSERT(m_maximum.isFinite());
     62     ASSERT(m_minimum.isFinite());
     63     ASSERT(m_step.isFinite());
     64     ASSERT(m_stepBase.isFinite());
     65 }
     66 
     67 Decimal StepRange::acceptableError() const
     68 {
     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);
     72 }
     73 
     74 Decimal StepRange::alignValueForStep(const Decimal& currentValue, const Decimal& newValue) const
     75 {
     76     DEFINE_STATIC_LOCAL(const Decimal, tenPowerOf21, (Decimal::Positive, 21, 1));
     77     if (newValue >= tenPowerOf21)
     78         return newValue;
     79 
     80     return stepMismatch(currentValue) ? newValue : roundByStep(newValue, m_stepBase);
     81 }
     82 
     83 Decimal StepRange::clampValue(const Decimal& value) const
     84 {
     85     const Decimal inRangeValue = std::max(m_minimum, std::min(value, m_maximum));
     86     if (!m_hasStep)
     87         return inRangeValue;
     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);
     93     return clampedValue;
     94 }
     95 
     96 Decimal StepRange::parseStep(AnyStepHandling anyStepHandling, const StepDescription& stepDescription, const String& stepString)
     97 {
     98     if (stepString.isEmpty())
     99         return stepDescription.defaultValue();
    100 
    101     if (equalIgnoringCase(stepString, "any")) {
    102         switch (anyStepHandling) {
    103         case RejectAny:
    104             return Decimal::nan();
    105         case AnyIsDefaultStep:
    106             return stepDescription.defaultValue();
    107         default:
    108             ASSERT_NOT_REACHED();
    109         }
    110     }
    111 
    112     Decimal step = parseToDecimalForNumberType(stepString);
    113     if (!step.isFinite() || step <= 0)
    114         return stepDescription.defaultValue();
    115 
    116     switch (stepDescription.stepValueShouldBe) {
    117     case StepValueShouldBeReal:
    118         step *= stepDescription.stepScaleFactor;
    119         break;
    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;
    124         break;
    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));
    129         break;
    130     default:
    131         ASSERT_NOT_REACHED();
    132     }
    133 
    134     ASSERT(step > 0);
    135     return step;
    136 }
    137 
    138 Decimal StepRange::roundByStep(const Decimal& value, const Decimal& base) const
    139 {
    140     return base + ((value - base) / m_step).round() * m_step;
    141 }
    142 
    143 bool StepRange::stepMismatch(const Decimal& valueForCheck) const
    144 {
    145     if (!m_hasStep)
    146         return false;
    147     if (!valueForCheck.isFinite())
    148         return false;
    149     const Decimal value = (valueForCheck - m_stepBase).abs();
    150     if (!value.isFinite())
    151         return false;
    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)
    157         return false;
    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
    163     // can't represent.
    164     const Decimal computedAcceptableError = acceptableError();
    165     return computedAcceptableError < remainder && remainder < (m_step - computedAcceptableError);
    166 }
    167 
    168 } // namespace WebCore
    169