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
|
/* -*- 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/InputType.h"
#include "mozilla/Assertions.h"
#include "mozilla/Likely.h"
#include "mozilla/dom/ButtonInputTypes.h"
#include "mozilla/dom/CheckableInputTypes.h"
#include "mozilla/dom/ColorInputType.h"
#include "mozilla/dom/DateTimeInputTypes.h"
#include "mozilla/dom/FileInputType.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/HiddenInputType.h"
#include "mozilla/dom/NumericInputTypes.h"
#include "mozilla/dom/SingleLineTextInputTypes.h"
#include "nsContentUtils.h"
#include "nsIFormControl.h"
using namespace mozilla;
using namespace mozilla::dom;
constexpr Decimal InputType::kStepAny;
/* static */ UniquePtr<InputType, InputType::DoNotDelete> InputType::Create(
HTMLInputElement* aInputElement, FormControlType aType, void* aMemory) {
UniquePtr<InputType, InputType::DoNotDelete> inputType;
switch (aType) {
// Single line text
case FormControlType::InputText:
inputType.reset(TextInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputTel:
inputType.reset(TelInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputEmail:
inputType.reset(EmailInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputSearch:
inputType.reset(SearchInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputPassword:
inputType.reset(PasswordInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputUrl:
inputType.reset(URLInputType::Create(aInputElement, aMemory));
break;
// Button
case FormControlType::InputButton:
inputType.reset(ButtonInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputSubmit:
inputType.reset(SubmitInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputImage:
inputType.reset(ImageInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputReset:
inputType.reset(ResetInputType::Create(aInputElement, aMemory));
break;
// Checkable
case FormControlType::InputCheckbox:
inputType.reset(CheckboxInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputRadio:
inputType.reset(RadioInputType::Create(aInputElement, aMemory));
break;
// Numeric
case FormControlType::InputNumber:
inputType.reset(NumberInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputRange:
inputType.reset(RangeInputType::Create(aInputElement, aMemory));
break;
// DateTime
case FormControlType::InputDate:
inputType.reset(DateInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputTime:
inputType.reset(TimeInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputMonth:
inputType.reset(MonthInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputWeek:
inputType.reset(WeekInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputDatetimeLocal:
inputType.reset(DateTimeLocalInputType::Create(aInputElement, aMemory));
break;
// Others
case FormControlType::InputColor:
inputType.reset(ColorInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputFile:
inputType.reset(FileInputType::Create(aInputElement, aMemory));
break;
case FormControlType::InputHidden:
inputType.reset(HiddenInputType::Create(aInputElement, aMemory));
break;
default:
inputType.reset(TextInputType::Create(aInputElement, aMemory));
}
return inputType;
}
bool InputType::IsMutable() const { return !mInputElement->IsDisabled(); }
bool InputType::IsValueEmpty() const { return mInputElement->IsValueEmpty(); }
void InputType::GetNonFileValueInternal(nsAString& aValue) const {
return mInputElement->GetNonFileValueInternal(aValue);
}
nsresult InputType::SetValueInternal(const nsAString& aValue,
const ValueSetterOptions& aOptions) {
RefPtr<HTMLInputElement> inputElement(mInputElement);
return inputElement->SetValueInternal(aValue, aOptions);
}
nsIFrame* InputType::GetPrimaryFrame() const {
return mInputElement->GetPrimaryFrame();
}
void InputType::DropReference() {
// Drop our (non ref-counted) reference.
mInputElement = nullptr;
}
bool InputType::IsTooLong() const { return false; }
bool InputType::IsTooShort() const { return false; }
bool InputType::IsValueMissing() const { return false; }
bool InputType::HasTypeMismatch() const { return false; }
Maybe<bool> InputType::HasPatternMismatch() const { return Some(false); }
bool InputType::IsRangeOverflow() const { return false; }
bool InputType::IsRangeUnderflow() const { return false; }
bool InputType::HasStepMismatch() const { return false; }
bool InputType::HasBadInput() const { return false; }
nsresult InputType::GetValidationMessage(
nsAString& aValidationMessage,
nsIConstraintValidation::ValidityStateType aType) {
aValidationMessage.Truncate();
switch (aType) {
case nsIConstraintValidation::VALIDITY_STATE_TOO_LONG: {
int32_t maxLength = mInputElement->MaxLength();
int32_t textLength = mInputElement->InputTextLength(CallerType::System);
nsAutoString strMaxLength;
nsAutoString strTextLength;
strMaxLength.AppendInt(maxLength);
strTextLength.AppendInt(textLength);
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationTextTooLong", mInputElement->OwnerDoc(), strMaxLength,
strTextLength);
}
case nsIConstraintValidation::VALIDITY_STATE_TOO_SHORT: {
int32_t minLength = mInputElement->MinLength();
int32_t textLength = mInputElement->InputTextLength(CallerType::System);
nsAutoString strMinLength;
nsAutoString strTextLength;
strMinLength.AppendInt(minLength);
strTextLength.AppendInt(textLength);
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationTextTooShort", mInputElement->OwnerDoc(), strMinLength,
strTextLength);
}
case nsIConstraintValidation::VALIDITY_STATE_VALUE_MISSING:
return GetValueMissingMessage(aValidationMessage);
case nsIConstraintValidation::VALIDITY_STATE_TYPE_MISMATCH: {
return GetTypeMismatchMessage(aValidationMessage);
}
case nsIConstraintValidation::VALIDITY_STATE_PATTERN_MISMATCH: {
nsAutoString title;
mInputElement->GetAttr(nsGkAtoms::title, title);
if (title.IsEmpty()) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationPatternMismatch",
mInputElement->OwnerDoc(), aValidationMessage);
}
if (title.Length() >
nsIConstraintValidation::sContentSpecifiedMaxLengthMessage) {
title.Truncate(
nsIConstraintValidation::sContentSpecifiedMaxLengthMessage);
}
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationPatternMismatchWithTitle", mInputElement->OwnerDoc(),
title);
}
case nsIConstraintValidation::VALIDITY_STATE_RANGE_OVERFLOW:
return GetRangeOverflowMessage(aValidationMessage);
case nsIConstraintValidation::VALIDITY_STATE_RANGE_UNDERFLOW:
return GetRangeUnderflowMessage(aValidationMessage);
case nsIConstraintValidation::VALIDITY_STATE_STEP_MISMATCH: {
Decimal value = mInputElement->GetValueAsDecimal();
if (MOZ_UNLIKELY(NS_WARN_IF(value.isNaN()))) {
// TODO(bug 1651070): This should ideally never happen, but we don't
// deal with lang changes correctly, so it could.
return GetBadInputMessage(aValidationMessage);
}
Decimal step = mInputElement->GetStep();
MOZ_ASSERT(step != kStepAny && step > Decimal(0));
Decimal stepBase = mInputElement->GetStepBase();
Decimal valueLow = value - NS_floorModulo(value - stepBase, step);
Decimal valueHigh = value + step - NS_floorModulo(value - stepBase, step);
Decimal maximum = mInputElement->GetMaximum();
if (maximum.isNaN() || valueHigh <= maximum) {
nsAutoString valueLowStr, valueHighStr;
ConvertNumberToString(valueLow, Localized::Yes, valueLowStr);
ConvertNumberToString(valueHigh, Localized::Yes, valueHighStr);
if (valueLowStr.Equals(valueHighStr)) {
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationStepMismatchOneValue", mInputElement->OwnerDoc(),
valueLowStr);
}
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationStepMismatch", mInputElement->OwnerDoc(),
valueLowStr, valueHighStr);
}
nsAutoString valueLowStr;
ConvertNumberToString(valueLow, Localized::Yes, valueLowStr);
return nsContentUtils::FormatMaybeLocalizedString(
aValidationMessage, nsContentUtils::eDOM_PROPERTIES,
"FormValidationStepMismatchOneValue", mInputElement->OwnerDoc(),
valueLowStr);
}
case nsIConstraintValidation::VALIDITY_STATE_BAD_INPUT:
return GetBadInputMessage(aValidationMessage);
default:
MOZ_ASSERT_UNREACHABLE("Unknown validity state");
return NS_ERROR_UNEXPECTED;
}
}
nsresult InputType::GetValueMissingMessage(nsAString& aMessage) {
return nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationValueMissing",
mInputElement->OwnerDoc(), aMessage);
}
nsresult InputType::GetTypeMismatchMessage(nsAString& aMessage) {
return NS_ERROR_UNEXPECTED;
}
nsresult InputType::GetRangeOverflowMessage(nsAString& aMessage) {
return NS_ERROR_UNEXPECTED;
}
nsresult InputType::GetRangeUnderflowMessage(nsAString& aMessage) {
return NS_ERROR_UNEXPECTED;
}
nsresult InputType::GetBadInputMessage(nsAString& aMessage) {
return NS_ERROR_UNEXPECTED;
}
auto InputType::ConvertStringToNumber(const nsAString& aValue) const
-> StringToNumberResult {
NS_WARNING("InputType::ConvertStringToNumber called");
return {};
}
bool InputType::ConvertNumberToString(Decimal, Localized, nsAString&) const {
NS_WARNING("InputType::ConvertNumberToString called");
return false;
}
bool InputType::ParseDate(const nsAString& aValue, uint32_t* aYear,
uint32_t* aMonth, uint32_t* aDay) const {
// TODO: move this function and implementation to DateTimeInpuTypeBase when
// refactoring is completed. Now we can only call HTMLInputElement::ParseDate
// from here, since the method is protected and only InputType is a friend
// class.
return mInputElement->ParseDate(aValue, aYear, aMonth, aDay);
}
bool InputType::ParseTime(const nsAString& aValue, uint32_t* aResult) const {
// see comment in InputType::ParseDate().
return HTMLInputElement::ParseTime(aValue, aResult);
}
bool InputType::ParseMonth(const nsAString& aValue, uint32_t* aYear,
uint32_t* aMonth) const {
// see comment in InputType::ParseDate().
return mInputElement->ParseMonth(aValue, aYear, aMonth);
}
bool InputType::ParseWeek(const nsAString& aValue, uint32_t* aYear,
uint32_t* aWeek) const {
// see comment in InputType::ParseDate().
return mInputElement->ParseWeek(aValue, aYear, aWeek);
}
bool InputType::ParseDateTimeLocal(const nsAString& aValue, uint32_t* aYear,
uint32_t* aMonth, uint32_t* aDay,
uint32_t* aTime) const {
// see comment in InputType::ParseDate().
return mInputElement->ParseDateTimeLocal(aValue, aYear, aMonth, aDay, aTime);
}
int32_t InputType::MonthsSinceJan1970(uint32_t aYear, uint32_t aMonth) const {
// see comment in InputType::ParseDate().
return mInputElement->MonthsSinceJan1970(aYear, aMonth);
}
double InputType::DaysSinceEpochFromWeek(uint32_t aYear, uint32_t aWeek) const {
// see comment in InputType::ParseDate().
return mInputElement->DaysSinceEpochFromWeek(aYear, aWeek);
}
uint32_t InputType::DayOfWeek(uint32_t aYear, uint32_t aMonth, uint32_t aDay,
bool isoWeek) const {
// see comment in InputType::ParseDate().
return mInputElement->DayOfWeek(aYear, aMonth, aDay, isoWeek);
}
uint32_t InputType::MaximumWeekInYear(uint32_t aYear) const {
// see comment in InputType::ParseDate().
return mInputElement->MaximumWeekInYear(aYear);
}
|