1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "ConstraintValidation.h"
8
9 #include "mozilla/ErrorResult.h"
10 #include "nsAString.h"
11 #include "nsIContent.h"
12
13 namespace mozilla::dom {
14
GetValidationMessage(nsAString & aValidationMessage,ErrorResult & aError)15 void ConstraintValidation::GetValidationMessage(nsAString& aValidationMessage,
16 ErrorResult& aError) {
17 aValidationMessage.Truncate();
18
19 if (IsCandidateForConstraintValidation() && !IsValid()) {
20 if (GetValidityState(VALIDITY_STATE_CUSTOM_ERROR)) {
21 aValidationMessage.Assign(mCustomValidity);
22 if (aValidationMessage.Length() > sContentSpecifiedMaxLengthMessage) {
23 aValidationMessage.Truncate(sContentSpecifiedMaxLengthMessage);
24 }
25 } else if (GetValidityState(VALIDITY_STATE_TOO_LONG)) {
26 GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_LONG);
27 } else if (GetValidityState(VALIDITY_STATE_TOO_SHORT)) {
28 GetValidationMessage(aValidationMessage, VALIDITY_STATE_TOO_SHORT);
29 } else if (GetValidityState(VALIDITY_STATE_VALUE_MISSING)) {
30 GetValidationMessage(aValidationMessage, VALIDITY_STATE_VALUE_MISSING);
31 } else if (GetValidityState(VALIDITY_STATE_TYPE_MISMATCH)) {
32 GetValidationMessage(aValidationMessage, VALIDITY_STATE_TYPE_MISMATCH);
33 } else if (GetValidityState(VALIDITY_STATE_PATTERN_MISMATCH)) {
34 GetValidationMessage(aValidationMessage, VALIDITY_STATE_PATTERN_MISMATCH);
35 } else if (GetValidityState(VALIDITY_STATE_RANGE_OVERFLOW)) {
36 GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_OVERFLOW);
37 } else if (GetValidityState(VALIDITY_STATE_RANGE_UNDERFLOW)) {
38 GetValidationMessage(aValidationMessage, VALIDITY_STATE_RANGE_UNDERFLOW);
39 } else if (GetValidityState(VALIDITY_STATE_STEP_MISMATCH)) {
40 GetValidationMessage(aValidationMessage, VALIDITY_STATE_STEP_MISMATCH);
41 } else if (GetValidityState(VALIDITY_STATE_BAD_INPUT)) {
42 GetValidationMessage(aValidationMessage, VALIDITY_STATE_BAD_INPUT);
43 } else {
44 // There should not be other validity states.
45 aError.Throw(NS_ERROR_UNEXPECTED);
46 return;
47 }
48 } else {
49 aValidationMessage.Truncate();
50 }
51 }
52
CheckValidity()53 bool ConstraintValidation::CheckValidity() {
54 nsCOMPtr<nsIContent> content = do_QueryInterface(this);
55 MOZ_ASSERT(content, "This class should be inherited by HTML elements only!");
56 return nsIConstraintValidation::CheckValidity(*content);
57 }
58
ConstraintValidation()59 ConstraintValidation::ConstraintValidation() : nsIConstraintValidation() {}
60
SetCustomValidity(const nsAString & aError)61 void ConstraintValidation::SetCustomValidity(const nsAString& aError) {
62 mCustomValidity.Assign(aError);
63 SetValidityState(VALIDITY_STATE_CUSTOM_ERROR, !mCustomValidity.IsEmpty());
64 }
65
66 } // namespace mozilla::dom
67