Bug 1379108 - Factor GetValidationMessage() out of HTMLInputElement. r=smaug
MozReview-Commit-ID: IFQXQTxoD9T
This commit is contained in:
@@ -7416,256 +7416,7 @@ nsresult
|
||||
HTMLInputElement::GetValidationMessage(nsAString& aValidationMessage,
|
||||
ValidityStateType aType)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
switch (aType)
|
||||
{
|
||||
case VALIDITY_STATE_TOO_LONG:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
int32_t maxLength = MaxLength();
|
||||
int32_t textLength = InputTextLength(CallerType::System);
|
||||
nsAutoString strMaxLength;
|
||||
nsAutoString strTextLength;
|
||||
|
||||
strMaxLength.AppendInt(maxLength);
|
||||
strTextLength.AppendInt(textLength);
|
||||
|
||||
const char16_t* params[] = { strMaxLength.get(), strTextLength.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationTextTooLong",
|
||||
params, message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_TOO_SHORT:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
int32_t minLength = MinLength();
|
||||
int32_t textLength = InputTextLength(CallerType::System);
|
||||
nsAutoString strMinLength;
|
||||
nsAutoString strTextLength;
|
||||
|
||||
strMinLength.AppendInt(minLength);
|
||||
strTextLength.AppendInt(textLength);
|
||||
|
||||
const char16_t* params[] = { strMinLength.get(), strTextLength.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationTextTooShort",
|
||||
params, message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_VALUE_MISSING:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
nsAutoCString key;
|
||||
switch (mType)
|
||||
{
|
||||
case NS_FORM_INPUT_FILE:
|
||||
key.AssignLiteral("FormValidationFileMissing");
|
||||
break;
|
||||
case NS_FORM_INPUT_CHECKBOX:
|
||||
key.AssignLiteral("FormValidationCheckboxMissing");
|
||||
break;
|
||||
case NS_FORM_INPUT_RADIO:
|
||||
key.AssignLiteral("FormValidationRadioMissing");
|
||||
break;
|
||||
case NS_FORM_INPUT_NUMBER:
|
||||
key.AssignLiteral("FormValidationBadInputNumber");
|
||||
break;
|
||||
default:
|
||||
key.AssignLiteral("FormValidationValueMissing");
|
||||
}
|
||||
rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
key.get(), message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_TYPE_MISMATCH:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
nsAutoCString key;
|
||||
if (mType == NS_FORM_INPUT_EMAIL) {
|
||||
key.AssignLiteral("FormValidationInvalidEmail");
|
||||
} else if (mType == NS_FORM_INPUT_URL) {
|
||||
key.AssignLiteral("FormValidationInvalidURL");
|
||||
} else {
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
key.get(), message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_PATTERN_MISMATCH:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
nsAutoString title;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::title, title);
|
||||
if (title.IsEmpty()) {
|
||||
rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationPatternMismatch",
|
||||
message);
|
||||
} else {
|
||||
if (title.Length() > nsIConstraintValidation::sContentSpecifiedMaxLengthMessage) {
|
||||
title.Truncate(nsIConstraintValidation::sContentSpecifiedMaxLengthMessage);
|
||||
}
|
||||
const char16_t* params[] = { title.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationPatternMismatchWithTitle",
|
||||
params, message);
|
||||
}
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_RANGE_OVERFLOW:
|
||||
{
|
||||
static const char kNumberOverTemplate[] = "FormValidationNumberRangeOverflow";
|
||||
static const char kDateOverTemplate[] = "FormValidationDateRangeOverflow";
|
||||
static const char kTimeOverTemplate[] = "FormValidationTimeRangeOverflow";
|
||||
|
||||
const char* msgTemplate;
|
||||
nsXPIDLString message;
|
||||
|
||||
nsAutoString maxStr;
|
||||
if (mType == NS_FORM_INPUT_NUMBER ||
|
||||
mType == NS_FORM_INPUT_RANGE) {
|
||||
msgTemplate = kNumberOverTemplate;
|
||||
|
||||
//We want to show the value as parsed when it's a number
|
||||
Decimal maximum = GetMaximum();
|
||||
MOZ_ASSERT(!maximum.isNaN());
|
||||
|
||||
char buf[32];
|
||||
DebugOnly<bool> ok = maximum.toString(buf, ArrayLength(buf));
|
||||
maxStr.AssignASCII(buf);
|
||||
MOZ_ASSERT(ok, "buf not big enough");
|
||||
} else if (IsDateTimeInputType(mType)) {
|
||||
msgTemplate = mType == NS_FORM_INPUT_TIME ? kTimeOverTemplate : kDateOverTemplate;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::max, maxStr);
|
||||
} else {
|
||||
msgTemplate = kNumberOverTemplate;
|
||||
NS_NOTREACHED("Unexpected input type");
|
||||
}
|
||||
|
||||
const char16_t* params[] = { maxStr.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
msgTemplate,
|
||||
params, message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_RANGE_UNDERFLOW:
|
||||
{
|
||||
static const char kNumberUnderTemplate[] = "FormValidationNumberRangeUnderflow";
|
||||
static const char kDateUnderTemplate[] = "FormValidationDateRangeUnderflow";
|
||||
static const char kTimeUnderTemplate[] = "FormValidationTimeRangeUnderflow";
|
||||
|
||||
const char* msgTemplate;
|
||||
nsXPIDLString message;
|
||||
|
||||
nsAutoString minStr;
|
||||
if (mType == NS_FORM_INPUT_NUMBER ||
|
||||
mType == NS_FORM_INPUT_RANGE) {
|
||||
msgTemplate = kNumberUnderTemplate;
|
||||
|
||||
Decimal minimum = GetMinimum();
|
||||
MOZ_ASSERT(!minimum.isNaN());
|
||||
|
||||
char buf[32];
|
||||
DebugOnly<bool> ok = minimum.toString(buf, ArrayLength(buf));
|
||||
minStr.AssignASCII(buf);
|
||||
MOZ_ASSERT(ok, "buf not big enough");
|
||||
} else if (IsDateTimeInputType(mType)) {
|
||||
msgTemplate = mType == NS_FORM_INPUT_TIME ? kTimeUnderTemplate : kDateUnderTemplate;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::min, minStr);
|
||||
} else {
|
||||
msgTemplate = kNumberUnderTemplate;
|
||||
NS_NOTREACHED("Unexpected input type");
|
||||
}
|
||||
|
||||
const char16_t* params[] = { minStr.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
msgTemplate,
|
||||
params, message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_STEP_MISMATCH:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
|
||||
Decimal value = GetValueAsDecimal();
|
||||
MOZ_ASSERT(!value.isNaN());
|
||||
|
||||
Decimal step = GetStep();
|
||||
MOZ_ASSERT(step != kStepAny && step > Decimal(0));
|
||||
|
||||
Decimal stepBase = GetStepBase();
|
||||
|
||||
Decimal valueLow = value - NS_floorModulo(value - stepBase, step);
|
||||
Decimal valueHigh = value + step - NS_floorModulo(value - stepBase, step);
|
||||
|
||||
Decimal maximum = GetMaximum();
|
||||
|
||||
if (maximum.isNaN() || valueHigh <= maximum) {
|
||||
nsAutoString valueLowStr, valueHighStr;
|
||||
mInputType->ConvertNumberToString(valueLow, valueLowStr);
|
||||
mInputType->ConvertNumberToString(valueHigh, valueHighStr);
|
||||
|
||||
if (valueLowStr.Equals(valueHighStr)) {
|
||||
const char16_t* params[] = { valueLowStr.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationStepMismatchOneValue",
|
||||
params, message);
|
||||
} else {
|
||||
const char16_t* params[] = { valueLowStr.get(), valueHighStr.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationStepMismatch",
|
||||
params, message);
|
||||
}
|
||||
} else {
|
||||
nsAutoString valueLowStr;
|
||||
mInputType->ConvertNumberToString(valueLow, valueLowStr);
|
||||
|
||||
const char16_t* params[] = { valueLowStr.get() };
|
||||
rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
"FormValidationStepMismatchOneValue",
|
||||
params, message);
|
||||
}
|
||||
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_BAD_INPUT:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
nsAutoCString key;
|
||||
if (mType == NS_FORM_INPUT_NUMBER) {
|
||||
key.AssignLiteral("FormValidationBadInputNumber");
|
||||
} else if (mType == NS_FORM_INPUT_EMAIL) {
|
||||
key.AssignLiteral("FormValidationInvalidEmail");
|
||||
} else if (mType == NS_FORM_INPUT_DATE && IsInputDateTimeEnabled()) {
|
||||
// For input date and time, only date may have an invalid value, as
|
||||
// we forbid or autocorrect values that are not in the valid range for
|
||||
// time. For example, in 12hr format, if user enters '2' in the hour
|
||||
// field, it will be treated as '02' and automatically advance to the
|
||||
// next field.
|
||||
key.AssignLiteral("FormValidationInvalidDate");
|
||||
} else {
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
||||
key.get(), message);
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
rv = nsIConstraintValidation::GetValidationMessage(aValidationMessage, aType);
|
||||
}
|
||||
|
||||
return rv;
|
||||
return mInputType->GetValidationMessage(aValidationMessage, aType);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP_(bool)
|
||||
|
||||
Reference in New Issue
Block a user