1 : /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (original author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsIConstraintValidition_h___
39 : #define nsIConstraintValidition_h___
40 :
41 : #include "nsISupports.h"
42 : #include "nsAutoPtr.h"
43 : #include "nsString.h"
44 :
45 : class nsDOMValidityState;
46 : class nsIDOMValidityState;
47 :
48 : #define NS_ICONSTRAINTVALIDATION_IID \
49 : { 0xca3824dc, 0x4f5c, 0x4878, \
50 : { 0xa6, 0x8a, 0x95, 0x54, 0x5f, 0xfa, 0x4b, 0xf9 } }
51 :
52 : /**
53 : * This interface is for form elements implementing the validity constraint API.
54 : * See: http://dev.w3.org/html5/spec/forms.html#the-constraint-validation-api
55 : *
56 : * This interface has to be implemented by all elements implementing the API
57 : * and only them.
58 : */
59 : class nsIConstraintValidation : public nsISupports
60 : {
61 : public:
62 :
63 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONSTRAINTVALIDATION_IID);
64 :
65 : friend class nsDOMValidityState;
66 :
67 : static const PRUint16 sContentSpecifiedMaxLengthMessage;
68 :
69 : virtual ~nsIConstraintValidation();
70 :
71 25 : bool IsValid() const { return mValidityBitField == 0; }
72 :
73 5 : bool IsCandidateForConstraintValidation() const {
74 5 : return !mBarredFromConstraintValidation;
75 : }
76 :
77 : NS_IMETHOD GetValidationMessage(nsAString& aValidationMessage);
78 :
79 : enum ValidityStateType
80 : {
81 : VALIDITY_STATE_VALUE_MISSING = 0x01, // 0b00000001
82 : VALIDITY_STATE_TYPE_MISMATCH = 0x02, // 0b00000010
83 : VALIDITY_STATE_PATTERN_MISMATCH = 0x04, // 0b00000100
84 : VALIDITY_STATE_TOO_LONG = 0x08, // 0b00001000
85 : VALIDITY_STATE_RANGE_UNDERFLOW = 0x10, // 0b00010000
86 : VALIDITY_STATE_RANGE_OVERFLOW = 0x20, // 0b00100000
87 : VALIDITY_STATE_STEP_MISMATCH = 0x40, // 0b01000000
88 : VALIDITY_STATE_CUSTOM_ERROR = 0x80 // 0b10000000
89 : };
90 :
91 : void SetValidityState(ValidityStateType mState,
92 : bool mValue);
93 :
94 : protected:
95 :
96 : // You can't instantiate an object from that class.
97 : nsIConstraintValidation();
98 :
99 : nsresult GetValidity(nsIDOMValidityState** aValidity);
100 : nsresult CheckValidity(bool* aValidity);
101 : void SetCustomValidity(const nsAString& aError);
102 :
103 0 : bool GetValidityState(ValidityStateType mState) const {
104 0 : return mValidityBitField & mState;
105 : }
106 :
107 : void SetBarredFromConstraintValidation(bool aBarred);
108 :
109 0 : virtual nsresult GetValidationMessage(nsAString& aValidationMessage,
110 : ValidityStateType aType) {
111 0 : return NS_OK;
112 : }
113 :
114 : private:
115 :
116 : /**
117 : * A bitfield representing the current validity state of the element.
118 : * Each bit represent an error. All bits to zero means the element is valid.
119 : */
120 : PRInt8 mValidityBitField;
121 :
122 : /**
123 : * A pointer to the ValidityState object.
124 : */
125 : nsRefPtr<nsDOMValidityState> mValidity;
126 :
127 : /**
128 : * Keeps track whether the element is barred from constraint validation.
129 : */
130 : bool mBarredFromConstraintValidation;
131 :
132 : /**
133 : * The string representing the custom error.
134 : */
135 : nsString mCustomValidity;
136 : };
137 :
138 : /**
139 : * Use these macro for class inheriting from nsIConstraintValidation to forward
140 : * functions to nsIConstraintValidation.
141 : */
142 : #define NS_FORWARD_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY \
143 : NS_IMETHOD GetValidity(nsIDOMValidityState** aValidity) { \
144 : return nsIConstraintValidation::GetValidity(aValidity); \
145 : } \
146 : NS_IMETHOD GetWillValidate(bool* aWillValidate) { \
147 : *aWillValidate = IsCandidateForConstraintValidation(); \
148 : return NS_OK; \
149 : } \
150 : NS_IMETHOD GetValidationMessage(nsAString& aValidationMessage) { \
151 : return nsIConstraintValidation::GetValidationMessage(aValidationMessage); \
152 : } \
153 : NS_IMETHOD CheckValidity(bool* aValidity) { \
154 : return nsIConstraintValidation::CheckValidity(aValidity); \
155 : }
156 :
157 : #define NS_FORWARD_NSICONSTRAINTVALIDATION \
158 : NS_FORWARD_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY \
159 : NS_IMETHOD SetCustomValidity(const nsAString& aError) { \
160 : nsIConstraintValidation::SetCustomValidity(aError); \
161 : return NS_OK; \
162 : }
163 :
164 :
165 : /* Use these macro when class declares functions from nsIConstraintValidation */
166 : #define NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(_from) \
167 : NS_IMETHODIMP _from::GetValidity(nsIDOMValidityState** aValidity) { \
168 : return nsIConstraintValidation::GetValidity(aValidity); \
169 : } \
170 : NS_IMETHODIMP _from::GetWillValidate(bool* aWillValidate) { \
171 : *aWillValidate = IsCandidateForConstraintValidation(); \
172 : return NS_OK; \
173 : } \
174 : NS_IMETHODIMP _from::GetValidationMessage(nsAString& aValidationMessage) { \
175 : return nsIConstraintValidation::GetValidationMessage(aValidationMessage); \
176 : } \
177 : NS_IMETHODIMP _from::CheckValidity(bool* aValidity) { \
178 : return nsIConstraintValidation::CheckValidity(aValidity); \
179 : }
180 :
181 : #define NS_IMPL_NSICONSTRAINTVALIDATION(_from) \
182 : NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(_from) \
183 : NS_IMETHODIMP _from::SetCustomValidity(const nsAString& aError) { \
184 : nsIConstraintValidation::SetCustomValidity(aError); \
185 : return NS_OK; \
186 : }
187 :
188 : NS_DEFINE_STATIC_IID_ACCESSOR(nsIConstraintValidation,
189 : NS_ICONSTRAINTVALIDATION_IID)
190 :
191 : #endif // nsIConstraintValidation_h___
192 :
|