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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_JS_CARD_PARSER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_JS_CARD_PARSER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "base/memory/referenced.h"
23 #include "frameworks/bridge/common/media_query/media_queryer.h"
24 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h"
25 #include "frameworks/bridge/js_frontend/js_command.h"
26 
27 namespace OHOS::Ace::Framework {
28 
29 enum class ParsingStatus { CREATE, UPDATE };
30 
31 class ACE_EXPORT JsCardParser : public Referenced {
32 public:
JsCardParser(const WeakPtr<PipelineBase> & context,const WeakPtr<AssetManager> & assertManager,std::unique_ptr<JsonValue> && rootBody)33     JsCardParser(const WeakPtr<PipelineBase>& context, const WeakPtr<AssetManager>& assertManager,
34         std::unique_ptr<JsonValue>&& rootBody)
35         : Referenced(false), context_(context), assetManager_(assertManager), rootBody_(std::move(rootBody))
36     {}
37     ~JsCardParser() override = default;
38 
39     void CreateDomNode(const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson,
40         int32_t parentId, bool isNewNode = false)
41     {
42         CreateDomNode(page, rootJson, parentId, dataJson_, eventJson_, styleJson_, nullptr, isNewNode);
43     }
ParseAttributes(const std::unique_ptr<JsonValue> & rootJson,int32_t nodeId,std::vector<std::pair<std::string,std::string>> & attrs,JsCommandDomElementOperator * command)44     void ParseAttributes(const std::unique_ptr<JsonValue>& rootJson, int32_t nodeId,
45         std::vector<std::pair<std::string, std::string>>& attrs, JsCommandDomElementOperator* command)
46     {
47         ParseAttributes(rootJson, nodeId, attrs, command, dataJson_, nullptr);
48     }
ParseEvents(const std::unique_ptr<JsonValue> & rootJson,std::vector<std::string> & events,const RefPtr<JsAcePage> & page,int32_t nodeId)49     void ParseEvents(const std::unique_ptr<JsonValue>& rootJson, std::vector<std::string>& events,
50         const RefPtr<JsAcePage>& page, int32_t nodeId)
51     {
52         ParseEvents(rootJson, eventJson_, events, page, nodeId);
53     }
54     void UpdateDomNode(const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson,
55         int32_t parentId, const std::vector<int>& idArray = std::vector<int>())
56     {
57         UpdateDomNode(page, rootJson, parentId, idArray, dataJson_, styleJson_, nullptr);
58     }
ParseVariable(std::string & value)59     void ParseVariable(std::string& value)
60     {
61         ParseVariable(value, dataJson_, nullptr);
62     }
ParseMultiVariable(std::string & value)63     void ParseMultiVariable(std::string& value)
64     {
65         ParseMultiVariable(value, dataJson_);
66     }
ParseStyles(const std::unique_ptr<JsonValue> & rootJson,int32_t nodeId,std::vector<std::pair<std::string,std::string>> & styles)67     void ParseStyles(const std::unique_ptr<JsonValue>& rootJson, int32_t nodeId,
68         std::vector<std::pair<std::string, std::string>>& styles)
69     {
70         ParseStyles(rootJson, nodeId, styles, styleJson_);
71     }
72 
GetVariable(std::string & value)73     bool GetVariable(std::string& value)
74     {
75         return GetVariable(value, dataJson_);
76     }
GetShownValue(std::string & value)77     bool GetShownValue(std::string& value)
78     {
79         return GetShownValue(value, dataJson_, nullptr);
80     }
81     bool GetShownValue(
82         std::string& value, const std::unique_ptr<JsonValue>& dataJson, const std::unique_ptr<JsonValue>& propsJson);
83     bool GetRepeatData(std::unique_ptr<JsonValue>& repeatValue, std::string& key);
84 
85     void UpdatePageData(const std::string& dataList, const RefPtr<JsAcePage>& page);
86     void LoadImageInfo();
87     void UpdateStyle(const RefPtr<JsAcePage>& page);
88     void ParseRepeatIndexItem(const std::unique_ptr<JsonValue>& repeatValue);
89     void SetRepeatItemValue(uint32_t index, const std::unique_ptr<JsonValue>& repeatValue, bool hasKeyValue);
90     bool Initialize();
91     void SetColorMode(ColorMode colorMode);
92 
GetPipelineContext()93     const WeakPtr<PipelineBase>& GetPipelineContext() const
94     {
95         return context_;
96     }
97 
SetDensity(double density)98     void SetDensity(double density)
99     {
100         density_ = density;
101     }
102 
ResetNodeId()103     void ResetNodeId()
104     {
105         maxNodeId_ = nodeId_;
106         numberOfForNode_ = 0;
107         nodeId_ = 0;
108     }
109 
GetNumberOfNodes()110     int32_t GetNumberOfNodes() const
111     {
112         return maxNodeId_;
113     }
114 
115     // add for test case
SetIsRepeat(bool isRepeat)116     void SetIsRepeat(bool isRepeat)
117     {
118         isRepeat_ = isRepeat;
119     }
120 
121     void OnSurfaceChanged(int32_t width, int32_t height);
122 
SetCardHapPath(const std::string & path)123     void SetCardHapPath(const std::string& path)
124     {
125         cardHapPath_ = path;
126     }
127 
128 private:
129     void GetResImageUrl(std::string& value);
130     bool GetI18nData(std::string& value);
131     void GetPlurals(std::string& value);
132     void ParseInlineStyles(
133         const std::unique_ptr<JsonValue>& rootJson, std::vector<std::pair<std::string, std::string>>& styles);
134     bool SelectStyle(const std::string& className, const std::unique_ptr<JsonValue>& styleClass,
135         std::vector<std::pair<std::string, std::string>>& styles);
136     void SelectMediaQueryStyle(const std::string& styleClass, std::vector<std::pair<std::string, std::string>>& styles);
137     void LoadMediaQueryStyle();
138     void LoadResImageUrl(const std::string& jsonFile, const std::string& splitStr, std::string& value);
139     void RegisterFont(const std::string& fontFamily);
140     std::string GetEventAction(const std::string& action, const std::string& actionType, int32_t nodeId = 0);
141     bool ParsePropsVariable(std::string& value, const std::unique_ptr<JsonValue>& propsJson);
142     bool ParsePropsArray(std::string& value, const std::unique_ptr<JsonValue>& propsJson);
143     bool ParseComplexExpression(std::string& value, const std::unique_ptr<JsonValue>& json);
144     bool ParseTernaryExpression(std::string& value, const std::unique_ptr<JsonValue>& propsJson = nullptr);
145     bool ParseLogicalExpression(std::string& value, const std::unique_ptr<JsonValue>& propsJson = nullptr);
146     bool ParseArrayExpression(
147         const std::string& expression, std::stack<std::string>& keyStack, const std::unique_ptr<JsonValue>& json);
148     bool ParseSpecialVariable(std::string& value);
149     void CreateDomNode(const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson,
150         int32_t parentId, const std::unique_ptr<JsonValue>& dataJson, const std::unique_ptr<JsonValue>& actionJson,
151         const std::unique_ptr<JsonValue>& styleJson, const std::unique_ptr<JsonValue>& propsJson = nullptr,
152         bool isNewNode = false);
153     void PreUpdateMethodToAction(const std::unique_ptr<JsonValue>& rootJson);
154     void ParseAttributes(const std::unique_ptr<JsonValue>& rootJson, int32_t nodeId,
155         std::vector<std::pair<std::string, std::string>>& attrs, JsCommandDomElementOperator* command,
156         const std::unique_ptr<JsonValue>& dataJson, const std::unique_ptr<JsonValue>& propsJson);
157     void ParseStyles(const std::unique_ptr<JsonValue>& rootJson, int32_t nodeId,
158         std::vector<std::pair<std::string, std::string>>& styles, const std::unique_ptr<JsonValue>& styleJson);
159     void ParseVariable(std::string& value, const std::unique_ptr<JsonValue>& dataJson,
160         const std::unique_ptr<JsonValue>& propsJson = nullptr);
161     void ParseMultiVariable(std::string& value, const std::unique_ptr<JsonValue>& dataJson,
162         const std::unique_ptr<JsonValue>& propsJson = nullptr);
163     void UpdateDomNode(const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson,
164         int32_t parentId, const std::vector<int>& idArray, const std::unique_ptr<JsonValue>& dataJson,
165         const std::unique_ptr<JsonValue>& styleJson, const std::unique_ptr<JsonValue>& propsJson);
166     void ParseEvents(const std::unique_ptr<JsonValue>& rootJson, const std::unique_ptr<JsonValue>& eventJson,
167         std::vector<std::string>& events, const RefPtr<Framework::JsAcePage>& page, int32_t nodeId);
168     void UpdateProps(const std::string& key, std::string value, const std::unique_ptr<JsonValue>& propsJson);
169     bool GetAndParseProps(std::string& value, const std::unique_ptr<JsonValue>& propsJson);
170     bool GetVariable(std::string& value, const std::unique_ptr<JsonValue>& dataJson);
171     template<typename T>
172     void ParseSpecialAttr(const std::function<void(const std::unique_ptr<JsonValue>&, std::vector<T>&)>& function,
173         std::string& value, std::vector<T>& vector);
174     template<typename T>
175     void ParseSpecialAttr(
176         const std::function<void(const std::unique_ptr<JsonValue>&, T&)>& function, std::string& variable, T& value);
177     bool ParsePointOperator(const std::string& tmpKey, std::stack<std::string>& keyStack, const std::string& dataJson);
178     void ResetRepeatIndexItem();
179     void CreateRepeatDomNode(
180         const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson, int32_t parentId);
181     void GetResourceValue(const std::string& path);
182     void GetClockConfig(const std::unique_ptr<JsonValue>& jsonDataSets, ClockConfig& clockConfig);
183     void ProcessRepeatNode(const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson,
184         const std::string& key, int32_t parentId, bool hasKeyValue, std::unique_ptr<JsonValue>& repeatValue);
185     void SetUpdateStatus(const RefPtr<Framework::JsAcePage>& page);
186     void GetShownAttr(const std::unique_ptr<JsonValue>& rootJson, const std::unique_ptr<JsonValue>& dataJson,
187         const std::unique_ptr<JsonValue>& propsJson, bool& shouldShow, bool& hasShownAttr);
188     void CreateBlockNode(
189         const RefPtr<Framework::JsAcePage>& page, const std::unique_ptr<JsonValue>& rootJson, int32_t parentId);
GetBoolValue(const std::unique_ptr<JsonValue> & rootJson,const std::string & key,bool & value,bool & hasAttr)190     void GetBoolValue(const std::unique_ptr<JsonValue>& rootJson, const std::string& key, bool& value, bool& hasAttr)
191     {
192         GetBoolValue(rootJson, dataJson_, nullptr, key, value, hasAttr);
193     }
194     void GetBoolValue(const std::unique_ptr<JsonValue>& rootJson, const std::unique_ptr<JsonValue>& dataJson,
195         const std::unique_ptr<JsonValue>& propsJson, const std::string& key, bool& value, bool& hasAttr);
196     void ParseVersionAndUpdateData();
197     void ReplaceParam(const std::unique_ptr<JsonValue>& node);
198 
199     double density_ = 1.0;
200     int32_t nodeId_ = 0;
201     int32_t maxNodeId_ = 0;
202     int32_t numberOfForNode_ = 0;
203     int32_t listNodeIndex_ = 0;
204     ColorMode colorMode_ = ColorMode::LIGHT;
205     ParsingStatus parsingStatus_ = ParsingStatus::CREATE;
206     std::vector<int32_t> idArray_;
207     WeakPtr<PipelineBase> context_;
208     WeakPtr<AssetManager> assetManager_;
209     std::unique_ptr<JsonValue> rootBody_;
210     std::unique_ptr<JsonValue> styleJson_;
211     std::unique_ptr<JsonValue> rootJson_;
212     std::unique_ptr<JsonValue> eventJson_;
213     std::unique_ptr<JsonValue> dataJson_;
214     std::unique_ptr<JsonValue> repeatJson_;
215     std::unique_ptr<JsonValue> resourceJson_;
216     std::unordered_map<std::string, std::string> imageUrlMap_;
217     std::unordered_map<std::string, std::string> methodToAction_;
218     std::unordered_map<int32_t, std::pair<std::vector<int32_t>, int32_t>> listIdMap_;
219     std::unordered_map<int32_t, int32_t> singleLoopMap_;
220     std::unordered_map<std::string, std::unique_ptr<JsonValue>> mediaQueryStyles_;
221     std::vector<std::pair<std::string, std::string>> customStyles_;
222     MediaQueryer mediaQueryer_;
223 
224     // for repeat attr
225     bool isRepeat_ = false;
226     std::string repeatIndex_;
227     std::string repeatItem_;
228     std::string cardHapPath_;
229 };
230 
231 } // namespace OHOS::Ace::Framework
232 
233 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_JS_CARD_PARSER_H