tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ValidityState.h (2873B)


      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 mozilla_dom_ValidityState_h
      8 #define mozilla_dom_ValidityState_h
      9 
     10 #include "js/TypeDecls.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsIConstraintValidation.h"
     13 #include "nsWrapperCache.h"
     14 
     15 namespace mozilla::dom {
     16 
     17 class ValidityState final : public nsISupports, public nsWrapperCache {
     18  ~ValidityState() = default;
     19 
     20 public:
     21  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     22  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ValidityState)
     23 
     24  friend class ::nsIConstraintValidation;
     25 
     26  nsIConstraintValidation* GetParentObject() const {
     27    return mConstraintValidation;
     28  }
     29 
     30  virtual JSObject* WrapObject(JSContext* aCx,
     31                               JS::Handle<JSObject*> aGivenProto) override;
     32 
     33  // Web IDL methods
     34  bool ValueMissing() const {
     35    return GetValidityState(
     36        nsIConstraintValidation::VALIDITY_STATE_VALUE_MISSING);
     37  }
     38  bool TypeMismatch() const {
     39    return GetValidityState(
     40        nsIConstraintValidation::VALIDITY_STATE_TYPE_MISMATCH);
     41  }
     42  bool PatternMismatch() const {
     43    return GetValidityState(
     44        nsIConstraintValidation::VALIDITY_STATE_PATTERN_MISMATCH);
     45  }
     46  bool TooLong() const {
     47    return GetValidityState(nsIConstraintValidation::VALIDITY_STATE_TOO_LONG);
     48  }
     49  bool TooShort() const {
     50    return GetValidityState(nsIConstraintValidation::VALIDITY_STATE_TOO_SHORT);
     51  }
     52  bool RangeUnderflow() const {
     53    return GetValidityState(
     54        nsIConstraintValidation::VALIDITY_STATE_RANGE_UNDERFLOW);
     55  }
     56  bool RangeOverflow() const {
     57    return GetValidityState(
     58        nsIConstraintValidation::VALIDITY_STATE_RANGE_OVERFLOW);
     59  }
     60  bool StepMismatch() const {
     61    return GetValidityState(
     62        nsIConstraintValidation::VALIDITY_STATE_STEP_MISMATCH);
     63  }
     64  bool BadInput() const {
     65    return GetValidityState(nsIConstraintValidation::VALIDITY_STATE_BAD_INPUT);
     66  }
     67  bool CustomError() const {
     68    return GetValidityState(
     69        nsIConstraintValidation::VALIDITY_STATE_CUSTOM_ERROR);
     70  }
     71  bool Valid() const {
     72    return !mConstraintValidation || mConstraintValidation->IsValid();
     73  }
     74 
     75 protected:
     76  explicit ValidityState(nsIConstraintValidation* aConstraintValidation);
     77 
     78  /**
     79   * Helper function to get a validity state from constraint validation
     80   * instance.
     81   */
     82  inline bool GetValidityState(
     83      nsIConstraintValidation::ValidityStateType aState) const {
     84    return mConstraintValidation &&
     85           mConstraintValidation->GetValidityState(aState);
     86  }
     87 
     88  nsCOMPtr<nsIConstraintValidation> mConstraintValidation;
     89 };
     90 
     91 }  // namespace mozilla::dom
     92 
     93 #endif  // mozilla_dom_ValidityState_h