• 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 "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             }
53         }
54     }
55 }
56 
57 } // namespace
58 
ParseInputOptions(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valArray)59 void JsiInputBridge::ParseInputOptions(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valArray)
60 {
61     shared_ptr<JsValue> properties;
62     int32_t len = 0;
63     if (!valArray->GetPropertyNames(runtime, properties, len)) {
64         LOGE("Get property names failed when parse input options.");
65         return;
66     }
67     for (int32_t i = 0; i < len; ++i) {
68         const auto& itemKey = properties->GetElement(runtime, i);
69         const auto& itemVal = valArray->GetProperty(runtime, itemKey);
70         InputOption inputOption;
71         if (itemVal->IsObject(runtime)) {
72             GetInputOption(runtime, itemVal, inputOption);
73             inputOptions_.push_back(inputOption);
74         }
75     }
76 }
77 
78 } // namespace OHOS::Ace::Framework
79