Bug 1345767 - Part 2: Factor IsTooLong/Short() out of HTMLInputElement. r?smaug
MozReview-Commit-ID: 5svYqBEFgzk
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -1461,19 +1461,19 @@ HTMLInputElement::AfterSetAttr(int32_t a
if (aName == nsGkAtoms::required || aName == nsGkAtoms::disabled ||
aName == nsGkAtoms::readonly) {
UpdateValueMissingValidityState();
// This *has* to be called *after* validity has changed.
if (aName == nsGkAtoms::readonly || aName == nsGkAtoms::disabled) {
UpdateBarredFromConstraintValidation();
}
- } else if (MinOrMaxLengthApplies() && aName == nsGkAtoms::maxlength) {
+ } else if (aName == nsGkAtoms::maxlength) {
UpdateTooLongValidityState();
- } else if (MinOrMaxLengthApplies() && aName == nsGkAtoms::minlength) {
+ } else if (aName == nsGkAtoms::minlength) {
UpdateTooShortValidityState();
} else if (aName == nsGkAtoms::pattern && mDoneCreating) {
UpdatePatternMismatchValidityState();
} else if (aName == nsGkAtoms::multiple) {
UpdateTypeMismatchValidityState();
} else if (aName == nsGkAtoms::max) {
UpdateHasRange();
if (mType == NS_FORM_INPUT_RANGE) {
@@ -7494,52 +7494,32 @@ HTMLInputElement::SetCustomValidity(cons
return NS_OK;
}
bool
HTMLInputElement::IsTooLong()
{
if (!mValueChanged ||
- !mLastValueChangeWasInteractive ||
- !MinOrMaxLengthApplies() ||
- !HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength)) {
+ !mLastValueChangeWasInteractive) {
return false;
}
- int32_t maxLength = MaxLength();
-
- // Maxlength of -1 means parsing error.
- if (maxLength == -1) {
- return false;
- }
-
- return InputTextLength(CallerType::System) > maxLength;
+ return mInputType->IsTooLong();
}
bool
HTMLInputElement::IsTooShort()
{
if (!mValueChanged ||
- !mLastValueChangeWasInteractive ||
- !MinOrMaxLengthApplies() ||
- !HasAttr(kNameSpaceID_None, nsGkAtoms::minlength)) {
+ !mLastValueChangeWasInteractive) {
return false;
}
- int32_t minLength = MinLength();
-
- // Minlength of -1 means parsing error.
- if (minLength == -1) {
- return false;
- }
-
- int32_t textLength = InputTextLength(CallerType::System);
-
- return textLength && textLength < minLength;
+ return mInputType->IsTooShort();
}
bool
HTMLInputElement::IsValueMissing() const
{
// Should use UpdateValueMissingValidityStateForRadio() for type radio.
MOZ_ASSERT(mType != NS_FORM_INPUT_RADIO);
--- a/dom/html/HTMLInputElement.h
+++ b/dom/html/HTMLInputElement.h
@@ -1123,21 +1123,16 @@ protected:
*/
bool DoesValueAsNumberApply() const { return DoesMinMaxApply(); }
/**
* Returns if autocomplete attribute applies for the current type.
*/
bool DoesAutocompleteApply() const;
- /**
- * Returns if the minlength or maxlength attributes apply for the current type.
- */
- bool MinOrMaxLengthApplies() const { return IsSingleLineTextControl(false, mType); }
-
void FreeData();
nsTextEditorState *GetEditorState() const;
/**
* Manages the internal data storage across type changes.
*/
void HandleTypeChange(uint8_t aNewType, bool aNotify);
--- a/dom/html/input/InputType.cpp
+++ b/dom/html/input/InputType.cpp
@@ -103,8 +103,20 @@ InputType::Create(mozilla::dom::HTMLInpu
}
void
InputType::DropReference()
{
// Drop our (non ref-counted) reference.
mInputElement = nullptr;
}
+
+bool
+InputType::IsTooLong() const
+{
+ return false;
+}
+
+bool
+InputType::IsTooShort() const
+{
+ return false;
+}
--- a/dom/html/input/InputType.h
+++ b/dom/html/input/InputType.h
@@ -30,16 +30,19 @@ public:
virtual ~InputType() {}
/**
* Drop the reference to the input element.
*/
void DropReference();
+ virtual bool IsTooLong() const;
+ virtual bool IsTooShort() const;
+
protected:
explicit InputType(mozilla::dom::HTMLInputElement* aInputElement)
: mInputElement(aInputElement)
{}
mozilla::dom::HTMLInputElement* mInputElement;
};
new file mode 100644
--- /dev/null
+++ b/dom/html/input/SingleLineTextInputTypes.cpp
@@ -0,0 +1,42 @@
+/* -*- 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 "SingleLineTextInputTypes.h"
+
+#include "mozilla/dom/HTMLInputElement.h"
+#include "mozilla/dom/BindingDeclarations.h"
+
+bool
+SingleLineTextInputTypeBase::IsTooLong() const
+{
+ int32_t maxLength = mInputElement->MaxLength();
+
+ // Maxlength of -1 means attribute isn't set or parsing error.
+ if (maxLength == -1) {
+ return false;
+ }
+
+ int32_t textLength =
+ mInputElement->InputTextLength(mozilla::dom::CallerType::System);
+
+ return textLength > maxLength;
+}
+
+bool
+SingleLineTextInputTypeBase::IsTooShort() const
+{
+ int32_t minLength = mInputElement->MinLength();
+
+ // Minlength of -1 means attribute isn't set or parsing error.
+ if (minLength == -1) {
+ return false;
+ }
+
+ int32_t textLength =
+ mInputElement->InputTextLength(mozilla::dom::CallerType::System);
+
+ return textLength && textLength < minLength;
+}
--- a/dom/html/input/SingleLineTextInputTypes.h
+++ b/dom/html/input/SingleLineTextInputTypes.h
@@ -9,16 +9,19 @@
#include "InputType.h"
class SingleLineTextInputTypeBase : public ::InputType
{
public:
~SingleLineTextInputTypeBase() override {}
+ bool IsTooLong() const override;
+ bool IsTooShort() const override;
+
protected:
explicit SingleLineTextInputTypeBase(
mozilla::dom::HTMLInputElement* aInputElement)
: InputType(aInputElement)
{}
};
// input type=text
--- a/dom/html/input/moz.build
+++ b/dom/html/input/moz.build
@@ -13,16 +13,17 @@ EXPORTS += [
'HiddenInputType.h',
'InputType.h',
'NumericInputTypes.h',
'SingleLineTextInputTypes.h',
]
UNIFIED_SOURCES += [
'InputType.cpp',
+ 'SingleLineTextInputTypes.cpp',
]
include('/ipc/chromium/chromium-config.mozbuild')
LOCAL_INCLUDES += [
'/dom/base',
'/dom/html',
]