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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/DateTimeInputTypes.h"
#include "js/Date.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/ShadowRoot.h"
#include "nsDOMTokenList.h"
namespace mozilla::dom {
const double DateTimeInputTypeBase::kMinimumYear = 1;
const double DateTimeInputTypeBase::kMaximumYear = 275760;
const double DateTimeInputTypeBase::kMaximumMonthInMaximumYear = 9;
const double DateTimeInputTypeBase::kMaximumWeekInMaximumYear = 37;
const double DateTimeInputTypeBase::kMsPerDay = 24 * 60 * 60 * 1000;
bool DateTimeInputTypeBase::IsMutable() const {
return !mInputElement->IsDisabledOrReadOnly();
}
bool DateTimeInputTypeBase::IsValueMissing() const {
if (!mInputElement->IsRequired()) {
return false;
}
if (!IsMutable()) {
return false;
}
return IsValueEmpty();
}
bool DateTimeInputTypeBase::IsRangeOverflow() const {
Decimal maximum = mInputElement->GetMaximum();
if (maximum.isNaN()) {
return false;
}
Decimal value = mInputElement->GetValueAsDecimal();
if (value.isNaN()) {
return false;
}
return value > maximum;
}
bool DateTimeInputTypeBase::IsRangeUnderflow() const {
Decimal minimum = mInputElement->GetMinimum();
if (minimum.isNaN()) {
return false;
}
Decimal value = mInputElement->GetValueAsDecimal();
if (value.isNaN()) {
return false;
}
return value < minimum;
}
bool DateTimeInputTypeBase::HasStepMismatch() const {
Decimal value = mInputElement->GetValueAsDecimal();
return mInputElement->ValueIsStepMismatch(value);
}
bool DateTimeInputTypeBase::HasBadInput() const {
ShadowRoot* shadow = mInputElement->GetShadowRoot();
if (!shadow) {
return false;
}
Element* editWrapperElement = shadow->GetElementById(u"edit-wrapper"_ns);
if (!editWrapperElement) {
return false;
}
bool allEmpty = true;
// Empty field does not imply bad input, but incomplete field does.
for (Element* child = editWrapperElement->GetFirstElementChild(); child;
child = child->GetNextElementSibling()) {
if (!child->ClassList()->Contains(u"datetime-edit-field"_ns)) {
continue;
}
nsAutoString value;
child->GetAttr(nsGkAtoms::value, value);
if (!value.IsEmpty()) {
allEmpty = false;
break;
}
}
// If some fields are available but input element's value is empty implies it
// has been sanitized.
return !allEmpty && IsValueEmpty();
}
nsresult DateTimeInputTypeBase::GetRangeOverflowMessage(nsAString& aMessage) {
nsAutoString maxStr;
mInputElement->GetAttr(nsGkAtoms::max, maxStr);
return nsContentUtils::FormatMaybeLocalizedString(
aMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationDateTimeRangeOverflow", mInputElement->OwnerDoc(), maxStr);
}
nsresult DateTimeInputTypeBase::GetRangeUnderflowMessage(nsAString& aMessage) {
nsAutoString minStr;
mInputElement->GetAttr(nsGkAtoms::min, minStr);
return nsContentUtils::FormatMaybeLocalizedString(
aMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationDateTimeRangeUnderflow", mInputElement->OwnerDoc(),
minStr);
}
void DateTimeInputTypeBase::MinMaxStepAttrChanged() {
if (Element* dateTimeBoxElement = mInputElement->GetDateTimeBoxElement()) {
AsyncEventDispatcher::RunDOMEventWhenSafe(
*dateTimeBoxElement, u"MozNotifyMinMaxStepAttrChanged"_ns,
CanBubble::eNo, ChromeOnlyDispatch::eNo);
}
}
bool DateTimeInputTypeBase::GetTimeFromMs(double aValue, uint16_t* aHours,
uint16_t* aMinutes,
uint16_t* aSeconds,
uint16_t* aMilliseconds) const {
MOZ_ASSERT(aValue >= 0 && aValue < kMsPerDay,
"aValue must be milliseconds within a day!");
uint32_t value = floor(aValue);
*aMilliseconds = value % 1000;
value /= 1000;
*aSeconds = value % 60;
value /= 60;
*aMinutes = value % 60;
value /= 60;
*aHours = value;
return true;
}
// input type=date
nsresult DateInputType::GetBadInputMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationInvalidDate",
mInputElement->OwnerDoc(), aMessage);
}
auto DateInputType::ConvertStringToNumber(const nsAString& aValue) const
-> StringToNumberResult {
uint32_t year, month, day;
if (!ParseDate(aValue, &year, &month, &day)) {
return {};
}
JS::ClippedTime time = JS::TimeClip(JS::MakeDate(year, month - 1, day));
if (!time.isValid()) {
return {};
}
return {Decimal::fromDouble(time.toDouble())};
}
bool DateInputType::ConvertNumberToString(Decimal aValue, Localized,
nsAString& aResultString) const {
MOZ_ASSERT(aValue.isFinite(), "aValue must be a valid non-Infinite number.");
aResultString.Truncate();
// The specs (and our JS APIs) require |aValue| to be truncated.
aValue = aValue.floor();
double year = JS::YearFromTime(aValue.toDouble());
double month = JS::MonthFromTime(aValue.toDouble());
double day = JS::DayFromTime(aValue.toDouble());
if (std::isnan(year) || std::isnan(month) || std::isnan(day)) {
return false;
}
aResultString.AppendPrintf("%04.0f-%02.0f-%02.0f", year, month + 1, day);
return true;
}
// input type=time
nsresult TimeInputType::GetBadInputMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationInvalidTime",
mInputElement->OwnerDoc(), aMessage);
}
auto TimeInputType::ConvertStringToNumber(const nsAString& aValue) const
-> StringToNumberResult {
uint32_t milliseconds;
if (!ParseTime(aValue, &milliseconds)) {
return {};
}
return {Decimal(int32_t(milliseconds))};
}
bool TimeInputType::ConvertNumberToString(Decimal aValue, Localized,
nsAString& aResultString) const {
MOZ_ASSERT(aValue.isFinite(), "aValue must be a valid non-Infinite number.");
aResultString.Truncate();
aValue = aValue.floor();
// Per spec, we need to truncate |aValue| and we should only represent
// times inside a day [00:00, 24:00[, which means that we should do a
// modulo on |aValue| using the number of milliseconds in a day (86400000).
uint32_t value =
NS_floorModulo(aValue, Decimal::fromDouble(kMsPerDay)).toDouble();
uint16_t milliseconds, seconds, minutes, hours;
if (!GetTimeFromMs(value, &hours, &minutes, &seconds, &milliseconds)) {
return false;
}
if (milliseconds != 0) {
aResultString.AppendPrintf("%02d:%02d:%02d.%03d", hours, minutes, seconds,
milliseconds);
} else if (seconds != 0) {
aResultString.AppendPrintf("%02d:%02d:%02d", hours, minutes, seconds);
} else {
aResultString.AppendPrintf("%02d:%02d", hours, minutes);
}
return true;
}
bool TimeInputType::HasReversedRange() const {
mozilla::Decimal maximum = mInputElement->GetMaximum();
if (maximum.isNaN()) {
return false;
}
mozilla::Decimal minimum = mInputElement->GetMinimum();
if (minimum.isNaN()) {
return false;
}
return maximum < minimum;
}
bool TimeInputType::IsReversedRangeUnderflowAndOverflow() const {
mozilla::Decimal maximum = mInputElement->GetMaximum();
mozilla::Decimal minimum = mInputElement->GetMinimum();
mozilla::Decimal value = mInputElement->GetValueAsDecimal();
MOZ_ASSERT(HasReversedRange(), "Must have reserved range.");
if (value.isNaN()) {
return false;
}
// When an element has a reversed range, and the value is more than the
// maximum and less than the minimum the element is simultaneously suffering
// from an underflow and suffering from an overflow.
return value > maximum && value < minimum;
}
bool TimeInputType::IsRangeOverflow() const {
return HasReversedRange() ? IsReversedRangeUnderflowAndOverflow()
: DateTimeInputTypeBase::IsRangeOverflow();
}
bool TimeInputType::IsRangeUnderflow() const {
return HasReversedRange() ? IsReversedRangeUnderflowAndOverflow()
: DateTimeInputTypeBase::IsRangeUnderflow();
}
nsresult TimeInputType::GetReversedRangeUnderflowAndOverflowMessage(
nsAString& aMessage) {
nsAutoString maxStr;
mInputElement->GetAttr(nsGkAtoms::max, maxStr);
nsAutoString minStr;
mInputElement->GetAttr(nsGkAtoms::min, minStr);
return nsContentUtils::FormatMaybeLocalizedString(
aMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationTimeReversedRangeUnderflowAndOverflow",
mInputElement->OwnerDoc(), minStr, maxStr);
}
nsresult TimeInputType::GetRangeOverflowMessage(nsAString& aMessage) {
return HasReversedRange()
? GetReversedRangeUnderflowAndOverflowMessage(aMessage)
: DateTimeInputTypeBase::GetRangeOverflowMessage(aMessage);
}
nsresult TimeInputType::GetRangeUnderflowMessage(nsAString& aMessage) {
return HasReversedRange()
? GetReversedRangeUnderflowAndOverflowMessage(aMessage)
: DateTimeInputTypeBase::GetRangeUnderflowMessage(aMessage);
}
// input type=week
nsresult WeekInputType::GetBadInputMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationInvalidWeek",
mInputElement->OwnerDoc(), aMessage);
}
auto WeekInputType::ConvertStringToNumber(const nsAString& aValue) const
-> StringToNumberResult {
uint32_t year, week;
if (!ParseWeek(aValue, &year, &week)) {
return {};
}
if (year < kMinimumYear || year > kMaximumYear) {
return {};
}
// Maximum week is 275760-W37, the week of 275760-09-13.
if (year == kMaximumYear && week > kMaximumWeekInMaximumYear) {
return {};
}
double days = DaysSinceEpochFromWeek(year, week);
return {Decimal::fromDouble(days * kMsPerDay)};
}
bool WeekInputType::ConvertNumberToString(Decimal aValue, Localized,
nsAString& aResultString) const {
MOZ_ASSERT(aValue.isFinite(), "aValue must be a valid non-Infinite number.");
aResultString.Truncate();
aValue = aValue.floor();
// Based on ISO 8601 date.
double year = JS::YearFromTime(aValue.toDouble());
double month = JS::MonthFromTime(aValue.toDouble());
double day = JS::DayFromTime(aValue.toDouble());
// Adding 1 since day starts from 0.
double dayInYear = JS::DayWithinYear(aValue.toDouble(), year) + 1;
// Return if aValue is outside the valid JS date-time range.
if (std::isnan(year) || std::isnan(month) || std::isnan(day) ||
std::isnan(dayInYear)) {
return false;
}
// DayOfWeek requires the year to be non-negative.
if (year < 0) {
return false;
}
// Adding 1 since month starts from 0.
uint32_t isoWeekday = DayOfWeek(year, month + 1, day, true);
// Target on Wednesday since ISO 8601 states that week 1 is the week
// with the first Thursday of that year.
uint32_t week = (dayInYear - isoWeekday + 10) / 7;
if (week < 1) {
year--;
if (year < 1) {
return false;
}
week = MaximumWeekInYear(year);
} else if (week > MaximumWeekInYear(year)) {
year++;
if (year > kMaximumYear ||
(year == kMaximumYear && week > kMaximumWeekInMaximumYear)) {
return false;
}
week = 1;
}
aResultString.AppendPrintf("%04.0f-W%02d", year, week);
return true;
}
// input type=month
nsresult MonthInputType::GetBadInputMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationInvalidMonth",
mInputElement->OwnerDoc(), aMessage);
}
auto MonthInputType::ConvertStringToNumber(const nsAString& aValue) const
-> StringToNumberResult {
uint32_t year, month;
if (!ParseMonth(aValue, &year, &month)) {
return {};
}
if (year < kMinimumYear || year > kMaximumYear) {
return {};
}
// Maximum valid month is 275760-09.
if (year == kMaximumYear && month > kMaximumMonthInMaximumYear) {
return {};
}
int32_t months = MonthsSinceJan1970(year, month);
return {Decimal(int32_t(months))};
}
bool MonthInputType::ConvertNumberToString(Decimal aValue, Localized,
nsAString& aResultString) const {
MOZ_ASSERT(aValue.isFinite(), "aValue must be a valid non-Infinite number.");
aResultString.Truncate();
aValue = aValue.floor();
double month = NS_floorModulo(aValue, Decimal(12)).toDouble();
month = (month < 0 ? month + 12 : month);
double year = 1970 + (aValue.toDouble() - month) / 12;
// Maximum valid month is 275760-09.
if (year < kMinimumYear || year > kMaximumYear) {
return false;
}
if (year == kMaximumYear && month > 8) {
return false;
}
aResultString.AppendPrintf("%04.0f-%02.0f", year, month + 1);
return true;
}
// input type=datetime-local
nsresult DateTimeLocalInputType::GetBadInputMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationInvalidDateTime",
mInputElement->OwnerDoc(), aMessage);
}
auto DateTimeLocalInputType::ConvertStringToNumber(
const nsAString& aValue) const -> StringToNumberResult {
uint32_t year, month, day, timeInMs;
if (!ParseDateTimeLocal(aValue, &year, &month, &day, &timeInMs)) {
return {};
}
JS::ClippedTime time =
JS::TimeClip(JS::MakeDate(year, month - 1, day, timeInMs));
if (!time.isValid()) {
return {};
}
return {Decimal::fromDouble(time.toDouble())};
}
bool DateTimeLocalInputType::ConvertNumberToString(
Decimal aValue, Localized, nsAString& aResultString) const {
MOZ_ASSERT(aValue.isFinite(), "aValue must be a valid non-Infinite number.");
aResultString.Truncate();
aValue = aValue.floor();
uint32_t timeValue =
NS_floorModulo(aValue, Decimal::fromDouble(kMsPerDay)).toDouble();
uint16_t milliseconds, seconds, minutes, hours;
if (!GetTimeFromMs(timeValue, &hours, &minutes, &seconds, &milliseconds)) {
return false;
}
double year = JS::YearFromTime(aValue.toDouble());
double month = JS::MonthFromTime(aValue.toDouble());
double day = JS::DayFromTime(aValue.toDouble());
if (std::isnan(year) || std::isnan(month) || std::isnan(day)) {
return false;
}
if (milliseconds != 0) {
aResultString.AppendPrintf("%04.0f-%02.0f-%02.0fT%02d:%02d:%02d.%03d", year,
month + 1, day, hours, minutes, seconds,
milliseconds);
} else if (seconds != 0) {
aResultString.AppendPrintf("%04.0f-%02.0f-%02.0fT%02d:%02d:%02d", year,
month + 1, day, hours, minutes, seconds);
} else {
aResultString.AppendPrintf("%04.0f-%02.0f-%02.0fT%02d:%02d", year,
month + 1, day, hours, minutes);
}
return true;
}
} // namespace mozilla::dom
|