nsIConstraintValidation.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsIConstraintValidition_h___
  6. #define nsIConstraintValidition_h___
  7. #include "nsISupports.h"
  8. #include "nsString.h"
  9. class nsIDOMValidityState;
  10. namespace mozilla {
  11. namespace dom {
  12. class ValidityState;
  13. } // namespace dom
  14. } // namespace mozilla
  15. #define NS_ICONSTRAINTVALIDATION_IID \
  16. { 0x983829da, 0x1aaf, 0x449c, \
  17. { 0xa3, 0x06, 0x85, 0xd4, 0xf0, 0x31, 0x1c, 0xf6 } }
  18. /**
  19. * This interface is for form elements implementing the validity constraint API.
  20. * See: http://dev.w3.org/html5/spec/forms.html#the-constraint-validation-api
  21. *
  22. * This interface has to be implemented by all elements implementing the API
  23. * and only them.
  24. */
  25. class nsIConstraintValidation : public nsISupports
  26. {
  27. public:
  28. NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONSTRAINTVALIDATION_IID);
  29. friend class mozilla::dom::ValidityState;
  30. static const uint16_t sContentSpecifiedMaxLengthMessage;
  31. virtual ~nsIConstraintValidation();
  32. bool IsValid() const { return mValidityBitField == 0; }
  33. bool IsCandidateForConstraintValidation() const {
  34. return !mBarredFromConstraintValidation;
  35. }
  36. NS_IMETHOD GetValidationMessage(nsAString& aValidationMessage);
  37. enum ValidityStateType
  38. {
  39. VALIDITY_STATE_VALUE_MISSING = 0x1 << 0,
  40. VALIDITY_STATE_TYPE_MISMATCH = 0x1 << 1,
  41. VALIDITY_STATE_PATTERN_MISMATCH = 0x1 << 2,
  42. VALIDITY_STATE_TOO_LONG = 0x1 << 3,
  43. VALIDITY_STATE_TOO_SHORT = 0x1 << 4,
  44. VALIDITY_STATE_RANGE_UNDERFLOW = 0x1 << 5,
  45. VALIDITY_STATE_RANGE_OVERFLOW = 0x1 << 6,
  46. VALIDITY_STATE_STEP_MISMATCH = 0x1 << 7,
  47. VALIDITY_STATE_BAD_INPUT = 0x1 << 8,
  48. VALIDITY_STATE_CUSTOM_ERROR = 0x1 << 9,
  49. };
  50. void SetValidityState(ValidityStateType aState,
  51. bool aValue);
  52. // Web IDL binding methods
  53. bool WillValidate() const {
  54. return IsCandidateForConstraintValidation();
  55. }
  56. mozilla::dom::ValidityState* Validity();
  57. bool CheckValidity();
  58. bool ReportValidity();
  59. protected:
  60. // You can't instantiate an object from that class.
  61. nsIConstraintValidation();
  62. nsresult GetValidity(nsIDOMValidityState** aValidity);
  63. nsresult CheckValidity(bool* aValidity);
  64. void SetCustomValidity(const nsAString& aError);
  65. bool GetValidityState(ValidityStateType aState) const
  66. {
  67. return mValidityBitField & aState;
  68. }
  69. void SetBarredFromConstraintValidation(bool aBarred);
  70. virtual nsresult GetValidationMessage(nsAString& aValidationMessage,
  71. ValidityStateType aType) {
  72. return NS_OK;
  73. }
  74. protected:
  75. /**
  76. * A pointer to the ValidityState object.
  77. */
  78. RefPtr<mozilla::dom::ValidityState> mValidity;
  79. private:
  80. /**
  81. * A bitfield representing the current validity state of the element.
  82. * Each bit represent an error. All bits to zero means the element is valid.
  83. */
  84. int16_t mValidityBitField;
  85. /**
  86. * Keeps track whether the element is barred from constraint validation.
  87. */
  88. bool mBarredFromConstraintValidation;
  89. /**
  90. * The string representing the custom error.
  91. */
  92. nsString mCustomValidity;
  93. };
  94. /**
  95. * Use these macro for class inheriting from nsIConstraintValidation to forward
  96. * functions to nsIConstraintValidation.
  97. */
  98. #define NS_FORWARD_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY \
  99. NS_IMETHOD GetValidity(nsIDOMValidityState** aValidity) { \
  100. return nsIConstraintValidation::GetValidity(aValidity); \
  101. } \
  102. NS_IMETHOD GetWillValidate(bool* aWillValidate) { \
  103. *aWillValidate = WillValidate(); \
  104. return NS_OK; \
  105. } \
  106. NS_IMETHOD GetValidationMessage(nsAString& aValidationMessage) { \
  107. return nsIConstraintValidation::GetValidationMessage(aValidationMessage); \
  108. } \
  109. using nsIConstraintValidation::CheckValidity; \
  110. NS_IMETHOD CheckValidity(bool* aValidity) { \
  111. return nsIConstraintValidation::CheckValidity(aValidity); \
  112. }
  113. #define NS_FORWARD_NSICONSTRAINTVALIDATION \
  114. NS_FORWARD_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY \
  115. NS_IMETHOD SetCustomValidity(const nsAString& aError) { \
  116. nsIConstraintValidation::SetCustomValidity(aError); \
  117. return NS_OK; \
  118. }
  119. /* Use these macro when class declares functions from nsIConstraintValidation */
  120. #define NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(_from) \
  121. NS_IMETHODIMP _from::GetValidity(nsIDOMValidityState** aValidity) { \
  122. return nsIConstraintValidation::GetValidity(aValidity); \
  123. } \
  124. NS_IMETHODIMP _from::GetWillValidate(bool* aWillValidate) { \
  125. *aWillValidate = WillValidate(); \
  126. return NS_OK; \
  127. } \
  128. NS_IMETHODIMP _from::GetValidationMessage(nsAString& aValidationMessage) { \
  129. return nsIConstraintValidation::GetValidationMessage(aValidationMessage); \
  130. } \
  131. NS_IMETHODIMP _from::CheckValidity(bool* aValidity) { \
  132. return nsIConstraintValidation::CheckValidity(aValidity); \
  133. }
  134. #define NS_IMPL_NSICONSTRAINTVALIDATION(_from) \
  135. NS_IMPL_NSICONSTRAINTVALIDATION_EXCEPT_SETCUSTOMVALIDITY(_from) \
  136. NS_IMETHODIMP _from::SetCustomValidity(const nsAString& aError) { \
  137. nsIConstraintValidation::SetCustomValidity(aError); \
  138. return NS_OK; \
  139. }
  140. NS_DEFINE_STATIC_IID_ACCESSOR(nsIConstraintValidation,
  141. NS_ICONSTRAINTVALIDATION_IID)
  142. #endif // nsIConstraintValidation_h___