1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_FORMATTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_FORMATTER_H
18 
19 #include <regex>
20 
21 namespace OHOS::Ace {
22 
23 struct TextEditingValue;
24 
25 class TextInputFormatter {
26 public:
27     virtual ~TextInputFormatter() = default;
28 
29     /**
30      * @brief Format the input text.
31      *
32      * @param[in] oldValue If multiple formatters called, this is the original value of the format chain.
33      * @param[out] newValue The value after formatting.
34      */
35     virtual void Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const = 0;
36 };
37 
38 class BlackListCharsFormatter : public TextInputFormatter {
39 public:
40     explicit BlackListCharsFormatter(std::wregex&& regex);
41     ~BlackListCharsFormatter() override = default;
42 
43     void Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const override;
44 
45 private:
46     std::wregex regex_;
47 };
48 
49 class WhiteListCharsFormatter : public TextInputFormatter {
50 public:
Format(const TextEditingValue & oldValue,TextEditingValue & newValue)51     void Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const override {}
52 };
53 
54 class NumberFormatter : public BlackListCharsFormatter {
55 public:
56     NumberFormatter();
57     ~NumberFormatter() override = default;
58 };
59 
60 class PhoneNumberFormatter : public BlackListCharsFormatter {
61 public:
62     PhoneNumberFormatter();
63     ~PhoneNumberFormatter() override = default;
64 };
65 
66 class UriFormatter : public BlackListCharsFormatter {
67 public:
68     UriFormatter();
69     ~UriFormatter() override = default;
70 };
71 
72 class EmailFormatter : public BlackListCharsFormatter {
73 public:
74     EmailFormatter();
75     ~EmailFormatter() override = default;
76 };
77 
78 class SingleLineFormatter : public BlackListCharsFormatter {
79 public:
80     SingleLineFormatter();
81     ~SingleLineFormatter() override = default;
82 };
83 
84 class LengthLimitingFormatter : public TextInputFormatter {
85 public:
LengthLimitingFormatter(uint32_t limit)86     explicit LengthLimitingFormatter(uint32_t limit) : limit_(limit) {}
87     ~LengthLimitingFormatter() override = default;
88     void Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const override;
89 
90 private:
91     const uint32_t limit_;
92 };
93 
94 } // namespace OHOS::Ace
95 
96 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_FORMATTER_H
97