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 "frameworks/bridge/js_frontend/engine/jsi/jsi_input_bridge.h"
17
18 #include "frameworks/bridge/common/utils/utils.h"
19
20 namespace OHOS::Ace::Framework {
21 namespace {
22
GetInputOption(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,InputOption & inputOption)23 void GetInputOption(
24 const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, InputOption& inputOption)
25 {
26 if (!valObject->IsObject(runtime)) {
27 LOGE("none found attrs");
28 return;
29 }
30 shared_ptr<JsValue> properties;
31 int32_t len = 0;
32 if (!valObject->GetPropertyNames(runtime, properties, len)) {
33 LOGE("Get property names failed when get input option.");
34 return;
35 }
36 for (int32_t i = 0; i < len; i++) {
37 const auto& key = properties->GetElement(runtime, i);
38 std::string keyStr = key->ToString(runtime);
39 if (keyStr.empty()) {
40 continue;
41 }
42 const auto& val = valObject->GetProperty(runtime, key);
43 if (val->IsNumber(runtime) || val->IsBoolean(runtime) || val->IsString(runtime)) {
44 std::string valStr = val->ToString(runtime);
45 if (valStr.empty()) {
46 continue;
47 }
48 if (strcmp(keyStr.c_str(), DOM_INPUT_OPTION_ICON) == 0) {
49 inputOption.image = valStr;
50 } else if (strcmp(keyStr.c_str(), DOM_INPUT_OPTION_CONTENT) == 0) {
51 inputOption.text = valStr;
52 } else {
53 LOGD("key : %{public}s unsupported. Ignoring!", keyStr.c_str());
54 }
55 } else {
56 LOGD("value of unsupported type. Ignoring!");
57 }
58 }
59 }
60
61 } // namespace
62
ParseInputOptions(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valArray)63 void JsiInputBridge::ParseInputOptions(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valArray)
64 {
65 shared_ptr<JsValue> properties;
66 int32_t len = 0;
67 if (!valArray->GetPropertyNames(runtime, properties, len)) {
68 LOGE("Get property names failed when parse input options.");
69 return;
70 }
71 for (int32_t i = 0; i < len; ++i) {
72 const auto& itemKey = properties->GetElement(runtime, i);
73 const auto& itemVal = valArray->GetProperty(runtime, itemKey);
74 InputOption inputOption;
75 if (itemVal->IsObject(runtime)) {
76 GetInputOption(runtime, itemVal, inputOption);
77 inputOptions_.push_back(inputOption);
78 }
79 }
80 }
81
82 } // namespace OHOS::Ace::Framework
83