SingleLineTextInputTypes.h (5430B)
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_SingleLineTextInputTypes_h__ 8 #define mozilla_dom_SingleLineTextInputTypes_h__ 9 10 #include "mozilla/dom/InputType.h" 11 12 namespace mozilla::dom { 13 14 class SingleLineTextInputTypeBase : public InputType { 15 public: 16 ~SingleLineTextInputTypeBase() override = default; 17 18 bool MinAndMaxLengthApply() const final { return true; } 19 bool IsTooLong() const final; 20 bool IsTooShort() const final; 21 bool IsValueMissing() const final; 22 // Can return Nothing() if the JS engine failed to evaluate the pattern. 23 Maybe<bool> HasPatternMismatch() const final; 24 25 protected: 26 explicit SingleLineTextInputTypeBase(HTMLInputElement* aInputElement) 27 : InputType(aInputElement) {} 28 29 bool IsMutable() const override; 30 }; 31 32 // input type=text 33 class TextInputType : public SingleLineTextInputTypeBase { 34 public: 35 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 36 return new (aMemory) TextInputType(aInputElement); 37 } 38 39 private: 40 explicit TextInputType(HTMLInputElement* aInputElement) 41 : SingleLineTextInputTypeBase(aInputElement) {} 42 }; 43 44 // input type=search 45 class SearchInputType : public SingleLineTextInputTypeBase { 46 public: 47 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 48 return new (aMemory) SearchInputType(aInputElement); 49 } 50 51 private: 52 explicit SearchInputType(HTMLInputElement* aInputElement) 53 : SingleLineTextInputTypeBase(aInputElement) {} 54 }; 55 56 // input type=tel 57 class TelInputType : public SingleLineTextInputTypeBase { 58 public: 59 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 60 return new (aMemory) TelInputType(aInputElement); 61 } 62 63 private: 64 explicit TelInputType(HTMLInputElement* aInputElement) 65 : SingleLineTextInputTypeBase(aInputElement) {} 66 }; 67 68 // input type=url 69 class URLInputType : public SingleLineTextInputTypeBase { 70 public: 71 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 72 return new (aMemory) URLInputType(aInputElement); 73 } 74 75 bool HasTypeMismatch() const override; 76 77 nsresult GetTypeMismatchMessage(nsAString& aMessage) override; 78 79 private: 80 explicit URLInputType(HTMLInputElement* aInputElement) 81 : SingleLineTextInputTypeBase(aInputElement) {} 82 }; 83 84 // input type=email 85 class EmailInputType : public SingleLineTextInputTypeBase { 86 public: 87 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 88 return new (aMemory) EmailInputType(aInputElement); 89 } 90 91 bool HasTypeMismatch() const override; 92 bool HasBadInput() const override; 93 94 nsresult GetTypeMismatchMessage(nsAString& aMessage) override; 95 nsresult GetBadInputMessage(nsAString& aMessage) override; 96 97 private: 98 explicit EmailInputType(HTMLInputElement* aInputElement) 99 : SingleLineTextInputTypeBase(aInputElement) {} 100 101 /** 102 * This helper method returns true if aValue is a valid email address. 103 * This is following the HTML5 specification: 104 * http://dev.w3.org/html5/spec/forms.html#valid-e-mail-address 105 * 106 * @param aValue the email address to check. 107 * @result whether the given string is a valid email address. 108 */ 109 static bool IsValidEmailAddress(const nsAString& aValue); 110 111 /** 112 * This helper method returns true if aValue is a valid email address list. 113 * Email address list is a list of email address separated by comas (,) which 114 * can be surrounded by space charecters. 115 * This is following the HTML5 specification: 116 * http://dev.w3.org/html5/spec/forms.html#valid-e-mail-address-list 117 * 118 * @param aValue the email address list to check. 119 * @result whether the given string is a valid email address list. 120 */ 121 static bool IsValidEmailAddressList(const nsAString& aValue); 122 123 /** 124 * Takes aEmail and attempts to convert everything after the first "@" 125 * character (if anything) to punycode before returning the complete result 126 * via the aEncodedEmail out-param. The aIndexOfAt out-param is set to the 127 * index of the "@" character. 128 * 129 * If no "@" is found in aEmail, aEncodedEmail is simply set to aEmail and 130 * the aIndexOfAt out-param is set to kNotFound. 131 * 132 * Returns true in all cases unless an attempt to punycode encode fails. If 133 * false is returned, aEncodedEmail has not been set. 134 * 135 * This function exists because NS_DomainToASCII() splits on ".", meaning that 136 * for 'user.name@sld.tld' it would treat "name@sld" as a label. We want to 137 * encode the domain part only. 138 */ 139 static bool PunycodeEncodeEmailAddress(const nsAString& aEmail, 140 nsAutoCString& aEncodedEmail, 141 uint32_t* aIndexOfAt); 142 }; 143 144 // input type=password 145 class PasswordInputType : public SingleLineTextInputTypeBase { 146 public: 147 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 148 return new (aMemory) PasswordInputType(aInputElement); 149 } 150 151 private: 152 explicit PasswordInputType(HTMLInputElement* aInputElement) 153 : SingleLineTextInputTypeBase(aInputElement) {} 154 }; 155 156 } // namespace mozilla::dom 157 158 #endif /* mozilla_dom_SingleLineTextInputTypes_h__ */