Bug 932755 - Add support for input/textarea minLength and tooShort. r=mrbkap
This commit is contained in:
@@ -1467,8 +1467,10 @@ HTMLInputElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
||||
if (aName == nsGkAtoms::readonly || aName == nsGkAtoms::disabled) {
|
||||
UpdateBarredFromConstraintValidation();
|
||||
}
|
||||
} else if (MaxLengthApplies() && aName == nsGkAtoms::maxlength) {
|
||||
} else if (MinOrMaxLengthApplies() && aName == nsGkAtoms::maxlength) {
|
||||
UpdateTooLongValidityState();
|
||||
} else if (MinOrMaxLengthApplies() && aName == nsGkAtoms::minlength) {
|
||||
UpdateTooShortValidityState();
|
||||
} else if (aName == nsGkAtoms::pattern && !mParserCreating) {
|
||||
UpdatePatternMismatchValidityState();
|
||||
} else if (aName == nsGkAtoms::multiple) {
|
||||
@@ -1589,6 +1591,7 @@ NS_IMPL_ENUM_ATTR_DEFAULT_VALUE(HTMLInputElement, InputMode, inputmode,
|
||||
kInputDefaultInputmode->tag)
|
||||
NS_IMPL_BOOL_ATTR(HTMLInputElement, Multiple, multiple)
|
||||
NS_IMPL_NON_NEGATIVE_INT_ATTR(HTMLInputElement, MaxLength, maxlength)
|
||||
NS_IMPL_NON_NEGATIVE_INT_ATTR(HTMLInputElement, MinLength, minlength)
|
||||
NS_IMPL_STRING_ATTR(HTMLInputElement, Name, name)
|
||||
NS_IMPL_BOOL_ATTR(HTMLInputElement, ReadOnly, readonly)
|
||||
NS_IMPL_BOOL_ATTR(HTMLInputElement, Required, required)
|
||||
@@ -5333,6 +5336,9 @@ HTMLInputElement::ParseAttribute(int32_t aNamespaceID,
|
||||
if (aAttribute == nsGkAtoms::maxlength) {
|
||||
return aResult.ParseNonNegativeIntValue(aValue);
|
||||
}
|
||||
if (aAttribute == nsGkAtoms::minlength) {
|
||||
return aResult.ParseNonNegativeIntValue(aValue);
|
||||
}
|
||||
if (aAttribute == nsGkAtoms::size) {
|
||||
return aResult.ParsePositiveIntValue(aValue);
|
||||
}
|
||||
@@ -7005,7 +7011,7 @@ HTMLInputElement::IsTooLong()
|
||||
{
|
||||
if (!mValueChanged ||
|
||||
!mLastValueChangeWasInteractive ||
|
||||
!MaxLengthApplies() ||
|
||||
!MinOrMaxLengthApplies() ||
|
||||
!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength)) {
|
||||
return false;
|
||||
}
|
||||
@@ -7023,6 +7029,29 @@ HTMLInputElement::IsTooLong()
|
||||
return textLength > maxLength;
|
||||
}
|
||||
|
||||
bool
|
||||
HTMLInputElement::IsTooShort()
|
||||
{
|
||||
if (!mValueChanged ||
|
||||
!mLastValueChangeWasInteractive ||
|
||||
!MinOrMaxLengthApplies() ||
|
||||
!HasAttr(kNameSpaceID_None, nsGkAtoms::minlength)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t minLength = MinLength();
|
||||
|
||||
// Minlength of -1 means parsing error.
|
||||
if (minLength == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t textLength = -1;
|
||||
GetTextLength(&textLength);
|
||||
|
||||
return textLength && textLength < minLength;
|
||||
}
|
||||
|
||||
bool
|
||||
HTMLInputElement::IsValueMissing() const
|
||||
{
|
||||
@@ -7284,6 +7313,12 @@ HTMLInputElement::UpdateTooLongValidityState()
|
||||
SetValidityState(VALIDITY_STATE_TOO_LONG, IsTooLong());
|
||||
}
|
||||
|
||||
void
|
||||
HTMLInputElement::UpdateTooShortValidityState()
|
||||
{
|
||||
SetValidityState(VALIDITY_STATE_TOO_SHORT, IsTooShort());
|
||||
}
|
||||
|
||||
void
|
||||
HTMLInputElement::UpdateValueMissingValidityStateForRadio(bool aIgnoreSelf)
|
||||
{
|
||||
@@ -7384,6 +7419,7 @@ HTMLInputElement::UpdateAllValidityStates(bool aNotify)
|
||||
{
|
||||
bool validBefore = IsValid();
|
||||
UpdateTooLongValidityState();
|
||||
UpdateTooShortValidityState();
|
||||
UpdateValueMissingValidityState();
|
||||
UpdateTypeMismatchValidityState();
|
||||
UpdatePatternMismatchValidityState();
|
||||
@@ -7442,6 +7478,26 @@ HTMLInputElement::GetValidationMessage(nsAString& aValidationMessage,
|
||||
aValidationMessage = message;
|
||||
break;
|
||||
}
|
||||
case VALIDITY_STATE_TOO_SHORT:
|
||||
{
|
||||
nsXPIDLString message;
|
||||
int32_t minLength = MinLength();
|
||||
int32_t textLength = -1;
|
||||
nsAutoString strMinLength;
|
||||
nsAutoString strTextLength;
|
||||
|
||||
GetTextLength(&textLength);
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user