• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/components/declaration/common/declaration.h"
17 
18 #include "base/geometry/calc_dimension.h"
19 #include "base/geometry/dimension.h"
20 #include "base/log/ace_trace.h"
21 #include "base/utils/string_utils.h"
22 #include "core/common/ace_application_info.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/decoration.h"
25 #include "core/components/declaration/common/declaration_constants.h"
26 #include "frameworks/bridge/common/utils/utils.h"
27 
28 namespace OHOS::Ace {
29 
30 using namespace Framework;
31 
32 namespace {
33 
34 constexpr uint32_t TRANSFORM_SINGLE = 1;
35 constexpr uint32_t TRANSFORM_DUAL = 2;
36 constexpr int32_t DIRECTION_ANGLE = 1;
37 constexpr int32_t DIRECTION_SIDE = 2;
38 constexpr int32_t DIRECTION_CORNER = 3;
39 constexpr int32_t MS_TO_S = 1000;
40 constexpr uint32_t COMMON_METHOD_FOCUS_ARGS_SIZE = 1;
41 constexpr Dimension TRANSFORM_ORIGIN_DEFAULT = 0.5_pct;
42 const char COMMON_METHOD_FOCUS[] = "focus";
43 
44 // Shared Transition Effect Type String
45 constexpr char SHARED_TRANSITION_EFFECT_STATIC[] = "static";
46 constexpr char SHARED_TRANSITION_EFFECT_EXCHANGE[] = "exchange";
47 
48 template<class T>
ParseFunctionValue(const std::string & line,const std::string & key,std::function<T (const std::string &)> parser)49 T ParseFunctionValue(const std::string& line, const std::string& key, std::function<T(const std::string&)> parser)
50 {
51     std::vector<std::string> strs;
52     StringUtils::SplitStr(line, " ", strs, true);
53     for (const auto& str : strs) {
54         if (str.empty()) {
55             continue;
56         }
57         auto leftIndex = str.find('(');
58         auto rightIndex = str.find(')');
59         if (leftIndex == std::string::npos || rightIndex == std::string::npos) {
60             continue;
61         }
62         if (leftIndex + 1 >= rightIndex) {
63             continue;
64         }
65         if (str.substr(0, leftIndex) != key) {
66             continue;
67         }
68 
69         auto valueStr = str.substr(leftIndex + 1, rightIndex - leftIndex - 1);
70         return parser(valueStr);
71     }
72     return T {};
73 }
74 
ParseSharedEffect(const std::string & effect,Declaration & declaration)75 RefPtr<SharedTransitionEffect> ParseSharedEffect(const std::string& effect, Declaration& declaration)
76 {
77     std::string effectTrim = effect;
78     RemoveHeadTailSpace(effectTrim);
79     if (effectTrim == SHARED_TRANSITION_EFFECT_STATIC) {
80         return SharedTransitionEffect::GetSharedTransitionEffect(
81             SharedTransitionEffectType::SHARED_EFFECT_STATIC, declaration.GetShareId());
82     } else if (effectTrim == SHARED_TRANSITION_EFFECT_EXCHANGE) {
83         return SharedTransitionEffect::GetSharedTransitionEffect(
84             SharedTransitionEffectType::SHARED_EFFECT_EXCHANGE, declaration.GetShareId());
85     } else {
86         LOGE("Parse shared effect failed. unknown effect: %{public}s, share id: %{public}s", effect.c_str(),
87             declaration.GetShareId().c_str());
88         return nullptr;
89     }
90 }
91 
ParseTransitionEffect(const std::string & option)92 TransitionEffect ParseTransitionEffect(const std::string& option)
93 {
94     static std::unordered_map<std::string, TransitionEffect> types = {
95         { "unfold", TransitionEffect::UNFOLD },
96         { "none", TransitionEffect::NONE },
97     };
98     auto pos = types.find(option);
99     if (pos != types.end()) {
100         return pos->second;
101     }
102     return TransitionEffect::NONE;
103 }
104 
ParseClickEffect(const std::string & effect)105 ClickSpringEffectType ParseClickEffect(const std::string& effect)
106 {
107     static std::unordered_map<std::string, ClickSpringEffectType> types = {
108         { "spring-small", ClickSpringEffectType::SMALL },
109         { "spring-medium", ClickSpringEffectType::MEDIUM },
110         { "spring-large", ClickSpringEffectType::LARGE },
111     };
112     auto pos = types.find(effect);
113     if (pos != types.end()) {
114         return pos->second;
115     }
116     return ClickSpringEffectType::NONE;
117 }
118 
StrToWindowBlurStyle(const std::string & value)119 inline WindowBlurStyle StrToWindowBlurStyle(const std::string& value)
120 {
121     static std::unordered_map<std::string, WindowBlurStyle> types = {
122         { "small_light", WindowBlurStyle::STYLE_BACKGROUND_SMALL_LIGHT },
123         { "medium_light", WindowBlurStyle::STYLE_BACKGROUND_MEDIUM_LIGHT },
124         { "large_light", WindowBlurStyle::STYLE_BACKGROUND_LARGE_LIGHT },
125         { "xlarge_light", WindowBlurStyle::STYLE_BACKGROUND_XLARGE_LIGHT },
126         { "small_dark", WindowBlurStyle::STYLE_BACKGROUND_SMALL_DARK },
127         { "medium_dark", WindowBlurStyle::STYLE_BACKGROUND_MEDIUM_DARK },
128         { "large_dark", WindowBlurStyle::STYLE_BACKGROUND_LARGE_DARK },
129         { "xlarge_dark", WindowBlurStyle::STYLE_BACKGROUND_XLARGE_DARK },
130     };
131     auto pos = types.find(value);
132     if (pos != types.end()) {
133         return pos->second;
134     }
135     return WindowBlurStyle::STYLE_BACKGROUND_SMALL_LIGHT;
136 }
137 
138 } // namespace
139 
Declaration()140 Declaration::Declaration()
141 {
142     backDecoration_ = AceType::MakeRefPtr<Decoration>();
143 }
144 
145 Declaration::~Declaration() = default;
146 
Init()147 void Declaration::Init()
148 {
149     InitCommonAttribute();
150     InitCommonStyle();
151     InitCommonEvent();
152     InitCommonMethod();
153     InitSpecialized();
154     auto& commonAttr = MaybeResetAttribute<CommonAttribute>(AttributeTag::COMMON_ATTR);
155     if (commonAttr.IsValid()) {
156         commonAttr.isRightToLeft = AceApplicationInfo::GetInstance().IsRightToLeft();
157     }
158 
159     auto& backgroundStyle = MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
160     if (backgroundStyle.IsValid()) {
161         backgroundStyle.backgroundImage = AceType::MakeRefPtr<BackgroundImage>();
162         backgroundStyle.borderImage = AceType::MakeRefPtr<BorderImage>();
163     }
164 }
165 
InitCommonAttribute()166 void Declaration::InitCommonAttribute()
167 {
168     for (auto attribute : DeclarationConstants::DEFAULT_ATTRS) {
169         AddCommonAttribute(attribute);
170     }
171 }
172 
InitCommonStyle()173 void Declaration::InitCommonStyle()
174 {
175     for (auto style : DeclarationConstants::DEFAULT_STYLES) {
176         AddCommonStyle(style);
177     }
178 }
179 
InitCommonEvent()180 void Declaration::InitCommonEvent()
181 {
182     for (auto event : DeclarationConstants::DEFAULT_EVENTS) {
183         AddCommonEvent(event);
184     }
185 }
186 
InitCommonMethod()187 void Declaration::InitCommonMethod()
188 {
189     for (auto method : DeclarationConstants::DEFAULT_METHODS) {
190         AddCommonMethod(method);
191     }
192 }
193 
AddCommonAttribute(AttributeTag tag)194 void Declaration::AddCommonAttribute(AttributeTag tag)
195 {
196     static const LinearEnumMapNode<AttributeTag, void (*)(Declaration&)> operators[] = {
197         { AttributeTag::COMMON_ATTR,
198             [](Declaration& declaration) {
199                 declaration.attributes_.try_emplace(
200                     AttributeTag::COMMON_ATTR, DeclarationConstants::DEFAULT_COMMON_ATTR);
201             } },
202         { AttributeTag::COMMON_DISABLED_ATTR,
203             [](Declaration& declaration) {
204                 declaration.attributes_.try_emplace(
205                     AttributeTag::COMMON_DISABLED_ATTR, DeclarationConstants::DEFAULT_DISABLED_ATTR);
206             } },
207         { AttributeTag::COMMON_FOCUSABLE_ATTR,
208             [](Declaration& declaration) {
209                 declaration.attributes_.try_emplace(
210                     AttributeTag::COMMON_FOCUSABLE_ATTR, DeclarationConstants::DEFAULT_FOCUSABLE_ATTR);
211             } },
212         { AttributeTag::COMMON_TOUCHABLE_ATTR,
213             [](Declaration& declaration) {
214                 declaration.attributes_.try_emplace(
215                     AttributeTag::COMMON_TOUCHABLE_ATTR, DeclarationConstants::DEFAULT_TOUCHABLE_ATTR);
216             } },
217         { AttributeTag::COMMON_DATA_ATTR,
218             [](Declaration& declaration) {
219                 declaration.attributes_.try_emplace(
220                     AttributeTag::COMMON_DATA_ATTR, DeclarationConstants::DEFAULT_DATA_ATTR);
221             } },
222         { AttributeTag::COMMON_CLICK_EFFECT_ATTR,
223             [](Declaration& declaration) {
224                 declaration.attributes_.try_emplace(
225                     AttributeTag::COMMON_CLICK_EFFECT_ATTR, DeclarationConstants::DEFAULT_CLICK_EFFECT_ATTR);
226             } },
227         { AttributeTag::COMMON_RENDER_ATTR,
228             [](Declaration& declaration) {
229                 declaration.attributes_.try_emplace(
230                     AttributeTag::COMMON_RENDER_ATTR, DeclarationConstants::DEFAULT_RENDER_ATTR);
231             } },
232         { AttributeTag::COMMON_MULTIMODAL_ATTR,
233             [](Declaration& declaration) {
234                 declaration.attributes_.try_emplace(
235                     AttributeTag::COMMON_MULTIMODAL_ATTR, DeclarationConstants::DEFAULT_MULTI_MODAL_ATTR);
236             } },
237     };
238     auto operatorIter = BinarySearchFindIndex(operators, ArraySize(operators), tag);
239     if (operatorIter != -1) {
240         operators[operatorIter].value(*this);
241     } else {
242         LOGW("attribute tag %{public}d is invalid", tag);
243     }
244 }
245 
AddCommonStyle(StyleTag tag)246 void Declaration::AddCommonStyle(StyleTag tag)
247 {
248     static const LinearEnumMapNode<StyleTag, void (*)(Declaration&)> operators[] = {
249         { StyleTag::COMMON_STYLE,
250             [](Declaration& declaration) {
251                 declaration.styles_.try_emplace(StyleTag::COMMON_STYLE, DeclarationConstants::DEFAULT_COMMON_STYLE);
252             } },
253         { StyleTag::COMMON_SIZE_STYLE,
254             [](Declaration& declaration) {
255                 declaration.styles_.try_emplace(StyleTag::COMMON_SIZE_STYLE, DeclarationConstants::DEFAULT_SIZE_STYLE);
256             } },
257         { StyleTag::COMMON_MARGIN_STYLE,
258             [](Declaration& declaration) {
259                 declaration.styles_.try_emplace(
260                     StyleTag::COMMON_MARGIN_STYLE, DeclarationConstants::DEFAULT_MARGIN_STYLE);
261             } },
262         { StyleTag::COMMON_PADDING_STYLE,
263             [](Declaration& declaration) {
264                 declaration.styles_.try_emplace(
265                     StyleTag::COMMON_PADDING_STYLE, DeclarationConstants::DEFAULT_PADDING_STYLE);
266             } },
267         { StyleTag::COMMON_BORDER_STYLE,
268             [](Declaration& declaration) {
269                 declaration.styles_.try_emplace(
270                     StyleTag::COMMON_BORDER_STYLE, DeclarationConstants::DEFAULT_BORDER_STYLE);
271             } },
272         { StyleTag::COMMON_BACKGROUND_STYLE,
273             [](Declaration& declaration) {
274                 declaration.styles_.try_emplace(
275                     StyleTag::COMMON_BACKGROUND_STYLE, DeclarationConstants::DEFAULT_BACKGROUND_STYLE);
276             } },
277         { StyleTag::COMMON_FLEX_STYLE,
278             [](Declaration& declaration) {
279                 declaration.styles_.try_emplace(StyleTag::COMMON_FLEX_STYLE, DeclarationConstants::DEFAULT_FLEX_STYLE);
280             } },
281         { StyleTag::COMMON_POSITION_STYLE,
282             [](Declaration& declaration) {
283                 declaration.styles_.try_emplace(
284                     StyleTag::COMMON_POSITION_STYLE, DeclarationConstants::DEFAULT_POSITION_STYLE);
285             } },
286         { StyleTag::COMMON_OPACITY_STYLE,
287             [](Declaration& declaration) {
288                 declaration.styles_.try_emplace(
289                     StyleTag::COMMON_OPACITY_STYLE, DeclarationConstants::DEFAULT_OPACITY_STYLE);
290             } },
291         { StyleTag::COMMON_VISIBILITY_STYLE,
292             [](Declaration& declaration) {
293                 declaration.styles_.try_emplace(
294                     StyleTag::COMMON_VISIBILITY_STYLE, DeclarationConstants::DEFAULT_VISIBILITY_STYLE);
295             } },
296         { StyleTag::COMMON_DISPLAY_STYLE,
297             [](Declaration& declaration) {
298                 declaration.styles_.try_emplace(
299                     StyleTag::COMMON_DISPLAY_STYLE, DeclarationConstants::DEFAULT_DISPLAY_STYLE);
300             } },
301         { StyleTag::COMMON_SHADOW_STYLE,
302             [](Declaration& declaration) {
303                 declaration.styles_.try_emplace(
304                     StyleTag::COMMON_SHADOW_STYLE, DeclarationConstants::DEFAULT_SHADOW_STYLE);
305             } },
306         { StyleTag::COMMON_OVERFLOW_STYLE,
307             [](Declaration& declaration) {
308                 declaration.styles_.try_emplace(
309                     StyleTag::COMMON_OVERFLOW_STYLE, DeclarationConstants::DEFAULT_OVERFLOW_STYLE);
310             } },
311         { StyleTag::COMMON_FILTER_STYLE,
312             [](Declaration& declaration) {
313                 declaration.styles_.try_emplace(
314                     StyleTag::COMMON_FILTER_STYLE, DeclarationConstants::DEFAULT_FILTER_STYLE);
315             } },
316         { StyleTag::COMMON_ANIMATION_STYLE,
317             [](Declaration& declaration) {
318                 declaration.styles_.try_emplace(
319                     StyleTag::COMMON_ANIMATION_STYLE, DeclarationConstants::DEFAULT_ANIMATION_STYLE);
320             } },
321         { StyleTag::COMMON_SHARE_TRANSITION_STYLE,
322             [](Declaration& declaration) {
323                 declaration.styles_.try_emplace(
324                     StyleTag::COMMON_SHARE_TRANSITION_STYLE, DeclarationConstants::DEFAULT_SHARE_TRANSITION_STYLE);
325             } },
326         { StyleTag::COMMON_CARD_TRANSITION_STYLE,
327             [](Declaration& declaration) {
328                 declaration.styles_.try_emplace(
329                     StyleTag::COMMON_CARD_TRANSITION_STYLE, DeclarationConstants::DEFAULT_CARD_TRANSITION_STYLE);
330             } },
331         { StyleTag::COMMON_PAGE_TRANSITION_STYLE,
332             [](Declaration& declaration) {
333                 declaration.styles_.try_emplace(
334                     StyleTag::COMMON_PAGE_TRANSITION_STYLE, DeclarationConstants::DEFAULT_PAGE_TRANSITION_STYLE);
335             } },
336         { StyleTag::COMMON_CLIP_PATH_STYLE,
337             [](Declaration& declaration) {
338               declaration.styles_.try_emplace(
339                   StyleTag::COMMON_CLIP_PATH_STYLE, DeclarationConstants::DEFAULT_CLIP_PATH_STYLE);
340             } },
341         { StyleTag::COMMON_MASK_STYLE,
342             [](Declaration& declaration) {
343               declaration.styles_.try_emplace(
344                   StyleTag::COMMON_MASK_STYLE, DeclarationConstants::DEFAULT_MASK_STYLE);
345             } },
346         { StyleTag::COMMON_IMAGE_STYLE,
347             [](Declaration& declaration) {
348               declaration.styles_.try_emplace(
349                   StyleTag::COMMON_IMAGE_STYLE, DeclarationConstants::DEFAULT_IMAGE_STYLE);
350             } },
351     };
352     auto operatorIter = BinarySearchFindIndex(operators, ArraySize(operators), tag);
353     if (operatorIter != -1) {
354         operators[operatorIter].value(*this);
355     } else {
356         LOGW("style tag %{public}d is invalid", tag);
357     }
358 }
359 
AddCommonEvent(EventTag tag)360 void Declaration::AddCommonEvent(EventTag tag)
361 {
362     static const LinearEnumMapNode<EventTag, void (*)(Declaration&)> operators[] = {
363         { EventTag::COMMON_RAW_EVENT,
364             [](Declaration& declaration) {
365                 declaration.events_.try_emplace(EventTag::COMMON_RAW_EVENT, DeclarationConstants::DEFAULT_RAW_EVENT);
366             } },
367         { EventTag::COMMON_GESTURE_EVENT,
368             [](Declaration& declaration) {
369                 declaration.events_.try_emplace(
370                     EventTag::COMMON_GESTURE_EVENT, DeclarationConstants::DEFAULT_GESTURE_EVENT);
371             } },
372         { EventTag::COMMON_REMOTE_MESSAGE_GRESURE_EVENT,
373             [](Declaration& declaration) {
374                 declaration.events_.try_emplace(
375                     EventTag::COMMON_REMOTE_MESSAGE_GRESURE_EVENT, DeclarationConstants::DEFAULT_GESTURE_EVENT);
376             } },
377         { EventTag::COMMON_FOCUS_EVENT,
378             [](Declaration& declaration) {
379                 declaration.events_.try_emplace(
380                     EventTag::COMMON_FOCUS_EVENT, DeclarationConstants::DEFAULT_FOCUS_EVENT);
381             } },
382         { EventTag::COMMON_KEY_EVENT,
383             [](Declaration& declaration) {
384                 declaration.events_.try_emplace(EventTag::COMMON_KEY_EVENT, DeclarationConstants::DEFAULT_KEY_EVENT);
385             } },
386         { EventTag::COMMON_MOUSE_EVENT,
387             [](Declaration& declaration) {
388                 declaration.events_.try_emplace(
389                     EventTag::COMMON_MOUSE_EVENT, DeclarationConstants::DEFAULT_MOUSE_EVENT);
390             } },
391         { EventTag::COMMON_SWIPE_EVENT,
392             [](Declaration& declaration) {
393                 declaration.events_.try_emplace(
394                     EventTag::COMMON_SWIPE_EVENT, DeclarationConstants::DEFAULT_SWIPE_EVENT);
395             } },
396         { EventTag::COMMON_ATTACH_EVENT,
397             [](Declaration& declaration) {
398                 declaration.events_.try_emplace(
399                     EventTag::COMMON_ATTACH_EVENT, DeclarationConstants::DEFAULT_ATTACH_EVENT);
400             } },
401         { EventTag::COMMON_CROWN_EVENT,
402             [](Declaration& declaration) {
403                 declaration.events_.try_emplace(
404                     EventTag::COMMON_CROWN_EVENT, DeclarationConstants::DEFAULT_CROWN_EVENT);
405             } },
406     };
407     auto operatorIter = BinarySearchFindIndex(operators, ArraySize(operators), tag);
408     if (operatorIter != -1) {
409         operators[operatorIter].value(*this);
410     } else {
411         LOGW("event tag %{public}d is invalid", tag);
412     }
413 }
414 
AddCommonMethod(MethodTag tag)415 void Declaration::AddCommonMethod(MethodTag tag)
416 {
417     static const LinearEnumMapNode<MethodTag, void (*)(Declaration&)> operators[] = {
418         { MethodTag::COMMON_METHOD,
419             [](Declaration& declaration) {
420                 declaration.methods_.try_emplace(MethodTag::COMMON_METHOD, DeclarationConstants::DEFAULT_METHOD);
421             } },
422     };
423     auto operatorIter = BinarySearchFindIndex(operators, ArraySize(operators), tag);
424     if (operatorIter != -1) {
425         operators[operatorIter].value(*this);
426     } else {
427         LOGW("method tag %{public}d is invalid", tag);
428     }
429 }
430 
AddSpecializedAttribute(std::shared_ptr<Attribute> && specializedAttribute)431 void Declaration::AddSpecializedAttribute(std::shared_ptr<Attribute>&& specializedAttribute)
432 {
433     attributes_.try_emplace(AttributeTag::SPECIALIZED_ATTR, std::move(specializedAttribute));
434 }
435 
AddSpecializedStyle(std::shared_ptr<Style> && specializedStyle)436 void Declaration::AddSpecializedStyle(std::shared_ptr<Style>&& specializedStyle)
437 {
438     styles_.try_emplace(StyleTag::SPECIALIZED_STYLE, std::move(specializedStyle));
439 }
440 
AddSpecializedEvent(std::shared_ptr<Event> && specializedEvent)441 void Declaration::AddSpecializedEvent(std::shared_ptr<Event>&& specializedEvent)
442 {
443     events_.try_emplace(EventTag::SPECIALIZED_EVENT, std::move(specializedEvent));
444 }
445 
AddSpecializedRemoteMessageEvent(std::shared_ptr<Event> && specializedEvent)446 void Declaration::AddSpecializedRemoteMessageEvent(std::shared_ptr<Event>&& specializedEvent)
447 {
448     events_.try_emplace(EventTag::SPECIALIZED_REMOTE_MESSAGE_EVENT, std::move(specializedEvent));
449 }
450 
AddSpecializedMethod(std::shared_ptr<Method> && specializedMethod)451 void Declaration::AddSpecializedMethod(std::shared_ptr<Method>&& specializedMethod)
452 {
453     methods_.try_emplace(MethodTag::SPECIALIZED_METHOD, std::move(specializedMethod));
454 }
455 
GetAttribute(AttributeTag tag) const456 Attribute& Declaration::GetAttribute(AttributeTag tag) const
457 {
458     auto it = attributes_.find(tag);
459     if (it != attributes_.end()) {
460         return *(it->second);
461     } else {
462         static Attribute errAttribute {
463             .tag = AttributeTag::UNKNOWN
464         };
465         return errAttribute;
466     }
467 }
468 
GetStyle(StyleTag tag) const469 Style& Declaration::GetStyle(StyleTag tag) const
470 {
471     auto it = styles_.find(tag);
472     if (it != styles_.end()) {
473         return *(it->second);
474     } else {
475         static Style errStyle { .tag = StyleTag::UNKNOWN };
476         return errStyle;
477     }
478 }
479 
GetEvent(EventTag tag) const480 Event& Declaration::GetEvent(EventTag tag) const
481 {
482     auto it = events_.find(tag);
483     if (it != events_.end()) {
484         return *(it->second);
485     } else {
486         static Event errEvent { .tag = EventTag::UNKNOWN };
487         return errEvent;
488     }
489 }
490 
GetMethod(MethodTag tag) const491 Method& Declaration::GetMethod(MethodTag tag) const
492 {
493     auto it = methods_.find(tag);
494     if (it != methods_.end()) {
495         return *(it->second);
496     } else {
497         static Method errMethod { .tag = MethodTag::UNKNOWN };
498         return errMethod;
499     }
500 }
501 
SetShowAttr(const std::string & showValue)502 void Declaration::SetShowAttr(const std::string& showValue)
503 {
504     auto& renderAttr = MaybeResetAttribute<CommonRenderAttribute>(AttributeTag::COMMON_RENDER_ATTR);
505     if (renderAttr.IsValid()) {
506         renderAttr.show = showValue;
507     }
508     auto& displayStyle = MaybeResetStyle<CommonDisplayStyle>(StyleTag::COMMON_DISPLAY_STYLE);
509     if (displayStyle.IsValid()) {
510         displayStyle.display = (showValue == "false") ? DisplayType::NONE : DisplayType::NO_SETTING;
511     }
512 }
513 
SetAttr(const std::vector<std::pair<std::string,std::string>> & attrs)514 void Declaration::SetAttr(const std::vector<std::pair<std::string, std::string>>& attrs)
515 {
516     ACE_SCOPED_TRACE("Declaration::SetAttr");
517     if (onSetAttribute_) {
518         onSetAttribute_();
519     }
520     static const std::string flagOn = "on";
521     static const std::string flagOff = "off";
522     static const std::string flagAuto = "auto";
523     // static linear map must be sorted by key.
524     static const LinearMapNode<void (*)(const std::string&, Declaration&)> attrSetters[] = {
525         { DOM_CLICK_EFFECT,
526             [](const std::string& value, Declaration& declaration) {
527                 auto& clickEffectAttr =
528                     declaration.MaybeResetAttribute<CommonClickEffectAttribute>(AttributeTag::COMMON_CLICK_EFFECT_ATTR);
529                 if (clickEffectAttr.IsValid()) {
530                     declaration.hasTransformStyle_ = true;
531                     declaration.hasClickEffect_ = true;
532                     clickEffectAttr.clickEffect = ParseClickEffect(value);
533                 }
534             } },
535         { DOM_DIR,
536             [](const std::string& value, Declaration& declaration) {
537                 auto& commonAttr = declaration.MaybeResetAttribute<CommonAttribute>(AttributeTag::COMMON_ATTR);
538                 if (commonAttr.IsValid()) {
539                     if (value == "rtl") {
540                         commonAttr.isRightToLeft = true;
541                         commonAttr.direction = TextDirection::RTL;
542                     } else if (value == "ltr") {
543                         commonAttr.isRightToLeft = false;
544                         commonAttr.direction = TextDirection::LTR;
545                     } else {
546                         commonAttr.isRightToLeft = AceApplicationInfo::GetInstance().IsRightToLeft();
547                         commonAttr.direction = TextDirection::AUTO;
548                     }
549                 }
550             } },
551         { DOM_FOCUSABLE,
552             [](const std::string& value, Declaration& declaration) {
553                 auto& focusableAttr =
554                     declaration.MaybeResetAttribute<CommonFocusableAttribute>(AttributeTag::COMMON_FOCUSABLE_ATTR);
555                 if (focusableAttr.IsValid()) {
556                     focusableAttr.focusable.first = StringToBool(value);
557                     focusableAttr.focusable.second = true; // Tag whether user defined focusable.
558                 }
559             } },
560         { DOM_ID,
561             [](const std::string& value, Declaration& declaration) {
562                 auto& commonAttr = declaration.MaybeResetAttribute<CommonAttribute>(AttributeTag::COMMON_ATTR);
563                 if (commonAttr.IsValid()) {
564                     commonAttr.id = value;
565                     declaration.hasIdAttr_ = true;
566                 }
567             } },
568 #ifndef WEARABLE_PRODUCT
569         { DOM_SCENE_LABEL,
570             [](const std::string& value, Declaration& declaration) {
571                 auto& multimodalAttr =
572                     declaration.MaybeResetAttribute<CommonMultimodalAttribute>(AttributeTag::COMMON_MULTIMODAL_ATTR);
573                 if (!multimodalAttr.IsValid()) {
574                     return;
575                 }
576                 static const LinearMapNode<SceneLabel> multimodalSceneMap[] = {
577                     { "audio", SceneLabel::AUDIO },
578                     { "common", SceneLabel::COMMON },
579                     { "page", SceneLabel::PAGE },
580                     { "switch", SceneLabel::SWITCH },
581                     { "video", SceneLabel::VIDEO },
582                 };
583                 auto iter = BinarySearchFindIndex(multimodalSceneMap, ArraySize(multimodalSceneMap), value.c_str());
584                 if (iter != -1) {
585                     multimodalAttr.scene = multimodalSceneMap[iter].value;
586                 }
587             } },
588 #endif
589         { DOM_SHOW,
590             [](const std::string& value, Declaration& declaration) {
591                 declaration.hasDisplayStyle_ = true;
592                 if (declaration.useLiteStyle_) {
593                     auto& visibilityStyle =
594                         declaration.MaybeResetStyle<CommonVisibilityStyle>(StyleTag::COMMON_VISIBILITY_STYLE);
595                     if (visibilityStyle.IsValid()) {
596                         visibilityStyle.visibility =
597                             (value == "true") ? VisibilityType::VISIBLE : VisibilityType::HIDDEN;
598                     }
599                 } else {
600                     declaration.SetShowAttr(value);
601                 }
602             } },
603 #ifndef WEARABLE_PRODUCT
604         { DOM_SPRING_EFFECT,
605             [](const std::string& value, Declaration& declaration) {
606                 auto& clickEffectAttr =
607                     declaration.MaybeResetAttribute<CommonClickEffectAttribute>(AttributeTag::COMMON_CLICK_EFFECT_ATTR);
608                 if (clickEffectAttr.IsValid()) {
609                     declaration.hasTransformStyle_ = true;
610                     declaration.hasClickEffect_ = true;
611                     clickEffectAttr.clickEffect = ParseClickEffect(value);
612                 }
613             } },
614         { DOM_SUBSCRIPT_FLAG,
615             [](const std::string& value, Declaration& declaration) {
616                 auto& multimodalAttr =
617                     declaration.MaybeResetAttribute<CommonMultimodalAttribute>(AttributeTag::COMMON_MULTIMODAL_ATTR);
618                 if (multimodalAttr.IsValid()) {
619                     multimodalAttr.useSubscript =
620                         (value == flagOn) || (value == flagAuto && declaration.IsSubscriptEnable());
621                 }
622             } },
623         { DOM_SUBSCRIPT_LABEL,
624             [](const std::string& value, Declaration& declaration) {
625                 auto& multimodalAttr =
626                     declaration.MaybeResetAttribute<CommonMultimodalAttribute>(AttributeTag::COMMON_MULTIMODAL_ATTR);
627                 if (multimodalAttr.IsValid()) {
628                     multimodalAttr.subscriptLabel = value;
629                 }
630             } },
631         { DOM_TOUCHABLE,
632             [](const std::string& value, Declaration& declaration) {
633                 auto& touchableAttr =
634                     declaration.MaybeResetAttribute<CommonTouchableAttribute>(AttributeTag::COMMON_TOUCHABLE_ATTR);
635                 if (touchableAttr.IsValid()) {
636                     touchableAttr.touchable = StringToBool(value);
637                 }
638             } },
639         { DOM_VOICE_LABEL,
640             [](const std::string& value, Declaration& declaration) {
641                 auto& multimodalAttr =
642                     declaration.MaybeResetAttribute<CommonMultimodalAttribute>(AttributeTag::COMMON_MULTIMODAL_ATTR);
643                 if (multimodalAttr.IsValid()) {
644                     multimodalAttr.voiceLabel = value;
645                 }
646             } },
647 #endif
648     };
649 
650     for (const auto& attr : attrs) {
651         if (attr.first == DOM_DISABLED) {
652             isDisabled_ = StringToBool(attr.second);
653         }
654 
655         if (attr.first == DOM_BUTTON_WAITING) {
656             isWaiting_ = StringToBool(attr.second);
657         }
658 
659         if (attr.first == DOM_CHECKED) {
660             isChecked_ = StringToBool(attr.second);
661         }
662 
663         if (SetSpecializedAttr(attr)) {
664             continue;
665         }
666         auto operatorIter = BinarySearchFindIndex(attrSetters, ArraySize(attrSetters), attr.first.c_str());
667         if (operatorIter != -1) {
668             attrSetters[operatorIter].value(attr.second, *this);
669         }
670     }
671 }
672 
SetStyle(const std::vector<std::pair<std::string,std::string>> & styles)673 void Declaration::SetStyle(const std::vector<std::pair<std::string, std::string>>& styles)
674 {
675     ACE_SCOPED_TRACE("Declaration::SetStyle");
676     for (const auto& style : styles) {
677         if (style.first.find(DOM_PSEUDO_CLASS_SYMBOL) == std::string::npos) {
678             SetCurrentStyle(style);
679         }
680     }
681 }
682 
SetCurrentStyle(const std::pair<std::string,std::string> & style)683 void Declaration::SetCurrentStyle(const std::pair<std::string, std::string>& style)
684 {
685     if (SetSpecializedStyle(style)) {
686         // If the subclass consumes this property, it will no longer look in the general property.
687         return;
688     }
689 
690     // Operator map for styles
691     static const LinearMapNode<void (*)(const std::string&, Declaration&)> styleSetter[] = {
692         { DOM_ALIGN_SELF,
693             [](const std::string& value, Declaration& declaration) {
694                 auto& commonStyle = declaration.MaybeResetStyle<CommonStyle>(StyleTag::COMMON_STYLE);
695                 if (commonStyle.IsValid()) {
696                     commonStyle.alignSelf = value;
697                 }
698             } },
699         { DOM_ANIMATION_DELAY,
700             [](const std::string& value, Declaration& declaration) {
701                 auto& animationStyle =
702                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
703                 if (animationStyle.IsValid()) {
704                     if (value.find("ms") != std::string::npos) {
705                         animationStyle.animationDelay = StringUtils::StringToInt(value);
706                         animationStyle.tweenOption.SetDelay(animationStyle.animationDelay);
707                     } else {
708                         animationStyle.animationDelay = StringUtils::StringToInt(value) * MS_TO_S;
709                         animationStyle.tweenOption.SetDelay(animationStyle.animationDelay);
710                     }
711                 }
712             } },
713         { DOM_ANIMATION_DIRECTION,
714             [](const std::string& value, Declaration& declaration) {
715                 auto& animationStyle =
716                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
717                 if (animationStyle.IsValid()) {
718                     animationStyle.tweenOption.SetAnimationDirection(StringToAnimationDirection(value));
719                 }
720             } },
721         { DOM_ANIMATION_DURATION,
722             [](const std::string& value, Declaration& declaration) {
723                 auto& animationStyle =
724                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
725                 if (animationStyle.IsValid()) {
726                     if (value.find("ms") != std::string::npos) {
727                         animationStyle.animationDuration = StringUtils::StringToInt(value);
728                         animationStyle.tweenOption.SetDuration(animationStyle.animationDuration);
729                     } else {
730                         animationStyle.animationDuration = StringUtils::StringToInt(value) * MS_TO_S;
731                         animationStyle.tweenOption.SetDuration(animationStyle.animationDuration);
732                     }
733                 }
734             } },
735         { DOM_ANIMATION_FILL_MODE,
736             [](const std::string& value, Declaration& declaration) {
737                 auto& animationStyle =
738                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
739                 if (animationStyle.IsValid()) {
740                     animationStyle.fillMode = StringToFillMode(value);
741                     animationStyle.tweenOption.SetFillMode(animationStyle.fillMode);
742                 }
743             } },
744         { DOM_ANIMATION_ITERATION_COUNT,
745             [](const std::string& value, Declaration& declaration) {
746                 auto& animationStyle =
747                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
748                 if (animationStyle.IsValid()) {
749                     animationStyle.iteration = StringUtils::StringToInt(value);
750                     animationStyle.tweenOption.SetIteration(animationStyle.iteration);
751                 }
752             } },
753         { DOM_ANIMATION_PLAY_STATE,
754             [](const std::string& value, Declaration& declaration) {
755                 auto& animationStyle =
756                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
757                 if (animationStyle.IsValid()) {
758                     animationStyle.animationOperation = StringToAnimationOperation(value);
759                 }
760             } },
761         { DOM_ANIMATION_TIMING_FUNCTION,
762             [](const std::string& value, Declaration& declaration) {
763                 auto& animationStyle =
764                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
765                 if (animationStyle.IsValid()) {
766                     animationStyle.curve = CreateCurve(value);
767                     animationStyle.tweenOption.SetCurve(animationStyle.curve);
768                 }
769             } },
770         { DOM_APPEARING_DURATION,
771             [](const std::string& value, Declaration& declaration) {
772                 auto& opacityStyle = declaration.MaybeResetStyle<CommonOpacityStyle>(StyleTag::COMMON_OPACITY_STYLE);
773                 if (opacityStyle.IsValid()) {
774                     opacityStyle.appearingDuration = StringUtils::StringToInt(value);
775                     declaration.hasDisplayStyle_ = true;
776                 }
777             } },
778         { DOM_ASPECT_RATIO,
779             [](const std::string& value, Declaration& declaration) {
780                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
781                 if (sizeStyle.IsValid()) {
782                     sizeStyle.aspectRatio = StringToDouble(value);
783                 }
784             } },
785         { DOM_BACKDROP_FILTER,
786             [](const std::string& value, Declaration& declaration) {
787                 declaration.hasDecorationStyle_ = true;
788                 auto radius = ParseFunctionValue<Dimension>(value, DOM_BLUR, StringToDimension);
789                 if (radius.IsValid()) {
790                     declaration.backDecoration_->SetBlurRadius(radius);
791                 } else {
792                     declaration.backDecoration_->SetBlurRadius(Dimension {});
793                 }
794             } },
795         { DOM_BACKGROUND, &Declaration::SetBackground },
796         { DOM_BACKGROUND_COLOR,
797             [](const std::string& value, Declaration& declaration) {
798                 auto& backgroundStyle =
799                     declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
800                 if (backgroundStyle.IsValid()) {
801                     auto backgroundColor = declaration.ParseColor(value);
802                     backgroundStyle.backgroundColor = backgroundColor;
803                     declaration.backDecoration_->SetBackgroundColor(backgroundColor);
804                     declaration.hasBackGroundColor_ = true;
805                     declaration.hasDecorationStyle_ = true;
806                 }
807             } },
808         { DOM_BACKGROUND_IMAGE,
809             [](const std::string& value, Declaration& declaration) {
810                 auto& backgroundStyle =
811                     declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
812                 if (backgroundStyle.IsValid()) {
813                     backgroundStyle.backgroundImage->SetSrc(value, declaration.GetThemeConstants());
814                     declaration.backDecoration_->SetImage(backgroundStyle.backgroundImage);
815                     // TODO: need to remain color and image in render box.
816                     declaration.backDecoration_->SetBackgroundColor(Color::TRANSPARENT);
817                     declaration.hasDecorationStyle_ = true;
818                 }
819             } },
820         { DOM_BACKGROUND_IMAGE_POSITION, &Declaration::SetBackgroundImagePosition },
821         { DOM_BACKGROUND_IMAGE_REPEAT,
822             [](const std::string& value, Declaration& declaration) {
823                 auto& backgroundStyle =
824                     declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
825                 if (backgroundStyle.IsValid()) {
826                     backgroundStyle.backgroundRepeat = ConvertStrToImageRepeat(value);
827                     backgroundStyle.backgroundImage->SetImageRepeat(backgroundStyle.backgroundRepeat);
828                     declaration.hasDecorationStyle_ = true;
829                 }
830             } },
831         { DOM_BACKGROUND_IMAGE_SIZE, &Declaration::SetBackgroundImageSize },
832         { DOM_BORDER, &Declaration::SetBorderOverall },
833         { DOM_BORDER_BOTTOM_COLOR,
834             [](const std::string& value, Declaration& declaration) {
835                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
836                 if (borderStyle.IsValid()) {
837                     borderStyle.border.SetBottomColor(declaration.ParseColor(value));
838                     declaration.hasBorderStyle_ = true;
839                     declaration.hasDecorationStyle_ = true;
840                 }
841             } },
842         { DOM_BORDER_BOTTOM_LEFT_RADIUS,
843             [](const std::string& value, Declaration& declaration) {
844                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
845                 if (borderStyle.IsValid()) {
846                     borderStyle.border.SetBottomLeftRadius(Radius(declaration.ParseDimension(value)));
847                     declaration.hasBorderStyle_ = true;
848                     declaration.hasBorderRadiusStyle_ = true;
849                     declaration.hasDecorationStyle_ = true;
850                 }
851             } },
852         { DOM_BORDER_BOTTOM_RIGHT_RADIUS,
853             [](const std::string& value, Declaration& declaration) {
854                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
855                 if (borderStyle.IsValid()) {
856                     borderStyle.border.SetBottomRightRadius(Radius(declaration.ParseDimension(value)));
857                     declaration.hasBorderStyle_ = true;
858                     declaration.hasBorderRadiusStyle_ = true;
859                     declaration.hasDecorationStyle_ = true;
860                 }
861             } },
862         { DOM_BORDER_BOTTOM_STYLE,
863             [](const std::string& value, Declaration& declaration) {
864                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
865                 if (borderStyle.IsValid()) {
866                     borderStyle.border.SetBottomStyle(ConvertStrToBorderStyle(value));
867                     declaration.hasBorderStyle_ = true;
868                     declaration.hasDecorationStyle_ = true;
869                 }
870             } },
871         { DOM_BORDER_BOTTOM_WIDTH,
872             [](const std::string& value, Declaration& declaration) {
873                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
874                 if (borderStyle.IsValid()) {
875                     borderStyle.border.SetBottomWidth(declaration.ParseDimension(value));
876                     declaration.hasBorderStyle_ = true;
877                     declaration.hasDecorationStyle_ = true;
878                 }
879             } },
880         { DOM_BORDER_COLOR, &Declaration::SetBorderColorForFourEdges },
881 
882         { DOM_BORDER_IMAGE, &Declaration::SetBorderImage},
883         { DOM_BORDER_IMAGE_OUTSET, &Declaration::SetBorderImageOutSetForFourEdges},
884         { DOM_BORDER_IMAGE_REPEAT, &Declaration::SetBorderImageRepeatForFourEdges},
885         { DOM_BORDER_IMAGE_SLICE, &Declaration::SetBorderImageSliceForFourEdges},
886         { DOM_BORDER_IMAGE_SOURCE, &Declaration::SetBorderImageFindUrl},
887         { DOM_BORDER_IMAGE_WIDTH, &Declaration::SetBorderImageWidthForFourEdges},
888 
889         { DOM_BORDER_LEFT_COLOR,
890             [](const std::string& value, Declaration& declaration) {
891                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
892                 if (borderStyle.IsValid()) {
893                     borderStyle.border.SetLeftColor(declaration.ParseColor(value));
894                     declaration.hasBorderStyle_ = true;
895                     declaration.hasDecorationStyle_ = true;
896                 }
897             } },
898         { DOM_BORDER_LEFT_STYLE,
899             [](const std::string& value, Declaration& declaration) {
900                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
901                 if (borderStyle.IsValid()) {
902                     borderStyle.border.SetLeftStyle(ConvertStrToBorderStyle(value));
903                     declaration.hasBorderStyle_ = true;
904                     declaration.hasDecorationStyle_ = true;
905                 }
906             } },
907         { DOM_BORDER_LEFT_WIDTH,
908             [](const std::string& value, Declaration& declaration) {
909                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
910                 if (borderStyle.IsValid()) {
911                     borderStyle.border.SetLeftWidth(declaration.ParseDimension(value));
912                     declaration.hasBorderStyle_ = true;
913                     declaration.hasDecorationStyle_ = true;
914                 }
915             } },
916         { DOM_BORDER_RADIUS,
917             [](const std::string& value, Declaration& declaration) {
918                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
919                 if (borderStyle.IsValid()) {
920                     borderStyle.border.SetBorderRadius(Radius(declaration.ParseDimension(value)));
921                     declaration.hasBorderStyle_ = true;
922                     declaration.hasBorderRadiusStyle_ = true;
923                     declaration.hasDecorationStyle_ = true;
924                 }
925             } },
926         { DOM_BORDER_RIGHT_COLOR,
927             [](const std::string& value, Declaration& declaration) {
928                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
929                 if (borderStyle.IsValid()) {
930                     borderStyle.border.SetRightColor(declaration.ParseColor(value));
931                     declaration.hasBorderStyle_ = true;
932                     declaration.hasDecorationStyle_ = true;
933                 }
934             } },
935         { DOM_BORDER_RIGHT_STYLE,
936             [](const std::string& value, Declaration& declaration) {
937                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
938                 if (borderStyle.IsValid()) {
939                     borderStyle.border.SetRightStyle(ConvertStrToBorderStyle(value));
940                     declaration.hasBorderStyle_ = true;
941                     declaration.hasDecorationStyle_ = true;
942                 }
943             } },
944         { DOM_BORDER_RIGHT_WIDTH,
945             [](const std::string& value, Declaration& declaration) {
946                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
947                 if (borderStyle.IsValid()) {
948                     borderStyle.border.SetRightWidth(declaration.ParseDimension(value));
949                     declaration.hasBorderStyle_ = true;
950                     declaration.hasDecorationStyle_ = true;
951                 }
952             } },
953         { DOM_BORDER_STYLE, &Declaration::SetBorderStyleForFourEdges },
954         { DOM_BORDER_TOP_COLOR,
955             [](const std::string& value, Declaration& declaration) {
956                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
957                 if (borderStyle.IsValid()) {
958                     borderStyle.border.SetTopColor(declaration.ParseColor(value));
959                     declaration.hasBorderStyle_ = true;
960                     declaration.hasDecorationStyle_ = true;
961                 }
962             } },
963         { DOM_BORDER_TOP_LEFT_RADIUS,
964             [](const std::string& value, Declaration& declaration) {
965                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
966                 if (borderStyle.IsValid()) {
967                     borderStyle.border.SetTopLeftRadius(Radius(declaration.ParseDimension(value)));
968                     declaration.hasBorderStyle_ = true;
969                     declaration.hasBorderRadiusStyle_ = true;
970                     declaration.hasDecorationStyle_ = true;
971                 }
972             } },
973         { DOM_BORDER_TOP_RIGHT_RADIUS,
974             [](const std::string& value, Declaration& declaration) {
975                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
976                 if (borderStyle.IsValid()) {
977                     borderStyle.border.SetTopRightRadius(Radius(declaration.ParseDimension(value)));
978                     declaration.hasBorderStyle_ = true;
979                     declaration.hasBorderRadiusStyle_ = true;
980                     declaration.hasDecorationStyle_ = true;
981                 }
982             } },
983         { DOM_BORDER_TOP_STYLE,
984             [](const std::string& value, Declaration& declaration) {
985                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
986                 if (borderStyle.IsValid()) {
987                     borderStyle.border.SetTopStyle(ConvertStrToBorderStyle(value));
988                     declaration.hasBorderStyle_ = true;
989                     declaration.hasDecorationStyle_ = true;
990                 }
991             } },
992         { DOM_BORDER_TOP_WIDTH,
993             [](const std::string& value, Declaration& declaration) {
994                 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
995                 if (borderStyle.IsValid()) {
996                     borderStyle.border.SetTopWidth(declaration.ParseDimension(value));
997                     declaration.hasBorderStyle_ = true;
998                     declaration.hasDecorationStyle_ = true;
999                 }
1000             } },
1001         { DOM_BORDER_WIDTH, &Declaration::SetBorderWidthForFourEdges },
1002         { DOM_POSITION_BOTTOM,
1003             [](const std::string& value, Declaration& declaration) {
1004                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1005                 if (positionStyle.IsValid() && !value.empty()) {
1006                     positionStyle.bottom = declaration.ParseDimension(value);
1007                     declaration.hasPositionStyle_ = true;
1008                     declaration.hasBottom_ = true;
1009                 }
1010             } },
1011         { DOM_BOX_SHADOW_BLUR,
1012             [](const std::string& value, Declaration& declaration) {
1013                 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1014                 if (shadowStyle.IsValid()) {
1015                     shadowStyle.shadow.SetBlurRadius(StringToDouble(value));
1016                     declaration.hasDecorationStyle_ = true;
1017                     declaration.hasShadowStyle_ = true;
1018                 }
1019             } },
1020         { DOM_BOX_SHADOW_COLOR,
1021             [](const std::string& value, Declaration& declaration) {
1022                 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1023                 if (shadowStyle.IsValid()) {
1024                     if (value.empty()) {
1025                         shadowStyle.shadow.SetColor(Color::BLACK);
1026                         return;
1027                     }
1028                     shadowStyle.shadow.SetColor(declaration.ParseColor(value));
1029                     declaration.hasDecorationStyle_ = true;
1030                     declaration.hasShadowStyle_ = true;
1031                 }
1032             } },
1033         { DOM_BOX_SHADOW_H,
1034             [](const std::string& value, Declaration& declaration) {
1035                 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1036                 if (shadowStyle.IsValid()) {
1037                     shadowStyle.shadow.SetOffsetX(StringToDouble(value));
1038                     declaration.hasDecorationStyle_ = true;
1039                     declaration.hasShadowStyle_ = true;
1040                 }
1041             } },
1042         { DOM_BOX_SHADOW_SPREAD,
1043             [](const std::string& value, Declaration& declaration) {
1044                 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1045                 if (shadowStyle.IsValid()) {
1046                     shadowStyle.shadow.SetSpreadRadius(StringToDouble(value));
1047                     declaration.hasDecorationStyle_ = true;
1048                     declaration.hasShadowStyle_ = true;
1049                 }
1050             } },
1051         { DOM_BOX_SHADOW_V,
1052             [](const std::string& value, Declaration& declaration) {
1053                 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1054                 if (shadowStyle.IsValid()) {
1055                     shadowStyle.shadow.SetOffsetY(StringToDouble(value));
1056                     declaration.hasDecorationStyle_ = true;
1057                     declaration.hasShadowStyle_ = true;
1058                 }
1059             } },
1060         { DOM_BOX_SIZING,
1061             [](const std::string& value, Declaration& declaration) {
1062                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1063                 if (sizeStyle.IsValid()) {
1064                     sizeStyle.boxSizing = ConvertStrToBoxSizing(value);
1065                 }
1066             } },
1067         { DOM_CLIP_PATH,
1068             [](const std::string& value, Declaration& declaration) {
1069                 auto& clipPathStyle =
1070                     declaration.MaybeResetStyle<CommonClipPathStyle>(StyleTag::COMMON_CLIP_PATH_STYLE);
1071                 if (clipPathStyle.IsValid()) {
1072                     clipPathStyle.clipPath = CreateClipPath(value);
1073                 }
1074             } },
1075         { DOM_DISPLAY,
1076             [](const std::string& value, Declaration& declaration) {
1077                 auto& displayStyle = declaration.MaybeResetStyle<CommonDisplayStyle>(StyleTag::COMMON_DISPLAY_STYLE);
1078                 if (displayStyle.IsValid()) {
1079                     displayStyle.display = (value == DOM_DISPLAY_NONE)
1080                                                ? DisplayType::NONE
1081                                                : (value == DOM_DISPLAY_GRID) ? DisplayType::GRID : DisplayType::FLEX;
1082                     declaration.hasDisplayStyle_ = true;
1083                 }
1084             } },
1085         { DOM_DISPLAY_INDEX,
1086             [](const std::string& value, Declaration& declaration) {
1087                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1088                 if (flexStyle.IsValid()) {
1089                     flexStyle.displayIndex = StringToInt(value);
1090                 }
1091             } },
1092         { DOM_POSITION_END,
1093             [](const std::string& value, Declaration& declaration) {
1094                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1095                 if (positionStyle.IsValid() && !value.empty()) {
1096                     if (declaration.IsRightToLeft()) {
1097                         positionStyle.left = declaration.ParseDimension(value);
1098                         declaration.hasLeft_ = true;
1099                     } else {
1100                         positionStyle.right = declaration.ParseDimension(value);
1101                         declaration.hasRight_ = true;
1102                     }
1103                     declaration.hasPositionStyle_ = true;
1104                 }
1105             } },
1106         { DOM_FILTER,
1107             [](const std::string& value, Declaration& declaration) {
1108                 declaration.hasFrontDecorationStyle_ = true;
1109                 if (!declaration.frontDecoration_) {
1110                     declaration.frontDecoration_ = AceType::MakeRefPtr<Decoration>();
1111                 }
1112                 auto radius = ParseFunctionValue<Dimension>(value, DOM_BLUR, StringToDimension);
1113                 if (radius.IsValid()) {
1114                     declaration.frontDecoration_->SetBlurRadius(radius);
1115                 } else {
1116                     declaration.frontDecoration_->SetBlurRadius(Dimension {});
1117                 }
1118             } },
1119         { DOM_FLEX,
1120             [](const std::string& value, Declaration& declaration) {
1121                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1122                 if (flexStyle.IsValid()) {
1123                     flexStyle.flexGrow = StringToDouble(value);
1124                 }
1125             } },
1126         { DOM_FLEX_BASIS,
1127             [](const std::string& value, Declaration& declaration) {
1128                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1129                 if (flexStyle.IsValid()) {
1130                     flexStyle.flexBasis = StringToDimension(value);
1131                 }
1132             } },
1133         { DOM_FLEX_GROW,
1134             [](const std::string& value, Declaration& declaration) {
1135                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1136                 if (flexStyle.IsValid()) {
1137                     flexStyle.flexGrow = StringToDouble(value);
1138                 }
1139             } },
1140         { DOM_FLEX_SHRINK,
1141             [](const std::string& value, Declaration& declaration) {
1142                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1143                 if (flexStyle.IsValid()) {
1144                     flexStyle.flexShrink = StringToDouble(value);
1145                 }
1146             } },
1147         { DOM_FLEX_WEIGHT,
1148             [](const std::string& value, Declaration& declaration) {
1149                 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1150                 if (flexStyle.IsValid()) {
1151                     flexStyle.flexWeight = StringToDouble(value);
1152                 }
1153             } },
1154         { DOM_HEIGHT,
1155             [](const std::string& value, Declaration& declaration) {
1156                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1157                 if (sizeStyle.IsValid()) {
1158                     sizeStyle.height = declaration.ParseCalcDimension(value);
1159                     declaration.hasBoxStyle_ = true;
1160                 }
1161             } },
1162         { DOM_IMAGE_FILL,
1163             [](const std::string& value, Declaration& declaration) {
1164                 auto& imageStyle = declaration.MaybeResetStyle<CommonImageStyle>(StyleTag::COMMON_IMAGE_STYLE);
1165                 if (imageStyle.IsValid()) {
1166                     imageStyle.imageFill = declaration.ParseColor(value);
1167                 }
1168             } },
1169         { DOM_LAYOUT_IN_BOX,
1170             [](const std::string& value, Declaration& declaration) {
1171                 auto& commonStyle = declaration.MaybeResetStyle<CommonStyle>(StyleTag::COMMON_STYLE);
1172                 if (commonStyle.IsValid()) {
1173                     commonStyle.layoutInBox = StringToBool(value);
1174                     declaration.hasBoxStyle_ = true;
1175                 }
1176             } },
1177         { DOM_POSITION_LEFT,
1178             [](const std::string& value, Declaration& declaration) {
1179                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1180                 if (positionStyle.IsValid() && !value.empty()) {
1181                     positionStyle.left = declaration.ParseDimension(value);
1182                     declaration.hasPositionStyle_ = true;
1183                     declaration.hasLeft_ = true;
1184                 }
1185             } },
1186         { DOM_MARGIN, &Declaration::SetMarginOverall },
1187         { DOM_MARGIN_BOTTOM,
1188             [](const std::string& value, Declaration& declaration) {
1189                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1190                 if (marginStyle.IsValid()) {
1191                     marginStyle.margin.SetBottom(declaration.ParseCalcDimension(value));
1192                     declaration.hasBoxStyle_ = true;
1193                 }
1194             } },
1195         { DOM_MARGIN_END,
1196             [](const std::string& value, Declaration& declaration) {
1197                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1198                 if (marginStyle.IsValid()) {
1199                     if (declaration.IsRightToLeft()) {
1200                         marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1201                     } else {
1202                         marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1203                     }
1204                     declaration.hasBoxStyle_ = true;
1205                 }
1206             } },
1207         { DOM_MARGIN_LEFT,
1208             [](const std::string& value, Declaration& declaration) {
1209                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1210                 if (marginStyle.IsValid()) {
1211                     marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1212                     declaration.hasBoxStyle_ = true;
1213                 }
1214             } },
1215         { DOM_MARGIN_RIGHT,
1216             [](const std::string& value, Declaration& declaration) {
1217                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1218                 if (marginStyle.IsValid()) {
1219                     marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1220                     declaration.hasBoxStyle_ = true;
1221                 }
1222             } },
1223         { DOM_MARGIN_START,
1224             [](const std::string& value, Declaration& declaration) {
1225                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1226                 if (marginStyle.IsValid()) {
1227                     if (declaration.IsRightToLeft()) {
1228                         marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1229                     } else {
1230                         marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1231                     }
1232                     declaration.hasBoxStyle_ = true;
1233                 }
1234             } },
1235         { DOM_MARGIN_TOP,
1236             [](const std::string& value, Declaration& declaration) {
1237                 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1238                 if (marginStyle.IsValid()) {
1239                     marginStyle.margin.SetTop(declaration.ParseCalcDimension(value));
1240                     declaration.hasBoxStyle_ = true;
1241                 }
1242             } },
1243         { DOM_MASK_IMAGE,
1244             [](const std::string& value, Declaration& declaration) {
1245                 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1246                 if (maskStyle.IsValid()) {
1247                     maskStyle.maskImage = value;
1248                 }
1249             } },
1250         { DOM_MASK_POSITION,
1251             [](const std::string& value, Declaration& declaration) {
1252                 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1253                 if (maskStyle.IsValid()) {
1254                     maskStyle.maskPosition = value;
1255                 }
1256             } },
1257         { DOM_MASK_SIZE,
1258             [](const std::string& value, Declaration& declaration) {
1259                 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1260                 if (maskStyle.IsValid()) {
1261                     maskStyle.maskSize = value;
1262                 }
1263             } },
1264         { DOM_MAX_HEIGHT,
1265             [](const std::string& value, Declaration& declaration) {
1266                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1267                 if (sizeStyle.IsValid()) {
1268                     sizeStyle.maxHeight = declaration.ParseCalcDimension(value);
1269                 }
1270             } },
1271         { DOM_MAX_WIDTH,
1272             [](const std::string& value, Declaration& declaration) {
1273                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1274                 if (sizeStyle.IsValid()) {
1275                     sizeStyle.maxWidth = declaration.ParseCalcDimension(value);
1276                 }
1277             } },
1278         { DOM_MIN_HEIGHT,
1279             [](const std::string& value, Declaration& declaration) {
1280                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1281                 if (sizeStyle.IsValid()) {
1282                     sizeStyle.minHeight = declaration.ParseCalcDimension(value);
1283                 }
1284             } },
1285         { DOM_MIN_WIDTH,
1286             [](const std::string& value, Declaration& declaration) {
1287                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1288                 if (sizeStyle.IsValid()) {
1289                     sizeStyle.minWidth = declaration.ParseCalcDimension(value);
1290                 }
1291             } },
1292         { DOM_OPACITY,
1293             [](const std::string& value, Declaration& declaration) {
1294                 auto& opacityStyle = declaration.MaybeResetStyle<CommonOpacityStyle>(StyleTag::COMMON_OPACITY_STYLE);
1295                 if (opacityStyle.IsValid()) {
1296                     opacityStyle.opacity = declaration.ParseDouble(value);
1297                     declaration.hasDisplayStyle_ = true;
1298                 }
1299             } },
1300         { DOM_OVERFLOW_STYLE,
1301             [](const std::string& value, Declaration& declaration) {
1302                 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1303                 if (overflowStyle.IsValid()) {
1304                     overflowStyle.overflow = ConvertStrToOverflow(value);
1305                     declaration.hasOverflowStyle_ = true;
1306                 }
1307             } },
1308         { DOM_SCROLL_OVER_SCROLL_EFFECT,
1309             [](const std::string& val, Declaration& declaration) {
1310                 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1311                 if (!overflowStyle.IsValid()) {
1312                     return;
1313                 }
1314                 if (val == DOM_SCROLL_EFFECT_SPRING) {
1315                     overflowStyle.edgeEffect = EdgeEffect::SPRING;
1316                 } else if (val == DOM_SCROLL_EFFECT_FADE) {
1317                     overflowStyle.edgeEffect = EdgeEffect::FADE;
1318                 } else {
1319                     overflowStyle.edgeEffect = EdgeEffect::NONE;
1320                 }
1321             } },
1322         { DOM_PADDING, &Declaration::SetPaddingOverall },
1323         { DOM_PADDING_BOTTOM,
1324             [](const std::string& value, Declaration& declaration) {
1325                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1326                 if (paddingStyle.IsValid()) {
1327                     paddingStyle.padding.SetBottom(declaration.ParseCalcDimension(value));
1328                     declaration.hasBoxStyle_ = true;
1329                 }
1330             } },
1331         { DOM_PADDING_END,
1332             [](const std::string& value, Declaration& declaration) {
1333                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1334                 if (paddingStyle.IsValid()) {
1335                     if (declaration.IsRightToLeft()) {
1336                         paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1337                     } else {
1338                         paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1339                     }
1340                     declaration.hasBoxStyle_ = true;
1341                 }
1342             } },
1343         { DOM_PADDING_LEFT,
1344             [](const std::string& value, Declaration& declaration) {
1345                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1346                 if (paddingStyle.IsValid()) {
1347                     paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1348                     declaration.hasBoxStyle_ = true;
1349                 }
1350             } },
1351         { DOM_PADDING_RIGHT,
1352             [](const std::string& value, Declaration& declaration) {
1353                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1354                 if (paddingStyle.IsValid()) {
1355                     paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1356                     declaration.hasBoxStyle_ = true;
1357                 }
1358             } },
1359         { DOM_PADDING_START,
1360             [](const std::string& value, Declaration& declaration) {
1361                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1362                 if (paddingStyle.IsValid()) {
1363                     if (declaration.IsRightToLeft()) {
1364                         paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1365                     } else {
1366                         paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1367                     }
1368                     declaration.hasBoxStyle_ = true;
1369                 }
1370             } },
1371         { DOM_PADDING_TOP,
1372             [](const std::string& value, Declaration& declaration) {
1373                 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1374                 if (paddingStyle.IsValid()) {
1375                     paddingStyle.padding.SetTop(declaration.ParseCalcDimension(value));
1376                     declaration.hasBoxStyle_ = true;
1377                 }
1378             } },
1379         { DOM_POSITION,
1380             [](const std::string& value, Declaration& declaration) {
1381                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1382                 if (positionStyle.IsValid() && !value.empty()) {
1383                     positionStyle.position =
1384                         value == DOM_POSITION_FIXED
1385                             ? PositionType::FIXED
1386                             : value == DOM_POSITION_ABSOLUTE ? PositionType::ABSOLUTE : PositionType::RELATIVE;
1387                     declaration.hasPositionStyle_ = true;
1388                 }
1389             } },
1390         { DOM_POSITION_RIGHT,
1391             [](const std::string& value, Declaration& declaration) {
1392                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1393                 if (positionStyle.IsValid() && !value.empty()) {
1394                     positionStyle.right = declaration.ParseDimension(value);
1395                     declaration.hasPositionStyle_ = true;
1396                     declaration.hasRight_ = true;
1397                 }
1398             } },
1399         { DOM_SCROLL_SCROLLBAR_COLOR,
1400             [](const std::string& val, Declaration& declaration) {
1401                 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1402                 if (overflowStyle.IsValid()) {
1403                     overflowStyle.scrollBarColor.first = true;
1404                     overflowStyle.scrollBarColor.second = declaration.ParseColor(val);
1405                 }
1406             } },
1407         { DOM_SCROLL_SCROLLBAR_WIDTH,
1408             [](const std::string& val, Declaration& declaration) {
1409                 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1410                 if (overflowStyle.IsValid()) {
1411                     overflowStyle.scrollBarWidth.first = true;
1412                     auto width = declaration.ParseDimension(val);
1413                     overflowStyle.scrollBarWidth.second = width.IsValid() ? width : Dimension();
1414                 }
1415             } },
1416         { DOM_SHARED_TRANSITION_EFFECT,
1417             [](const std::string& value, Declaration& declaration) {
1418                 auto& shareTransitionStyle =
1419                     declaration.MaybeResetStyle<CommonShareTransitionStyle>(StyleTag::COMMON_SHARE_TRANSITION_STYLE);
1420                 if (shareTransitionStyle.IsValid()) {
1421                     shareTransitionStyle.sharedEffect = ParseSharedEffect(value, declaration);
1422                 }
1423             } },
1424         { DOM_SHARED_TRANSITION_TIMING_FUNCTION,
1425             [](const std::string& value, Declaration& declaration) {
1426                 auto& shareTransitionStyle =
1427                     declaration.MaybeResetStyle<CommonShareTransitionStyle>(StyleTag::COMMON_SHARE_TRANSITION_STYLE);
1428                 if (shareTransitionStyle.IsValid()) {
1429                     shareTransitionStyle.curve = CreateCurve(value);
1430                     shareTransitionStyle.sharedTransitionOption.SetCurve(shareTransitionStyle.curve);
1431                 }
1432             } },
1433         { DOM_POSITION_START,
1434             [](const std::string& value, Declaration& declaration) {
1435                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1436                 if (positionStyle.IsValid() && !value.empty()) {
1437                     if (declaration.IsRightToLeft()) {
1438                         positionStyle.right = declaration.ParseDimension(value);
1439                         declaration.hasRight_ = true;
1440                     } else {
1441                         positionStyle.left = declaration.ParseDimension(value);
1442                         declaration.hasLeft_ = true;
1443                     }
1444                     declaration.hasPositionStyle_ = true;
1445                 }
1446             } },
1447         { DOM_POSITION_TOP,
1448             [](const std::string& value, Declaration& declaration) {
1449                 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1450                 if (positionStyle.IsValid() && !value.empty()) {
1451                     positionStyle.top = declaration.ParseDimension(value);
1452                     declaration.hasPositionStyle_ = true;
1453                     declaration.hasTop_ = true;
1454                 }
1455             } },
1456         { DOM_TRANSFORM_ORIGIN,
1457             [](const std::string& val, Declaration& declaration) {
1458                 declaration.hasTransformStyle_ = true;
1459                 auto& animationStyle =
1460                     declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
1461                 if (!animationStyle.IsValid()) {
1462                     LOGD("don't support animation style");
1463                     return;
1464                 }
1465 
1466                 std::vector<std::string> offsets;
1467                 StringUtils::StringSpliter(val, ' ', offsets);
1468                 if (offsets.size() == TRANSFORM_SINGLE) {
1469                     Dimension originDimensionX = TRANSFORM_ORIGIN_DEFAULT;
1470                     Dimension originDimensionY = TRANSFORM_ORIGIN_DEFAULT;
1471                     // for Enum
1472                     if (CheckTransformEnum(val)) {
1473                         auto resultX = ConvertStrToTransformOrigin(val, Axis::HORIZONTAL);
1474                         if (resultX.first) {
1475                             originDimensionX = resultX.second;
1476                         }
1477                         auto resultY = ConvertStrToTransformOrigin(val, Axis::VERTICAL);
1478                         if (resultY.first) {
1479                             originDimensionY = resultY.second;
1480                         }
1481                     } else {
1482                         // for Dimension
1483                         originDimensionX = declaration.ParseDimension(val);
1484                     }
1485                     animationStyle.tweenOption.SetTransformOrigin(originDimensionX, originDimensionY);
1486                     animationStyle.transformOriginX = originDimensionX;
1487                     animationStyle.transformOriginY = originDimensionY;
1488                 } else if (offsets.size() == TRANSFORM_DUAL) {
1489                     Dimension originDimensionX = TRANSFORM_ORIGIN_DEFAULT;
1490                     Dimension originDimensionY = TRANSFORM_ORIGIN_DEFAULT;
1491                     if (CheckTransformEnum(offsets[0])) {
1492                         auto result = ConvertStrToTransformOrigin(offsets[0], Axis::HORIZONTAL);
1493                         if (result.first) {
1494                             originDimensionX = result.second;
1495                         }
1496                     } else {
1497                         originDimensionX = declaration.ParseDimension(offsets[0]);
1498                     }
1499 
1500                     if (CheckTransformEnum(offsets[1])) {
1501                         auto result = ConvertStrToTransformOrigin(offsets[1], Axis::VERTICAL);
1502                         if (result.first) {
1503                             originDimensionY = result.second;
1504                         }
1505                     } else {
1506                         originDimensionY = declaration.ParseDimension(offsets[1]);
1507                     }
1508                     animationStyle.tweenOption.SetTransformOrigin(originDimensionX, originDimensionY);
1509                     animationStyle.transformOriginX = originDimensionX;
1510                     animationStyle.transformOriginY = originDimensionY;
1511                 }
1512                 declaration.hasTransformOriginStyle_ = true;
1513             } },
1514         { DOM_TRANSITION_DURATION,
1515             [](const std::string& value, Declaration& declaration) {
1516                 auto& pageTransitionStyle =
1517                     declaration.MaybeResetStyle<CommonPageTransitionStyle>(StyleTag::COMMON_PAGE_TRANSITION_STYLE);
1518                 if (pageTransitionStyle.IsValid()) {
1519                     if (value.find("ms") != std::string::npos) {
1520                         pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value);
1521                     } else if (value.find('s') != std::string::npos) {
1522                         pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value) * MS_TO_S;
1523                     } else {
1524                         // default unit is ms
1525                         pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value);
1526                     }
1527                     pageTransitionStyle.transitionEnterOption.SetDuration(pageTransitionStyle.transitionDuration);
1528                     pageTransitionStyle.transitionExitOption.SetDuration(pageTransitionStyle.transitionDuration);
1529                 }
1530             } },
1531         // card transition
1532         { DOM_TRANSITION_EFFECT,
1533             [](const std::string& value, Declaration& declaration) {
1534                 declaration.hasTransitionAnimation_ = true;
1535                 auto& cardTransitionStyle =
1536                     declaration.MaybeResetStyle<CommonCardTransitionStyle>(StyleTag::COMMON_CARD_TRANSITION_STYLE);
1537                 if (cardTransitionStyle.IsValid()) {
1538                     declaration.hasTransformStyle_ = true;
1539                     cardTransitionStyle.transitionEffect = ParseTransitionEffect(value);
1540                 }
1541             } },
1542         { DOM_TRANSITION_TIMING_FUNCTION,
1543             [](const std::string& value, Declaration& declaration) {
1544                 auto& pageTransitionStyle =
1545                     declaration.MaybeResetStyle<CommonPageTransitionStyle>(StyleTag::COMMON_PAGE_TRANSITION_STYLE);
1546                 if (pageTransitionStyle.IsValid()) {
1547                     pageTransitionStyle.curve = CreateCurve(value);
1548                     pageTransitionStyle.transitionEnterOption.SetCurve(pageTransitionStyle.curve);
1549                     pageTransitionStyle.transitionExitOption.SetCurve(pageTransitionStyle.curve);
1550                 }
1551             } },
1552         { DOM_VISIBILITY,
1553             [](const std::string& value, Declaration& declaration) {
1554                 auto& visibilityStyle =
1555                     declaration.MaybeResetStyle<CommonVisibilityStyle>(StyleTag::COMMON_VISIBILITY_STYLE);
1556                 if (visibilityStyle.IsValid()) {
1557                     visibilityStyle.visibility =
1558                         (value == DOM_VISIBILITY_HIDDEN) ? VisibilityType::HIDDEN : VisibilityType::VISIBLE;
1559                     declaration.hasDisplayStyle_ = true;
1560                 }
1561             } },
1562         { DOM_WIDTH,
1563             [](const std::string& value, Declaration& declaration) {
1564                 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1565                 if (sizeStyle.IsValid()) {
1566                     sizeStyle.width = declaration.ParseCalcDimension(value);
1567                     declaration.hasBoxStyle_ = true;
1568                 }
1569             } },
1570         { DOM_WINDOW_FILTER,
1571             [](const std::string& value, Declaration& declaration) {
1572                 declaration.hasDecorationStyle_ = true;
1573                 std::vector<std::string> offsets;
1574                 StringUtils::StringSpliter(value, ' ', offsets);
1575                 // progress
1576                 if (offsets.size() >= 1) {
1577                     auto parseValue = ParseFunctionValue<Dimension>(offsets[0], DOM_BLUR, StringToDimension);
1578                     if (parseValue.Unit() == DimensionUnit::PERCENT) {
1579                         auto progress = parseValue.Value();
1580                         if (GreatNotEqual(progress, 0.0) && LessOrEqual(progress, 1.0)) {
1581                             declaration.backDecoration_->SetWindowBlurProgress(static_cast<float>(progress));
1582                         }
1583                     } else {
1584                         declaration.backDecoration_->SetWindowBlurProgress(static_cast<float>(0.0f));
1585                     }
1586                 }
1587                 // style
1588                 if (offsets.size() >= 2) {
1589                     auto windowBlurStyle = StrToWindowBlurStyle(offsets[1]);
1590                     declaration.backDecoration_->SetWindowBlurStyle(windowBlurStyle);
1591                 }
1592             } },
1593         { DOM_ZINDEX,
1594             [](const std::string& value, Declaration& declaration) {
1595                 auto& commonStyle = declaration.MaybeResetStyle<CommonStyle>(StyleTag::COMMON_STYLE);
1596                 if (commonStyle.IsValid()) {
1597                     commonStyle.zIndex = StringToInt(value);
1598                 }
1599             } },
1600     };
1601 
1602     auto operatorIter = BinarySearchFindIndex(styleSetter, ArraySize(styleSetter), style.first.c_str());
1603     if (operatorIter != -1) {
1604         styleSetter[operatorIter].value(style.second, *this);
1605     }
1606 
1607     auto& renderAttr = static_cast<CommonRenderAttribute&>(GetAttribute(AttributeTag::COMMON_RENDER_ATTR));
1608     static const std::unordered_set<std::string> displayStyleSet = { DOM_OPACITY, DOM_DISPLAY, DOM_VISIBILITY };
1609     if (displayStyleSet.find(style.first) != displayStyleSet.end() &&
1610         AceApplicationInfo::GetInstance().GetIsCardType() && renderAttr.show == "false") {
1611         SetShowAttr(renderAttr.show);
1612     }
1613 }
1614 
AddEvent(int32_t pageId,const std::string & eventId,const std::vector<std::string> & events)1615 void Declaration::AddEvent(int32_t pageId, const std::string& eventId, const std::vector<std::string>& events)
1616 {
1617     ACE_SCOPED_TRACE("Declaration::AddEvent");
1618     static const LinearMapNode<void (*)(int32_t, const std::string&, Declaration&)> eventSetters[] = {
1619         { DOM_BLUR,
1620             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1621                 auto& focusableEvent = declaration.MaybeResetEvent<CommonFocusEvent>(EventTag::COMMON_FOCUS_EVENT);
1622                 if (focusableEvent.IsValid()) {
1623                     focusableEvent.blur.eventMarker = EventMarker(eventId, DOM_BLUR, pageId);
1624                     focusableEvent.blur.isRefreshed = true;
1625                 }
1626             } },
1627         { DOM_CAPTURE_TOUCH_CANCEL,
1628             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1629                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1630                 if (rawEvent.IsValid()) {
1631                     rawEvent.captureTouchCancel.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_CANCEL, pageId);
1632                     rawEvent.captureTouchCancel.isRefreshed = true;
1633                 }
1634             } },
1635         { DOM_CAPTURE_TOUCH_END,
1636             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1637                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1638                 if (rawEvent.IsValid()) {
1639                     rawEvent.captureTouchEnd.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_END, pageId);
1640                     rawEvent.captureTouchEnd.isRefreshed = true;
1641                 }
1642             } },
1643         { DOM_CAPTURE_TOUCH_MOVE,
1644             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1645                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1646                 if (rawEvent.IsValid()) {
1647                     rawEvent.captureTouchMove.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_MOVE, pageId);
1648                     rawEvent.captureTouchMove.isRefreshed = true;
1649                 }
1650             } },
1651         { DOM_CAPTURE_TOUCH_START,
1652             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1653                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1654                 if (rawEvent.IsValid()) {
1655                     rawEvent.captureTouchStart.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_START, pageId);
1656                     rawEvent.captureTouchStart.isRefreshed = true;
1657                 }
1658             } },
1659         { DOM_CATCH_BUBBLE_CLICK,
1660           [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1661             auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1662             if (gestureEvent.IsValid()) {
1663                 gestureEvent.click.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_CLICK, pageId);
1664                 gestureEvent.click.eventMarker.SetCatchMode(true);
1665                 gestureEvent.click.isRefreshed = true;
1666             }
1667           } },
1668         { DOM_CATCH_BUBBLE_TOUCH_CANCEL,
1669             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1670                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1671                 if (rawEvent.IsValid()) {
1672                     rawEvent.catchBubbleTouchCancel.eventMarker =
1673                         EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_CANCEL, pageId);
1674                     rawEvent.catchBubbleTouchCancel.isRefreshed = true;
1675                 }
1676             } },
1677         { DOM_CATCH_BUBBLE_TOUCH_END,
1678             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1679                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1680                 if (rawEvent.IsValid()) {
1681                     rawEvent.catchBubbleTouchEnd.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_END, pageId);
1682                     rawEvent.catchBubbleTouchEnd.isRefreshed = true;
1683                 }
1684             } },
1685         { DOM_CATCH_BUBBLE_TOUCH_MOVE,
1686             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1687                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1688                 if (rawEvent.IsValid()) {
1689                     rawEvent.catchBubbleTouchMove.eventMarker =
1690                         EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_MOVE, pageId);
1691                     rawEvent.catchBubbleTouchMove.isRefreshed = true;
1692                 }
1693             } },
1694         { DOM_CATCH_BUBBLE_TOUCH_START,
1695             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1696                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1697                 if (rawEvent.IsValid()) {
1698                     rawEvent.catchBubbleTouchStart.eventMarker =
1699                         EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_START, pageId);
1700                     rawEvent.catchBubbleTouchStart.isRefreshed = true;
1701                 }
1702             } },
1703         { DOM_CATCH_CAPTURE_TOUCH_CANCEL,
1704             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1705                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1706                 if (rawEvent.IsValid()) {
1707                     rawEvent.catchCaptureTouchCancel.eventMarker =
1708                         EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_CANCEL, pageId);
1709                     rawEvent.catchCaptureTouchCancel.isRefreshed = true;
1710                 }
1711             } },
1712         { DOM_CATCH_CAPTURE_TOUCH_END,
1713             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1714                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1715                 if (rawEvent.IsValid()) {
1716                     rawEvent.catchCaptureTouchEnd.eventMarker =
1717                         EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_END, pageId);
1718                     rawEvent.catchCaptureTouchEnd.isRefreshed = true;
1719                 }
1720             } },
1721         { DOM_CATCH_CAPTURE_TOUCH_MOVE,
1722             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1723                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1724                 if (rawEvent.IsValid()) {
1725                     rawEvent.catchCaptureTouchMove.eventMarker =
1726                         EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_MOVE, pageId);
1727                     rawEvent.catchCaptureTouchMove.isRefreshed = true;
1728                 }
1729             } },
1730         { DOM_CATCH_CAPTURE_TOUCH_START,
1731             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1732                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1733                 if (rawEvent.IsValid()) {
1734                     rawEvent.catchCaptureTouchStart.eventMarker =
1735                         EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_START, pageId);
1736                     rawEvent.catchCaptureTouchStart.isRefreshed = true;
1737                 }
1738             } },
1739         { DOM_CLICK,
1740             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1741                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1742                 if (gestureEvent.IsValid()) {
1743                     gestureEvent.click.eventMarker = EventMarker(eventId, DOM_CLICK, pageId);
1744                     gestureEvent.click.eventMarker.SetCatchMode(false);
1745                     gestureEvent.click.isRefreshed = true;
1746                 }
1747             } },
1748         { DOM_DOUBLE_CLICK,
1749             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1750                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1751                 if (gestureEvent.IsValid()) {
1752                     gestureEvent.doubleClick.eventMarker = EventMarker(eventId, DOM_DOUBLE_CLICK, pageId);
1753                     gestureEvent.doubleClick.isRefreshed = true;
1754                 }
1755             } },
1756         { DOM_DRAG,
1757             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1758                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1759                 if (gestureEvent.IsValid()) {
1760                     gestureEvent.drag.eventMarker = EventMarker(eventId, DOM_DRAG, pageId);
1761                     gestureEvent.drag.isRefreshed = true;
1762                 }
1763             } },
1764         { DOM_DRAG_END,
1765             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1766                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1767                 if (gestureEvent.IsValid()) {
1768                     gestureEvent.dragEnd.eventMarker = EventMarker(eventId, DOM_DRAG_END, pageId);
1769                     gestureEvent.dragEnd.isRefreshed = true;
1770                 }
1771             } },
1772         { DOM_DRAG_ENTER,
1773             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1774                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1775                 if (gestureEvent.IsValid()) {
1776                     gestureEvent.dragEnter.eventMarker = EventMarker(eventId, DOM_DRAG_ENTER, pageId);
1777                     gestureEvent.dragEnter.isRefreshed = true;
1778                 }
1779             } },
1780         { DOM_DRAG_LEAVE,
1781             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1782                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1783                 if (gestureEvent.IsValid()) {
1784                     gestureEvent.dragLeave.eventMarker = EventMarker(eventId, DOM_DRAG_LEAVE, pageId);
1785                     gestureEvent.dragLeave.isRefreshed = true;
1786                 }
1787             } },
1788         { DOM_DRAG_OVER,
1789             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1790                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1791                 if (gestureEvent.IsValid()) {
1792                     gestureEvent.dragOver.eventMarker = EventMarker(eventId, DOM_DRAG_OVER, pageId);
1793                     gestureEvent.dragOver.isRefreshed = true;
1794                 }
1795             } },
1796         { DOM_DRAG_START,
1797             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1798                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1799                 if (gestureEvent.IsValid()) {
1800                     gestureEvent.dragStart.eventMarker = EventMarker(eventId, DOM_DRAG_START, pageId);
1801                     gestureEvent.dragStart.isRefreshed = true;
1802                 }
1803             } },
1804         { DOM_DRAG_DROP,
1805             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1806                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1807                 if (gestureEvent.IsValid()) {
1808                     gestureEvent.dragDrop.eventMarker = EventMarker(eventId, DOM_DRAG_DROP, pageId);
1809                     gestureEvent.dragDrop.isRefreshed = true;
1810                 }
1811             } },
1812         { DOM_FOCUS,
1813             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1814                 auto& focusEvent = declaration.MaybeResetEvent<CommonFocusEvent>(EventTag::COMMON_FOCUS_EVENT);
1815                 if (focusEvent.IsValid()) {
1816                     focusEvent.focus.eventMarker = EventMarker(eventId, DOM_FOCUS, pageId);
1817                     focusEvent.focus.isRefreshed = true;
1818                 }
1819             } },
1820         { DOM_HOVER,
1821             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1822                 auto& mouseEvent = declaration.MaybeResetEvent<CommonMouseEvent>(EventTag::COMMON_MOUSE_EVENT);
1823                 if (mouseEvent.IsValid()) {
1824                     mouseEvent.mouseHover.eventMarker = EventMarker(eventId, DOM_HOVER, pageId);
1825                     mouseEvent.mouseHover.isRefreshed = true;
1826                 }
1827             } },
1828         { DOM_KEY,
1829             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1830                 auto& keyEvent = declaration.MaybeResetEvent<CommonKeyEvent>(EventTag::COMMON_KEY_EVENT);
1831                 if (keyEvent.IsValid()) {
1832                     keyEvent.key.eventMarker = EventMarker(eventId, DOM_KEY, pageId);
1833                     keyEvent.key.isRefreshed = true;
1834                 }
1835             } },
1836         { DOM_LONG_PRESS,
1837             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1838                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1839                 if (gestureEvent.IsValid()) {
1840                     gestureEvent.longPress.eventMarker = EventMarker(eventId, DOM_LONG_PRESS, pageId);
1841                     gestureEvent.longPress.isRefreshed = true;
1842                 }
1843             } },
1844         { DOM_MOUSE,
1845             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1846                 auto& mouseEvent = declaration.MaybeResetEvent<CommonMouseEvent>(EventTag::COMMON_MOUSE_EVENT);
1847                 if (mouseEvent.IsValid()) {
1848                     mouseEvent.mouse.eventMarker = EventMarker(eventId, DOM_MOUSE, pageId);
1849                     mouseEvent.mouse.isRefreshed = true;
1850                 }
1851             } },
1852         { DOM_PINCH_CANCEL,
1853             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1854                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1855                 if (gestureEvent.IsValid()) {
1856                     gestureEvent.pinchCancel.eventMarker = EventMarker(eventId, DOM_PINCH_CANCEL, pageId);
1857                     gestureEvent.pinchCancel.isRefreshed = true;
1858                 }
1859             } },
1860         { DOM_PINCH_END,
1861             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1862                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1863                 if (gestureEvent.IsValid()) {
1864                     gestureEvent.pinchEnd.eventMarker = EventMarker(eventId, DOM_PINCH_END, pageId);
1865                     gestureEvent.pinchEnd.isRefreshed = true;
1866                 }
1867             } },
1868         { DOM_PINCH_START,
1869             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1870                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1871                 if (gestureEvent.IsValid()) {
1872                     gestureEvent.pinchStart.eventMarker = EventMarker(eventId, DOM_PINCH_START, pageId);
1873                     gestureEvent.pinchStart.isRefreshed = true;
1874                 }
1875             } },
1876         { DOM_PINCH_UPDATE,
1877             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1878                 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1879                 if (gestureEvent.IsValid()) {
1880                     gestureEvent.pinchUpdate.eventMarker = EventMarker(eventId, DOM_PINCH_UPDATE, pageId);
1881                     gestureEvent.pinchUpdate.isRefreshed = true;
1882                 }
1883             } },
1884         { DOM_CROWN_ROTATE,
1885             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1886                 auto& crownEvent = declaration.MaybeResetEvent<CommonCrownEvent>(EventTag::COMMON_CROWN_EVENT);
1887                 if (crownEvent.IsValid()) {
1888                     crownEvent.rotate.eventMarker = EventMarker(eventId, DOM_CROWN_ROTATE, pageId);
1889                     crownEvent.rotate.isRefreshed = true;
1890                 }
1891             } },
1892         { DOM_SWIPE,
1893             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1894                 auto& swipeEvent = declaration.MaybeResetEvent<CommonSwipeEvent>(EventTag::COMMON_SWIPE_EVENT);
1895                 if (swipeEvent.IsValid()) {
1896                     swipeEvent.swipe.eventMarker = EventMarker(eventId, DOM_SWIPE, pageId);
1897                     swipeEvent.swipe.isRefreshed = true;
1898                 }
1899             } },
1900         { DOM_TOUCH_CANCEL,
1901             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1902                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1903                 if (rawEvent.IsValid()) {
1904                     rawEvent.touchCancel.eventMarker = EventMarker(eventId, DOM_TOUCH_CANCEL, pageId);
1905                     rawEvent.touchCancel.isRefreshed = true;
1906                 }
1907             } },
1908         { DOM_TOUCH_END,
1909             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1910                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1911                 if (rawEvent.IsValid()) {
1912                     rawEvent.touchEnd.eventMarker = EventMarker(eventId, DOM_TOUCH_END, pageId);
1913                     rawEvent.touchEnd.isRefreshed = true;
1914                 }
1915             } },
1916         { DOM_TOUCH_MOVE,
1917             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1918                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1919                 if (rawEvent.IsValid()) {
1920                     rawEvent.touchMove.eventMarker = EventMarker(eventId, DOM_TOUCH_MOVE, pageId);
1921                     rawEvent.touchMove.isRefreshed = true;
1922                 }
1923             } },
1924         { DOM_TOUCH_START,
1925             [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1926                 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1927                 if (rawEvent.IsValid()) {
1928                     rawEvent.touchStart.eventMarker = EventMarker(eventId, DOM_TOUCH_START, pageId);
1929                     rawEvent.touchStart.isRefreshed = true;
1930                 }
1931             } },
1932     };
1933     for (const auto& event : events) {
1934         if (SetSpecializedEvent(pageId, eventId, event)) {
1935             continue;
1936         }
1937         auto setterIter = BinarySearchFindIndex(eventSetters, ArraySize(eventSetters), event.c_str());
1938         if (setterIter != -1) {
1939             eventSetters[setterIter].value(pageId, eventId, *this);
1940         }
1941     }
1942 }
1943 
CallMethod(const std::string & method,const std::string & args)1944 void Declaration::CallMethod(const std::string& method, const std::string& args)
1945 {
1946     if (method == COMMON_METHOD_FOCUS) {
1947         if (!focusableController_) {
1948             LOGE("CallMethod: call focus method failed, focusableController_ is null");
1949             return;
1950         }
1951 
1952         bool shouldFocus = true;
1953         std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
1954         if (argsValue && argsValue->IsArray() && argsValue->GetArraySize() == COMMON_METHOD_FOCUS_ARGS_SIZE) {
1955             std::unique_ptr<JsonValue> focusValue = argsValue->GetArrayItem(0)->GetValue(COMMON_METHOD_FOCUS);
1956             if (focusValue && focusValue->IsBool()) {
1957                 shouldFocus = focusValue->GetBool();
1958             }
1959         }
1960         OnRequestFocus(shouldFocus);
1961     } else if (method == DOM_LIST_METHOD_SCROLL_BY) {
1962         std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
1963         if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() != 1) {
1964             LOGE("parse args error");
1965             return;
1966         }
1967         std::unique_ptr<JsonValue> scrollByPara = argsValue->GetArrayItem(0);
1968         double x = scrollByPara->GetDouble("dx", 0.0);
1969         double y = scrollByPara->GetDouble("dy", 0.0);
1970         bool isSmooth = scrollByPara->GetBool("smooth", true);
1971         OnScrollBy(x, y, isSmooth);
1972     } else {
1973         CallSpecializedMethod(method, args);
1974     }
1975 }
1976 
OnRequestFocus(bool shouldFocus)1977 void Declaration::OnRequestFocus(bool shouldFocus)
1978 {
1979     auto& commonMethod = MaybeResetMethod<CommonMethod>(MethodTag::COMMON_METHOD);
1980     if (commonMethod.IsValid()) {
1981         commonMethod.Focus(focusableController_, shouldFocus);
1982     }
1983 }
1984 
OnScrollBy(double dx,double dy,bool isSmooth)1985 void Declaration::OnScrollBy(double dx, double dy, bool isSmooth)
1986 {
1987     auto& commonMethod = MaybeResetMethod<CommonMethod>(MethodTag::COMMON_METHOD);
1988     if (commonMethod.IsValid()) {
1989         commonMethod.ScrollBy(positionController_, dx, dy, isSmooth);
1990     }
1991 }
1992 
SetPaddingOverall(const std::string & value,Declaration & declaration)1993 void Declaration::SetPaddingOverall(const std::string& value, Declaration& declaration)
1994 {
1995     auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1996     if (!paddingStyle.IsValid()) {
1997         LOGD("don't support padding");
1998         return;
1999     }
2000 
2001     std::vector<std::string> offsets;
2002     StringUtils::StringSpliter(value, ' ', offsets);
2003     switch (offsets.size()) {
2004         case 1:
2005             paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[0]));
2006             paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[0]));
2007             paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2008             paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[0]));
2009             break;
2010         case 2:
2011             paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[1]));
2012             paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2013             paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2014             paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[0]));
2015             break;
2016         case 3:
2017             paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[1]));
2018             paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2019             paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2020             paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[2]));
2021             break;
2022         case 4:
2023             paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[3]));
2024             paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2025             paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2026             paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[2]));
2027             break;
2028         default:
2029             break;
2030     }
2031     declaration.hasBoxStyle_ = true;
2032 }
2033 
SetMarginOverall(const std::string & value,Declaration & declaration)2034 void Declaration::SetMarginOverall(const std::string& value, Declaration& declaration)
2035 {
2036     auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
2037     if (!marginStyle.IsValid()) {
2038         LOGD("don't support margin");
2039         return;
2040     }
2041 
2042     std::vector<std::string> offsets;
2043     StringUtils::StringSpliter(value, ' ', offsets);
2044     switch (offsets.size()) {
2045         case 1:
2046             marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[0]));
2047             marginStyle.margin.SetRight(declaration.ParseDimension(offsets[0]));
2048             marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2049             marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[0]));
2050             break;
2051         case 2:
2052             marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[1]));
2053             marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2054             marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2055             marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[0]));
2056             break;
2057         case 3:
2058             marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[1]));
2059             marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2060             marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2061             marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[2]));
2062             break;
2063         case 4:
2064             marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[3]));
2065             marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2066             marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2067             marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[2]));
2068             break;
2069         default:
2070             break;
2071     }
2072     declaration.hasBoxStyle_ = true;
2073 }
2074 
SetBorderImageWidthForFourEdges(const std::string & value,Declaration & declaration)2075 void Declaration::SetBorderImageWidthForFourEdges(const std::string& value, Declaration& declaration)
2076 {
2077     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2078     std::vector<std::string> offsets;
2079     StringUtils::StringSpliter(value, ' ', offsets);
2080     switch (offsets.size()) {
2081         case 1:
2082             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2083             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]),
2084                 BorderImageDirection::BOTTOM);
2085             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::LEFT);
2086             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::RIGHT);
2087             declaration.backDecoration_->SetHasBorderImageWidth(true);
2088             break;
2089         case 2:
2090             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2091             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]),
2092                 BorderImageDirection::BOTTOM);
2093             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2094             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2095             declaration.backDecoration_->SetHasBorderImageWidth(true);
2096             break;
2097         case 3:
2098             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2099             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[2]),
2100                 BorderImageDirection::BOTTOM);
2101             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2102             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2103             declaration.backDecoration_->SetHasBorderImageWidth(true);
2104             break;
2105         case 4:
2106             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2107             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[2]),
2108                 BorderImageDirection::BOTTOM);
2109             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2110             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(offsets[3]), BorderImageDirection::RIGHT);
2111             declaration.backDecoration_->SetHasBorderImageWidth(true);
2112             break;
2113         default:
2114             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(value), BorderImageDirection::TOP);
2115             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(value),
2116                 BorderImageDirection::BOTTOM);
2117             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(value), BorderImageDirection::LEFT);
2118             borderStyle.border.SetBorderImageWidth(declaration.ParseDimension(value), BorderImageDirection::RIGHT);
2119             declaration.backDecoration_->SetHasBorderImageWidth(false);
2120             break;
2121     }
2122     declaration.hasDecorationStyle_ = true;
2123     declaration.hasBorderStyle_ = true;
2124 }
2125 
SetBorderImageSliceForFourEdges(const std::string & value,Declaration & declaration)2126 void Declaration::SetBorderImageSliceForFourEdges(const std::string& value, Declaration& declaration)
2127 {
2128     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2129     std::vector<std::string> offsets;
2130     StringUtils::StringSpliter(value, ' ', offsets);
2131     switch (offsets.size()) {
2132         case 1:
2133             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::LEFT);
2134             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2135             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::RIGHT);
2136             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]),
2137                 BorderImageDirection::BOTTOM);
2138             declaration.backDecoration_->SetHasBorderImageSlice(true);
2139             break;
2140         case 2:
2141             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2142             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2143             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2144             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]),
2145                 BorderImageDirection::BOTTOM);
2146             declaration.backDecoration_->SetHasBorderImageSlice(true);
2147             break;
2148         case 3:
2149             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2150             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2151             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2152             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[2]),
2153                 BorderImageDirection::BOTTOM);
2154             declaration.backDecoration_->SetHasBorderImageSlice(true);
2155             break;
2156         case 4:
2157             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[3]), BorderImageDirection::LEFT);
2158             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2159             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2160             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(offsets[2]),
2161                 BorderImageDirection::BOTTOM);
2162             declaration.backDecoration_->SetHasBorderImageSlice(true);
2163             break;
2164         default:
2165             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(value), BorderImageDirection::LEFT);
2166             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(value), BorderImageDirection::TOP);
2167             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(value), BorderImageDirection::RIGHT);
2168             borderStyle.border.SetBorderImageSlice(declaration.ParseDimension(value),
2169                 BorderImageDirection::BOTTOM);
2170             declaration.backDecoration_->SetHasBorderImageSlice(false);
2171             break;
2172     }
2173     declaration.hasDecorationStyle_ = true;
2174     declaration.hasBorderStyle_ = true;
2175 }
2176 
SetBorderImageOutSetForFourEdges(const std::string & value,Declaration & declaration)2177 void Declaration::SetBorderImageOutSetForFourEdges(const std::string& value, Declaration& declaration)
2178 {
2179     auto& bs = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2180     std::vector<std::string> offsets;
2181     StringUtils::StringSpliter(value, ' ', offsets);
2182     switch (offsets.size()) {
2183         case 1:
2184             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::LEFT);
2185             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2186             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::RIGHT);
2187             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::BOTTOM);
2188             declaration.backDecoration_->SetHasBorderImageOutset(true);
2189             break;
2190         case 2:
2191             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2192             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2193             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2194             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::BOTTOM);
2195             declaration.backDecoration_->SetHasBorderImageOutset(true);
2196             break;
2197         case 3:
2198             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[1]), BorderImageDirection::LEFT);
2199             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2200             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2201             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[2]), BorderImageDirection::BOTTOM);
2202             declaration.backDecoration_->SetHasBorderImageOutset(true);
2203             break;
2204         case 4:
2205             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[3]), BorderImageDirection::LEFT);
2206             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[0]), BorderImageDirection::TOP);
2207             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[1]), BorderImageDirection::RIGHT);
2208             bs.border.SetBorderImageOutset(declaration.ParseDimension(offsets[2]), BorderImageDirection::BOTTOM);
2209             declaration.backDecoration_->SetHasBorderImageOutset(true);
2210             break;
2211         default:
2212             bs.border.SetBorderImageOutset(declaration.ParseDimension(value), BorderImageDirection::LEFT);
2213             bs.border.SetBorderImageOutset(declaration.ParseDimension(value), BorderImageDirection::TOP);
2214             bs.border.SetBorderImageOutset(declaration.ParseDimension(value), BorderImageDirection::RIGHT);
2215             bs.border.SetBorderImageOutset(declaration.ParseDimension(value), BorderImageDirection::BOTTOM);
2216             declaration.backDecoration_->SetHasBorderImageOutset(false);
2217             break;
2218     }
2219     declaration.hasDecorationStyle_ = true;
2220     declaration.hasBorderStyle_ = true;
2221 }
2222 
SetBorderImageRepeatForFourEdges(const std::string & value,Declaration & declaration)2223 void Declaration::SetBorderImageRepeatForFourEdges(const std::string& value, Declaration& declaration)
2224 {
2225     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2226     borderStyle.border.SetBorderImageRepeat(ConvertStrToBorderImageRepeat(value));
2227     borderStyle.border.SetBorderImageRepeat(ConvertStrToBorderImageRepeat(value));
2228     borderStyle.border.SetBorderImageRepeat(ConvertStrToBorderImageRepeat(value));
2229     borderStyle.border.SetBorderImageRepeat(ConvertStrToBorderImageRepeat(value));
2230     declaration.backDecoration_->SetHasBorderImageRepeat(true);
2231     declaration.hasDecorationStyle_ = true;
2232     declaration.hasBorderStyle_ = true;
2233 }
2234 
SetBorderImage(const std::string & value,Declaration & declaration)2235 void Declaration::SetBorderImage(const std::string& value, Declaration& declaration)
2236 {
2237     declaration.backDecoration_->SetHasBorderImageSource(false);
2238     declaration.backDecoration_->SetHasBorderImageGradient(false);
2239     SetBorderImageSliceForFourEdges("", declaration);
2240     SetBorderImageWidthForFourEdges("", declaration);
2241     SetBorderImageOutSetForFourEdges("", declaration);
2242     SetBorderImageRepeatForFourEdges("", declaration);
2243 
2244     auto borderImageJson = JsonUtil::ParseJsonString(value);
2245     if (!borderImageJson->IsObject()) {
2246         LOGE("borderImageJson json is not Object");
2247         return;
2248     }
2249     if (borderImageJson->Contains(DOM_VALUES) && borderImageJson->GetValue(DOM_VALUES)->IsArray() &&
2250         borderImageJson->GetValue(DOM_VALUES)->GetArraySize() > 0) {
2251 
2252         auto values = borderImageJson->GetValue(DOM_VALUES)->GetArrayItem(0);
2253 
2254         if (values->Contains("url")) {
2255             SetBorderImageUrl(values, declaration);
2256         } else {
2257             SetBorderImageGradient(values, declaration);
2258         }
2259     }
2260 }
2261 
SetBorderImageGradient(const std::unique_ptr<JsonValue> & values,Declaration & declaration)2262 void Declaration::SetBorderImageGradient(const std::unique_ptr<JsonValue>& values, Declaration& declaration)
2263 {
2264     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2265     backgroundStyle.gradientBorderImage = Gradient();
2266     if (values->Contains(DOM_GRADIENT_TYPE) && values->GetValue(DOM_GRADIENT_TYPE)->IsString()) {
2267         SetBorderImageGradientType(values->GetValue(DOM_GRADIENT_TYPE)->GetString(), declaration);
2268     }
2269     if (values->Contains(DOM_GRADIENT_DIRECTIONS) && values->GetValue(DOM_GRADIENT_DIRECTIONS)->IsArray()) {
2270         SetBorderImageGradientDirections(values->GetValue(DOM_GRADIENT_DIRECTIONS), declaration);
2271     }
2272     if (values->Contains(DOM_GRADIENT_VALUES) && values->GetValue(DOM_GRADIENT_VALUES)->IsArray()) {
2273         SetBorderImageGradientColor(values->GetValue(DOM_GRADIENT_VALUES), declaration);
2274     }
2275     if (values->Contains("slice") && values->GetValue("slice")->IsArray()) {
2276         std::unique_ptr<JsonValue> sliceItem = values->GetValue("slice");
2277         std::string sliceStr;
2278         for (int32_t i = 0; i < sliceItem->GetArraySize(); i++) {
2279             sliceStr += sliceItem->GetArrayItem(i)->GetString() + " ";
2280         }
2281         SetBorderImageSliceForFourEdges(sliceStr, declaration);
2282     }
2283     declaration.backDecoration_->SetHasBorderImageGradient(true);
2284     declaration.hasDecorationStyle_ = true;
2285     declaration.hasBorderStyle_ = true;
2286 }
2287 
SetBorderImageUrl(const std::unique_ptr<JsonValue> & values,Declaration & declaration)2288 void Declaration::SetBorderImageUrl(const std::unique_ptr<JsonValue>& values, Declaration& declaration)
2289 {
2290     if (values->Contains("url") && values->GetValue("url")->IsString()) {
2291         SetBorderImageFindUrl(values->GetValue("url")->GetString(), declaration);
2292     }
2293     if (values->Contains("slice") && values->GetValue("slice")->IsArray()) {
2294         std::unique_ptr<JsonValue> sliceItem = values->GetValue("slice");
2295         std::string sliceStr;
2296         for (int32_t i = 0; i < sliceItem->GetArraySize(); i++) {
2297             sliceStr += sliceItem->GetArrayItem(i)->GetString() + " ";
2298         }
2299         SetBorderImageSliceForFourEdges(sliceStr, declaration);
2300     }
2301     if (values->Contains("width") && values->GetValue("width")->IsArray()) {
2302         std::unique_ptr<JsonValue> widthItem = values->GetValue("width");
2303 
2304         std::string widthStr;
2305         for (int32_t i = 0; i < widthItem->GetArraySize(); i++) {
2306             widthStr += widthItem->GetArrayItem(i)->GetString() + " ";
2307         }
2308         SetBorderImageWidthForFourEdges(widthStr, declaration);
2309     }
2310     if (values->Contains("outset") && values->GetValue("outset")->IsArray()) {
2311         std::unique_ptr<JsonValue> outsetItem = values->GetValue("outset");
2312 
2313         std::string outsetStr;
2314         for (int32_t i = 0; i < outsetItem->GetArraySize(); i++) {
2315             outsetStr += outsetItem->GetArrayItem(i)->GetString() + " ";
2316         }
2317         SetBorderImageOutSetForFourEdges(outsetStr, declaration);
2318     }
2319     if (values->Contains("repeat") && values->GetValue("repeat")->IsString()) {
2320         SetBorderImageRepeatForFourEdges(values->GetValue("repeat")->GetString(), declaration);
2321     }
2322     declaration.hasDecorationStyle_ = true;
2323     declaration.hasBorderStyle_ = true;
2324 }
2325 
SetBorderImageFindUrl(const std::string & value,Declaration & declaration)2326 void Declaration::SetBorderImageFindUrl(const std::string& value, Declaration& declaration)
2327 {
2328     auto& backgroundStyle =
2329                     declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2330     if (backgroundStyle.IsValid()) {
2331         backgroundStyle.borderImage->SetSrc(value);
2332         declaration.backDecoration_->SetBorderImage(backgroundStyle.borderImage);
2333         declaration.backDecoration_->SetHasBorderImageSource(true);
2334         declaration.hasDecorationStyle_ = true;
2335     }
2336 }
2337 
SetBorderImageGradientType(const std::string & gradientType,Declaration & declaration)2338 void Declaration::SetBorderImageGradientType(const std::string& gradientType, Declaration& declaration)
2339 {
2340     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2341     if (!backgroundStyle.IsValid()) {
2342         return;
2343     }
2344     // default: LINEAR
2345     backgroundStyle.gradientBorderImage.SetType(GradientType::LINEAR);
2346     if (gradientType == DOM_RADIAL_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT) {
2347         backgroundStyle.gradientBorderImage.SetType(GradientType::RADIAL);
2348     } else if (gradientType == DOM_SWEEP_GRADIENT || gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2349         backgroundStyle.gradientBorderImage.SetType(GradientType::SWEEP);
2350     }
2351 
2352     if (gradientType == DOM_REPEATING_LINEAR_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT ||
2353         gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2354         backgroundStyle.gradientBorderImage.SetRepeat(true);
2355     }
2356     declaration.hasDecorationStyle_ = true;
2357 }
2358 
SetBorderImageGradientDirections(const std::unique_ptr<JsonValue> & gradientDirections,Declaration & declaration)2359 void Declaration::SetBorderImageGradientDirections(const std::unique_ptr<JsonValue>& gradientDirections,
2360     Declaration& declaration)
2361 {
2362     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2363     if (!backgroundStyle.IsValid()) {
2364         return;
2365     }
2366 
2367     std::unique_ptr<JsonValue> angleItem;
2368     std::unique_ptr<JsonValue> sideItem;
2369     std::unique_ptr<JsonValue> cornerItem;
2370     GradientDirection direction;
2371     switch (gradientDirections->GetArraySize()) {
2372         case DIRECTION_ANGLE:
2373             angleItem = gradientDirections->GetArrayItem(0);
2374             if (angleItem->IsString()) {
2375                 LinearGradient linearGradient;
2376                 linearGradient.angle = AnimatableDimension(StringToDouble(angleItem->GetString()));
2377                 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2378                 declaration.hasDecorationStyle_ = true;
2379             }
2380             break;
2381         case DIRECTION_SIDE:
2382             sideItem = gradientDirections->GetArrayItem(1);
2383             if (sideItem->IsString()) {
2384                 direction = StrToGradientDirection(sideItem->GetString());
2385                 LinearGradient linearGradient;
2386                 if (LinearGradient::IsXAxis(direction)) {
2387                     linearGradient.linearX = direction;
2388                 } else {
2389                     linearGradient.linearY = direction;
2390                 }
2391                 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2392                 declaration.hasDecorationStyle_ = true;
2393             }
2394             break;
2395         case DIRECTION_CORNER:
2396             sideItem = gradientDirections->GetArrayItem(1);
2397             cornerItem = gradientDirections->GetArrayItem(2);
2398             if (sideItem->IsString() && cornerItem->IsString()) {
2399                 LinearGradient linearGradient;
2400                 auto direction1 = StrToGradientDirection(sideItem->GetString());
2401                 auto direction2 = StrToGradientDirection(cornerItem->GetString());
2402                 if ((LinearGradient::IsXAxis(direction1) && LinearGradient::IsXAxis(direction2)) ||
2403                     (!LinearGradient::IsXAxis(direction1) && !LinearGradient::IsXAxis(direction2))) {
2404                     linearGradient.linearY = GradientDirection::BOTTOM;
2405                     break;
2406                 } else {
2407                     if (LinearGradient::IsXAxis(direction1)) {
2408                         linearGradient.linearX = direction1;
2409                         linearGradient.linearY = direction2;
2410                     } else {
2411                         linearGradient.linearY = direction1;
2412                         linearGradient.linearX = direction2;
2413                     }
2414                 }
2415                 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2416                 declaration.hasDecorationStyle_ = true;
2417             }
2418             break;
2419         default:
2420             LOGE("gradientDirectionsLength error");
2421             break;
2422     }
2423 }
2424 
SetBorderImageGradientColor(const std::unique_ptr<JsonValue> & gradientColorValues,Declaration & declaration)2425 void Declaration::SetBorderImageGradientColor(const std::unique_ptr<JsonValue>& gradientColorValues,
2426     Declaration& declaration)
2427 {
2428     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2429     if (!backgroundStyle.IsValid()) {
2430         return;
2431     }
2432     backgroundStyle.gradientBorderImage.ClearColors();
2433     int32_t gradientColorValuesLength = gradientColorValues->GetArraySize();
2434     for (int32_t i = 0; i < gradientColorValuesLength; i++) {
2435         std::string gradientColorValue = gradientColorValues->GetArrayItem(i)->GetString();
2436         GradientColor gradientColor;
2437         RemoveHeadTailSpace(gradientColorValue);
2438         auto index = gradientColorValue.find(' ');
2439         if (index != std::string::npos && index != 0) {
2440             std::string color = gradientColorValue.substr(0, index);
2441             std::string area = gradientColorValue.substr(index + 1, gradientColorValue.size() - index - 1);
2442             gradientColor.SetColor(declaration.ParseColor(color));
2443             gradientColor.SetHasValue(true);
2444             if (area.find("px") != std::string::npos) {
2445                 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PX);
2446             } else if (area.find('%') != std::string::npos) {
2447                 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PERCENT);
2448             } else {
2449                 LOGW("gradientColor DimensionUnit is incorrect)");
2450                 gradientColor.SetHasValue(false);
2451             }
2452         } else {
2453             gradientColor.SetHasValue(false);
2454             gradientColor.SetColor(declaration.ParseColor(gradientColorValue));
2455         }
2456         backgroundStyle.gradientBorderImage.AddColor(gradientColor);
2457         declaration.hasDecorationStyle_ = true;
2458     }
2459 }
2460 
SetBorderOverall(const std::string & value,Declaration & declaration)2461 void Declaration::SetBorderOverall(const std::string& value, Declaration& declaration)
2462 {
2463     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2464     if (!borderStyle.IsValid()) {
2465         LOGD("don't support border");
2466         return;
2467     }
2468 
2469     std::vector<std::string> offsets;
2470     StringUtils::StringSpliter(value, ' ', offsets);
2471     switch (offsets.size()) {
2472         case 1:
2473             if (offsets[0].find("px") != std::string::npos) {
2474                 SetBorderWidthForFourEdges(offsets[0], declaration);
2475             } else if (offsets[0] == "solid" || offsets[0] == "dotted" || offsets[0] == "dashed") {
2476                 SetBorderStyleForFourEdges(offsets[0], declaration);
2477             } else {
2478                 SetBorderColorForFourEdges(offsets[0], declaration);
2479             }
2480             break;
2481         case 2:
2482             SetBorderWidthForFourEdges(offsets[0], declaration);
2483             SetBorderStyleForFourEdges(offsets[1], declaration);
2484             break;
2485         case 3:
2486             SetBorderWidthForFourEdges(offsets[0], declaration);
2487             SetBorderStyleForFourEdges(offsets[1], declaration);
2488             SetBorderColorForFourEdges(offsets[2], declaration);
2489             break;
2490         default:
2491             break;
2492     }
2493 }
2494 
SetBorderWidthForFourEdges(const std::string & value,Declaration & declaration)2495 void Declaration::SetBorderWidthForFourEdges(const std::string& value, Declaration& declaration)
2496 {
2497     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2498     if (borderStyle.IsValid()) {
2499         borderStyle.border.SetWidth(declaration.ParseDimension(value));
2500         declaration.hasDecorationStyle_ = true;
2501         declaration.hasBorderStyle_ = true;
2502     }
2503 }
2504 
SetBorderColorForFourEdges(const std::string & value,Declaration & declaration)2505 void Declaration::SetBorderColorForFourEdges(const std::string& value, Declaration& declaration)
2506 {
2507     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2508     if (borderStyle.IsValid()) {
2509         borderStyle.border.SetColor(declaration.ParseColor(value));
2510         declaration.hasDecorationStyle_ = true;
2511         declaration.hasBorderStyle_ = true;
2512     }
2513 }
2514 
SetBorderStyleForFourEdges(const std::string & value,Declaration & declaration)2515 void Declaration::SetBorderStyleForFourEdges(const std::string& value, Declaration& declaration)
2516 {
2517     auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2518     if (borderStyle.IsValid()) {
2519         borderStyle.border.SetStyle(ConvertStrToBorderStyle(value));
2520         declaration.hasDecorationStyle_ = true;
2521         declaration.hasBorderStyle_ = true;
2522     }
2523 }
2524 
SetMaskGradient(const std::string & value,Declaration & declaration)2525 void Declaration::SetMaskGradient(const std::string& value, Declaration& declaration)
2526 {
2527     Declaration::SetBackground(value, declaration);
2528 }
2529 
SetBackground(const std::string & value,Declaration & declaration)2530 void Declaration::SetBackground(const std::string& value, Declaration& declaration)
2531 {
2532     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2533     if (!backgroundStyle.IsValid()) {
2534         LOGD("don't support background style");
2535         return;
2536     }
2537 
2538     LOGD("Declaration::SetBackground value:%{private}s", value.c_str());
2539     auto backgroundJson = JsonUtil::ParseJsonString(value);
2540     if (!backgroundJson->IsObject()) {
2541         LOGE("background json is not Object");
2542         return;
2543     }
2544     if (backgroundJson->Contains(DOM_VALUES) && backgroundJson->GetValue(DOM_VALUES)->IsArray() &&
2545         backgroundJson->GetValue(DOM_VALUES)->GetArraySize() > 0) {
2546         backgroundStyle.gradient = Gradient();
2547         auto values = backgroundJson->GetValue(DOM_VALUES)->GetArrayItem(0);
2548         // gradient type and repeating
2549         if (values->Contains(DOM_GRADIENT_TYPE) && values->GetValue(DOM_GRADIENT_TYPE)->IsString()) {
2550             SetGradientType(values->GetValue(DOM_GRADIENT_TYPE)->GetString(), declaration);
2551         }
2552         // linearGradient direction
2553         if (values->Contains(DOM_GRADIENT_DIRECTIONS) && values->GetValue(DOM_GRADIENT_DIRECTIONS)->IsArray()) {
2554             SetGradientDirections(values->GetValue(DOM_GRADIENT_DIRECTIONS), declaration);
2555         }
2556         // radialGradient shape
2557         if (values->Contains(DOM_GRADIENT_SHAPE) && values->GetValue(DOM_GRADIENT_SHAPE)->IsString()) {
2558             SetGradientShape(values->GetValue(DOM_GRADIENT_SHAPE)->GetString(), declaration);
2559         }
2560         // radialGradient size
2561         if (values->Contains(DOM_GRADIENT_SIZE) && values->GetValue(DOM_GRADIENT_SIZE)->IsString()) {
2562             SetGradientSize(values->GetValue(DOM_GRADIENT_SIZE)->GetString(), declaration);
2563         }
2564         // radialGradient or sweepGradient position
2565         if (values->Contains(DOM_GRADIENT_POSITION) && values->GetValue(DOM_GRADIENT_POSITION)->IsString()) {
2566             SetGradientPosition(values->GetValue(DOM_GRADIENT_POSITION)->GetString(), declaration);
2567         }
2568         // sweepGradient startAngle and endAngle
2569         if (values->Contains(DOM_GRADIENT_ANGLE) && values->GetValue(DOM_GRADIENT_ANGLE)->IsString()) {
2570             SetGradientAngle(values->GetValue(DOM_GRADIENT_ANGLE)->GetString(), declaration);
2571         }
2572         // sweepGradient rotation
2573         if (values->Contains(DOM_GRADIENT_ROTATION) && values->GetValue(DOM_GRADIENT_ROTATION)->IsString()) {
2574             SetGradientRotation(values->GetValue(DOM_GRADIENT_ROTATION)->GetString(), declaration);
2575         }
2576         // gradient color stops
2577         if (values->Contains(DOM_GRADIENT_VALUES) && values->GetValue(DOM_GRADIENT_VALUES)->IsArray()) {
2578             SetGradientColor(values->GetValue(DOM_GRADIENT_VALUES), declaration);
2579         }
2580     }
2581     declaration.hasDecorationStyle_ = true;
2582     declaration.hasBackGroundColor_ = true;
2583 }
2584 
SetGradientType(const std::string & gradientType,Declaration & declaration)2585 void Declaration::SetGradientType(const std::string& gradientType, Declaration& declaration)
2586 {
2587     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2588     if (!backgroundStyle.IsValid()) {
2589         return;
2590     }
2591     // default: LINEAR
2592     backgroundStyle.gradient.SetType(GradientType::LINEAR);
2593     if (gradientType == DOM_RADIAL_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT) {
2594         backgroundStyle.gradient.SetType(GradientType::RADIAL);
2595     } else if (gradientType == DOM_SWEEP_GRADIENT || gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2596         backgroundStyle.gradient.SetType(GradientType::SWEEP);
2597     }
2598 
2599     if (gradientType == DOM_REPEATING_LINEAR_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT ||
2600         gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2601         backgroundStyle.gradient.SetRepeat(true);
2602     }
2603     declaration.hasDecorationStyle_ = true;
2604 }
2605 
SetGradientDirections(const std::unique_ptr<JsonValue> & gradientDirections,Declaration & declaration)2606 void Declaration::SetGradientDirections(const std::unique_ptr<JsonValue>& gradientDirections, Declaration& declaration)
2607 {
2608     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2609     if (!backgroundStyle.IsValid()) {
2610         return;
2611     }
2612 
2613     std::unique_ptr<JsonValue> angleItem;
2614     std::unique_ptr<JsonValue> sideItem;
2615     std::unique_ptr<JsonValue> cornerItem;
2616     GradientDirection direction;
2617     switch (gradientDirections->GetArraySize()) {
2618         case DIRECTION_ANGLE:
2619             angleItem = gradientDirections->GetArrayItem(0);
2620             if (angleItem->IsString()) {
2621                 LinearGradient linearGradient;
2622                 linearGradient.angle = AnimatableDimension(StringToDouble(angleItem->GetString()));
2623                 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2624                 declaration.hasDecorationStyle_ = true;
2625             }
2626             break;
2627         case DIRECTION_SIDE:
2628             sideItem = gradientDirections->GetArrayItem(1);
2629             if (sideItem->IsString()) {
2630                 direction = StrToGradientDirection(sideItem->GetString());
2631                 LinearGradient linearGradient;
2632                 if (LinearGradient::IsXAxis(direction)) {
2633                     linearGradient.linearX = direction;
2634                 } else {
2635                     linearGradient.linearY = direction;
2636                 }
2637                 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2638                 declaration.hasDecorationStyle_ = true;
2639             }
2640             break;
2641         case DIRECTION_CORNER:
2642             sideItem = gradientDirections->GetArrayItem(1);
2643             cornerItem = gradientDirections->GetArrayItem(2);
2644             if (sideItem->IsString() && cornerItem->IsString()) {
2645                 LinearGradient linearGradient;
2646                 auto direction1 = StrToGradientDirection(sideItem->GetString());
2647                 auto direction2 = StrToGradientDirection(cornerItem->GetString());
2648                 if ((LinearGradient::IsXAxis(direction1) && LinearGradient::IsXAxis(direction2)) ||
2649                     (!LinearGradient::IsXAxis(direction1) && !LinearGradient::IsXAxis(direction2))) {
2650                     linearGradient.linearY = GradientDirection::BOTTOM;
2651                     break;
2652                 } else {
2653                     if (LinearGradient::IsXAxis(direction1)) {
2654                         linearGradient.linearX = direction1;
2655                         linearGradient.linearY = direction2;
2656                     } else {
2657                         linearGradient.linearY = direction1;
2658                         linearGradient.linearX = direction2;
2659                     }
2660                 }
2661                 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2662                 declaration.hasDecorationStyle_ = true;
2663             }
2664             break;
2665         default:
2666             LOGE("gradientDirectionsLength error");
2667             break;
2668     }
2669 }
2670 
SetGradientColor(const std::unique_ptr<JsonValue> & gradientColorValues,Declaration & declaration)2671 void Declaration::SetGradientColor(const std::unique_ptr<JsonValue>& gradientColorValues, Declaration& declaration)
2672 {
2673     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2674     if (!backgroundStyle.IsValid()) {
2675         return;
2676     }
2677 
2678     backgroundStyle.gradient.ClearColors();
2679     int32_t gradientColorValuesLength = gradientColorValues->GetArraySize();
2680     for (int32_t i = 0; i < gradientColorValuesLength; i++) {
2681         std::string gradientColorValue = gradientColorValues->GetArrayItem(i)->GetString();
2682         GradientColor gradientColor;
2683         RemoveHeadTailSpace(gradientColorValue);
2684         auto index = gradientColorValue.find(' ');
2685         if (index != std::string::npos && index != 0) {
2686             std::string color = gradientColorValue.substr(0, index);
2687             std::string area = gradientColorValue.substr(index + 1, gradientColorValue.size() - index - 1);
2688             gradientColor.SetColor(declaration.ParseColor(color));
2689             gradientColor.SetHasValue(true);
2690             if (area.find("px") != std::string::npos) {
2691                 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PX);
2692             } else if (area.find('%') != std::string::npos) {
2693                 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PERCENT);
2694             } else {
2695                 LOGW("gradientColor DimensionUnit is incorrect)");
2696                 gradientColor.SetHasValue(false);
2697             }
2698         } else {
2699             gradientColor.SetHasValue(false);
2700             gradientColor.SetColor(declaration.ParseColor(gradientColorValue));
2701         }
2702         backgroundStyle.gradient.AddColor(gradientColor);
2703         declaration.hasDecorationStyle_ = true;
2704     }
2705 }
2706 
SetGradientShape(const std::string & gradientShape,Declaration & declaration)2707 void Declaration::SetGradientShape(const std::string& gradientShape, Declaration& declaration)
2708 {
2709     // if empty do nothing, If shape is omitted, the ending shape defaults to a circle if the <size> is a single
2710     // <length>, and to an ellipse otherwise.
2711     if (gradientShape.empty()) {
2712         return;
2713     }
2714     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2715     if (!backgroundStyle.IsValid()) {
2716         return;
2717     }
2718 
2719     if (gradientShape == DOM_GRADIENT_SHAPE_ELLIPSE) {
2720         backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::ELLIPSE;
2721     } else {
2722         backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::CIRCLE;
2723     }
2724     declaration.hasDecorationStyle_ = true;
2725 }
2726 
SetGradientSize(const std::string & gradientSize,Declaration & declaration)2727 void Declaration::SetGradientSize(const std::string& gradientSize, Declaration& declaration)
2728 {
2729     if (gradientSize.empty()) {
2730         return;
2731     }
2732     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2733     if (!backgroundStyle.IsValid()) {
2734         return;
2735     }
2736     // 1. closest-corner | closest-side | farthest-corner | farthest-side
2737     auto extent = ParseRadialGradientSize(gradientSize);
2738     if (extent) {
2739         backgroundStyle.gradient.GetRadialGradient().radialSizeType = extent;
2740         declaration.hasDecorationStyle_ = true;
2741         return;
2742     }
2743 
2744     std::vector<std::string> offsets;
2745     StringUtils::StringSpliter(gradientSize, ' ', offsets);
2746     if (offsets.size() == 1) {
2747         // 2. if circle: <length>
2748         auto circleSize = StringToDimension(offsets[0]);
2749 
2750         if (circleSize.Unit() != DimensionUnit::PX) {
2751             LOGE("circle only support length");
2752             return;
2753         }
2754         if (backgroundStyle.gradient.GetRadialGradient().radialShape &&
2755             backgroundStyle.gradient.GetRadialGradient().radialShape != RadialShapeType::CIRCLE) {
2756             LOGE("only circle support one size");
2757             return;
2758         }
2759         backgroundStyle.gradient.GetRadialGradient().radialVerticalSize = circleSize;
2760         backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::CIRCLE;
2761         declaration.hasDecorationStyle_ = true;
2762     } else if (offsets.size() == 2) {
2763         // 3. if ellipse: <length-percentage>{2}
2764         auto horizontalSize = StringToDimension(offsets[0]);
2765         auto verticalSize = StringToDimension(offsets[1]);
2766 
2767         if (backgroundStyle.gradient.GetRadialGradient().radialShape &&
2768             backgroundStyle.gradient.GetRadialGradient().radialShape != RadialShapeType::ELLIPSE) {
2769             LOGE("only ellipse support two size");
2770             return;
2771         }
2772         backgroundStyle.gradient.GetRadialGradient().radialHorizontalSize = horizontalSize;
2773         backgroundStyle.gradient.GetRadialGradient().radialVerticalSize = verticalSize;
2774         backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::ELLIPSE;
2775         declaration.hasDecorationStyle_ = true;
2776     } else {
2777         LOGE("unsupported offset size");
2778     }
2779 }
2780 
SetGradientPosition(const std::string & gradientPosition,Declaration & declaration)2781 void Declaration::SetGradientPosition(const std::string& gradientPosition, Declaration& declaration)
2782 {
2783     if (gradientPosition.empty()) {
2784         return;
2785     }
2786     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2787     if (!backgroundStyle.IsValid()) {
2788         return;
2789     }
2790     // position determines the center of gradient default is center
2791     BackgroundImagePosition position;
2792     if (ParseBackgroundImagePosition(gradientPosition, position)) {
2793         auto xAxisPosition = Dimension(position.GetSizeValueX(),
2794             position.GetSizeTypeX() == BackgroundImagePositionType::PX ? DimensionUnit::PX : DimensionUnit::PERCENT);
2795         auto yAxisPosition = Dimension(position.GetSizeValueY(),
2796             position.GetSizeTypeY() == BackgroundImagePositionType::PX ? DimensionUnit::PX : DimensionUnit::PERCENT);
2797         if (backgroundStyle.gradient.GetType() == GradientType::RADIAL) {
2798             backgroundStyle.gradient.GetRadialGradient().radialCenterX = xAxisPosition;
2799             backgroundStyle.gradient.GetRadialGradient().radialCenterY = yAxisPosition;
2800             declaration.hasDecorationStyle_ = true;
2801         } else if (backgroundStyle.gradient.GetType() == GradientType::SWEEP) {
2802             backgroundStyle.gradient.GetSweepGradient().centerX = xAxisPosition;
2803             backgroundStyle.gradient.GetSweepGradient().centerY = yAxisPosition;
2804             declaration.hasDecorationStyle_ = true;
2805         }
2806     } else {
2807         LOGE("ParseBackgroundImagePosition failed");
2808     }
2809 }
2810 
SetGradientAngle(const std::string & gradientAngle,Declaration & declaration)2811 void Declaration::SetGradientAngle(const std::string& gradientAngle, Declaration& declaration)
2812 {
2813     if (gradientAngle.empty()) {
2814         return;
2815     }
2816     auto backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2817     if (!backgroundStyle.IsValid()) {
2818         LOGE("backgroundStyle is invalid");
2819         return;
2820     }
2821     std::vector<std::string> offsets;
2822     StringUtils::StringSpliter(gradientAngle, ' ', offsets);
2823     if (!offsets.empty()) {
2824         auto startAngle = StringUtils::StringToDegree(offsets[0]);
2825         backgroundStyle.gradient.GetSweepGradient().startAngle = AnimatableDimension(startAngle);
2826         if (offsets.size() > 1) {
2827             auto endAngle = StringUtils::StringToDegree(offsets[1]);
2828             backgroundStyle.gradient.GetSweepGradient().endAngle = AnimatableDimension(endAngle);
2829         }
2830         declaration.hasDecorationStyle_ = true;
2831     }
2832 }
2833 
SetGradientRotation(const std::string & gradientRotation,Declaration & declaration)2834 void Declaration::SetGradientRotation(const std::string& gradientRotation, Declaration& declaration)
2835 {
2836     if (gradientRotation.empty()) {
2837         return;
2838     }
2839     auto backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2840     if (!backgroundStyle.IsValid()) {
2841         return;
2842     }
2843     std::vector<std::string> offsets;
2844     StringUtils::StringSpliter(gradientRotation, ' ', offsets);
2845     if (!offsets.empty()) {
2846         auto rotationAngle = StringUtils::StringToDegree(offsets[0]);
2847         backgroundStyle.gradient.GetSweepGradient().rotation = AnimatableDimension(rotationAngle);
2848         declaration.hasDecorationStyle_ = true;
2849     }
2850 }
2851 
SetBgImgSizeX(const BackgroundImageSizeType type,const double value,BackgroundImageSize & bgImgSize)2852 void SetBgImgSizeX(const BackgroundImageSizeType type, const double value, BackgroundImageSize& bgImgSize)
2853 {
2854     bgImgSize.SetSizeTypeX(type);
2855     bgImgSize.SetSizeValueX(value);
2856 }
2857 
SetBgImgSizeY(const BackgroundImageSizeType type,const double value,BackgroundImageSize & bgImgSize)2858 void SetBgImgSizeY(const BackgroundImageSizeType type, const double value, BackgroundImageSize& bgImgSize)
2859 {
2860     bgImgSize.SetSizeTypeY(type);
2861     bgImgSize.SetSizeValueY(value);
2862 }
2863 
SetBackgroundImageSize(const std::string & value,Declaration & declaration)2864 void Declaration::SetBackgroundImageSize(const std::string& value, Declaration& declaration)
2865 {
2866     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2867     if (!backgroundStyle.IsValid()) {
2868         LOGD("don't support background");
2869         return;
2870     }
2871 
2872     static const LinearMapNode<BackgroundImageSizeType> bgImageSizeType[] = {
2873         { DOM_BACKGROUND_IMAGE_SIZE_AUTO, BackgroundImageSizeType::AUTO },
2874         { DOM_BACKGROUND_IMAGE_SIZE_CONTAIN, BackgroundImageSizeType::CONTAIN },
2875         { DOM_BACKGROUND_IMAGE_SIZE_COVER, BackgroundImageSizeType::COVER },
2876     };
2877     BackgroundImageSize bgImgSize;
2878     auto spaceIndex = value.find(' ', 0);
2879     if (spaceIndex != std::string::npos) {
2880         std::string valueX = value.substr(0, spaceIndex);
2881         std::string valueY = value.substr(spaceIndex + 1, value.size() - spaceIndex - 1);
2882         if (valueX.find("px") != std::string::npos) {
2883             SetBgImgSizeX(BackgroundImageSizeType::LENGTH, StringToDouble(valueX), bgImgSize);
2884         } else if (valueX.find('%') != std::string::npos) {
2885             SetBgImgSizeX(BackgroundImageSizeType::PERCENT, StringToDouble(valueX), bgImgSize);
2886         } else {
2887             bgImgSize.SetSizeTypeX(BackgroundImageSizeType::AUTO);
2888         }
2889         if (valueY.find("px") != std::string::npos) {
2890             SetBgImgSizeY(BackgroundImageSizeType::LENGTH, StringToDouble(valueY), bgImgSize);
2891         } else if (valueY.find('%') != std::string::npos) {
2892             SetBgImgSizeY(BackgroundImageSizeType::PERCENT, StringToDouble(valueY), bgImgSize);
2893         } else {
2894             bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2895         }
2896     } else {
2897         auto sizeTypeIter = BinarySearchFindIndex(bgImageSizeType, ArraySize(bgImageSizeType), value.c_str());
2898         if (sizeTypeIter != -1) {
2899             bgImgSize.SetSizeTypeX(bgImageSizeType[sizeTypeIter].value);
2900             bgImgSize.SetSizeTypeY(bgImageSizeType[sizeTypeIter].value);
2901         } else if (value.find("px") != std::string::npos) {
2902             SetBgImgSizeX(BackgroundImageSizeType::LENGTH, StringToDouble(value), bgImgSize);
2903             bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2904         } else if (value.find('%') != std::string::npos) {
2905             SetBgImgSizeX(BackgroundImageSizeType::PERCENT, StringToDouble(value), bgImgSize);
2906             bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2907         } else {
2908             bgImgSize.SetSizeTypeX(BackgroundImageSizeType::AUTO);
2909             bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2910         }
2911     }
2912     backgroundStyle.backgroundImage->SetImageSize(
2913         bgImgSize.GetSizeTypeX(), bgImgSize.GetSizeValueX(), bgImgSize.GetSizeTypeY(), bgImgSize.GetSizeValueY());
2914     declaration.hasDecorationStyle_ = true;
2915 }
2916 
SetBgImgPositionX(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2917 void SetBgImgPositionX(
2918     const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2919 {
2920     bgImgPosition.SetSizeTypeX(type);
2921     bgImgPosition.SetSizeValueX(value);
2922 }
2923 
SetBgImgPositionY(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2924 void SetBgImgPositionY(
2925     const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2926 {
2927     bgImgPosition.SetSizeTypeY(type);
2928     bgImgPosition.SetSizeValueY(value);
2929 }
2930 
SetBgImgPosition(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2931 void SetBgImgPosition(
2932     const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2933 {
2934     SetBgImgPositionX(type, value, bgImgPosition);
2935     SetBgImgPositionY(type, value, bgImgPosition);
2936 }
2937 
BgImgPositionIsValid(const std::string & posX,const std::string & posY)2938 bool BgImgPositionIsValid(const std::string& posX, const std::string& posY)
2939 {
2940     if ((std::strcmp(posX.c_str(), DOM_BACKGROUND_IMAGE_POSITION_CENTER) == 0) ||
2941         (std::strcmp(posY.c_str(), DOM_BACKGROUND_IMAGE_POSITION_CENTER) == 0)) {
2942         return true;
2943     }
2944 
2945     static const std::unordered_set<std::string> horizonSet = {
2946         DOM_BACKGROUND_IMAGE_POSITION_LEFT,
2947         DOM_BACKGROUND_IMAGE_POSITION_RIGHT,
2948     };
2949     static const std::unordered_set<std::string> verticalSet = {
2950         DOM_BACKGROUND_IMAGE_POSITION_TOP,
2951         DOM_BACKGROUND_IMAGE_POSITION_BOTTOM,
2952     };
2953 
2954     // posX and posY are not strictly corresponding to horizontal or vertical, but they must not conflict,
2955     // for example both of them are "top" is invalid.
2956     if (posX.find("px") != std::string::npos || posX.find('%') != std::string::npos ||
2957         horizonSet.find(posX) != horizonSet.end()) {
2958         if (posY.find("px") != std::string::npos || posY.find('%') != std::string::npos ||
2959             verticalSet.find(posY) != verticalSet.end()) {
2960             return true;
2961         }
2962     }
2963 
2964     return verticalSet.find(posX) != verticalSet.end() && horizonSet.find(posY) != horizonSet.end();
2965 }
2966 
SetBackgroundImagePosition(const std::string & value,Declaration & declaration)2967 void Declaration::SetBackgroundImagePosition(const std::string& value, Declaration& declaration)
2968 {
2969     auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2970     if (!backgroundStyle.IsValid()) {
2971         LOGD("don't support background");
2972         return;
2973     }
2974 
2975     static const LinearMapNode<void (*)(BackgroundImagePosition&)> backGroundPositionOperators[] = {
2976         { DOM_BACKGROUND_IMAGE_POSITION_BOTTOM,
2977             [](BackgroundImagePosition& backgroundImagePosition) {
2978                 SetBgImgPositionY(BackgroundImagePositionType::PERCENT, 100.0, backgroundImagePosition);
2979             } },
2980         { DOM_BACKGROUND_IMAGE_POSITION_LEFT,
2981             [](BackgroundImagePosition& backgroundImagePosition) {
2982                 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, 0.0, backgroundImagePosition);
2983             } },
2984         { DOM_BACKGROUND_IMAGE_POSITION_RIGHT,
2985             [](BackgroundImagePosition& backgroundImagePosition) {
2986                 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, 100.0, backgroundImagePosition);
2987             } },
2988         { DOM_BACKGROUND_IMAGE_POSITION_TOP,
2989             [](BackgroundImagePosition& backgroundImagePosition) {
2990                 SetBgImgPositionY(BackgroundImagePositionType::PERCENT, 0.0, backgroundImagePosition);
2991             } },
2992     };
2993     BackgroundImagePosition backgroundImagePosition;
2994 
2995     auto index = value.find(' ', 0);
2996     if (index != std::string::npos) {
2997         std::string valueX = value.substr(0, index);
2998         std::string valueY = value.substr(index + 1, value.size() - index - 1);
2999         if (!BgImgPositionIsValid(valueX, valueY)) {
3000             return;
3001         }
3002         // The input is valid,so set the default is (center,center),
3003         // if the value is different, the default value is overwritten.
3004         SetBgImgPosition(BackgroundImagePositionType::PERCENT, 50.0, backgroundImagePosition);
3005         if (valueX.find("px") != std::string::npos) {
3006             SetBgImgPositionX(BackgroundImagePositionType::PX, StringToDouble(valueX), backgroundImagePosition);
3007         } else if (valueX.find('%') != std::string::npos) {
3008             SetBgImgPositionX(BackgroundImagePositionType::PERCENT, StringToDouble(valueX), backgroundImagePosition);
3009         } else {
3010             auto operatorIterX = BinarySearchFindIndex(
3011                 backGroundPositionOperators, ArraySize(backGroundPositionOperators), valueX.c_str());
3012             if (operatorIterX != -1) {
3013                 backGroundPositionOperators[operatorIterX].value(backgroundImagePosition);
3014             }
3015         }
3016         if (valueY.find("px") != std::string::npos) {
3017             SetBgImgPositionY(BackgroundImagePositionType::PX, StringToDouble(valueY), backgroundImagePosition);
3018         } else if (valueY.find('%') != std::string::npos) {
3019             SetBgImgPositionY(BackgroundImagePositionType::PERCENT, StringToDouble(valueY), backgroundImagePosition);
3020         } else {
3021             auto operatorIterY = BinarySearchFindIndex(
3022                 backGroundPositionOperators, ArraySize(backGroundPositionOperators), valueY.c_str());
3023             if (operatorIterY != -1) {
3024                 backGroundPositionOperators[operatorIterY].value(backgroundImagePosition);
3025             }
3026         }
3027     } else {
3028         SetBgImgPosition(BackgroundImagePositionType::PERCENT, 50.0, backgroundImagePosition);
3029         if (value.find("px") != std::string::npos) {
3030             SetBgImgPositionX(BackgroundImagePositionType::PX, StringToDouble(value), backgroundImagePosition);
3031         } else if (value.find('%') != std::string::npos) {
3032             SetBgImgPositionX(BackgroundImagePositionType::PERCENT, StringToDouble(value), backgroundImagePosition);
3033         } else {
3034             auto operatorIter = BinarySearchFindIndex(
3035                 backGroundPositionOperators, ArraySize(backGroundPositionOperators), value.c_str());
3036             if (operatorIter != -1) {
3037                 backGroundPositionOperators[operatorIter].value(backgroundImagePosition);
3038             }
3039         }
3040     }
3041     backgroundStyle.backgroundImage->SetImagePosition(backgroundImagePosition.GetSizeTypeX(),
3042         backgroundImagePosition.GetSizeValueX(), backgroundImagePosition.GetSizeTypeY(),
3043         backgroundImagePosition.GetSizeValueY());
3044     declaration.hasDecorationStyle_ = true;
3045 }
3046 
BindPipelineContext(const WeakPtr<PipelineContext> & pipelineContext)3047 void Declaration::BindPipelineContext(const WeakPtr<PipelineContext>& pipelineContext)
3048 {
3049     pipelineContext_ = pipelineContext;
3050 }
3051 
ResetDefaultStyles()3052 void Declaration::ResetDefaultStyles()
3053 {
3054     auto& sizeStyle = static_cast<CommonSizeStyle&>(GetStyle(StyleTag::COMMON_SIZE_STYLE));
3055     if (sizeStyle.IsValid() && !sizeStyle.IsShared()) {
3056         sizeStyle.width = Dimension(-1.0, DimensionUnit::PX);
3057         sizeStyle.height = Dimension(-1.0, DimensionUnit::PX);
3058         sizeStyle.minWidth = Dimension(0.0);
3059         sizeStyle.minHeight = Dimension(0.0);
3060         sizeStyle.maxWidth = Dimension(Size::INFINITE_SIZE);
3061         sizeStyle.maxHeight = Dimension(Size::INFINITE_SIZE);
3062         sizeStyle.aspectRatio = -1.0;
3063     }
3064 
3065     auto& paddingStyle = static_cast<CommonPaddingStyle&>(GetStyle(StyleTag::COMMON_PADDING_STYLE));
3066     if (paddingStyle.IsValid() && !paddingStyle.IsShared()) {
3067         paddingStyle.padding = Edge(Dimension(0.0));
3068     }
3069 
3070     auto& marginStyle = static_cast<CommonMarginStyle&>(GetStyle(StyleTag::COMMON_MARGIN_STYLE));
3071     if (marginStyle.IsValid() && !marginStyle.IsShared()) {
3072         marginStyle.margin = Edge(Dimension(0.0));
3073     }
3074 
3075     auto& flexStyle = static_cast<CommonFlexStyle&>(GetStyle(StyleTag::COMMON_FLEX_STYLE));
3076     if (flexStyle.IsValid() && !flexStyle.IsShared()) {
3077         flexStyle.flexGrow = 0.0;
3078         flexStyle.flexShrink = 1.0;
3079         flexStyle.flexBasis = 0.0_px;
3080         flexStyle.flexWeight = 0.0;
3081         flexStyle.displayIndex = 1;
3082     }
3083 
3084     auto& opacityStyle = static_cast<CommonOpacityStyle&>(GetStyle(StyleTag::COMMON_OPACITY_STYLE));
3085     if (opacityStyle.IsValid() && !opacityStyle.IsShared()) {
3086         opacityStyle.opacity = 1.0;
3087     }
3088 
3089     auto& displayStyle = static_cast<CommonDisplayStyle&>(GetStyle(StyleTag::COMMON_DISPLAY_STYLE));
3090     if (displayStyle.IsValid() && !displayStyle.IsShared()) {
3091         displayStyle.display = DisplayType::NO_SETTING;
3092     }
3093     hasDisplayStyle_ = false;
3094 
3095     auto& visibilityStyle = static_cast<CommonVisibilityStyle&>(GetStyle(StyleTag::COMMON_VISIBILITY_STYLE));
3096     if (visibilityStyle.IsValid() && !visibilityStyle.IsShared()) {
3097         visibilityStyle.visibility = VisibilityType::NO_SETTING;
3098     }
3099 
3100     auto& borderStyle = static_cast<CommonBorderStyle&>(GetStyle(StyleTag::COMMON_BORDER_STYLE));
3101     if (borderStyle.IsValid() && !borderStyle.IsShared()) {
3102         borderStyle.border.SetBorderEdge(BorderEdge(Color::BLACK, Dimension(), BorderStyle::SOLID));
3103     }
3104 
3105     auto& borderImageStyle = static_cast<CommonBorderStyle&>(GetStyle(StyleTag::COMMON_BORDER_STYLE));
3106     if (borderImageStyle.IsValid() && !borderImageStyle.IsShared()) {
3107         borderImageStyle.border.SetBorderImageEdge(
3108             BorderImageEdge(Dimension(), Dimension(), Dimension(), BorderImageRepeat::STRETCH));
3109     }
3110 
3111     auto& background = static_cast<CommonBackgroundStyle&>(GetStyle(StyleTag::COMMON_BACKGROUND_STYLE));
3112     if (background.IsValid() && !background.IsShared()) {
3113         background.gradient = Gradient();
3114         background.gradientBorderImage = Gradient();
3115         background.backgroundImage = AceType::MakeRefPtr<BackgroundImage>();
3116         background.borderImage = AceType::MakeRefPtr<BorderImage>();
3117     }
3118 
3119     auto& renderAttr = static_cast<CommonRenderAttribute&>(GetAttribute(AttributeTag::COMMON_RENDER_ATTR));
3120     if (renderAttr.IsValid() && !renderAttr.IsShared() && !renderAttr.show.empty()) {
3121         hasDisplayStyle_ = true;
3122         SetShowAttr(renderAttr.show);
3123     }
3124 
3125     backDecoration_ = AceType::MakeRefPtr<Decoration>();
3126     frontDecoration_ = AceType::MakeRefPtr<Decoration>();
3127 }
3128 
3129 // Convert transform style to json format, such as rotate(50deg) to {"ratate":"50deg"}
GetTransformJsonValue(const std::string & value)3130 std::string Declaration::GetTransformJsonValue(const std::string& value)
3131 {
3132     auto rightIndex = value.find('(');
3133     auto leftIndex = value.find(')');
3134     std::string jsonValue = value;
3135 
3136     if (rightIndex != std::string::npos && leftIndex != std::string::npos && (leftIndex - 1 - rightIndex > 0)) {
3137         std::string transformType = value.substr(0, rightIndex);
3138         std::string transformValue = value.substr(rightIndex + 1, leftIndex - 1 - rightIndex);
3139         jsonValue = "{\"" + transformType + "\":\"" + transformValue + "\"}";
3140     }
3141 
3142     return jsonValue;
3143 }
3144 
GetTransformType(const std::unique_ptr<JsonValue> & transformJson)3145 std::string Declaration::GetTransformType(const std::unique_ptr<JsonValue>& transformJson)
3146 {
3147     if (transformJson->IsNull()) {
3148         LOGE("transformJson is null");
3149         return "";
3150     }
3151     return transformJson->GetKey();
3152 }
3153 
GetTransformTypeValue(const std::unique_ptr<JsonValue> & transformJson)3154 std::string Declaration::GetTransformTypeValue(const std::unique_ptr<JsonValue>& transformJson)
3155 {
3156     if (transformJson->IsNull()) {
3157         LOGE("transformJson is null");
3158         return "";
3159     }
3160     std::string jsonValue = transformJson->GetString();
3161     if (jsonValue.empty()) {
3162         double jsonDouble = transformJson->GetDouble();
3163         return std::to_string(jsonDouble);
3164     }
3165     return jsonValue;
3166 }
3167 
GetThemeManager() const3168 RefPtr<ThemeManager> Declaration::GetThemeManager() const
3169 {
3170     auto context = pipelineContext_.Upgrade();
3171     if (!context) {
3172         return nullptr;
3173     }
3174     return context->GetThemeManager();
3175 }
3176 
GetThemeConstants() const3177 RefPtr<ThemeConstants> Declaration::GetThemeConstants() const
3178 {
3179     auto themeManager = GetThemeManager();
3180     if (!themeManager) {
3181         return nullptr;
3182     }
3183     return themeManager->GetThemeConstants();
3184 }
3185 
ParseColor(const std::string & value,uint32_t maskAlpha) const3186 Color Declaration::ParseColor(const std::string& value, uint32_t maskAlpha) const
3187 {
3188     auto themeConstants = GetThemeConstants();
3189     auto&& noRefFunc = [&value, maskAlpha = maskAlpha]() { return Color::FromString(value, maskAlpha); };
3190     auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetColor(refId); };
3191     return ParseThemeReference<Color>(value, noRefFunc, idRefFunc, Color::TRANSPARENT);
3192 }
3193 
ParseDouble(const std::string & value) const3194 double Declaration::ParseDouble(const std::string& value) const
3195 {
3196     auto themeConstants = GetThemeConstants();
3197     auto&& noRefFunc = [&value]() { return StringUtils::StringToDouble(value); };
3198     auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDouble(refId); };
3199     return ParseThemeReference<double>(value, noRefFunc, idRefFunc, 0.0);
3200 }
3201 
ParseDimension(const std::string & value,bool useVp) const3202 Dimension Declaration::ParseDimension(const std::string& value, bool useVp) const
3203 {
3204     auto themeConstants = GetThemeConstants();
3205     auto&& noRefFunc = [&value, useVp]() { return StringUtils::StringToDimension(value, useVp); };
3206     auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDimension(refId); };
3207     return ParseThemeReference<Dimension>(value, noRefFunc, idRefFunc, Dimension());
3208 }
3209 
ParseCalcDimension(const std::string & value,bool useVp) const3210 CalcDimension Declaration::ParseCalcDimension(const std::string& value, bool useVp) const
3211 {
3212     if (value.find("calc") != std::string::npos) {
3213         return StringUtils::StringToCalcDimension(value, useVp);
3214     } else {
3215         return ParseDimension(value, useVp);
3216     }
3217 }
3218 
ParseLineHeight(const std::string & value) const3219 Dimension Declaration::ParseLineHeight(const std::string& value) const
3220 {
3221     auto themeConstants = GetThemeConstants();
3222     const auto& parseResult = ThemeUtils::ParseThemeIdReference(value, themeConstants);
3223     if (!parseResult.parseSuccess) {
3224         return StringUtils::StringToDimension(value);
3225     }
3226     auto&& noRefFunc = [&value]() { return StringUtils::StringToDouble(value); };
3227     auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDouble(refId); };
3228     auto lineHeightScale = ParseThemeReference<double>(value, noRefFunc, idRefFunc, 1.0);
3229     // If got 0.0 from ThemeConstants, use default 1.0
3230     lineHeightScale = NearZero(lineHeightScale) ? 1.0 : lineHeightScale;
3231     return Dimension(lineHeightScale, DimensionUnit::PERCENT);
3232 }
3233 
ParseFontFamilies(const std::string & value) const3234 std::vector<std::string> Declaration::ParseFontFamilies(const std::string& value) const
3235 {
3236     std::vector<std::string> fontFamilies;
3237     std::stringstream stream(value);
3238     std::string fontFamily;
3239 
3240     auto themeConstants = GetThemeConstants();
3241     auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetString(refId); };
3242 
3243     while (getline(stream, fontFamily, ',')) {
3244         auto&& noRefFunc = [&fontFamily]() { return fontFamily; };
3245         fontFamilies.emplace_back(ParseThemeReference<std::string>(fontFamily, noRefFunc, idRefFunc, fontFamily));
3246     }
3247     return fontFamilies;
3248 }
3249 
ParsePreferFontSizes(const std::string & value) const3250 std::vector<Dimension> Declaration::ParsePreferFontSizes(const std::string& value) const
3251 {
3252     std::vector<Dimension> prefers;
3253     std::stringstream stream(value);
3254     std::string fontSize;
3255     while (getline(stream, fontSize, ',')) {
3256         prefers.emplace_back(ParseDimension(fontSize));
3257     }
3258     std::sort(prefers.begin(), prefers.end(),
3259         [](const Dimension& left, const Dimension& right) { return left.Value() > right.Value(); });
3260     return prefers;
3261 }
3262 
ParseImageSrc(const std::string & imgSrc) const3263 std::string Declaration::ParseImageSrc(const std::string& imgSrc) const
3264 {
3265     return ThemeUtils::ProcessImageSource(imgSrc, GetThemeConstants());
3266 }
3267 
IsRightToLeft() const3268 bool Declaration::IsRightToLeft() const
3269 {
3270     bool isRightToLeft = false;
3271     auto& commonAttr = static_cast<CommonAttribute&>(GetAttribute(AttributeTag::COMMON_ATTR));
3272     if (commonAttr.IsValid()) {
3273         isRightToLeft = commonAttr.isRightToLeft;
3274     }
3275     return isRightToLeft;
3276 }
3277 
SetClickEvent(const EventMarker & onClick)3278 void Declaration::SetClickEvent(const EventMarker& onClick)
3279 {
3280     auto& gestureEvent = MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
3281     if (gestureEvent.IsValid()) {
3282         gestureEvent.click.eventMarker = onClick;
3283         gestureEvent.click.eventMarker.SetCatchMode(false);
3284         gestureEvent.click.isRefreshed = true;
3285     }
3286 }
3287 
SetRemoteMessageEvent(const EventMarker & remoteMessage)3288 void Declaration::SetRemoteMessageEvent(const EventMarker& remoteMessage)
3289 {
3290     LOGI("Declaration::SetRemoteMessageEvent");
3291     auto& gestureEvent = MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_REMOTE_MESSAGE_GRESURE_EVENT);
3292     if (gestureEvent.IsValid()) {
3293         LOGI("Declaration::SetRemoteMessageEvent IsValid");
3294         gestureEvent.click.eventMarker = remoteMessage;
3295         gestureEvent.click.eventMarker.SetCatchMode(false);
3296         gestureEvent.click.isRefreshed = true;
3297     }
3298 }
3299 
3300 } // namespace OHOS::Ace
3301