OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * Copyright (C) 2011 Apple Inc. All rights reserved. | 3 * Copyright (C) 2011 Apple Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // In case of "123.456" | 87 // In case of "123.456" |
88 return RealNumberRenderSize(sizeOfSign + sizeBeforeDecimalPoint, sizeOfD
igits - sizeBeforeDecimalPoint); | 88 return RealNumberRenderSize(sizeOfSign + sizeBeforeDecimalPoint, sizeOfD
igits - sizeBeforeDecimalPoint); |
89 } | 89 } |
90 | 90 |
91 // In case of "0.00012345" | 91 // In case of "0.00012345" |
92 const unsigned sizeOfZero = 1; | 92 const unsigned sizeOfZero = 1; |
93 const unsigned numberOfZeroAfterDecimalPoint = -sizeBeforeDecimalPoint; | 93 const unsigned numberOfZeroAfterDecimalPoint = -sizeBeforeDecimalPoint; |
94 return RealNumberRenderSize(sizeOfSign + sizeOfZero , numberOfZeroAfterDecim
alPoint + sizeOfDigits); | 94 return RealNumberRenderSize(sizeOfSign + sizeOfZero , numberOfZeroAfterDecim
alPoint + sizeOfDigits); |
95 } | 95 } |
96 | 96 |
97 PassRefPtr<InputType> NumberInputType::create(HTMLInputElement* element) | 97 PassRefPtr<InputType> NumberInputType::create(HTMLInputElement& element) |
98 { | 98 { |
99 return adoptRef(new NumberInputType(element)); | 99 return adoptRef(new NumberInputType(element)); |
100 } | 100 } |
101 | 101 |
102 void NumberInputType::countUsage() | 102 void NumberInputType::countUsage() |
103 { | 103 { |
104 observeFeatureIfVisible(UseCounter::InputTypeNumber); | 104 observeFeatureIfVisible(UseCounter::InputTypeNumber); |
105 } | 105 } |
106 | 106 |
107 const AtomicString& NumberInputType::formControlType() const | 107 const AtomicString& NumberInputType::formControlType() const |
108 { | 108 { |
109 return InputTypeNames::number(); | 109 return InputTypeNames::number(); |
110 } | 110 } |
111 | 111 |
112 void NumberInputType::setValue(const String& sanitizedValue, bool valueChanged,
TextFieldEventBehavior eventBehavior) | 112 void NumberInputType::setValue(const String& sanitizedValue, bool valueChanged,
TextFieldEventBehavior eventBehavior) |
113 { | 113 { |
114 if (!valueChanged && sanitizedValue.isEmpty() && !element()->innerTextValue(
).isEmpty()) | 114 if (!valueChanged && sanitizedValue.isEmpty() && !element().innerTextValue()
.isEmpty()) |
115 updateInnerTextValue(); | 115 updateInnerTextValue(); |
116 TextFieldInputType::setValue(sanitizedValue, valueChanged, eventBehavior); | 116 TextFieldInputType::setValue(sanitizedValue, valueChanged, eventBehavior); |
117 } | 117 } |
118 | 118 |
119 double NumberInputType::valueAsDouble() const | 119 double NumberInputType::valueAsDouble() const |
120 { | 120 { |
121 return parseToDoubleForNumberType(element()->value()); | 121 return parseToDoubleForNumberType(element().value()); |
122 } | 122 } |
123 | 123 |
124 void NumberInputType::setValueAsDouble(double newValue, TextFieldEventBehavior e
ventBehavior, ExceptionState& es) const | 124 void NumberInputType::setValueAsDouble(double newValue, TextFieldEventBehavior e
ventBehavior, ExceptionState& es) const |
125 { | 125 { |
126 // FIXME: We should use numeric_limits<double>::max for number input type. | 126 // FIXME: We should use numeric_limits<double>::max for number input type. |
127 const double floatMax = numeric_limits<float>::max(); | 127 const double floatMax = numeric_limits<float>::max(); |
128 if (newValue < -floatMax) { | 128 if (newValue < -floatMax) { |
129 es.throwUninformativeAndGenericDOMException(InvalidStateError); | 129 es.throwUninformativeAndGenericDOMException(InvalidStateError); |
130 return; | 130 return; |
131 } | 131 } |
132 if (newValue > floatMax) { | 132 if (newValue > floatMax) { |
133 es.throwUninformativeAndGenericDOMException(InvalidStateError); | 133 es.throwUninformativeAndGenericDOMException(InvalidStateError); |
134 return; | 134 return; |
135 } | 135 } |
136 element()->setValue(serializeForNumberType(newValue), eventBehavior); | 136 element().setValue(serializeForNumberType(newValue), eventBehavior); |
137 } | 137 } |
138 | 138 |
139 void NumberInputType::setValueAsDecimal(const Decimal& newValue, TextFieldEventB
ehavior eventBehavior, ExceptionState& es) const | 139 void NumberInputType::setValueAsDecimal(const Decimal& newValue, TextFieldEventB
ehavior eventBehavior, ExceptionState& es) const |
140 { | 140 { |
141 // FIXME: We should use numeric_limits<double>::max for number input type. | 141 // FIXME: We should use numeric_limits<double>::max for number input type. |
142 const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max()); | 142 const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max()); |
143 if (newValue < -floatMax) { | 143 if (newValue < -floatMax) { |
144 es.throwUninformativeAndGenericDOMException(InvalidStateError); | 144 es.throwUninformativeAndGenericDOMException(InvalidStateError); |
145 return; | 145 return; |
146 } | 146 } |
147 if (newValue > floatMax) { | 147 if (newValue > floatMax) { |
148 es.throwUninformativeAndGenericDOMException(InvalidStateError); | 148 es.throwUninformativeAndGenericDOMException(InvalidStateError); |
149 return; | 149 return; |
150 } | 150 } |
151 element()->setValue(serializeForNumberType(newValue), eventBehavior); | 151 element().setValue(serializeForNumberType(newValue), eventBehavior); |
152 } | 152 } |
153 | 153 |
154 bool NumberInputType::typeMismatchFor(const String& value) const | 154 bool NumberInputType::typeMismatchFor(const String& value) const |
155 { | 155 { |
156 return !value.isEmpty() && !std::isfinite(parseToDoubleForNumberType(value))
; | 156 return !value.isEmpty() && !std::isfinite(parseToDoubleForNumberType(value))
; |
157 } | 157 } |
158 | 158 |
159 bool NumberInputType::typeMismatch() const | 159 bool NumberInputType::typeMismatch() const |
160 { | 160 { |
161 ASSERT(!typeMismatchFor(element()->value())); | 161 ASSERT(!typeMismatchFor(element().value())); |
162 return false; | 162 return false; |
163 } | 163 } |
164 | 164 |
165 StepRange NumberInputType::createStepRange(AnyStepHandling anyStepHandling) cons
t | 165 StepRange NumberInputType::createStepRange(AnyStepHandling anyStepHandling) cons
t |
166 { | 166 { |
167 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (numb
erDefaultStep, numberDefaultStepBase, numberStepScaleFactor)); | 167 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (numb
erDefaultStep, numberDefaultStepBase, numberStepScaleFactor)); |
168 const Decimal stepBase = parseToDecimalForNumberType(element()->fastGetAttri
bute(minAttr), numberDefaultStepBase); | 168 const Decimal stepBase = parseToDecimalForNumberType(element().fastGetAttrib
ute(minAttr), numberDefaultStepBase); |
169 // FIXME: We should use numeric_limits<double>::max for number input type. | 169 // FIXME: We should use numeric_limits<double>::max for number input type. |
170 const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max()); | 170 const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max()); |
171 const Decimal minimum = parseToNumber(element()->fastGetAttribute(minAttr),
-floatMax); | 171 const Decimal minimum = parseToNumber(element().fastGetAttribute(minAttr), -
floatMax); |
172 const Decimal maximum = parseToNumber(element()->fastGetAttribute(maxAttr),
floatMax); | 172 const Decimal maximum = parseToNumber(element().fastGetAttribute(maxAttr), f
loatMax); |
173 const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription,
element()->fastGetAttribute(stepAttr)); | 173 const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription,
element().fastGetAttribute(stepAttr)); |
174 return StepRange(stepBase, minimum, maximum, step, stepDescription); | 174 return StepRange(stepBase, minimum, maximum, step, stepDescription); |
175 } | 175 } |
176 | 176 |
177 bool NumberInputType::sizeShouldIncludeDecoration(int defaultSize, int& preferre
dSize) const | 177 bool NumberInputType::sizeShouldIncludeDecoration(int defaultSize, int& preferre
dSize) const |
178 { | 178 { |
179 preferredSize = defaultSize; | 179 preferredSize = defaultSize; |
180 | 180 |
181 const String stepString = element()->fastGetAttribute(stepAttr); | 181 const String stepString = element().fastGetAttribute(stepAttr); |
182 if (equalIgnoringCase(stepString, "any")) | 182 if (equalIgnoringCase(stepString, "any")) |
183 return false; | 183 return false; |
184 | 184 |
185 const Decimal minimum = parseToDecimalForNumberType(element()->fastGetAttrib
ute(minAttr)); | 185 const Decimal minimum = parseToDecimalForNumberType(element().fastGetAttribu
te(minAttr)); |
186 if (!minimum.isFinite()) | 186 if (!minimum.isFinite()) |
187 return false; | 187 return false; |
188 | 188 |
189 const Decimal maximum = parseToDecimalForNumberType(element()->fastGetAttrib
ute(maxAttr)); | 189 const Decimal maximum = parseToDecimalForNumberType(element().fastGetAttribu
te(maxAttr)); |
190 if (!maximum.isFinite()) | 190 if (!maximum.isFinite()) |
191 return false; | 191 return false; |
192 | 192 |
193 const Decimal step = parseToDecimalForNumberType(stepString, 1); | 193 const Decimal step = parseToDecimalForNumberType(stepString, 1); |
194 ASSERT(step.isFinite()); | 194 ASSERT(step.isFinite()); |
195 | 195 |
196 RealNumberRenderSize size = calculateRenderSize(minimum).max(calculateRender
Size(maximum).max(calculateRenderSize(step))); | 196 RealNumberRenderSize size = calculateRenderSize(minimum).max(calculateRender
Size(maximum).max(calculateRenderSize(step))); |
197 | 197 |
198 preferredSize = size.sizeBeforeDecimalPoint + size.sizeAfteDecimalPoint + (s
ize.sizeAfteDecimalPoint ? 1 : 0); | 198 preferredSize = size.sizeBeforeDecimalPoint + size.sizeAfteDecimalPoint + (s
ize.sizeAfteDecimalPoint ? 1 : 0); |
199 | 199 |
(...skipping 29 matching lines...) Expand all Loading... |
229 return ch == 'e' || ch == 'E'; | 229 return ch == 'e' || ch == 'E'; |
230 } | 230 } |
231 | 231 |
232 String NumberInputType::localizeValue(const String& proposedValue) const | 232 String NumberInputType::localizeValue(const String& proposedValue) const |
233 { | 233 { |
234 if (proposedValue.isEmpty()) | 234 if (proposedValue.isEmpty()) |
235 return proposedValue; | 235 return proposedValue; |
236 // We don't localize scientific notations. | 236 // We don't localize scientific notations. |
237 if (proposedValue.find(isE) != kNotFound) | 237 if (proposedValue.find(isE) != kNotFound) |
238 return proposedValue; | 238 return proposedValue; |
239 return element()->locale().convertToLocalizedNumber(proposedValue); | 239 return element().locale().convertToLocalizedNumber(proposedValue); |
240 } | 240 } |
241 | 241 |
242 String NumberInputType::visibleValue() const | 242 String NumberInputType::visibleValue() const |
243 { | 243 { |
244 return localizeValue(element()->value()); | 244 return localizeValue(element().value()); |
245 } | 245 } |
246 | 246 |
247 String NumberInputType::convertFromVisibleValue(const String& visibleValue) cons
t | 247 String NumberInputType::convertFromVisibleValue(const String& visibleValue) cons
t |
248 { | 248 { |
249 if (visibleValue.isEmpty()) | 249 if (visibleValue.isEmpty()) |
250 return visibleValue; | 250 return visibleValue; |
251 // We don't localize scientific notations. | 251 // We don't localize scientific notations. |
252 if (visibleValue.find(isE) != kNotFound) | 252 if (visibleValue.find(isE) != kNotFound) |
253 return visibleValue; | 253 return visibleValue; |
254 return element()->locale().convertFromLocalizedNumber(visibleValue); | 254 return element().locale().convertFromLocalizedNumber(visibleValue); |
255 } | 255 } |
256 | 256 |
257 String NumberInputType::sanitizeValue(const String& proposedValue) const | 257 String NumberInputType::sanitizeValue(const String& proposedValue) const |
258 { | 258 { |
259 if (proposedValue.isEmpty()) | 259 if (proposedValue.isEmpty()) |
260 return proposedValue; | 260 return proposedValue; |
261 return std::isfinite(parseToDoubleForNumberType(proposedValue)) ? proposedVa
lue : emptyString(); | 261 return std::isfinite(parseToDoubleForNumberType(proposedValue)) ? proposedVa
lue : emptyString(); |
262 } | 262 } |
263 | 263 |
264 bool NumberInputType::hasBadInput() const | 264 bool NumberInputType::hasBadInput() const |
265 { | 265 { |
266 String standardValue = convertFromVisibleValue(element()->innerTextValue()); | 266 String standardValue = convertFromVisibleValue(element().innerTextValue()); |
267 return !standardValue.isEmpty() && !std::isfinite(parseToDoubleForNumberType
(standardValue)); | 267 return !standardValue.isEmpty() && !std::isfinite(parseToDoubleForNumberType
(standardValue)); |
268 } | 268 } |
269 | 269 |
270 String NumberInputType::badInputText() const | 270 String NumberInputType::badInputText() const |
271 { | 271 { |
272 return locale().queryString(WebLocalizedString::ValidationBadInputForNumber)
; | 272 return locale().queryString(WebLocalizedString::ValidationBadInputForNumber)
; |
273 } | 273 } |
274 | 274 |
275 String NumberInputType::rangeOverflowText(const Decimal& maximum) const | 275 String NumberInputType::rangeOverflowText(const Decimal& maximum) const |
276 { | 276 { |
(...skipping 17 matching lines...) Expand all Loading... |
294 | 294 |
295 bool NumberInputType::isNumberField() const | 295 bool NumberInputType::isNumberField() const |
296 { | 296 { |
297 return true; | 297 return true; |
298 } | 298 } |
299 | 299 |
300 void NumberInputType::minOrMaxAttributeChanged() | 300 void NumberInputType::minOrMaxAttributeChanged() |
301 { | 301 { |
302 InputType::minOrMaxAttributeChanged(); | 302 InputType::minOrMaxAttributeChanged(); |
303 | 303 |
304 if (element()->renderer()) | 304 if (element().renderer()) |
305 element()->renderer()->setNeedsLayoutAndPrefWidthsRecalc(); | 305 element().renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
306 } | 306 } |
307 | 307 |
308 void NumberInputType::stepAttributeChanged() | 308 void NumberInputType::stepAttributeChanged() |
309 { | 309 { |
310 InputType::stepAttributeChanged(); | 310 InputType::stepAttributeChanged(); |
311 | 311 |
312 if (element()->renderer()) | 312 if (element().renderer()) |
313 element()->renderer()->setNeedsLayoutAndPrefWidthsRecalc(); | 313 element().renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
314 } | 314 } |
315 | 315 |
316 } // namespace WebCore | 316 } // namespace WebCore |
OLD | NEW |