diff options
Diffstat (limited to 'dom/html/ConstraintValidation.cpp')
-rw-r--r-- | dom/html/ConstraintValidation.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/dom/html/ConstraintValidation.cpp b/dom/html/ConstraintValidation.cpp new file mode 100644 index 0000000000..1d256a3092 --- /dev/null +++ b/dom/html/ConstraintValidation.cpp @@ -0,0 +1,66 @@ +/* -*- 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 "ConstraintValidation.h" + +#include "mozilla/ErrorResult.h" +#include "nsAString.h" +#include "nsIContent.h" + +namespace mozilla::dom { + +void ConstraintValidation::GetValidationMessage(nsAString& aValidationMessage, + ErrorResult& aError) { + aValidationMessage.Truncate(); + + if (IsCandidateForConstraintValidation() && !IsValid()) { + if (GetValidityState(VALIDITY_STATE_CUSTOM_ERROR)) { + aValidationMessage.Assign(mCustomValidity); + if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) { + aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage); + } + } else if (GetValidityState(VALIDITY_STATE_TOO_LONG)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_LONG); + } else if (GetValidityState(VALIDITY_STATE_TOO_SHORT)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_SHORT); + } 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); + } else if (GetValidityState(VALIDITY_STATE_RANGE_OVERFLOW)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_OVERFLOW); + } else if (GetValidityState(VALIDITY_STATE_RANGE_UNDERFLOW)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_UNDERFLOW); + } else if (GetValidityState(VALIDITY_STATE_STEP_MISMATCH)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_STEP_MISMATCH); + } else if (GetValidityState(VALIDITY_STATE_BAD_INPUT)) { + GetValidationMessage(aValidationMessage, VALIDITY_STATE_BAD_INPUT); + } else { + // There should not be other validity states. + aError.Throw(NS_ERROR_UNEXPECTED); + return; + } + } else { + aValidationMessage.Truncate(); + } +} + +bool ConstraintValidation::CheckValidity() { + nsCOMPtr<nsIContent> content = do_QueryInterface(this); + MOZ_ASSERT(content, "This class should be inherited by HTML elements only!"); + return nsIConstraintValidation::CheckValidity(*content); +} + +ConstraintValidation::ConstraintValidation() = default; + +void ConstraintValidation::SetCustomValidity(const nsAString& aError) { + mCustomValidity.Assign(aError); + SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty()); +} + +} // namespace mozilla::dom |