Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
| 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
| 17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 20 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 23 | * SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #if ENABLE(INPUT_MULTIPLE_FIELDS_UI) |
| 28 | #include "core/html/shadow/DateTimeEditElement.h" |
| 29 | |
| 30 | #include "HTMLNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 31 | #include "core/dom/KeyboardEvent.h" |
| 32 | #include "core/dom/MouseEvent.h" |
| 33 | #include "core/dom/Text.h" |
| 34 | #include "core/html/DateTimeFieldsState.h" |
| 35 | #include "core/html/shadow/DateTimeFieldElements.h" |
| 36 | #include "core/html/shadow/DateTimeSymbolicFieldElement.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 37 | #include "core/html/shadow/ShadowElementNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 38 | #include "core/page/EventHandler.h" |
| 39 | #include "core/platform/DateComponents.h" |
| 40 | #include "core/platform/graphics/FontCache.h" |
| 41 | #include "core/platform/text/DateTimeFormat.h" |
| 42 | #include "core/platform/text/PlatformLocale.h" |
| 43 | #include "core/rendering/style/RenderStyle.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 44 | #include "core/rendering/style/StyleInheritedData.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 45 | #include <wtf/DateMath.h> |
| 46 | #include <wtf/text/StringBuilder.h> |
| 47 | |
| 48 | namespace WebCore { |
| 49 | |
| 50 | using namespace HTMLNames; |
| 51 | using namespace WTF::Unicode; |
| 52 | |
| 53 | class DateTimeEditBuilder : private DateTimeFormat::TokenHandler { |
| 54 | WTF_MAKE_NONCOPYABLE(DateTimeEditBuilder); |
| 55 | |
| 56 | public: |
| 57 | // The argument objects must be alive until this object dies. |
| 58 | DateTimeEditBuilder(DateTimeEditElement&, const DateTimeEditElement::LayoutParameters&, const DateComponents&); |
| 59 | |
| 60 | bool build(const String&); |
| 61 | |
| 62 | private: |
| 63 | bool needMillisecondField() const; |
| 64 | bool shouldAMPMFieldDisabled() const; |
| 65 | bool shouldDayOfMonthFieldDisabled() const; |
| 66 | bool shouldHourFieldDisabled() const; |
| 67 | bool shouldMillisecondFieldDisabled() const; |
| 68 | bool shouldMinuteFieldDisabled() const; |
| 69 | bool shouldSecondFieldDisabled() const; |
| 70 | bool shouldYearFieldDisabled() const; |
| 71 | inline const StepRange& stepRange() const { return m_parameters.stepRange; } |
| 72 | DateTimeNumericFieldElement::Step createStep(double msPerFieldUnit, double msPerFieldSize) const; |
| 73 | |
| 74 | // DateTimeFormat::TokenHandler functions. |
| 75 | virtual void visitField(DateTimeFormat::FieldType, int) OVERRIDE FINAL; |
| 76 | virtual void visitLiteral(const String&) OVERRIDE FINAL; |
| 77 | |
| 78 | DateTimeEditElement& m_editElement; |
| 79 | const DateComponents m_dateValue; |
| 80 | const DateTimeEditElement::LayoutParameters& m_parameters; |
| 81 | DateTimeNumericFieldElement::Range m_dayRange; |
| 82 | DateTimeNumericFieldElement::Range m_hour23Range; |
| 83 | DateTimeNumericFieldElement::Range m_minuteRange; |
| 84 | DateTimeNumericFieldElement::Range m_secondRange; |
| 85 | DateTimeNumericFieldElement::Range m_millisecondRange; |
| 86 | }; |
| 87 | |
| 88 | DateTimeEditBuilder::DateTimeEditBuilder(DateTimeEditElement& elemnt, const DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& dateValue) |
| 89 | : m_editElement(elemnt) |
| 90 | , m_dateValue(dateValue) |
| 91 | , m_parameters(layoutParameters) |
| 92 | , m_dayRange(1, 31) |
| 93 | , m_hour23Range(0, 23) |
| 94 | , m_minuteRange(0, 59) |
| 95 | , m_secondRange(0, 59) |
| 96 | , m_millisecondRange(0, 999) |
| 97 | { |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 98 | if (m_dateValue.type() == DateComponents::Date || m_dateValue.type() == DateComponents::DateTimeLocal) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 99 | if (m_parameters.minimum.type() != DateComponents::Invalid |
| 100 | && m_parameters.maximum.type() != DateComponents::Invalid |
| 101 | && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() |
| 102 | && m_parameters.minimum.month() == m_parameters.maximum.month() |
| 103 | && m_parameters.minimum.monthDay() <= m_parameters.maximum.monthDay()) { |
| 104 | m_dayRange.minimum = m_parameters.minimum.monthDay(); |
| 105 | m_dayRange.maximum = m_parameters.maximum.monthDay(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (m_dateValue.type() == DateComponents::Time || m_dayRange.isSingleton()) { |
| 110 | if (m_parameters.minimum.type() != DateComponents::Invalid |
| 111 | && m_parameters.maximum.type() != DateComponents::Invalid |
| 112 | && m_parameters.minimum.hour() <= m_parameters.maximum.hour()) { |
| 113 | m_hour23Range.minimum = m_parameters.minimum.hour(); |
| 114 | m_hour23Range.maximum = m_parameters.maximum.hour(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (m_hour23Range.isSingleton() && m_parameters.minimum.minute() <= m_parameters.maximum.minute()) { |
| 119 | m_minuteRange.minimum = m_parameters.minimum.minute(); |
| 120 | m_minuteRange.maximum = m_parameters.maximum.minute(); |
| 121 | } |
| 122 | if (m_minuteRange.isSingleton() && m_parameters.minimum.second() <= m_parameters.maximum.second()) { |
| 123 | m_secondRange.minimum = m_parameters.minimum.second(); |
| 124 | m_secondRange.maximum = m_parameters.maximum.second(); |
| 125 | } |
| 126 | if (m_secondRange.isSingleton() && m_parameters.minimum.millisecond() <= m_parameters.maximum.millisecond()) { |
| 127 | m_millisecondRange.minimum = m_parameters.minimum.millisecond(); |
| 128 | m_millisecondRange.maximum = m_parameters.maximum.millisecond(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | bool DateTimeEditBuilder::build(const String& formatString) |
| 133 | { |
| 134 | m_editElement.resetFields(); |
| 135 | return DateTimeFormat::parse(formatString, *this); |
| 136 | } |
| 137 | |
| 138 | bool DateTimeEditBuilder::needMillisecondField() const |
| 139 | { |
| 140 | return m_dateValue.millisecond() |
| 141 | || !stepRange().minimum().remainder(static_cast<int>(msPerSecond)).isZero() |
| 142 | || !stepRange().step().remainder(static_cast<int>(msPerSecond)).isZero(); |
| 143 | } |
| 144 | |
| 145 | void DateTimeEditBuilder::visitField(DateTimeFormat::FieldType fieldType, int count) |
| 146 | { |
| 147 | const int countForAbbreviatedMonth = 3; |
| 148 | const int countForFullMonth = 4; |
| 149 | const int countForNarrowMonth = 5; |
| 150 | Document* const document = m_editElement.document(); |
| 151 | |
| 152 | switch (fieldType) { |
| 153 | case DateTimeFormat::FieldTypeDayOfMonth: { |
| 154 | RefPtr<DateTimeFieldElement> field = DateTimeDayFieldElement::create(document, m_editElement, m_parameters.placeholderForDay, m_dayRange); |
| 155 | m_editElement.addField(field); |
| 156 | if (shouldDayOfMonthFieldDisabled()) { |
| 157 | field->setValueAsDate(m_dateValue); |
| 158 | field->setDisabled(); |
| 159 | } |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | case DateTimeFormat::FieldTypeHour11: { |
| 164 | DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerHour * 12); |
| 165 | RefPtr<DateTimeFieldElement> field = DateTimeHour11FieldElement::create(document, m_editElement, m_hour23Range, step); |
| 166 | m_editElement.addField(field); |
| 167 | if (shouldHourFieldDisabled()) { |
| 168 | field->setValueAsDate(m_dateValue); |
| 169 | field->setDisabled(); |
| 170 | } |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | case DateTimeFormat::FieldTypeHour12: { |
| 175 | DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerHour * 12); |
| 176 | RefPtr<DateTimeFieldElement> field = DateTimeHour12FieldElement::create(document, m_editElement, m_hour23Range, step); |
| 177 | m_editElement.addField(field); |
| 178 | if (shouldHourFieldDisabled()) { |
| 179 | field->setValueAsDate(m_dateValue); |
| 180 | field->setDisabled(); |
| 181 | } |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | case DateTimeFormat::FieldTypeHour23: { |
| 186 | DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerDay); |
| 187 | RefPtr<DateTimeFieldElement> field = DateTimeHour23FieldElement::create(document, m_editElement, m_hour23Range, step); |
| 188 | m_editElement.addField(field); |
| 189 | if (shouldHourFieldDisabled()) { |
| 190 | field->setValueAsDate(m_dateValue); |
| 191 | field->setDisabled(); |
| 192 | } |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | case DateTimeFormat::FieldTypeHour24: { |
| 197 | DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerDay); |
| 198 | RefPtr<DateTimeFieldElement> field = DateTimeHour24FieldElement::create(document, m_editElement, m_hour23Range, step); |
| 199 | m_editElement.addField(field); |
| 200 | if (shouldHourFieldDisabled()) { |
| 201 | field->setValueAsDate(m_dateValue); |
| 202 | field->setDisabled(); |
| 203 | } |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | case DateTimeFormat::FieldTypeMinute: { |
| 208 | DateTimeNumericFieldElement::Step step = createStep(msPerMinute, msPerHour); |
| 209 | RefPtr<DateTimeNumericFieldElement> field = DateTimeMinuteFieldElement::create(document, m_editElement, m_minuteRange, step); |
| 210 | m_editElement.addField(field); |
| 211 | if (shouldMinuteFieldDisabled()) { |
| 212 | field->setValueAsDate(m_dateValue); |
| 213 | field->setDisabled(); |
| 214 | } |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | case DateTimeFormat::FieldTypeMonth: // Fallthrough. |
| 219 | case DateTimeFormat::FieldTypeMonthStandAlone: { |
| 220 | int minMonth = 0, maxMonth = 11; |
| 221 | if (m_parameters.minimum.type() != DateComponents::Invalid |
| 222 | && m_parameters.maximum.type() != DateComponents::Invalid |
| 223 | && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() |
| 224 | && m_parameters.minimum.month() <= m_parameters.maximum.month()) { |
| 225 | minMonth = m_parameters.minimum.month(); |
| 226 | maxMonth = m_parameters.maximum.month(); |
| 227 | } |
| 228 | RefPtr<DateTimeFieldElement> field; |
| 229 | switch (count) { |
| 230 | case countForNarrowMonth: // Fallthrough. |
| 231 | case countForAbbreviatedMonth: |
| 232 | field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.shortMonthLabels() : m_parameters.locale.shortStandAloneMonthLabels(), minMonth, maxMonth); |
| 233 | break; |
| 234 | case countForFullMonth: |
| 235 | field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.monthLabels() : m_parameters.locale.standAloneMonthLabels(), minMonth, maxMonth); |
| 236 | break; |
| 237 | default: |
| 238 | field = DateTimeMonthFieldElement::create(document, m_editElement, m_parameters.placeholderForMonth, DateTimeNumericFieldElement::Range(minMonth + 1, maxMonth + 1)); |
| 239 | break; |
| 240 | } |
| 241 | m_editElement.addField(field); |
| 242 | if (minMonth == maxMonth && minMonth == m_dateValue.month() && m_dateValue.type() != DateComponents::Month) { |
| 243 | field->setValueAsDate(m_dateValue); |
| 244 | field->setDisabled(); |
| 245 | } |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | case DateTimeFormat::FieldTypePeriod: { |
| 250 | RefPtr<DateTimeFieldElement> field = DateTimeAMPMFieldElement::create(document, m_editElement, m_parameters.locale.timeAMPMLabels()); |
| 251 | m_editElement.addField(field); |
| 252 | if (shouldAMPMFieldDisabled()) { |
| 253 | field->setValueAsDate(m_dateValue); |
| 254 | field->setDisabled(); |
| 255 | } |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | case DateTimeFormat::FieldTypeSecond: { |
| 260 | DateTimeNumericFieldElement::Step step = createStep(msPerSecond, msPerMinute); |
| 261 | RefPtr<DateTimeNumericFieldElement> field = DateTimeSecondFieldElement::create(document, m_editElement, m_secondRange, step); |
| 262 | m_editElement.addField(field); |
| 263 | if (shouldSecondFieldDisabled()) { |
| 264 | field->setValueAsDate(m_dateValue); |
| 265 | field->setDisabled(); |
| 266 | } |
| 267 | |
| 268 | if (needMillisecondField()) { |
| 269 | visitLiteral(m_parameters.locale.localizedDecimalSeparator()); |
| 270 | visitField(DateTimeFormat::FieldTypeFractionalSecond, 3); |
| 271 | } |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | case DateTimeFormat::FieldTypeFractionalSecond: { |
| 276 | DateTimeNumericFieldElement::Step step = createStep(1, msPerSecond); |
| 277 | RefPtr<DateTimeNumericFieldElement> field = DateTimeMillisecondFieldElement::create(document, m_editElement, m_millisecondRange, step); |
| 278 | m_editElement.addField(field); |
| 279 | if (shouldMillisecondFieldDisabled()) { |
| 280 | field->setValueAsDate(m_dateValue); |
| 281 | field->setDisabled(); |
| 282 | } |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | case DateTimeFormat::FieldTypeWeekOfYear: { |
| 287 | DateTimeNumericFieldElement::Range range(DateComponents::minimumWeekNumber, DateComponents::maximumWeekNumber); |
| 288 | if (m_parameters.minimum.type() != DateComponents::Invalid |
| 289 | && m_parameters.maximum.type() != DateComponents::Invalid |
| 290 | && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() |
| 291 | && m_parameters.minimum.week() <= m_parameters.maximum.week()) { |
| 292 | range.minimum = m_parameters.minimum.week(); |
| 293 | range.maximum = m_parameters.maximum.week(); |
| 294 | } |
| 295 | m_editElement.addField(DateTimeWeekFieldElement::create(document, m_editElement, range)); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | case DateTimeFormat::FieldTypeYear: { |
| 300 | DateTimeYearFieldElement::Parameters yearParams; |
| 301 | if (m_parameters.minimum.type() == DateComponents::Invalid) { |
| 302 | yearParams.minimumYear = DateComponents::minimumYear(); |
| 303 | yearParams.minIsSpecified = false; |
| 304 | } else { |
| 305 | yearParams.minimumYear = m_parameters.minimum.fullYear(); |
| 306 | yearParams.minIsSpecified = true; |
| 307 | } |
| 308 | if (m_parameters.maximum.type() == DateComponents::Invalid) { |
| 309 | yearParams.maximumYear = DateComponents::maximumYear(); |
| 310 | yearParams.maxIsSpecified = false; |
| 311 | } else { |
| 312 | yearParams.maximumYear = m_parameters.maximum.fullYear(); |
| 313 | yearParams.maxIsSpecified = true; |
| 314 | } |
| 315 | if (yearParams.minimumYear > yearParams.maximumYear) { |
| 316 | std::swap(yearParams.minimumYear, yearParams.maximumYear); |
| 317 | std::swap(yearParams.minIsSpecified, yearParams.maxIsSpecified); |
| 318 | } |
| 319 | yearParams.placeholder = m_parameters.placeholderForYear; |
| 320 | RefPtr<DateTimeFieldElement> field = DateTimeYearFieldElement::create(document, m_editElement, yearParams); |
| 321 | m_editElement.addField(field); |
| 322 | if (shouldYearFieldDisabled()) { |
| 323 | field->setValueAsDate(m_dateValue); |
| 324 | field->setDisabled(); |
| 325 | } |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | default: |
| 330 | return; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | bool DateTimeEditBuilder::shouldAMPMFieldDisabled() const |
| 335 | { |
| 336 | return shouldHourFieldDisabled() |
| 337 | || (m_hour23Range.minimum < 12 && m_hour23Range.maximum < 12 && m_dateValue.hour() < 12) |
| 338 | || (m_hour23Range.minimum >= 12 && m_hour23Range.maximum >= 12 && m_dateValue.hour() >= 12); |
| 339 | } |
| 340 | |
| 341 | bool DateTimeEditBuilder::shouldDayOfMonthFieldDisabled() const |
| 342 | { |
| 343 | return m_dayRange.isSingleton() && m_dayRange.minimum == m_dateValue.monthDay() && m_dateValue.type() != DateComponents::Date; |
| 344 | } |
| 345 | |
| 346 | bool DateTimeEditBuilder::shouldHourFieldDisabled() const |
| 347 | { |
| 348 | if (m_hour23Range.isSingleton() && m_hour23Range.minimum == m_dateValue.hour() |
| 349 | && !(shouldMinuteFieldDisabled() && shouldSecondFieldDisabled() && shouldMillisecondFieldDisabled())) |
| 350 | return true; |
| 351 | |
| 352 | if (m_dateValue.type() == DateComponents::Time) |
| 353 | return false; |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 354 | ASSERT(m_dateValue.type() == DateComponents::DateTimeLocal); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 355 | |
| 356 | if (shouldDayOfMonthFieldDisabled()) { |
| 357 | ASSERT(m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear()); |
| 358 | ASSERT(m_parameters.minimum.month() == m_parameters.maximum.month()); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | const Decimal decimalMsPerDay(static_cast<int>(msPerDay)); |
| 363 | Decimal hourPartOfMinimum = (stepRange().minimum().abs().remainder(decimalMsPerDay) / static_cast<int>(msPerHour)).floor(); |
| 364 | return hourPartOfMinimum == m_dateValue.hour() && stepRange().step().remainder(decimalMsPerDay).isZero(); |
| 365 | } |
| 366 | |
| 367 | bool DateTimeEditBuilder::shouldMillisecondFieldDisabled() const |
| 368 | { |
| 369 | if (m_millisecondRange.isSingleton() && m_millisecondRange.minimum == m_dateValue.millisecond()) |
| 370 | return true; |
| 371 | |
| 372 | const Decimal decimalMsPerSecond(static_cast<int>(msPerSecond)); |
| 373 | return stepRange().minimum().abs().remainder(decimalMsPerSecond) == m_dateValue.millisecond() && stepRange().step().remainder(decimalMsPerSecond).isZero(); |
| 374 | } |
| 375 | |
| 376 | bool DateTimeEditBuilder::shouldMinuteFieldDisabled() const |
| 377 | { |
| 378 | if (m_minuteRange.isSingleton() && m_minuteRange.minimum == m_dateValue.minute()) |
| 379 | return true; |
| 380 | |
| 381 | const Decimal decimalMsPerHour(static_cast<int>(msPerHour)); |
| 382 | Decimal minutePartOfMinimum = (stepRange().minimum().abs().remainder(decimalMsPerHour) / static_cast<int>(msPerMinute)).floor(); |
| 383 | return minutePartOfMinimum == m_dateValue.minute() && stepRange().step().remainder(decimalMsPerHour).isZero(); |
| 384 | } |
| 385 | |
| 386 | bool DateTimeEditBuilder::shouldSecondFieldDisabled() const |
| 387 | { |
| 388 | if (m_secondRange.isSingleton() && m_secondRange.minimum == m_dateValue.second()) |
| 389 | return true; |
| 390 | |
| 391 | const Decimal decimalMsPerMinute(static_cast<int>(msPerMinute)); |
| 392 | Decimal secondPartOfMinimum = (stepRange().minimum().abs().remainder(decimalMsPerMinute) / static_cast<int>(msPerSecond)).floor(); |
| 393 | return secondPartOfMinimum == m_dateValue.second() && stepRange().step().remainder(decimalMsPerMinute).isZero(); |
| 394 | } |
| 395 | |
| 396 | bool DateTimeEditBuilder::shouldYearFieldDisabled() const |
| 397 | { |
| 398 | return m_parameters.minimum.type() != DateComponents::Invalid |
| 399 | && m_parameters.maximum.type() != DateComponents::Invalid |
| 400 | && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() |
| 401 | && m_parameters.minimum.fullYear() == m_dateValue.fullYear(); |
| 402 | } |
| 403 | |
| 404 | void DateTimeEditBuilder::visitLiteral(const String& text) |
| 405 | { |
| 406 | DEFINE_STATIC_LOCAL(AtomicString, textPseudoId, ("-webkit-datetime-edit-text", AtomicString::ConstructFromLiteral)); |
| 407 | ASSERT(text.length()); |
| 408 | RefPtr<HTMLDivElement> element = HTMLDivElement::create(m_editElement.document()); |
| 409 | element->setPseudo(textPseudoId); |
| 410 | if (m_parameters.locale.isRTL() && text.length()) { |
| 411 | Direction dir = direction(text[0]); |
| 412 | if (dir == SegmentSeparator || dir == WhiteSpaceNeutral || dir == OtherNeutral) |
| 413 | element->appendChild(Text::create(m_editElement.document(), String(&rightToLeftMark, 1))); |
| 414 | } |
| 415 | element->appendChild(Text::create(m_editElement.document(), text)); |
| 416 | m_editElement.fieldsWrapperElement()->appendChild(element); |
| 417 | } |
| 418 | |
| 419 | DateTimeNumericFieldElement::Step DateTimeEditBuilder::createStep(double msPerFieldUnit, double msPerFieldSize) const |
| 420 | { |
| 421 | const Decimal msPerFieldUnitDecimal(static_cast<int>(msPerFieldUnit)); |
| 422 | const Decimal msPerFieldSizeDecimal(static_cast<int>(msPerFieldSize)); |
| 423 | Decimal stepMilliseconds = stepRange().step(); |
| 424 | ASSERT(!msPerFieldUnitDecimal.isZero()); |
| 425 | ASSERT(!msPerFieldSizeDecimal.isZero()); |
| 426 | ASSERT(!stepMilliseconds.isZero()); |
| 427 | |
| 428 | DateTimeNumericFieldElement::Step step(1, 0); |
| 429 | |
| 430 | if (stepMilliseconds.remainder(msPerFieldSizeDecimal).isZero()) |
| 431 | stepMilliseconds = msPerFieldSizeDecimal; |
| 432 | |
| 433 | if (msPerFieldSizeDecimal.remainder(stepMilliseconds).isZero() && stepMilliseconds.remainder(msPerFieldUnitDecimal).isZero()) { |
| 434 | step.step = static_cast<int>((stepMilliseconds / msPerFieldUnitDecimal).toDouble()); |
| 435 | step.stepBase = static_cast<int>((stepRange().stepBase() / msPerFieldUnitDecimal).floor().remainder(msPerFieldSizeDecimal / msPerFieldUnitDecimal).toDouble()); |
| 436 | } |
| 437 | return step; |
| 438 | } |
| 439 | |
| 440 | // ---------------------------- |
| 441 | |
| 442 | DateTimeEditElement::EditControlOwner::~EditControlOwner() |
| 443 | { |
| 444 | } |
| 445 | |
| 446 | DateTimeEditElement::DateTimeEditElement(Document* document, EditControlOwner& editControlOwner) |
| 447 | : HTMLDivElement(divTag, document) |
| 448 | , m_editControlOwner(&editControlOwner) |
| 449 | { |
| 450 | DEFINE_STATIC_LOCAL(AtomicString, dateTimeEditPseudoId, ("-webkit-datetime-edit", AtomicString::ConstructFromLiteral)); |
| 451 | setPseudo(dateTimeEditPseudoId); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 452 | setAttribute(idAttr, ShadowElementNames::dateTimeEdit()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 453 | setHasCustomStyleCallbacks(); |
| 454 | } |
| 455 | |
| 456 | DateTimeEditElement::~DateTimeEditElement() |
| 457 | { |
| 458 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 459 | m_fields[fieldIndex]->removeEventHandler(); |
| 460 | } |
| 461 | |
| 462 | inline Element* DateTimeEditElement::fieldsWrapperElement() const |
| 463 | { |
| 464 | ASSERT(firstChild()); |
| 465 | return toElement(firstChild()); |
| 466 | } |
| 467 | |
| 468 | void DateTimeEditElement::addField(PassRefPtr<DateTimeFieldElement> field) |
| 469 | { |
| 470 | if (m_fields.size() == m_fields.capacity()) |
| 471 | return; |
| 472 | m_fields.append(field.get()); |
| 473 | fieldsWrapperElement()->appendChild(field); |
| 474 | } |
| 475 | |
| 476 | bool DateTimeEditElement::anyEditableFieldsHaveValues() const |
| 477 | { |
| 478 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) { |
| 479 | if (!m_fields[fieldIndex]->isDisabled() && m_fields[fieldIndex]->hasValue()) |
| 480 | return true; |
| 481 | } |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | void DateTimeEditElement::blurByOwner() |
| 486 | { |
| 487 | if (DateTimeFieldElement* field = focusedField()) |
| 488 | field->blur(); |
| 489 | } |
| 490 | |
| 491 | PassRefPtr<DateTimeEditElement> DateTimeEditElement::create(Document* document, EditControlOwner& editControlOwner) |
| 492 | { |
| 493 | RefPtr<DateTimeEditElement> container = adoptRef(new DateTimeEditElement(document, editControlOwner)); |
| 494 | return container.release(); |
| 495 | } |
| 496 | |
| 497 | PassRefPtr<RenderStyle> DateTimeEditElement::customStyleForRenderer() |
| 498 | { |
| 499 | // FIXME: This is a kind of layout. We might want to introduce new renderer. |
| 500 | FontCachePurgePreventer fontCachePurgePreventer; |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 501 | RefPtr<RenderStyle> originalStyle = originalStyleForRenderer(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 502 | RefPtr<RenderStyle> style = RenderStyle::clone(originalStyle.get()); |
| 503 | float width = 0; |
| 504 | for (Node* child = fieldsWrapperElement()->firstChild(); child; child = child->nextSibling()) { |
| 505 | if (!child->isElementNode()) |
| 506 | continue; |
| 507 | Element* childElement = toElement(child); |
| 508 | if (childElement->isDateTimeFieldElement()) { |
| 509 | // We need to pass the Font of this element because child elements |
| 510 | // can't resolve inherited style at this timing. |
| 511 | width += static_cast<DateTimeFieldElement*>(childElement)->maximumWidth(style->font()); |
| 512 | } else { |
| 513 | // ::-webkit-datetime-edit-text case. It has no |
| 514 | // border/padding/margin in html.css. |
| 515 | width += style->font().width(childElement->textContent()); |
| 516 | } |
| 517 | } |
| 518 | style->setWidth(Length(ceilf(width), Fixed)); |
| 519 | return style.release(); |
| 520 | } |
| 521 | |
| 522 | void DateTimeEditElement::didBlurFromField() |
| 523 | { |
| 524 | if (m_editControlOwner) |
| 525 | m_editControlOwner->didBlurFromControl(); |
| 526 | } |
| 527 | |
| 528 | void DateTimeEditElement::didFocusOnField() |
| 529 | { |
| 530 | if (m_editControlOwner) |
| 531 | m_editControlOwner->didFocusOnControl(); |
| 532 | } |
| 533 | |
| 534 | void DateTimeEditElement::disabledStateChanged() |
| 535 | { |
| 536 | updateUIState(); |
| 537 | } |
| 538 | |
| 539 | DateTimeFieldElement* DateTimeEditElement::fieldAt(size_t fieldIndex) const |
| 540 | { |
| 541 | return fieldIndex < m_fields.size() ? m_fields[fieldIndex] : 0; |
| 542 | } |
| 543 | |
| 544 | size_t DateTimeEditElement::fieldIndexOf(const DateTimeFieldElement& field) const |
| 545 | { |
| 546 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) { |
| 547 | if (m_fields[fieldIndex] == &field) |
| 548 | return fieldIndex; |
| 549 | } |
| 550 | return invalidFieldIndex; |
| 551 | } |
| 552 | |
| 553 | void DateTimeEditElement::focusIfNoFocus() |
| 554 | { |
| 555 | if (focusedFieldIndex() != invalidFieldIndex) |
| 556 | return; |
| 557 | focusOnNextFocusableField(0); |
| 558 | } |
| 559 | |
| 560 | void DateTimeEditElement::focusByOwner(Node* oldFocusedNode) |
| 561 | { |
| 562 | if (oldFocusedNode && oldFocusedNode->isElementNode() && toElement(oldFocusedNode)->isDateTimeFieldElement()) { |
| 563 | DateTimeFieldElement* oldFocusedField = static_cast<DateTimeFieldElement*>(oldFocusedNode); |
| 564 | size_t index = fieldIndexOf(*oldFocusedField); |
| 565 | if (index != invalidFieldIndex && oldFocusedField->isFocusable()) { |
| 566 | oldFocusedField->focus(); |
| 567 | return; |
| 568 | } |
| 569 | } |
| 570 | focusOnNextFocusableField(0); |
| 571 | } |
| 572 | |
| 573 | DateTimeFieldElement* DateTimeEditElement::focusedField() const |
| 574 | { |
| 575 | return fieldAt(focusedFieldIndex()); |
| 576 | } |
| 577 | |
| 578 | size_t DateTimeEditElement::focusedFieldIndex() const |
| 579 | { |
| 580 | Node* const focusedFieldNode = document()->focusedNode(); |
| 581 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) { |
| 582 | if (m_fields[fieldIndex] == focusedFieldNode) |
| 583 | return fieldIndex; |
| 584 | } |
| 585 | return invalidFieldIndex; |
| 586 | } |
| 587 | |
| 588 | void DateTimeEditElement::fieldValueChanged() |
| 589 | { |
| 590 | if (m_editControlOwner) |
| 591 | m_editControlOwner->editControlValueChanged(); |
| 592 | } |
| 593 | |
| 594 | bool DateTimeEditElement::focusOnNextFocusableField(size_t startIndex) |
| 595 | { |
| 596 | for (size_t fieldIndex = startIndex; fieldIndex < m_fields.size(); ++fieldIndex) { |
| 597 | if (m_fields[fieldIndex]->isFocusable()) { |
| 598 | m_fields[fieldIndex]->focus(); |
| 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | bool DateTimeEditElement::focusOnNextField(const DateTimeFieldElement& field) |
| 606 | { |
| 607 | const size_t startFieldIndex = fieldIndexOf(field); |
| 608 | if (startFieldIndex == invalidFieldIndex) |
| 609 | return false; |
| 610 | return focusOnNextFocusableField(startFieldIndex + 1); |
| 611 | } |
| 612 | |
| 613 | bool DateTimeEditElement::focusOnPreviousField(const DateTimeFieldElement& field) |
| 614 | { |
| 615 | const size_t startFieldIndex = fieldIndexOf(field); |
| 616 | if (startFieldIndex == invalidFieldIndex) |
| 617 | return false; |
| 618 | size_t fieldIndex = startFieldIndex; |
| 619 | while (fieldIndex > 0) { |
| 620 | --fieldIndex; |
| 621 | if (m_fields[fieldIndex]->isFocusable()) { |
| 622 | m_fields[fieldIndex]->focus(); |
| 623 | return true; |
| 624 | } |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 629 | bool DateTimeEditElement::isDateTimeEditElement() const |
| 630 | { |
| 631 | return true; |
| 632 | } |
| 633 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 634 | bool DateTimeEditElement::isDisabled() const |
| 635 | { |
| 636 | return m_editControlOwner && m_editControlOwner->isEditControlOwnerDisabled(); |
| 637 | } |
| 638 | |
| 639 | bool DateTimeEditElement::isFieldOwnerDisabled() const |
| 640 | { |
| 641 | return isDisabled(); |
| 642 | } |
| 643 | |
| 644 | bool DateTimeEditElement::isFieldOwnerReadOnly() const |
| 645 | { |
| 646 | return isReadOnly(); |
| 647 | } |
| 648 | |
| 649 | bool DateTimeEditElement::isReadOnly() const |
| 650 | { |
| 651 | return m_editControlOwner && m_editControlOwner->isEditControlOwnerReadOnly(); |
| 652 | } |
| 653 | |
| 654 | void DateTimeEditElement::layout(const LayoutParameters& layoutParameters, const DateComponents& dateValue) |
| 655 | { |
| 656 | DEFINE_STATIC_LOCAL(AtomicString, fieldsWrapperPseudoId, ("-webkit-datetime-edit-fields-wrapper", AtomicString::ConstructFromLiteral)); |
| 657 | if (!firstChild()) { |
| 658 | RefPtr<HTMLDivElement> element = HTMLDivElement::create(document()); |
| 659 | element->setPseudo(fieldsWrapperPseudoId); |
| 660 | appendChild(element.get()); |
| 661 | } |
| 662 | Element* fieldsWrapper = fieldsWrapperElement(); |
| 663 | |
| 664 | size_t focusedFieldIndex = this->focusedFieldIndex(); |
| 665 | DateTimeFieldElement* const focusedField = fieldAt(focusedFieldIndex); |
| 666 | const AtomicString focusedFieldId = focusedField ? focusedField->shadowPseudoId() : nullAtom; |
| 667 | |
| 668 | DateTimeEditBuilder builder(*this, layoutParameters, dateValue); |
| 669 | Node* lastChildToBeRemoved = fieldsWrapper->lastChild(); |
| 670 | if (!builder.build(layoutParameters.dateTimeFormat) || m_fields.isEmpty()) { |
| 671 | lastChildToBeRemoved = fieldsWrapper->lastChild(); |
| 672 | builder.build(layoutParameters.fallbackDateTimeFormat); |
| 673 | } |
| 674 | |
| 675 | if (focusedFieldIndex != invalidFieldIndex) { |
| 676 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) { |
| 677 | if (m_fields[fieldIndex]->shadowPseudoId() == focusedFieldId) { |
| 678 | focusedFieldIndex = fieldIndex; |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | if (DateTimeFieldElement* field = fieldAt(std::min(focusedFieldIndex, m_fields.size() - 1))) |
| 683 | field->focus(); |
| 684 | } |
| 685 | |
| 686 | if (lastChildToBeRemoved) { |
| 687 | for (Node* childNode = fieldsWrapper->firstChild(); childNode; childNode = fieldsWrapper->firstChild()) { |
| 688 | fieldsWrapper->removeChild(childNode); |
| 689 | if (childNode == lastChildToBeRemoved) |
| 690 | break; |
| 691 | } |
| 692 | setNeedsStyleRecalc(); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | AtomicString DateTimeEditElement::localeIdentifier() const |
| 697 | { |
| 698 | return m_editControlOwner ? m_editControlOwner->localeIdentifier() : nullAtom; |
| 699 | } |
| 700 | |
| 701 | void DateTimeEditElement::readOnlyStateChanged() |
| 702 | { |
| 703 | updateUIState(); |
| 704 | } |
| 705 | |
| 706 | void DateTimeEditElement::resetFields() |
| 707 | { |
| 708 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 709 | m_fields[fieldIndex]->removeEventHandler(); |
| 710 | m_fields.shrink(0); |
| 711 | } |
| 712 | |
| 713 | void DateTimeEditElement::defaultEventHandler(Event* event) |
| 714 | { |
| 715 | // In case of control owner forward event to control, e.g. DOM |
| 716 | // dispatchEvent method. |
| 717 | if (DateTimeFieldElement* field = focusedField()) { |
| 718 | field->defaultEventHandler(event); |
| 719 | if (event->defaultHandled()) |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | HTMLDivElement::defaultEventHandler(event); |
| 724 | } |
| 725 | |
| 726 | void DateTimeEditElement::setValueAsDate(const LayoutParameters& layoutParameters, const DateComponents& date) |
| 727 | { |
| 728 | layout(layoutParameters, date); |
| 729 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 730 | m_fields[fieldIndex]->setValueAsDate(date); |
| 731 | } |
| 732 | |
| 733 | void DateTimeEditElement::setValueAsDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) |
| 734 | { |
| 735 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 736 | m_fields[fieldIndex]->setValueAsDateTimeFieldsState(dateTimeFieldsState); |
| 737 | } |
| 738 | |
| 739 | void DateTimeEditElement::setEmptyValue(const LayoutParameters& layoutParameters, const DateComponents& dateForReadOnlyField) |
| 740 | { |
| 741 | layout(layoutParameters, dateForReadOnlyField); |
| 742 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 743 | m_fields[fieldIndex]->setEmptyValue(DateTimeFieldElement::DispatchNoEvent); |
| 744 | } |
| 745 | |
| 746 | bool DateTimeEditElement::hasFocusedField() |
| 747 | { |
| 748 | return focusedFieldIndex() != invalidFieldIndex; |
| 749 | } |
| 750 | |
| 751 | void DateTimeEditElement::setOnlyYearMonthDay(const DateComponents& date) |
| 752 | { |
| 753 | ASSERT(date.type() == DateComponents::Date); |
| 754 | |
| 755 | if (!m_editControlOwner) |
| 756 | return; |
| 757 | |
| 758 | DateTimeFieldsState dateTimeFieldsState = valueAsDateTimeFieldsState(); |
| 759 | dateTimeFieldsState.setYear(date.fullYear()); |
| 760 | dateTimeFieldsState.setMonth(date.month() + 1); |
| 761 | dateTimeFieldsState.setDayOfMonth(date.monthDay()); |
| 762 | setValueAsDateTimeFieldsState(dateTimeFieldsState); |
| 763 | m_editControlOwner->editControlValueChanged(); |
| 764 | } |
| 765 | |
| 766 | void DateTimeEditElement::stepDown() |
| 767 | { |
| 768 | if (DateTimeFieldElement* const field = focusedField()) |
| 769 | field->stepDown(); |
| 770 | } |
| 771 | |
| 772 | void DateTimeEditElement::stepUp() |
| 773 | { |
| 774 | if (DateTimeFieldElement* const field = focusedField()) |
| 775 | field->stepUp(); |
| 776 | } |
| 777 | |
| 778 | void DateTimeEditElement::updateUIState() |
| 779 | { |
| 780 | if (isDisabled()) { |
| 781 | if (DateTimeFieldElement* field = focusedField()) |
| 782 | field->blur(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | String DateTimeEditElement::value() const |
| 787 | { |
| 788 | if (!m_editControlOwner) |
| 789 | return emptyString(); |
| 790 | return m_editControlOwner->formatDateTimeFieldsState(valueAsDateTimeFieldsState()); |
| 791 | } |
| 792 | |
| 793 | DateTimeFieldsState DateTimeEditElement::valueAsDateTimeFieldsState() const |
| 794 | { |
| 795 | DateTimeFieldsState dateTimeFieldsState; |
| 796 | for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 797 | m_fields[fieldIndex]->populateDateTimeFieldsState(dateTimeFieldsState); |
| 798 | return dateTimeFieldsState; |
| 799 | } |
| 800 | |
| 801 | } // namespace WebCore |
| 802 | |
| 803 | #endif |