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

Unified Diff: Source/core/html/HTMLInputElement.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/HTMLInputElement.cpp
diff --git a/Source/core/html/HTMLInputElement.cpp b/Source/core/html/HTMLInputElement.cpp
index e01e185c1e9e8ed49d1c4f0bcfc461fc7838bf34..d0704564663d4357c2add46e8a78209cd188920e 100644
--- a/Source/core/html/HTMLInputElement.cpp
+++ b/Source/core/html/HTMLInputElement.cpp
@@ -110,6 +110,7 @@ HTMLInputElement::HTMLInputElement(Document& document, HTMLFormElement* form, bo
: HTMLTextFormControlElement(inputTag, document, form)
, m_size(defaultSize)
, m_maxLength(maximumLength)
+ , m_minLength(0)
, m_maxResults(-1)
, m_isChecked(false)
, m_reflectsCheckedAttribute(true)
@@ -210,6 +211,7 @@ bool HTMLInputElement::isValidValue(const String& value) const
&& !m_inputType->rangeUnderflow(value)
&& !m_inputType->rangeOverflow(value)
&& !tooLong(value, IgnoreDirtyFlag)
+ && !tooShort(value, IgnoreDirtyFlag)
&& !m_inputType->patternMismatch(value)
&& !m_inputType->valueMissing(value);
}
@@ -219,6 +221,11 @@ bool HTMLInputElement::tooLong() const
return willValidate() && tooLong(value(), CheckDirtyFlag);
}
+bool HTMLInputElement::tooShort() const
+{
+ return willValidate() && tooShort(value(), CheckDirtyFlag);
+}
+
bool HTMLInputElement::typeMismatch() const
{
return willValidate() && m_inputType->typeMismatch();
@@ -244,6 +251,11 @@ bool HTMLInputElement::tooLong(const String& value, NeedsToCheckDirtyFlag check)
return m_inputType->tooLong(value, check);
}
+bool HTMLInputElement::tooShort(const String& value, NeedsToCheckDirtyFlag check) const
+{
+ return m_inputType->tooShort(value, check);
+}
+
bool HTMLInputElement::rangeUnderflow() const
{
return willValidate() && m_inputType->rangeUnderflow(value());
@@ -671,9 +683,11 @@ void HTMLInputElement::parseAttribute(const QualifiedName& name, const AtomicStr
setChecked(!value.isNull());
m_reflectsCheckedAttribute = true;
}
- } else if (name == maxlengthAttr)
+ } else if (name == maxlengthAttr) {
parseMaxLengthAttribute(value);
- else if (name == sizeAttr) {
+ } else if (name == minlengthAttr) {
+ parseMinLengthAttribute(value);
+ } else if (name == sizeAttr) {
int oldSize = m_size;
int valueAsInteger = value.toInt();
m_size = valueAsInteger > 0 ? valueAsInteger : defaultSize;
@@ -1291,14 +1305,31 @@ int HTMLInputElement::maxLength() const
return m_maxLength;
}
+int HTMLInputElement::minLength() const
+{
+ return m_minLength;
+}
+
void HTMLInputElement::setMaxLength(int maxLength, ExceptionState& exceptionState)
{
if (maxLength < 0)
exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(maxLength) + ") is negative.");
+ else if (maxLength < m_minLength)
+ exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMinimumBound("maxLength", maxLength, m_minLength));
else
setIntegralAttribute(maxlengthAttr, maxLength);
}
+void HTMLInputElement::setMinLength(int minLength, ExceptionState& exceptionState)
+{
+ if (minLength < 0)
+ exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(minLength) + ") is negative.");
+ else if (minLength > m_maxLength)
+ exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("minLength", minLength, m_maxLength));
+ else
+ setIntegralAttribute(minlengthAttr, minLength);
+}
+
bool HTMLInputElement::multiple() const
{
return fastHasAttribute(multipleAttr);
@@ -1597,6 +1628,18 @@ void HTMLInputElement::parseMaxLengthAttribute(const AtomicString& value)
setNeedsValidityCheck();
}
+void HTMLInputElement::parseMinLengthAttribute(const AtomicString& value)
+{
+ int minLength;
+ if (!parseHTMLInteger(value, minLength))
+ minLength = 0;
+ if (minLength < 0)
+ minLength = 0;
+ m_minLength = minLength;
+ setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::fromAttribute(minlengthAttr));
+ setNeedsValidityCheck();
+}
+
void HTMLInputElement::updateValueIfNeeded()
{
String newValue = sanitizeValue(m_valueIfDirty);

Powered by Google App Engine
This is Rietveld 408576698