Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Unified Diff: Source/core/html/HTMLTextAreaElement.cpp

Issue 435753003: Implement minlength for <input> and <textarea>. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Add layout tests for minlength & maxlength together; fix comments Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLTextAreaElement.cpp
diff --git a/Source/core/html/HTMLTextAreaElement.cpp b/Source/core/html/HTMLTextAreaElement.cpp
index bd11c103d89d92ec18bcf450aae93ad93c8c8a9d..40863e34fa78a496551e4670e735abb8c1b9d05d 100644
--- a/Source/core/html/HTMLTextAreaElement.cpp
+++ b/Source/core/html/HTMLTextAreaElement.cpp
@@ -189,9 +189,11 @@ void HTMLTextAreaElement::parseAttribute(const QualifiedName& name, const Atomic
}
} else if (name == accesskeyAttr) {
// ignore for the moment
- } else if (name == maxlengthAttr)
+ } else if (name == maxlengthAttr) {
setNeedsValidityCheck();
- else
+ } else if (name == minlengthAttr) {
+ setNeedsValidityCheck();
+ } else
HTMLTextFormControlElement::parseAttribute(name, value);
}
@@ -451,14 +453,35 @@ int HTMLTextAreaElement::maxLength() const
return ok && value >= 0 ? value : -1;
}
+int HTMLTextAreaElement::minLength() const
+{
+ bool ok;
+ int value = getAttribute(minlengthAttr).string().toInt(&ok);
tkent 2014/10/15 00:50:25 should use parseHTMLInteger() like HTMLInputElemen
Bartek Nowierski 2014/10/16 04:55:14 Good point! I'll change it for both minLength and
+ return ok && value >= 0 ? value : -1;
+}
+
void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionState& exceptionState)
{
+ int min = minLength();
if (newValue < 0)
exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
+ else if (min >= 0 && newValue < min)
+ exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMinimumBound("maxLength", newValue, min));
else
setIntegralAttribute(maxlengthAttr, newValue);
}
+void HTMLTextAreaElement::setMinLength(int newValue, ExceptionState& exceptionState)
+{
+ int max = maxLength();
+ if (newValue < 0)
+ exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
+ else if (max >= 0 && newValue > max)
+ exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("minLength", newValue, max));
+ else
+ setIntegralAttribute(minlengthAttr, newValue);
+}
+
String HTMLTextAreaElement::suggestedValue() const
{
return m_suggestedValue;
@@ -490,6 +513,9 @@ String HTMLTextAreaElement::validationMessage() const
if (tooLong())
return locale().validationMessageTooLongText(computeLengthForSubmission(value()), maxLength());
+ if (tooShort())
+ return locale().validationMessageTooShortText(computeLengthForSubmission(value()), minLength());
+
return String();
}
@@ -510,6 +536,12 @@ bool HTMLTextAreaElement::tooLong() const
return willValidate() && tooLong(0, CheckDirtyFlag);
}
+bool HTMLTextAreaElement::tooShort() const
+{
+ // We should not call value() for performance.
+ return willValidate() && tooShort(0, CheckDirtyFlag);
+}
+
bool HTMLTextAreaElement::tooLong(const String* value, NeedsToCheckDirtyFlag check) const
{
// Return false for the default value or value set by script even if it is
@@ -523,9 +555,24 @@ bool HTMLTextAreaElement::tooLong(const String* value, NeedsToCheckDirtyFlag che
return computeLengthForSubmission(value ? *value : this->value()) > static_cast<unsigned>(max);
}
+bool HTMLTextAreaElement::tooShort(const String* value, NeedsToCheckDirtyFlag check) const
+{
+ // Return false for the default value or value set by script even if it is
+ // shorter than minLength.
+ if (check == CheckDirtyFlag && !lastChangeWasUserEdit())
+ return false;
+
+ int min = minLength();
+ if (min <= 0)
+ return false;
+ // An empty string is excluded from minlength check.
+ unsigned len = computeLengthForSubmission(value ? *value : this->value());
+ return len > 0 && len < static_cast<unsigned>(min);
+}
+
bool HTMLTextAreaElement::isValidValue(const String& candidate) const
{
- return !valueMissing(&candidate) && !tooLong(&candidate, IgnoreDirtyFlag);
+ return !valueMissing(&candidate) && !tooLong(&candidate, IgnoreDirtyFlag) && !tooShort(&candidate, IgnoreDirtyFlag);
}
void HTMLTextAreaElement::accessKeyAction(bool)

Powered by Google App Engine
This is Rietveld 408576698