2010-08-18 11:28:08 -07:00
|
|
|
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-08-18 11:28:08 -07:00
|
|
|
|
2010-08-21 11:52:49 -07:00
|
|
|
#include "nsIConstraintValidation.h"
|
2010-08-18 11:28:08 -07:00
|
|
|
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2013-06-19 07:24:37 -07:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2013-08-16 17:32:47 -07:00
|
|
|
#include "mozilla/dom/HTMLFieldSetElement.h"
|
2013-01-06 11:20:02 -08:00
|
|
|
#include "mozilla/dom/ValidityState.h"
|
2010-09-09 22:08:56 -07:00
|
|
|
#include "nsIFormControl.h"
|
2011-08-11 06:29:50 -07:00
|
|
|
#include "nsContentUtils.h"
|
2010-08-21 10:52:57 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint16_t nsIConstraintValidation::sContentSpecifiedMaxLengthMessage = 256;
|
2010-10-25 15:02:24 -07:00
|
|
|
|
2013-08-16 17:32:47 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-08-21 11:52:49 -07:00
|
|
|
nsIConstraintValidation::nsIConstraintValidation()
|
2010-08-21 10:52:57 -07:00
|
|
|
: mValidityBitField(0)
|
2010-09-09 22:08:56 -07:00
|
|
|
// By default, all elements are subjects to constraint validation.
|
2011-10-17 07:59:28 -07:00
|
|
|
, mBarredFromConstraintValidation(false)
|
2010-08-21 10:52:57 -07:00
|
|
|
{
|
|
|
|
}
|
2010-08-18 11:28:08 -07:00
|
|
|
|
2010-08-21 11:52:49 -07:00
|
|
|
nsIConstraintValidation::~nsIConstraintValidation()
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-16 10:01:01 -08:00
|
|
|
mozilla::dom::ValidityState*
|
|
|
|
nsIConstraintValidation::Validity()
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
|
|
|
if (!mValidity) {
|
2013-01-06 11:20:02 -08:00
|
|
|
mValidity = new mozilla::dom::ValidityState(this);
|
2010-08-18 11:28:08 -07:00
|
|
|
}
|
|
|
|
|
2013-01-16 10:01:01 -08:00
|
|
|
return mValidity;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIConstraintValidation::GetValidity(nsIDOMValidityState** aValidity)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aValidity);
|
|
|
|
|
|
|
|
NS_ADDREF(*aValidity = Validity());
|
2010-08-18 11:28:08 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-26 22:46:17 -07:00
|
|
|
NS_IMETHODIMP
|
2010-08-21 11:52:49 -07:00
|
|
|
nsIConstraintValidation::GetValidationMessage(nsAString& aValidationMessage)
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
|
|
|
aValidationMessage.Truncate();
|
|
|
|
|
2010-08-21 11:51:38 -07:00
|
|
|
if (IsCandidateForConstraintValidation() && !IsValid()) {
|
2010-10-15 06:05:14 -07:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(content, "This class should be inherited by HTML elements only!");
|
|
|
|
|
|
|
|
nsAutoString authorMessage;
|
|
|
|
content->GetAttr(kNameSpaceID_None, nsGkAtoms::x_moz_errormessage,
|
|
|
|
authorMessage);
|
|
|
|
|
|
|
|
if (!authorMessage.IsEmpty()) {
|
|
|
|
aValidationMessage.Assign(authorMessage);
|
2010-10-25 15:02:24 -07:00
|
|
|
if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) {
|
|
|
|
aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage);
|
|
|
|
}
|
2010-10-15 06:05:14 -07:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_CUSTOM_ERROR)) {
|
2010-08-18 11:28:08 -07:00
|
|
|
aValidationMessage.Assign(mCustomValidity);
|
2010-10-25 15:02:24 -07:00
|
|
|
if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) {
|
|
|
|
aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage);
|
|
|
|
}
|
2010-08-21 10:52:57 -07:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_TOO_LONG)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_LONG);
|
|
|
|
} else if (GetValidityState(VALIDITY_STATE_VALUE_MISSING)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_VALUE_MISSING);
|
|
|
|
} else if (GetValidityState(VALIDITY_STATE_TYPE_MISMATCH)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_TYPE_MISMATCH);
|
|
|
|
} else if (GetValidityState(VALIDITY_STATE_PATTERN_MISMATCH)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_PATTERN_MISMATCH);
|
2012-06-22 02:36:24 -07:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_RANGE_OVERFLOW)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_OVERFLOW);
|
2012-06-22 02:38:20 -07:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_RANGE_UNDERFLOW)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_UNDERFLOW);
|
2012-06-22 12:02:20 -07:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_STEP_MISMATCH)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_STEP_MISMATCH);
|
2014-01-30 04:54:12 -08:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_BAD_INPUT)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_BAD_INPUT);
|
2010-08-18 11:28:08 -07:00
|
|
|
} else {
|
2012-06-22 12:02:20 -07:00
|
|
|
// There should not be other validity states.
|
2010-08-18 11:28:08 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aValidationMessage.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-16 10:01:01 -08:00
|
|
|
bool
|
|
|
|
nsIConstraintValidation::CheckValidity()
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
2010-08-21 11:51:38 -07:00
|
|
|
if (!IsCandidateForConstraintValidation() || IsValid()) {
|
2013-01-16 10:01:01 -08:00
|
|
|
return true;
|
2010-08-18 11:28:08 -07:00
|
|
|
}
|
|
|
|
|
2010-08-21 11:51:38 -07:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(content, "This class should be inherited by HTML elements only!");
|
|
|
|
|
2013-01-16 10:01:01 -08:00
|
|
|
nsContentUtils::DispatchTrustedEvent(content->OwnerDoc(), content,
|
|
|
|
NS_LITERAL_STRING("invalid"),
|
|
|
|
false, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIConstraintValidation::CheckValidity(bool* aValidity)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aValidity);
|
|
|
|
|
|
|
|
*aValidity = CheckValidity();
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-08-18 11:28:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 22:08:56 -07:00
|
|
|
void
|
|
|
|
nsIConstraintValidation::SetValidityState(ValidityStateType aState,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aValue)
|
2010-09-09 22:08:56 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool previousValidity = IsValid();
|
2010-09-09 22:08:56 -07:00
|
|
|
|
|
|
|
if (aValue) {
|
|
|
|
mValidityBitField |= aState;
|
|
|
|
} else {
|
|
|
|
mValidityBitField &= ~aState;
|
|
|
|
}
|
|
|
|
|
2013-08-16 17:32:47 -07:00
|
|
|
// Inform the form and fieldset elements if our validity has changed.
|
2010-09-09 22:08:56 -07:00
|
|
|
if (previousValidity != IsValid() && IsCandidateForConstraintValidation()) {
|
|
|
|
nsCOMPtr<nsIFormControl> formCtrl = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(formCtrl, "This interface should be used by form elements!");
|
|
|
|
|
2013-08-16 17:32:47 -07:00
|
|
|
HTMLFormElement* form =
|
|
|
|
static_cast<HTMLFormElement*>(formCtrl->GetFormElement());
|
2010-09-09 22:08:56 -07:00
|
|
|
if (form) {
|
|
|
|
form->UpdateValidity(IsValid());
|
|
|
|
}
|
2013-08-16 17:32:47 -07:00
|
|
|
HTMLFieldSetElement* fieldSet = formCtrl->GetFieldSet();
|
|
|
|
if (fieldSet) {
|
|
|
|
fieldSet->UpdateValidity(IsValid());
|
|
|
|
}
|
2010-09-09 22:08:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-21 11:51:38 -07:00
|
|
|
void
|
2010-08-21 11:52:49 -07:00
|
|
|
nsIConstraintValidation::SetCustomValidity(const nsAString& aError)
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
|
|
|
mCustomValidity.Assign(aError);
|
2010-08-21 10:52:57 -07:00
|
|
|
SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty());
|
2010-08-18 11:28:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 22:08:56 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsIConstraintValidation::SetBarredFromConstraintValidation(bool aBarred)
|
2010-08-18 11:28:08 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool previousBarred = mBarredFromConstraintValidation;
|
2010-09-09 22:08:56 -07:00
|
|
|
|
|
|
|
mBarredFromConstraintValidation = aBarred;
|
|
|
|
|
2013-08-16 17:32:47 -07:00
|
|
|
// Inform the form and fieldset elements if our status regarding constraint
|
|
|
|
// validation is going to change.
|
2010-09-09 22:08:56 -07:00
|
|
|
if (!IsValid() && previousBarred != mBarredFromConstraintValidation) {
|
|
|
|
nsCOMPtr<nsIFormControl> formCtrl = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(formCtrl, "This interface should be used by form elements!");
|
|
|
|
|
2013-08-16 17:32:47 -07:00
|
|
|
// If the element is going to be barred from constraint validation, we can
|
|
|
|
// inform the form and fieldset that we are now valid. Otherwise, we are now
|
|
|
|
// invalid.
|
|
|
|
HTMLFormElement* form =
|
|
|
|
static_cast<HTMLFormElement*>(formCtrl->GetFormElement());
|
2010-09-09 22:08:56 -07:00
|
|
|
if (form) {
|
|
|
|
form->UpdateValidity(aBarred);
|
|
|
|
}
|
2013-08-16 17:32:47 -07:00
|
|
|
HTMLFieldSetElement* fieldSet = formCtrl->GetFieldSet();
|
|
|
|
if (fieldSet) {
|
|
|
|
fieldSet->UpdateValidity(aBarred);
|
|
|
|
}
|
2010-09-09 22:08:56 -07:00
|
|
|
}
|
2010-08-18 11:28:08 -07:00
|
|
|
}
|
|
|
|
|