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