nsIConstraintValidation.h (3243B)
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 #ifndef nsIConstraintValidition_h___ 8 #define nsIConstraintValidition_h___ 9 10 #include "nsISupports.h" 11 12 class nsIContent; 13 14 namespace mozilla::dom { 15 class ValidityState; 16 } // namespace mozilla::dom 17 18 #define NS_ICONSTRAINTVALIDATION_IID \ 19 {0x983829da, 0x1aaf, 0x449c, {0xa3, 0x06, 0x85, 0xd4, 0xf0, 0x31, 0x1c, 0xf6}} 20 21 /** 22 * This interface is for form elements implementing the validity constraint API. 23 * See: http://dev.w3.org/html5/spec/forms.html#the-constraint-validation-api 24 * 25 * This interface has to be implemented by all elements implementing the API 26 * and only them. 27 */ 28 class nsIConstraintValidation : public nsISupports { 29 public: 30 NS_INLINE_DECL_STATIC_IID(NS_ICONSTRAINTVALIDATION_IID); 31 32 friend class mozilla::dom::ValidityState; 33 34 static const uint16_t sContentSpecifiedMaxLengthMessage; 35 36 virtual ~nsIConstraintValidation(); 37 38 bool IsValid() const { return mValidityBitField == 0; } 39 40 bool IsCandidateForConstraintValidation() const { 41 return !mBarredFromConstraintValidation; 42 } 43 44 enum ValidityStateType { 45 VALIDITY_STATE_VALUE_MISSING = 0x1 << 0, 46 VALIDITY_STATE_TYPE_MISMATCH = 0x1 << 1, 47 VALIDITY_STATE_PATTERN_MISMATCH = 0x1 << 2, 48 VALIDITY_STATE_TOO_LONG = 0x1 << 3, 49 VALIDITY_STATE_TOO_SHORT = 0x1 << 4, 50 VALIDITY_STATE_RANGE_UNDERFLOW = 0x1 << 5, 51 VALIDITY_STATE_RANGE_OVERFLOW = 0x1 << 6, 52 VALIDITY_STATE_STEP_MISMATCH = 0x1 << 7, 53 VALIDITY_STATE_BAD_INPUT = 0x1 << 8, 54 VALIDITY_STATE_CUSTOM_ERROR = 0x1 << 9, 55 }; 56 57 void SetValidityState(ValidityStateType aState, bool aValue); 58 59 /** 60 * Check the validity of this object. If it is not valid, file a "invalid" 61 * event on the aEventTarget. 62 * 63 * @param aEventTarget The target of the event. 64 * @param aDefaultAction Set to true if default action should be taken, 65 * see EventTarget::DispatchEvent. 66 * @return whether it's valid. 67 */ 68 bool CheckValidity(nsIContent& aEventTarget, 69 bool* aEventDefaultAction = nullptr) const; 70 71 // Web IDL binding methods 72 bool WillValidate() const { return IsCandidateForConstraintValidation(); } 73 mozilla::dom::ValidityState* Validity(); 74 bool ReportValidity(); 75 76 protected: 77 // You can't instantiate an object from that class. 78 nsIConstraintValidation(); 79 80 bool GetValidityState(ValidityStateType aState) const { 81 return mValidityBitField & aState; 82 } 83 84 void SetBarredFromConstraintValidation(bool aBarred); 85 86 /** 87 * A pointer to the ValidityState object. 88 */ 89 RefPtr<mozilla::dom::ValidityState> mValidity; 90 91 private: 92 /** 93 * A bitfield representing the current validity state of the element. 94 * Each bit represent an error. All bits to zero means the element is valid. 95 */ 96 int16_t mValidityBitField; 97 98 /** 99 * Keeps track whether the element is barred from constraint validation. 100 */ 101 bool mBarredFromConstraintValidation; 102 }; 103 104 #endif // nsIConstraintValidation_h___