diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/html/ConstraintValidation.cpp | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-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..096e401e82 --- /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() : nsIConstraintValidation() {} + +void ConstraintValidation::SetCustomValidity(const nsAString& aError) { + mCustomValidity.Assign(aError); + SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty()); +} + +} // namespace mozilla::dom |