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 #include "core/common/ime/text_input_formatter.h"
17 
18 #include "base/utils/string_utils.h"
19 #include "core/common/ime/text_editing_value.h"
20 
21 namespace OHOS::Ace {
22 
BlackListCharsFormatter(std::wregex && regex)23 BlackListCharsFormatter::BlackListCharsFormatter(std::wregex&& regex) : regex_(regex) {}
24 
Format(const TextEditingValue & oldValue,TextEditingValue & newValue) const25 void BlackListCharsFormatter::Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const
26 {
27     newValue.SelectionAwareTextManipulation(
28         [this](std::wstring& manipulateText) { manipulateText = std::regex_replace(manipulateText, regex_, L""); });
29 }
30 
31 // Only allow \d.-e
NumberFormatter()32 NumberFormatter::NumberFormatter() : BlackListCharsFormatter(std::wregex(L"[^\\d.\\-e]+")) {}
33 
34 // Only allow \d-+
PhoneNumberFormatter()35 PhoneNumberFormatter::PhoneNumberFormatter() : BlackListCharsFormatter(std::wregex(L"[^\\d\\-\\+\\*\\#]+")) {}
36 
37 // Use Common specifications
EmailFormatter()38 EmailFormatter::EmailFormatter()
39     : BlackListCharsFormatter(std::wregex(L"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{1,}$"))
40 {}
41 
42 // Allow \d\w-_.~!*'();:@&=+$,/?#[]
UriFormatter()43 UriFormatter::UriFormatter() : BlackListCharsFormatter(std::wregex(L"[^\\d\\w-_.~!*'();:@&=+$,/?#[\\]]+")) {}
44 
SingleLineFormatter()45 SingleLineFormatter::SingleLineFormatter() : BlackListCharsFormatter(std::wregex(L"\n")) {}
46 
Format(const TextEditingValue & oldValue,TextEditingValue & newValue) const47 void LengthLimitingFormatter::Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const
48 {
49     auto text = newValue.GetWideText();
50     if (text.length() > limit_) {
51         int32_t exceedLen = static_cast<int32_t>(text.length()) - static_cast<int32_t>(limit_);
52         int32_t removeBeforeExtent = std::min(exceedLen, newValue.selection.extentOffset);
53         int32_t removeAfterExtent = exceedLen - removeBeforeExtent;
54         if (removeBeforeExtent > 0) {
55             int32_t eraseStart = newValue.selection.extentOffset - removeBeforeExtent;
56             int32_t preCharIndex = eraseStart - 1;
57             if (preCharIndex >= 0 && preCharIndex < static_cast<int32_t>(text.length()) &&
58                 StringUtils::NotInBmp(text[preCharIndex])) {
59                 --eraseStart;
60                 ++exceedLen;
61                 ++removeBeforeExtent;
62             }
63             if (static_cast<size_t>(eraseStart + exceedLen) > text.length()) {
64                 return;
65             }
66             text.erase(eraseStart, removeBeforeExtent);
67             newValue.selection.Update(eraseStart);
68         }
69         if (removeAfterExtent > 0) {
70             int32_t eraseStart = static_cast<int32_t>(text.length()) - removeAfterExtent;
71             auto preCharIndex = eraseStart - 1;
72             if (preCharIndex >= 0 && preCharIndex < static_cast<int32_t>(text.length()) &&
73                 StringUtils::NotInBmp(text[preCharIndex])) {
74                 --eraseStart;
75             }
76             if (eraseStart >= 0 && (static_cast<int32_t>(text.length()) - eraseStart > 0)) {
77                 text.erase(eraseStart);
78             }
79         }
80         if (exceedLen > 0) {
81             newValue.text = StringUtils::ToString(text);
82         }
83     }
84 }
85 
86 } // namespace OHOS::Ace
87