• 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_editing_value.h"
17 
18 #include "base/json/json_util.h"
19 #include "base/log/log.h"
20 #include "base/utils/macros.h"
21 #include "base/utils/string_utils.h"
22 
23 namespace OHOS::Ace {
24 namespace {
25 
26 // Negotiated fields with Java
27 const char TEXT[] = "text";
28 const char HINT[] = "hint";
29 const char SELECTION_START[] = "selectionStart";
30 const char SELECTION_END[] = "selectionEnd";
31 
32 } // namespace
33 
ParseFromJson(const JsonValue & json)34 void TextEditingValue::ParseFromJson(const JsonValue& json)
35 {
36     text = json.GetString(TEXT);
37     hint = json.GetString(HINT);
38     selection.baseOffset = json.GetInt(SELECTION_START, -1);
39     selection.extentOffset = json.GetInt(SELECTION_END, -1);
40 }
41 
ToJsonString() const42 std::string TextEditingValue::ToJsonString() const
43 {
44     auto json = JsonUtil::Create(true);
45     json->Put(TEXT, text.c_str());
46     json->Put(HINT, hint.c_str());
47     json->Put(SELECTION_START, selection.baseOffset);
48     json->Put(SELECTION_END, selection.extentOffset);
49     return json->ToString();
50 }
51 
operator ==(const TextEditingValue & other) const52 bool TextEditingValue::operator==(const TextEditingValue& other) const
53 {
54     if (selection != other.selection) {
55         return false;
56     }
57 
58     return text == other.text && hint == other.hint;
59 }
60 
operator !=(const TextEditingValue & other) const61 bool TextEditingValue::operator!=(const TextEditingValue& other) const
62 {
63     return !operator==(other);
64 }
65 
GetWideText() const66 std::wstring TextEditingValue::GetWideText() const
67 {
68     return StringUtils::ToWstring(text);
69 }
70 
MoveLeft()71 void TextEditingValue::MoveLeft()
72 {
73     if (selection.extentOffset <= 1) {
74         selection.Update(0);
75         return;
76     }
77 
78     auto utf16Text = StringUtils::Str8ToStr16(text);
79     int32_t prevCharIndex = std::min(selection.extentOffset - 1, static_cast<int32_t>(utf16Text.length()));
80     selection.Update(StringUtils::NotInUtf16Bmp(utf16Text[prevCharIndex]) ? prevCharIndex - 1 : prevCharIndex);
81 }
82 
MoveRight()83 void TextEditingValue::MoveRight()
84 {
85     auto utf16Text = StringUtils::Str8ToStr16(text);
86     if (static_cast<size_t>(selection.extentOffset) >= utf16Text.length() - 1) {
87         selection.Update(utf16Text.length());
88         return;
89     }
90 
91     int32_t nextCharIndex = selection.extentOffset;
92     selection.Update(StringUtils::NotInUtf16Bmp(utf16Text[nextCharIndex])
93                          ? std::min(static_cast<int32_t>(utf16Text.length()), nextCharIndex + 2)
94                          : nextCharIndex + 1);
95 }
96 
MoveToPosition(int32_t position)97 void TextEditingValue::MoveToPosition(int32_t position)
98 {
99     if (position < 0) {
100         selection.Update(0);
101         return;
102     }
103     auto utf16Text = StringUtils::Str8ToStr16(text);
104     if (static_cast<size_t>(position) >= utf16Text.length()) {
105         selection.Update(utf16Text.length());
106         return;
107     }
108     selection.Update(position);
109 }
110 
UpdateSelection(int32_t both)111 void TextEditingValue::UpdateSelection(int32_t both)
112 {
113     UpdateSelection(both, both);
114 }
115 
UpdateSelection(int32_t start,int32_t end)116 void TextEditingValue::UpdateSelection(int32_t start, int32_t end)
117 {
118     if (start < 0) {
119         start = 0;
120     }
121     if (static_cast<size_t>(end) > GetWideText().length()) {
122         end = static_cast<int32_t>(GetWideText().length());
123     }
124     selection.Update(start, end);
125 }
126 
SelectionAwareTextManipulation(const TextManipulation & manipulation)127 void TextEditingValue::SelectionAwareTextManipulation(const TextManipulation& manipulation)
128 {
129     if (!manipulation) {
130         return;
131     }
132 
133     auto wideText = GetWideText();
134     int32_t start = selection.GetStart();
135     int32_t end = selection.GetEnd();
136     if (static_cast<size_t>(end) > wideText.length() || start > end) {
137         LOGD("Illegal selection for manipulate: start %{public}d, end %{public}d", start, end);
138         return;
139     }
140 
141     if ((start <= 0) && (end <= 0)) {
142         manipulation(wideText);
143     } else {
144         std::wstring beforeSelection;
145         if ((start > 0) && (static_cast<size_t>(start) <= wideText.length())) {
146             beforeSelection = wideText.substr(0, start);
147             manipulation(beforeSelection);
148         }
149 
150         std::wstring inSelection;
151         if (start != end) {
152             inSelection = wideText.substr(start, end - start);
153             manipulation(inSelection);
154         }
155 
156         std::wstring afterSelection;
157         size_t lenLeft = wideText.length() - static_cast<size_t>(end);
158         if (lenLeft > 0) {
159             afterSelection = wideText.substr(end, lenLeft);
160             manipulation(afterSelection);
161         }
162 
163         wideText = beforeSelection + inSelection + afterSelection;
164         if (selection.baseOffset > selection.extentOffset) {
165             selection.Update(beforeSelection.length() + inSelection.length(), beforeSelection.length());
166         } else {
167             selection.Update(beforeSelection.length(), beforeSelection.length() + inSelection.length());
168         }
169     }
170 
171     text = StringUtils::ToString(wideText);
172 }
173 
GetBeforeSelection() const174 std::string TextEditingValue::GetBeforeSelection() const
175 {
176     auto wideText = GetWideText();
177     int32_t start = selection.GetStart();
178     if (static_cast<size_t>(start) > wideText.length()) {
179         LOGD("Illegal selection for GetBeforeSelection: start %{public}d", start);
180         return "";
181     }
182 
183     std::string beforeText;
184     if (start > 0) {
185         std::wstring beforeSelection = wideText.substr(0, start);
186         beforeText = StringUtils::ToString(beforeSelection);
187     }
188     return beforeText;
189 }
190 
GetSelectedText() const191 std::string TextEditingValue::GetSelectedText() const
192 {
193     auto wideText = GetWideText();
194     int32_t start = selection.GetStart();
195     int32_t end = selection.GetEnd();
196     if (static_cast<size_t>(end) > wideText.length() || start > end) {
197         LOGD("Illegal selection for GetSelectedText: start %{public}d, end %{public}d", start, end);
198         return "";
199     }
200 
201     std::string selectedText;
202     if (start < 0) {
203         start = 0;
204     }
205     if (end > 0 && start != end) {
206         std::wstring inSelection = wideText.substr(start, end - start);
207         selectedText = StringUtils::ToString(inSelection);
208     }
209     return selectedText;
210 }
211 
GetSelectedText(const TextSelection & textSelection) const212 std::string TextEditingValue::GetSelectedText(const TextSelection& textSelection) const
213 {
214     auto wideText = GetWideText();
215     int32_t start = textSelection.GetStart();
216     int32_t end = textSelection.GetEnd();
217     std::string selectedText;
218     if (start < 0) {
219         start = 0;
220     }
221     if (static_cast<size_t>(end) > wideText.length()) {
222         end = static_cast<int32_t>(wideText.length());
223     }
224 
225     if (end > 0 && start < end) {
226         std::wstring inSelection = wideText.substr(start, end - start);
227         selectedText = StringUtils::ToString(inSelection);
228     }
229     return selectedText;
230 }
231 
GetAfterSelection() const232 std::string TextEditingValue::GetAfterSelection() const
233 {
234     auto wideText = GetWideText();
235     int32_t end = selection.GetEnd();
236     if (static_cast<size_t>(end) > wideText.length()) {
237         LOGD("Illegal selection for GetAfterSelection: start %{public}d", end);
238         return "";
239     }
240 
241     std::string afterText;
242     if (end <= 0) {
243         afterText = StringUtils::ToString(wideText);
244     } else {
245         std::wstring afterSelection = wideText.substr(end);
246         afterText = StringUtils::ToString(afterSelection);
247     }
248     return afterText;
249 }
250 
Delete(int32_t start,int32_t end)251 void TextEditingValue::Delete(int32_t start, int32_t end)
252 {
253     auto wideText = GetWideText();
254     auto length = (int32_t)wideText.length();
255     int32_t startPos = std::max(std::min(start, end), 0);
256     int32_t endPos = std::min(std::max(start, end), length);
257     if (startPos >= endPos) {
258         return;
259     }
260 
261     auto textAfterDelete = wideText.substr(0, startPos) + wideText.substr(endPos);
262     text = StringUtils::ToString(textAfterDelete);
263     selection.Update(startPos);
264 }
265 
266 } // namespace OHOS::Ace
267