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 void FilterValue(); 42 void FilterValueType(std::string& value); 43 std::string GetSelectedLimitValue(int32_t& index, int32_t& startIndex); 44 GetWideText()45 std::wstring GetWideText() 46 { 47 return StringUtils::ToWstring(content_); 48 } 49 GetTextValue()50 std::string GetTextValue() 51 { 52 return content_; 53 } 54 IsEmpty()55 bool IsEmpty() 56 { 57 return content_.empty(); 58 } 59 SetTextValue(std::string && value)60 void SetTextValue(std::string&& value) 61 { 62 content_ = value; 63 FilterValue(); 64 } 65 SetTextValue(const std::string & value)66 void SetTextValue(const std::string& value) 67 { 68 content_ = value; 69 FilterValue(); 70 } 71 SetTextValueOnly(std::string && value)72 void SetTextValueOnly(std::string&& value) 73 { 74 content_ = value; 75 } 76 Reset()77 void Reset() 78 { 79 content_ = ""; 80 } 81 82 private: 83 bool EraseEmoji(); 84 void FormatIndex(int32_t& startIndex, int32_t& endIndex); 85 void FilterTextInputStyle(bool& textChanged, std::string& result); 86 bool FilterWithEvent(const std::string& filter, std::string& result); 87 std::string PreprocessString(int32_t startIndex, int32_t endIndex, const std::string& value); 88 static std::string RemoveErrorTextFromValue(const std::string& value, const std::string& errorText); 89 static std::string FilterWithRegex(const std::string& filter, std::string& result); 90 static bool FilterWithEmail(std::string& result); 91 static bool FilterWithAscii(std::string& result); 92 static bool FilterWithDecimal(std::string& result); 93 94 std::string content_; 95 WeakPtr<Pattern> pattern_; 96 }; 97 } // namespace OHOS::Ace::NG 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H