Bug 1345767 - Part 3: Factor IsValueMissing() out of HTMLInputElement. r=smaug

MozReview-Commit-ID: AE6SSA43Vx5

--HG--
extra : rebase_source : b51483419c3a0804871f78842b576406790bd8fc
This commit is contained in:
Jessica Jong 2017-05-04 14:47:08 +08:00
parent b15f522fed
commit ee7fa8727c
15 changed files with 185 additions and 25 deletions

View File

@ -7544,31 +7544,7 @@ HTMLInputElement::IsValueMissing() const
// Should use UpdateValueMissingValidityStateForRadio() for type radio.
MOZ_ASSERT(mType != NS_FORM_INPUT_RADIO);
if (!HasAttr(kNameSpaceID_None, nsGkAtoms::required) ||
!DoesRequiredApply()) {
return false;
}
if (!IsMutable()) {
return false;
}
switch (GetValueMode()) {
case VALUE_MODE_VALUE:
return IsValueEmpty();
case VALUE_MODE_FILENAME:
return GetFilesOrDirectoriesInternal().IsEmpty();
case VALUE_MODE_DEFAULT_ON:
// This should not be used for type radio.
// See the MOZ_ASSERT at the beginning of the method.
return !mChecked;
case VALUE_MODE_DEFAULT:
default:
return false;
}
return mInputType->IsValueMissing();
}
bool

View File

@ -134,6 +134,7 @@ class HTMLInputElement final : public nsGenericHTMLFormElementWithState,
{
friend class AfterSetFilesOrDirectoriesCallback;
friend class DispatchChangeEventCallback;
friend class ::InputType;
public:
using nsIConstraintValidation::GetValidationMessage;

View File

@ -0,0 +1,23 @@
/* -*- 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 "CheckableInputTypes.h"
#include "mozilla/dom/HTMLInputElement.h"
bool
CheckboxInputType::IsValueMissing() const
{
if (!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
return false;
}
if (!IsMutable()) {
return false;
}
return !mInputElement->Checked();
}

View File

@ -30,6 +30,8 @@ public:
return new (aMemory) CheckboxInputType(aInputElement);
}
bool IsValueMissing() const override;
private:
explicit CheckboxInputType(mozilla::dom::HTMLInputElement* aInputElement)
: CheckableInputTypeBase(aInputElement)

View File

@ -0,0 +1,30 @@
/* -*- 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 "DateTimeInputTypes.h"
#include "mozilla/dom/HTMLInputElement.h"
bool
DateTimeInputTypeBase::IsMutable() const
{
return !mInputElement->IsDisabled() &&
!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly);
}
bool
DateTimeInputTypeBase::IsValueMissing() const
{
if (!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
return false;
}
if (!IsMutable()) {
return false;
}
return IsValueEmpty();
}

View File

@ -14,10 +14,14 @@ class DateTimeInputTypeBase : public ::InputType
public:
~DateTimeInputTypeBase() override {}
bool IsValueMissing() const override;
protected:
explicit DateTimeInputTypeBase(mozilla::dom::HTMLInputElement* aInputElement)
: InputType(aInputElement)
{}
bool IsMutable() const override;
};
// input type=date

View File

@ -0,0 +1,23 @@
/* -*- 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 "FileInputType.h"
#include "mozilla/dom/HTMLInputElement.h"
bool
FileInputType::IsValueMissing() const
{
if (!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
return false;
}
if (!IsMutable()) {
return false;
}
return mInputElement->GetFilesOrDirectoriesInternal().IsEmpty();
}

View File

@ -19,6 +19,8 @@ public:
return new (aMemory) FileInputType(aInputElement);
}
bool IsValueMissing() const override;
private:
explicit FileInputType(mozilla::dom::HTMLInputElement* aInputElement)
: InputType(aInputElement)

View File

@ -102,6 +102,18 @@ InputType::Create(mozilla::dom::HTMLInputElement* aInputElement, uint8_t aType,
return inputType;
}
bool
InputType::IsMutable() const
{
return !mInputElement->IsDisabled();
}
bool
InputType::IsValueEmpty() const
{
return mInputElement->IsValueEmpty();
}
void
InputType::DropReference()
{
@ -120,3 +132,9 @@ InputType::IsTooShort() const
{
return false;
}
bool
InputType::IsValueMissing() const
{
return false;
}

View File

@ -37,12 +37,31 @@ public:
virtual bool IsTooLong() const;
virtual bool IsTooShort() const;
virtual bool IsValueMissing() const;
protected:
explicit InputType(mozilla::dom::HTMLInputElement* aInputElement)
: mInputElement(aInputElement)
{}
/**
* Get the mutable state of the element.
* When the element isn't mutable (immutable), the value or checkedness
* should not be changed by the user.
*
* See: https://html.spec.whatwg.org/multipage/forms.html#the-input-element:concept-fe-mutable
*/
virtual bool IsMutable() const;
/**
* Returns whether the input element's current value is the empty string.
* This only makes sense for some input types; does NOT make sense for file
* inputs.
*
* @return whether the input element's current value is the empty string.
*/
bool IsValueEmpty() const;
mozilla::dom::HTMLInputElement* mInputElement;
};

View File

@ -0,0 +1,30 @@
/* -*- 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 "NumericInputTypes.h"
#include "mozilla/dom/HTMLInputElement.h"
bool
NumberInputType::IsMutable() const
{
return !mInputElement->IsDisabled() &&
!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly);
}
bool
NumberInputType::IsValueMissing() const
{
if (!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
return false;
}
if (!IsMutable()) {
return false;
}
return IsValueEmpty();
}

View File

@ -30,10 +30,14 @@ public:
return new (aMemory) NumberInputType(aInputElement);
}
bool IsValueMissing() const override;
private:
explicit NumberInputType(mozilla::dom::HTMLInputElement* aInputElement)
: NumericInputTypeBase(aInputElement)
{}
bool IsMutable() const override;
};
// input type=range

View File

@ -9,6 +9,13 @@
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/BindingDeclarations.h"
bool
SingleLineTextInputTypeBase::IsMutable() const
{
return !mInputElement->IsDisabled() &&
!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly);
}
bool
SingleLineTextInputTypeBase::IsTooLong() const
{
@ -40,3 +47,17 @@ SingleLineTextInputTypeBase::IsTooShort() const
return textLength && textLength < minLength;
}
bool
SingleLineTextInputTypeBase::IsValueMissing() const
{
if (!mInputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::required)) {
return false;
}
if (!IsMutable()) {
return false;
}
return IsValueEmpty();
}

View File

@ -16,12 +16,15 @@ public:
bool IsTooLong() const override;
bool IsTooShort() const override;
bool IsValueMissing() const override;
protected:
explicit SingleLineTextInputTypeBase(
mozilla::dom::HTMLInputElement* aInputElement)
: InputType(aInputElement)
{}
bool IsMutable() const override;
};
// input type=text

View File

@ -17,7 +17,11 @@ EXPORTS += [
]
UNIFIED_SOURCES += [
'CheckableInputTypes.cpp',
'DateTimeInputTypes.cpp',
'FileInputType.cpp',
'InputType.cpp',
'NumericInputTypes.cpp',
'SingleLineTextInputTypes.cpp',
]