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