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 // Only allow \d\w-_.@
EmailFormatter()39 EmailFormatter::EmailFormatter() : BlackListCharsFormatter(std::wregex(L"[^\\d\\w\\-\\._@]+")) {}
40
41 // Allow \d\w-_.~!*'();:@&=+$,/?#[]
UriFormatter()42 UriFormatter::UriFormatter() : BlackListCharsFormatter(std::wregex(L"[^\\d\\w-_.~!*'();:@&=+$,/?#[\\]]+")) {}
43
SingleLineFormatter()44 SingleLineFormatter::SingleLineFormatter() : BlackListCharsFormatter(std::wregex(L"\n")) {}
45
Format(const TextEditingValue & oldValue,TextEditingValue & newValue) const46 void LengthLimitingFormatter::Format(const TextEditingValue& oldValue, TextEditingValue& newValue) const
47 {
48 auto text = newValue.GetWideText();
49 if (text.length() > limit_) {
50 int32_t exceedLen = static_cast<int32_t>(text.length()) - static_cast<int32_t>(limit_);
51 int32_t removeBeforeExtent = std::min(exceedLen, newValue.selection.extentOffset);
52 int32_t removeAfterExtent = exceedLen - removeBeforeExtent;
53 if (removeBeforeExtent > 0) {
54 int32_t eraseStart = newValue.selection.extentOffset - removeBeforeExtent;
55 int32_t preCharIndex = eraseStart - 1;
56 if (preCharIndex >= 0 && preCharIndex < static_cast<int32_t>(text.length()) &&
57 StringUtils::NotInBmp(text[preCharIndex])) {
58 --eraseStart;
59 ++exceedLen;
60 ++removeBeforeExtent;
61 }
62 if (static_cast<size_t>(eraseStart + exceedLen) > text.length()) {
63 return;
64 }
65 text.erase(eraseStart, removeBeforeExtent);
66 newValue.selection.Update(eraseStart);
67 }
68 if (removeAfterExtent > 0) {
69 int32_t eraseStart = text.length() - removeAfterExtent;
70 auto preCharIndex = eraseStart - 1;
71 if (preCharIndex >= 0 && preCharIndex < static_cast<int32_t>(text.length()) &&
72 StringUtils::NotInBmp(text[preCharIndex])) {
73 --eraseStart;
74 }
75 if (eraseStart >= 0 && (static_cast<int32_t>(text.length()) - eraseStart > 0)) {
76 text.erase(eraseStart);
77 }
78 }
79 if (exceedLen > 0) {
80 newValue.text = StringUtils::ToString(text);
81 }
82 }
83 }
84
85 } // namespace OHOS::Ace
86