1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "StepRange.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include <wtf/MathExtras.h>
#include <wtf/text/WTFString.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
StepRange::StepRange()
: m_maximum(100)
, m_minimum(0)
, m_step(1)
, m_stepBase(0)
, m_stepBaseDecimalPlaces(0)
, m_stepDecimalPlaces(0)
, m_hasStep(false)
{
}
StepRange::StepRange(const StepRange& stepRange)
: m_maximum(stepRange.m_maximum)
, m_minimum(stepRange.m_minimum)
, m_step(stepRange.m_step)
, m_stepBase(stepRange.m_stepBase)
, m_stepDescription(stepRange.m_stepDescription)
, m_stepBaseDecimalPlaces(stepRange.m_stepBaseDecimalPlaces)
, m_stepDecimalPlaces(stepRange.m_stepDecimalPlaces)
, m_hasStep(stepRange.m_hasStep)
{
}
StepRange::StepRange(const DoubleWithDecimalPlaces& stepBase, double minimum, double maximum, const DoubleWithDecimalPlacesOrMissing& step, const StepDescription& stepDescription)
: m_maximum(maximum)
, m_minimum(minimum)
, m_step(step.value.value)
, m_stepBase(stepBase.value)
, m_stepDescription(stepDescription)
, m_stepBaseDecimalPlaces(stepBase.decimalPlaces)
, m_stepDecimalPlaces(step.value.decimalPlaces)
, m_hasStep(step.hasValue)
{
ASSERT(isfinite(m_maximum));
ASSERT(isfinite(m_minimum));
ASSERT(isfinite(m_step));
ASSERT(isfinite(m_stepBase));
}
double StepRange::acceptableError() const
{
return m_step / pow(2.0, FLT_MANT_DIG);
}
double StepRange::alignValueForStep(double currentValue, unsigned currentDecimalPlaces, double newValue) const
{
if (newValue >= pow(10.0, 21.0))
return newValue;
if (stepMismatch(currentValue)) {
double scale = pow(10.0, static_cast<double>(max(m_stepDecimalPlaces, currentDecimalPlaces)));
newValue = round(newValue * scale) / scale;
} else {
double scale = pow(10.0, static_cast<double>(max(m_stepDecimalPlaces, m_stepBaseDecimalPlaces)));
newValue = round((m_stepBase + round((newValue - m_stepBase) / m_step) * m_step) * scale) / scale;
}
return newValue;
}
double StepRange::clampValue(double value) const
{
double clampedValue = max(m_minimum, min(value, m_maximum));
if (!m_hasStep)
return clampedValue;
// Rounds clampedValue to minimum + N * step.
clampedValue = m_minimum + round((clampedValue - m_minimum) / m_step) * m_step;
if (clampedValue > m_maximum)
clampedValue -= m_step;
ASSERT(clampedValue >= m_minimum);
ASSERT(clampedValue <= m_maximum);
return clampedValue;
}
StepRange::DoubleWithDecimalPlacesOrMissing StepRange::parseStep(AnyStepHandling anyStepHandling, const StepDescription& stepDescription, const String& stepString)
{
if (stepString.isEmpty())
return DoubleWithDecimalPlacesOrMissing(stepDescription.defaultValue());
if (equalIgnoringCase(stepString, "any")) {
switch (anyStepHandling) {
case RejectAny:
return DoubleWithDecimalPlacesOrMissing(DoubleWithDecimalPlaces(1), false);
case AnyIsDefaultStep:
return DoubleWithDecimalPlacesOrMissing(stepDescription.defaultValue());
default:
ASSERT_NOT_REACHED();
}
}
DoubleWithDecimalPlacesOrMissing step(0);
step.value.value = parseToDoubleForNumberTypeWithDecimalPlaces(stepString, &step.value.decimalPlaces);
if (!isfinite(step.value.value) || step.value.value <= 0.0)
return DoubleWithDecimalPlacesOrMissing(stepDescription.defaultValue());
switch (stepDescription.stepValueShouldBe) {
case StepValueShouldBeReal:
step.value.value *= stepDescription.stepScaleFactor;
break;
case ParsedStepValueShouldBeInteger:
// For date, month, and week, the parsed value should be an integer for some types.
step.value.value = max(round(step.value.value), 1.0);
step.value.value *= stepDescription.stepScaleFactor;
break;
case ScaledStepValueShouldBeInteger:
// For datetime, datetime-local, time, the result should be an integer.
step.value.value *= stepDescription.stepScaleFactor;
step.value.value = max(round(step.value.value), 1.0);
break;
default:
ASSERT_NOT_REACHED();
}
ASSERT(step.value.value > 0);
return step;
}
bool StepRange::stepMismatch(double doubleValue) const
{
if (!m_hasStep)
return false;
if (!isfinite(doubleValue))
return false;
doubleValue = fabs(doubleValue - m_stepBase);
if (isinf(doubleValue))
return false;
// double's fractional part size is DBL_MAN_DIG-bit. If the current value
// is greater than step*2^DBL_MANT_DIG, the following computation for
// remainder makes no sense.
if (doubleValue / pow(2.0, DBL_MANT_DIG) > m_step)
return false;
// The computation follows HTML5 4.10.7.2.10 `The step attribute' :
// ... that number subtracted from the step base is not an integral multiple
// of the allowed value step, the element is suffering from a step mismatch.
double remainder = fabs(doubleValue - m_step * round(doubleValue / m_step));
// Accepts erros in lower fractional part which IEEE 754 single-precision
// can't represent.
double computedAcceptableError = acceptableError();
return computedAcceptableError < remainder && remainder < (m_step - computedAcceptableError);
}
} // namespace WebCore
|