1 /*
2  * Copyright (c) 2023-2023 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_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H
18 
19 #include <string>
20 #include <utility>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/string_utils.h"
25 #include "core/common/ime/text_input_type.h"
26 #include "core/components_ng/pattern/pattern.h"
27 
28 namespace OHOS::Ace::NG {
29 class ContentController : public virtual AceType {
30     DECLARE_ACE_TYPE(ContentController, AceType);
31 
32 public:
ContentController(const WeakPtr<Pattern> & pattern)33     explicit ContentController(const WeakPtr<Pattern>& pattern) : pattern_(pattern) {}
34     ~ContentController() override = default;
35     bool InsertValue(int32_t index, const std::string& value);
36     bool ReplaceSelectedValue(int32_t startIndex, int32_t endIndex, const std::string& value);
37     std::string GetSelectedValue(int32_t startIndex, int32_t endIndex);
38     std::string GetValueBeforeIndex(int32_t index);
39     std::string GetValueAfterIndex(int32_t index);
40     void erase(int32_t startIndex, int32_t length);
41     int32_t Delete(int32_t startIndex, int32_t length, bool isBackward);
42     int32_t GetDeleteLength(int32_t startIndex, int32_t length, bool isBackward);
43     bool IsIndexBeforeOrInEmoji(int32_t index);
44     void FilterValue();
45     void FilterValueType(std::string& value);
46     std::string GetSelectedLimitValue(int32_t& index, int32_t& startIndex);
47     void FilterTextInputStyle(bool& textChanged, std::string& result);
48 
GetWideText()49     std::wstring GetWideText()
50     {
51         return StringUtils::ToWstring(content_);
52     }
53 
GetTextValue()54     std::string GetTextValue()
55     {
56         return content_;
57     }
58 
IsEmpty()59     bool IsEmpty()
60     {
61         return content_.empty();
62     }
63 
SetTextValue(std::string && value)64     void SetTextValue(std::string&& value)
65     {
66         content_ = value;
67         FilterValue();
68     }
69 
SetTextValue(const std::string & value)70     void SetTextValue(const std::string& value)
71     {
72         content_ = value;
73         FilterValue();
74     }
75 
SetTextValueOnly(std::string && value)76     void SetTextValueOnly(std::string&& value)
77     {
78         content_ = value;
79     }
80 
Reset()81     void Reset()
82     {
83         content_ = "";
84     }
85 
GetInsertValue()86     std::string GetInsertValue()
87     {
88         return insertValue_;
89     }
90 
91 private:
92     void FormatIndex(int32_t& startIndex, int32_t& endIndex);
93     bool FilterWithEvent(const std::string& filter, std::string& result);
94     std::string PreprocessString(int32_t startIndex, int32_t endIndex, const std::string& value);
95     static std::string RemoveErrorTextFromValue(const std::string& value, const std::string& errorText);
96     static std::string FilterWithRegex(const std::string& filter, std::string& result);
97     static bool FilterWithEmail(std::string& result);
98     static bool FilterWithAscii(std::string& result);
99     static bool FilterWithDecimal(std::string& result);
100 
101     std::string insertValue_;
102     std::string content_;
103     WeakPtr<Pattern> pattern_;
104 };
105 } // namespace OHOS::Ace::NG
106 
107 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H