2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 22:28:08 +04:00
|
|
|
|
2010-08-21 22:52:49 +04:00
|
|
|
#include "nsIConstraintValidation.h"
|
2010-08-18 22:28:08 +04:00
|
|
|
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2013-06-19 18:24:37 +04:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2013-08-17 04:32:47 +04:00
|
|
|
#include "mozilla/dom/HTMLFieldSetElement.h"
|
2016-04-15 11:13:00 +03:00
|
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
2013-01-06 23:20:02 +04:00
|
|
|
#include "mozilla/dom/ValidityState.h"
|
2010-09-10 09:08:56 +04:00
|
|
|
#include "nsIFormControl.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h"
|
2010-08-21 21:52:57 +04:00
|
|
|
|
2016-04-15 11:13:00 +03:00
|
|
|
#include "nsIFormSubmitObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint16_t nsIConstraintValidation::sContentSpecifiedMaxLengthMessage = 256;
|
2010-10-26 02:02:24 +04:00
|
|
|
|
2013-08-17 04:32:47 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-08-21 22:52:49 +04:00
|
|
|
nsIConstraintValidation::nsIConstraintValidation()
|
2010-08-21 21:52:57 +04:00
|
|
|
: mValidityBitField(0)
|
2010-09-10 09:08:56 +04:00
|
|
|
// By default, all elements are subjects to constraint validation.
|
2011-10-17 18:59:28 +04:00
|
|
|
, mBarredFromConstraintValidation(false)
|
2010-08-21 21:52:57 +04:00
|
|
|
{
|
|
|
|
}
|
2010-08-18 22:28:08 +04:00
|
|
|
|
2010-08-21 22:52:49 +04:00
|
|
|
nsIConstraintValidation::~nsIConstraintValidation()
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-16 22:01:01 +04:00
|
|
|
mozilla::dom::ValidityState*
|
|
|
|
nsIConstraintValidation::Validity()
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
|
|
|
if (!mValidity) {
|
2013-01-06 23:20:02 +04:00
|
|
|
mValidity = new mozilla::dom::ValidityState(this);
|
2010-08-18 22:28:08 +04:00
|
|
|
}
|
|
|
|
|
2013-01-16 22:01:01 +04:00
|
|
|
return mValidity;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIConstraintValidation::GetValidity(nsIDOMValidityState** aValidity)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aValidity);
|
|
|
|
|
|
|
|
NS_ADDREF(*aValidity = Validity());
|
2010-08-18 22:28:08 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-27 09:46:17 +04:00
|
|
|
NS_IMETHODIMP
|
2010-08-21 22:52:49 +04:00
|
|
|
nsIConstraintValidation::GetValidationMessage(nsAString& aValidationMessage)
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
|
|
|
aValidationMessage.Truncate();
|
|
|
|
|
2010-08-21 22:51:38 +04:00
|
|
|
if (IsCandidateForConstraintValidation() && !IsValid()) {
|
2010-10-15 17:05:14 +04: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-26 02:02:24 +04:00
|
|
|
if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) {
|
|
|
|
aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage);
|
|
|
|
}
|
2010-10-15 17:05:14 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_CUSTOM_ERROR)) {
|
2010-08-18 22:28:08 +04:00
|
|
|
aValidationMessage.Assign(mCustomValidity);
|
2010-10-26 02:02:24 +04:00
|
|
|
if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) {
|
|
|
|
aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage);
|
|
|
|
}
|
2010-08-21 21:52:57 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_TOO_LONG)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_LONG);
|
2016-08-17 07:11:24 +03:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_TOO_SHORT)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_SHORT);
|
2010-08-21 21:52:57 +04:00
|
|
|
} 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 13:36:24 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_RANGE_OVERFLOW)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_OVERFLOW);
|
2012-06-22 13:38:20 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_RANGE_UNDERFLOW)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_UNDERFLOW);
|
2012-06-22 23:02:20 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_STEP_MISMATCH)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_STEP_MISMATCH);
|
2014-01-30 16:54:12 +04:00
|
|
|
} else if (GetValidityState(VALIDITY_STATE_BAD_INPUT)) {
|
|
|
|
GetValidationMessage(aValidationMessage, VALIDITY_STATE_BAD_INPUT);
|
2010-08-18 22:28:08 +04:00
|
|
|
} else {
|
2012-06-22 23:02:20 +04:00
|
|
|
// There should not be other validity states.
|
2010-08-18 22:28:08 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aValidationMessage.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-16 22:01:01 +04:00
|
|
|
bool
|
|
|
|
nsIConstraintValidation::CheckValidity()
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
2010-08-21 22:51:38 +04:00
|
|
|
if (!IsCandidateForConstraintValidation() || IsValid()) {
|
2013-01-16 22:01:01 +04:00
|
|
|
return true;
|
2010-08-18 22:28:08 +04:00
|
|
|
}
|
|
|
|
|
2010-08-21 22:51:38 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(content, "This class should be inherited by HTML elements only!");
|
|
|
|
|
2013-01-16 22:01:01 +04: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 22:28:08 +04:00
|
|
|
}
|
|
|
|
|
2016-04-15 11:13:00 +03:00
|
|
|
bool
|
|
|
|
nsIConstraintValidation::ReportValidity()
|
|
|
|
{
|
|
|
|
if (!IsCandidateForConstraintValidation() || IsValid()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(this);
|
|
|
|
MOZ_ASSERT(content, "This class should be inherited by HTML elements only!");
|
|
|
|
|
|
|
|
bool defaultAction = true;
|
|
|
|
nsContentUtils::DispatchTrustedEvent(content->OwnerDoc(), content,
|
|
|
|
NS_LITERAL_STRING("invalid"),
|
|
|
|
false, true, &defaultAction);
|
|
|
|
if (!defaultAction) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> service =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!service) {
|
|
|
|
NS_WARNING("No observer service available!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> theEnum;
|
|
|
|
nsresult rv = service->EnumerateObservers(NS_INVALIDFORMSUBMIT_SUBJECT,
|
|
|
|
getter_AddRefs(theEnum));
|
|
|
|
|
|
|
|
// Return true on error here because that's what we always did
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
|
|
|
|
bool hasObserver = false;
|
|
|
|
rv = theEnum->HasMoreElements(&hasObserver);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMutableArray> invalidElements =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
|
|
|
invalidElements->AppendElement(content, false);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
nsCOMPtr<nsISupports> inst;
|
|
|
|
nsCOMPtr<nsIFormSubmitObserver> observer;
|
|
|
|
bool more = true;
|
|
|
|
while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
|
|
|
|
theEnum->GetNext(getter_AddRefs(inst));
|
|
|
|
observer = do_QueryInterface(inst);
|
|
|
|
|
|
|
|
if (observer) {
|
|
|
|
observer->NotifyInvalidSubmit(nullptr, invalidElements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content->IsHTMLElement(nsGkAtoms::input) &&
|
|
|
|
nsContentUtils::IsFocusedContent(content)) {
|
|
|
|
HTMLInputElement* inputElement =
|
|
|
|
HTMLInputElement::FromContentOrNull(content);
|
|
|
|
|
|
|
|
inputElement->UpdateValidityUIBits(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::Element* element = content->AsElement();
|
|
|
|
element->UpdateState(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-10 09:08:56 +04:00
|
|
|
void
|
|
|
|
nsIConstraintValidation::SetValidityState(ValidityStateType aState,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aValue)
|
2010-09-10 09:08:56 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool previousValidity = IsValid();
|
2010-09-10 09:08:56 +04:00
|
|
|
|
|
|
|
if (aValue) {
|
|
|
|
mValidityBitField |= aState;
|
|
|
|
} else {
|
|
|
|
mValidityBitField &= ~aState;
|
|
|
|
}
|
|
|
|
|
2013-08-17 04:32:47 +04:00
|
|
|
// Inform the form and fieldset elements if our validity has changed.
|
2010-09-10 09:08:56 +04:00
|
|
|
if (previousValidity != IsValid() && IsCandidateForConstraintValidation()) {
|
|
|
|
nsCOMPtr<nsIFormControl> formCtrl = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(formCtrl, "This interface should be used by form elements!");
|
|
|
|
|
2013-08-17 04:32:47 +04:00
|
|
|
HTMLFormElement* form =
|
|
|
|
static_cast<HTMLFormElement*>(formCtrl->GetFormElement());
|
2010-09-10 09:08:56 +04:00
|
|
|
if (form) {
|
|
|
|
form->UpdateValidity(IsValid());
|
|
|
|
}
|
2013-08-17 04:32:47 +04:00
|
|
|
HTMLFieldSetElement* fieldSet = formCtrl->GetFieldSet();
|
|
|
|
if (fieldSet) {
|
|
|
|
fieldSet->UpdateValidity(IsValid());
|
|
|
|
}
|
2010-09-10 09:08:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-21 22:51:38 +04:00
|
|
|
void
|
2010-08-21 22:52:49 +04:00
|
|
|
nsIConstraintValidation::SetCustomValidity(const nsAString& aError)
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
|
|
|
mCustomValidity.Assign(aError);
|
2010-08-21 21:52:57 +04:00
|
|
|
SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty());
|
2010-08-18 22:28:08 +04:00
|
|
|
}
|
|
|
|
|
2010-09-10 09:08:56 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsIConstraintValidation::SetBarredFromConstraintValidation(bool aBarred)
|
2010-08-18 22:28:08 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool previousBarred = mBarredFromConstraintValidation;
|
2010-09-10 09:08:56 +04:00
|
|
|
|
|
|
|
mBarredFromConstraintValidation = aBarred;
|
|
|
|
|
2013-08-17 04:32:47 +04:00
|
|
|
// Inform the form and fieldset elements if our status regarding constraint
|
|
|
|
// validation is going to change.
|
2010-09-10 09:08:56 +04:00
|
|
|
if (!IsValid() && previousBarred != mBarredFromConstraintValidation) {
|
|
|
|
nsCOMPtr<nsIFormControl> formCtrl = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(formCtrl, "This interface should be used by form elements!");
|
|
|
|
|
2013-08-17 04:32:47 +04: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-10 09:08:56 +04:00
|
|
|
if (form) {
|
|
|
|
form->UpdateValidity(aBarred);
|
|
|
|
}
|
2013-08-17 04:32:47 +04:00
|
|
|
HTMLFieldSetElement* fieldSet = formCtrl->GetFieldSet();
|
|
|
|
if (fieldSet) {
|
|
|
|
fieldSet->UpdateValidity(aBarred);
|
|
|
|
}
|
2010-09-10 09:08:56 +04:00
|
|
|
}
|
2010-08-18 22:28:08 +04:00
|
|
|
}
|
|
|
|
|