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_GESTURE_EVENT,
373 [](Declaration& declaration) {
374 declaration.events_.try_emplace(
375 EventTag::COMMON_REMOTE_MESSAGE_GESTURE_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 declaration.backDecoration_->SetBackgroundColor(Color::TRANSPARENT);
816 declaration.hasDecorationStyle_ = true;
817 }
818 } },
819 { DOM_BACKGROUND_IMAGE_POSITION, &Declaration::SetBackgroundImagePosition },
820 { DOM_BACKGROUND_IMAGE_REPEAT,
821 [](const std::string& value, Declaration& declaration) {
822 auto& backgroundStyle =
823 declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
824 if (backgroundStyle.IsValid()) {
825 backgroundStyle.backgroundRepeat = ConvertStrToImageRepeat(value);
826 backgroundStyle.backgroundImage->SetImageRepeat(backgroundStyle.backgroundRepeat);
827 declaration.hasDecorationStyle_ = true;
828 }
829 } },
830 { DOM_BACKGROUND_IMAGE_SIZE, &Declaration::SetBackgroundImageSize },
831 { DOM_BORDER, &Declaration::SetBorderOverall },
832 { DOM_BORDER_BOTTOM_COLOR,
833 [](const std::string& value, Declaration& declaration) {
834 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
835 if (borderStyle.IsValid()) {
836 borderStyle.border.SetBottomColor(declaration.ParseColor(value));
837 declaration.hasBorderStyle_ = true;
838 declaration.hasDecorationStyle_ = true;
839 }
840 } },
841 { DOM_BORDER_BOTTOM_LEFT_RADIUS,
842 [](const std::string& value, Declaration& declaration) {
843 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
844 if (borderStyle.IsValid()) {
845 borderStyle.border.SetBottomLeftRadius(Radius(declaration.ParseDimension(value)));
846 declaration.hasBorderStyle_ = true;
847 declaration.hasBorderRadiusStyle_ = true;
848 declaration.hasDecorationStyle_ = true;
849 }
850 } },
851 { DOM_BORDER_BOTTOM_RIGHT_RADIUS,
852 [](const std::string& value, Declaration& declaration) {
853 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
854 if (borderStyle.IsValid()) {
855 borderStyle.border.SetBottomRightRadius(Radius(declaration.ParseDimension(value)));
856 declaration.hasBorderStyle_ = true;
857 declaration.hasBorderRadiusStyle_ = true;
858 declaration.hasDecorationStyle_ = true;
859 }
860 } },
861 { DOM_BORDER_BOTTOM_STYLE,
862 [](const std::string& value, Declaration& declaration) {
863 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
864 if (borderStyle.IsValid()) {
865 borderStyle.border.SetBottomStyle(ConvertStrToBorderStyle(value));
866 declaration.hasBorderStyle_ = true;
867 declaration.hasDecorationStyle_ = true;
868 }
869 } },
870 { DOM_BORDER_BOTTOM_WIDTH,
871 [](const std::string& value, Declaration& declaration) {
872 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
873 if (borderStyle.IsValid()) {
874 borderStyle.border.SetBottomWidth(declaration.ParseDimension(value));
875 declaration.hasBorderStyle_ = true;
876 declaration.hasDecorationStyle_ = true;
877 }
878 } },
879 { DOM_BORDER_COLOR, &Declaration::SetBorderColorForFourEdges },
880
881 { DOM_BORDER_IMAGE, &Declaration::SetBorderImage},
882 { DOM_BORDER_IMAGE_OUTSET, &Declaration::SetBorderImageOutSetForFourEdges},
883 { DOM_BORDER_IMAGE_REPEAT, &Declaration::SetBorderImageRepeatForFourEdges},
884 { DOM_BORDER_IMAGE_SLICE, &Declaration::SetBorderImageSliceForFourEdges},
885 { DOM_BORDER_IMAGE_SOURCE, &Declaration::SetBorderImageFindUrl},
886 { DOM_BORDER_IMAGE_WIDTH, &Declaration::SetBorderImageWidthForFourEdges},
887
888 { DOM_BORDER_LEFT_COLOR,
889 [](const std::string& value, Declaration& declaration) {
890 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
891 if (borderStyle.IsValid()) {
892 borderStyle.border.SetLeftColor(declaration.ParseColor(value));
893 declaration.hasBorderStyle_ = true;
894 declaration.hasDecorationStyle_ = true;
895 }
896 } },
897 { DOM_BORDER_LEFT_STYLE,
898 [](const std::string& value, Declaration& declaration) {
899 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
900 if (borderStyle.IsValid()) {
901 borderStyle.border.SetLeftStyle(ConvertStrToBorderStyle(value));
902 declaration.hasBorderStyle_ = true;
903 declaration.hasDecorationStyle_ = true;
904 }
905 } },
906 { DOM_BORDER_LEFT_WIDTH,
907 [](const std::string& value, Declaration& declaration) {
908 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
909 if (borderStyle.IsValid()) {
910 borderStyle.border.SetLeftWidth(declaration.ParseDimension(value));
911 declaration.hasBorderStyle_ = true;
912 declaration.hasDecorationStyle_ = true;
913 }
914 } },
915 { DOM_BORDER_RADIUS,
916 [](const std::string& value, Declaration& declaration) {
917 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
918 if (borderStyle.IsValid()) {
919 borderStyle.border.SetBorderRadius(Radius(declaration.ParseDimension(value)));
920 declaration.hasBorderStyle_ = true;
921 declaration.hasBorderRadiusStyle_ = true;
922 declaration.hasDecorationStyle_ = true;
923 }
924 } },
925 { DOM_BORDER_RIGHT_COLOR,
926 [](const std::string& value, Declaration& declaration) {
927 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
928 if (borderStyle.IsValid()) {
929 borderStyle.border.SetRightColor(declaration.ParseColor(value));
930 declaration.hasBorderStyle_ = true;
931 declaration.hasDecorationStyle_ = true;
932 }
933 } },
934 { DOM_BORDER_RIGHT_STYLE,
935 [](const std::string& value, Declaration& declaration) {
936 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
937 if (borderStyle.IsValid()) {
938 borderStyle.border.SetRightStyle(ConvertStrToBorderStyle(value));
939 declaration.hasBorderStyle_ = true;
940 declaration.hasDecorationStyle_ = true;
941 }
942 } },
943 { DOM_BORDER_RIGHT_WIDTH,
944 [](const std::string& value, Declaration& declaration) {
945 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
946 if (borderStyle.IsValid()) {
947 borderStyle.border.SetRightWidth(declaration.ParseDimension(value));
948 declaration.hasBorderStyle_ = true;
949 declaration.hasDecorationStyle_ = true;
950 }
951 } },
952 { DOM_BORDER_STYLE, &Declaration::SetBorderStyleForFourEdges },
953 { DOM_BORDER_TOP_COLOR,
954 [](const std::string& value, Declaration& declaration) {
955 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
956 if (borderStyle.IsValid()) {
957 borderStyle.border.SetTopColor(declaration.ParseColor(value));
958 declaration.hasBorderStyle_ = true;
959 declaration.hasDecorationStyle_ = true;
960 }
961 } },
962 { DOM_BORDER_TOP_LEFT_RADIUS,
963 [](const std::string& value, Declaration& declaration) {
964 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
965 if (borderStyle.IsValid()) {
966 borderStyle.border.SetTopLeftRadius(Radius(declaration.ParseDimension(value)));
967 declaration.hasBorderStyle_ = true;
968 declaration.hasBorderRadiusStyle_ = true;
969 declaration.hasDecorationStyle_ = true;
970 }
971 } },
972 { DOM_BORDER_TOP_RIGHT_RADIUS,
973 [](const std::string& value, Declaration& declaration) {
974 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
975 if (borderStyle.IsValid()) {
976 borderStyle.border.SetTopRightRadius(Radius(declaration.ParseDimension(value)));
977 declaration.hasBorderStyle_ = true;
978 declaration.hasBorderRadiusStyle_ = true;
979 declaration.hasDecorationStyle_ = true;
980 }
981 } },
982 { DOM_BORDER_TOP_STYLE,
983 [](const std::string& value, Declaration& declaration) {
984 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
985 if (borderStyle.IsValid()) {
986 borderStyle.border.SetTopStyle(ConvertStrToBorderStyle(value));
987 declaration.hasBorderStyle_ = true;
988 declaration.hasDecorationStyle_ = true;
989 }
990 } },
991 { DOM_BORDER_TOP_WIDTH,
992 [](const std::string& value, Declaration& declaration) {
993 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
994 if (borderStyle.IsValid()) {
995 borderStyle.border.SetTopWidth(declaration.ParseDimension(value));
996 declaration.hasBorderStyle_ = true;
997 declaration.hasDecorationStyle_ = true;
998 }
999 } },
1000 { DOM_BORDER_WIDTH, &Declaration::SetBorderWidthForFourEdges },
1001 { DOM_POSITION_BOTTOM,
1002 [](const std::string& value, Declaration& declaration) {
1003 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1004 if (positionStyle.IsValid() && !value.empty()) {
1005 positionStyle.bottom = declaration.ParseDimension(value);
1006 declaration.hasPositionStyle_ = true;
1007 declaration.hasBottom_ = true;
1008 }
1009 } },
1010 { DOM_BOX_SHADOW_BLUR,
1011 [](const std::string& value, Declaration& declaration) {
1012 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1013 if (shadowStyle.IsValid()) {
1014 shadowStyle.shadow.SetBlurRadius(StringToDouble(value));
1015 declaration.hasDecorationStyle_ = true;
1016 declaration.hasShadowStyle_ = true;
1017 }
1018 } },
1019 { DOM_BOX_SHADOW_COLOR,
1020 [](const std::string& value, Declaration& declaration) {
1021 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1022 if (shadowStyle.IsValid()) {
1023 if (value.empty()) {
1024 shadowStyle.shadow.SetColor(Color::BLACK);
1025 return;
1026 }
1027 shadowStyle.shadow.SetColor(declaration.ParseColor(value));
1028 declaration.hasDecorationStyle_ = true;
1029 declaration.hasShadowStyle_ = true;
1030 }
1031 } },
1032 { DOM_BOX_SHADOW_H,
1033 [](const std::string& value, Declaration& declaration) {
1034 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1035 if (shadowStyle.IsValid()) {
1036 shadowStyle.shadow.SetOffsetX(StringToDouble(value));
1037 declaration.hasDecorationStyle_ = true;
1038 declaration.hasShadowStyle_ = true;
1039 }
1040 } },
1041 { DOM_BOX_SHADOW_SPREAD,
1042 [](const std::string& value, Declaration& declaration) {
1043 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1044 if (shadowStyle.IsValid()) {
1045 shadowStyle.shadow.SetSpreadRadius(StringToDouble(value));
1046 declaration.hasDecorationStyle_ = true;
1047 declaration.hasShadowStyle_ = true;
1048 }
1049 } },
1050 { DOM_BOX_SHADOW_V,
1051 [](const std::string& value, Declaration& declaration) {
1052 auto& shadowStyle = declaration.MaybeResetStyle<CommonShadowStyle>(StyleTag::COMMON_SHADOW_STYLE);
1053 if (shadowStyle.IsValid()) {
1054 shadowStyle.shadow.SetOffsetY(StringToDouble(value));
1055 declaration.hasDecorationStyle_ = true;
1056 declaration.hasShadowStyle_ = true;
1057 }
1058 } },
1059 { DOM_BOX_SIZING,
1060 [](const std::string& value, Declaration& declaration) {
1061 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1062 if (sizeStyle.IsValid()) {
1063 sizeStyle.boxSizing = ConvertStrToBoxSizing(value);
1064 }
1065 } },
1066 { DOM_CLIP_PATH,
1067 [](const std::string& value, Declaration& declaration) {
1068 auto& clipPathStyle =
1069 declaration.MaybeResetStyle<CommonClipPathStyle>(StyleTag::COMMON_CLIP_PATH_STYLE);
1070 if (clipPathStyle.IsValid()) {
1071 clipPathStyle.clipPath = CreateClipPath(value);
1072 }
1073 } },
1074 { DOM_DISPLAY,
1075 [](const std::string& value, Declaration& declaration) {
1076 auto& displayStyle = declaration.MaybeResetStyle<CommonDisplayStyle>(StyleTag::COMMON_DISPLAY_STYLE);
1077 if (displayStyle.IsValid()) {
1078 if (value == DOM_DISPLAY_NONE) {
1079 displayStyle.display = DisplayType::NONE;
1080 } else if (value == DOM_DISPLAY_GRID) {
1081 displayStyle.display = DisplayType::GRID;
1082 } else if (value == DOM_DISPLAY_FLEX) {
1083 displayStyle.display = DisplayType::FLEX;
1084 } else if (value == DOM_DISPLAY_BLOCK) {
1085 displayStyle.display = DisplayType::BLOCK;
1086 } else if (value == DOM_DISPLAY_INLINE) {
1087 displayStyle.display = DisplayType::INLINE;
1088 } else if (value == DOM_DISPLAY_INLINE_BLOCK) {
1089 displayStyle.display = DisplayType::INLINE_BLOCK;
1090 } else if (value == DOM_DISPLAY_INLINE_FLEX) {
1091 displayStyle.display = DisplayType::INLINE_FLEX;
1092 }
1093 declaration.hasDisplayStyle_ = true;
1094 }
1095 } },
1096 { DOM_DISPLAY_INDEX,
1097 [](const std::string& value, Declaration& declaration) {
1098 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1099 if (flexStyle.IsValid()) {
1100 flexStyle.displayIndex = StringToInt(value);
1101 }
1102 } },
1103 { DOM_POSITION_END,
1104 [](const std::string& value, Declaration& declaration) {
1105 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1106 if (positionStyle.IsValid() && !value.empty()) {
1107 if (declaration.IsRightToLeft()) {
1108 positionStyle.left = declaration.ParseDimension(value);
1109 declaration.hasLeft_ = true;
1110 } else {
1111 positionStyle.right = declaration.ParseDimension(value);
1112 declaration.hasRight_ = true;
1113 }
1114 declaration.hasPositionStyle_ = true;
1115 }
1116 } },
1117 { DOM_FILTER,
1118 [](const std::string& value, Declaration& declaration) {
1119 declaration.hasFrontDecorationStyle_ = true;
1120 if (!declaration.frontDecoration_) {
1121 declaration.frontDecoration_ = AceType::MakeRefPtr<Decoration>();
1122 }
1123 auto radius = ParseFunctionValue<Dimension>(value, DOM_BLUR, StringToDimension);
1124 if (radius.IsValid()) {
1125 declaration.frontDecoration_->SetBlurRadius(radius);
1126 } else {
1127 declaration.frontDecoration_->SetBlurRadius(Dimension {});
1128 }
1129 } },
1130 { DOM_FLEX,
1131 [](const std::string& value, Declaration& declaration) {
1132 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1133 if (flexStyle.IsValid()) {
1134 flexStyle.flexGrow = StringToDouble(value);
1135 }
1136 } },
1137 { DOM_FLEX_BASIS,
1138 [](const std::string& value, Declaration& declaration) {
1139 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1140 if (flexStyle.IsValid()) {
1141 flexStyle.flexBasis = StringToDimension(value);
1142 }
1143 } },
1144 { DOM_FLEX_GROW,
1145 [](const std::string& value, Declaration& declaration) {
1146 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1147 if (flexStyle.IsValid()) {
1148 flexStyle.flexGrow = StringToDouble(value);
1149 }
1150 } },
1151 { DOM_FLEX_SHRINK,
1152 [](const std::string& value, Declaration& declaration) {
1153 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1154 if (flexStyle.IsValid()) {
1155 flexStyle.flexShrink = StringToDouble(value);
1156 }
1157 } },
1158 { DOM_FLEX_WEIGHT,
1159 [](const std::string& value, Declaration& declaration) {
1160 auto& flexStyle = declaration.MaybeResetStyle<CommonFlexStyle>(StyleTag::COMMON_FLEX_STYLE);
1161 if (flexStyle.IsValid()) {
1162 flexStyle.flexWeight = StringToDouble(value);
1163 }
1164 } },
1165 { DOM_HEIGHT,
1166 [](const std::string& value, Declaration& declaration) {
1167 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1168 if (sizeStyle.IsValid()) {
1169 sizeStyle.height = declaration.ParseCalcDimension(value);
1170 declaration.hasBoxStyle_ = true;
1171 }
1172 } },
1173 { DOM_IMAGE_FILL,
1174 [](const std::string& value, Declaration& declaration) {
1175 auto& imageStyle = declaration.MaybeResetStyle<CommonImageStyle>(StyleTag::COMMON_IMAGE_STYLE);
1176 if (imageStyle.IsValid()) {
1177 imageStyle.imageFill = declaration.ParseColor(value);
1178 }
1179 } },
1180 { DOM_LAYOUT_IN_BOX,
1181 [](const std::string& value, Declaration& declaration) {
1182 auto& commonStyle = declaration.MaybeResetStyle<CommonStyle>(StyleTag::COMMON_STYLE);
1183 if (commonStyle.IsValid()) {
1184 commonStyle.layoutInBox = StringToBool(value);
1185 declaration.hasBoxStyle_ = true;
1186 }
1187 } },
1188 { DOM_POSITION_LEFT,
1189 [](const std::string& value, Declaration& declaration) {
1190 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1191 if (positionStyle.IsValid() && !value.empty()) {
1192 positionStyle.left = declaration.ParseDimension(value);
1193 declaration.hasPositionStyle_ = true;
1194 declaration.hasLeft_ = true;
1195 }
1196 } },
1197 { DOM_MARGIN, &Declaration::SetMarginOverall },
1198 { DOM_MARGIN_BOTTOM,
1199 [](const std::string& value, Declaration& declaration) {
1200 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1201 if (marginStyle.IsValid()) {
1202 marginStyle.margin.SetBottom(declaration.ParseCalcDimension(value));
1203 declaration.hasBoxStyle_ = true;
1204 }
1205 } },
1206 { DOM_MARGIN_END,
1207 [](const std::string& value, Declaration& declaration) {
1208 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1209 if (marginStyle.IsValid()) {
1210 if (declaration.IsRightToLeft()) {
1211 marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1212 } else {
1213 marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1214 }
1215 declaration.hasBoxStyle_ = true;
1216 }
1217 } },
1218 { DOM_MARGIN_LEFT,
1219 [](const std::string& value, Declaration& declaration) {
1220 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1221 if (marginStyle.IsValid()) {
1222 marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1223 declaration.hasBoxStyle_ = true;
1224 }
1225 } },
1226 { DOM_MARGIN_RIGHT,
1227 [](const std::string& value, Declaration& declaration) {
1228 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1229 if (marginStyle.IsValid()) {
1230 marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1231 declaration.hasBoxStyle_ = true;
1232 }
1233 } },
1234 { DOM_MARGIN_START,
1235 [](const std::string& value, Declaration& declaration) {
1236 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1237 if (marginStyle.IsValid()) {
1238 if (declaration.IsRightToLeft()) {
1239 marginStyle.margin.SetRight(declaration.ParseCalcDimension(value));
1240 } else {
1241 marginStyle.margin.SetLeft(declaration.ParseCalcDimension(value));
1242 }
1243 declaration.hasBoxStyle_ = true;
1244 }
1245 } },
1246 { DOM_MARGIN_TOP,
1247 [](const std::string& value, Declaration& declaration) {
1248 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
1249 if (marginStyle.IsValid()) {
1250 marginStyle.margin.SetTop(declaration.ParseCalcDimension(value));
1251 declaration.hasBoxStyle_ = true;
1252 }
1253 } },
1254 { DOM_MASK_IMAGE,
1255 [](const std::string& value, Declaration& declaration) {
1256 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1257 if (maskStyle.IsValid()) {
1258 maskStyle.maskImage = value;
1259 }
1260 } },
1261 { DOM_MASK_POSITION,
1262 [](const std::string& value, Declaration& declaration) {
1263 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1264 if (maskStyle.IsValid()) {
1265 maskStyle.maskPosition = value;
1266 }
1267 } },
1268 { DOM_MASK_SIZE,
1269 [](const std::string& value, Declaration& declaration) {
1270 auto& maskStyle = declaration.MaybeResetStyle<CommonMaskStyle>(StyleTag::COMMON_MASK_STYLE);
1271 if (maskStyle.IsValid()) {
1272 maskStyle.maskSize = value;
1273 }
1274 } },
1275 { DOM_MAX_HEIGHT,
1276 [](const std::string& value, Declaration& declaration) {
1277 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1278 if (sizeStyle.IsValid()) {
1279 sizeStyle.maxHeight = declaration.ParseCalcDimension(value);
1280 }
1281 } },
1282 { DOM_MAX_WIDTH,
1283 [](const std::string& value, Declaration& declaration) {
1284 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1285 if (sizeStyle.IsValid()) {
1286 sizeStyle.maxWidth = declaration.ParseCalcDimension(value);
1287 }
1288 } },
1289 { DOM_MIN_HEIGHT,
1290 [](const std::string& value, Declaration& declaration) {
1291 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1292 if (sizeStyle.IsValid()) {
1293 sizeStyle.minHeight = declaration.ParseCalcDimension(value);
1294 }
1295 } },
1296 { DOM_MIN_WIDTH,
1297 [](const std::string& value, Declaration& declaration) {
1298 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1299 if (sizeStyle.IsValid()) {
1300 sizeStyle.minWidth = declaration.ParseCalcDimension(value);
1301 }
1302 } },
1303 { DOM_OPACITY,
1304 [](const std::string& value, Declaration& declaration) {
1305 auto& opacityStyle = declaration.MaybeResetStyle<CommonOpacityStyle>(StyleTag::COMMON_OPACITY_STYLE);
1306 if (opacityStyle.IsValid()) {
1307 opacityStyle.opacity = declaration.ParseDouble(value);
1308 declaration.hasDisplayStyle_ = true;
1309 }
1310 } },
1311 { DOM_OVERFLOW_STYLE,
1312 [](const std::string& value, Declaration& declaration) {
1313 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1314 if (overflowStyle.IsValid()) {
1315 overflowStyle.overflow = ConvertStrToOverflow(value);
1316 declaration.hasOverflowStyle_ = true;
1317 }
1318 } },
1319 { DOM_SCROLL_OVER_SCROLL_EFFECT,
1320 [](const std::string& val, Declaration& declaration) {
1321 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1322 if (!overflowStyle.IsValid()) {
1323 return;
1324 }
1325 if (val == DOM_SCROLL_EFFECT_SPRING) {
1326 overflowStyle.edgeEffect = EdgeEffect::SPRING;
1327 } else if (val == DOM_SCROLL_EFFECT_FADE) {
1328 overflowStyle.edgeEffect = EdgeEffect::FADE;
1329 } else {
1330 overflowStyle.edgeEffect = EdgeEffect::NONE;
1331 }
1332 } },
1333 { DOM_PADDING, &Declaration::SetPaddingOverall },
1334 { DOM_PADDING_BOTTOM,
1335 [](const std::string& value, Declaration& declaration) {
1336 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1337 if (paddingStyle.IsValid()) {
1338 paddingStyle.padding.SetBottom(declaration.ParseCalcDimension(value));
1339 declaration.hasBoxStyle_ = true;
1340 }
1341 } },
1342 { DOM_PADDING_END,
1343 [](const std::string& value, Declaration& declaration) {
1344 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1345 if (paddingStyle.IsValid()) {
1346 if (declaration.IsRightToLeft()) {
1347 paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1348 } else {
1349 paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1350 }
1351 declaration.hasBoxStyle_ = true;
1352 }
1353 } },
1354 { DOM_PADDING_LEFT,
1355 [](const std::string& value, Declaration& declaration) {
1356 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1357 if (paddingStyle.IsValid()) {
1358 paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1359 declaration.hasBoxStyle_ = true;
1360 }
1361 } },
1362 { DOM_PADDING_RIGHT,
1363 [](const std::string& value, Declaration& declaration) {
1364 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1365 if (paddingStyle.IsValid()) {
1366 paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1367 declaration.hasBoxStyle_ = true;
1368 }
1369 } },
1370 { DOM_PADDING_START,
1371 [](const std::string& value, Declaration& declaration) {
1372 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1373 if (paddingStyle.IsValid()) {
1374 if (declaration.IsRightToLeft()) {
1375 paddingStyle.padding.SetRight(declaration.ParseCalcDimension(value));
1376 } else {
1377 paddingStyle.padding.SetLeft(declaration.ParseCalcDimension(value));
1378 }
1379 declaration.hasBoxStyle_ = true;
1380 }
1381 } },
1382 { DOM_PADDING_TOP,
1383 [](const std::string& value, Declaration& declaration) {
1384 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
1385 if (paddingStyle.IsValid()) {
1386 paddingStyle.padding.SetTop(declaration.ParseCalcDimension(value));
1387 declaration.hasBoxStyle_ = true;
1388 }
1389 } },
1390 { DOM_POSITION,
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.position =
1395 value == DOM_POSITION_FIXED
1396 ? PositionType::PTFIXED
1397 : value == DOM_POSITION_ABSOLUTE ? PositionType::PTABSOLUTE : PositionType::PTRELATIVE;
1398 declaration.hasPositionStyle_ = true;
1399 }
1400 } },
1401 { DOM_POSITION_RIGHT,
1402 [](const std::string& value, Declaration& declaration) {
1403 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1404 if (positionStyle.IsValid() && !value.empty()) {
1405 positionStyle.right = declaration.ParseDimension(value);
1406 declaration.hasPositionStyle_ = true;
1407 declaration.hasRight_ = true;
1408 }
1409 } },
1410 { DOM_SCROLL_SCROLLBAR_COLOR,
1411 [](const std::string& val, Declaration& declaration) {
1412 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1413 if (overflowStyle.IsValid()) {
1414 overflowStyle.scrollBarColor.first = true;
1415 overflowStyle.scrollBarColor.second = declaration.ParseColor(val);
1416 }
1417 } },
1418 { DOM_SCROLL_SCROLLBAR_WIDTH,
1419 [](const std::string& val, Declaration& declaration) {
1420 auto& overflowStyle = declaration.MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
1421 if (overflowStyle.IsValid()) {
1422 overflowStyle.scrollBarWidth.first = true;
1423 auto width = declaration.ParseDimension(val);
1424 overflowStyle.scrollBarWidth.second = width.IsValid() ? width : Dimension();
1425 }
1426 } },
1427 { DOM_SHARED_TRANSITION_EFFECT,
1428 [](const std::string& value, Declaration& declaration) {
1429 auto& shareTransitionStyle =
1430 declaration.MaybeResetStyle<CommonShareTransitionStyle>(StyleTag::COMMON_SHARE_TRANSITION_STYLE);
1431 if (shareTransitionStyle.IsValid()) {
1432 shareTransitionStyle.sharedEffect = ParseSharedEffect(value, declaration);
1433 }
1434 } },
1435 { DOM_SHARED_TRANSITION_TIMING_FUNCTION,
1436 [](const std::string& value, Declaration& declaration) {
1437 auto& shareTransitionStyle =
1438 declaration.MaybeResetStyle<CommonShareTransitionStyle>(StyleTag::COMMON_SHARE_TRANSITION_STYLE);
1439 if (shareTransitionStyle.IsValid()) {
1440 shareTransitionStyle.curve = CreateCurve(value);
1441 shareTransitionStyle.sharedTransitionOption.SetCurve(shareTransitionStyle.curve);
1442 }
1443 } },
1444 { DOM_POSITION_START,
1445 [](const std::string& value, Declaration& declaration) {
1446 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1447 if (positionStyle.IsValid() && !value.empty()) {
1448 if (declaration.IsRightToLeft()) {
1449 positionStyle.right = declaration.ParseDimension(value);
1450 declaration.hasRight_ = true;
1451 } else {
1452 positionStyle.left = declaration.ParseDimension(value);
1453 declaration.hasLeft_ = true;
1454 }
1455 declaration.hasPositionStyle_ = true;
1456 }
1457 } },
1458 { DOM_POSITION_TOP,
1459 [](const std::string& value, Declaration& declaration) {
1460 auto& positionStyle = declaration.MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
1461 if (positionStyle.IsValid() && !value.empty()) {
1462 positionStyle.top = declaration.ParseDimension(value);
1463 declaration.hasPositionStyle_ = true;
1464 declaration.hasTop_ = true;
1465 }
1466 } },
1467 { DOM_TRANSFORM_ORIGIN,
1468 [](const std::string& val, Declaration& declaration) {
1469 declaration.hasTransformStyle_ = true;
1470 auto& animationStyle =
1471 declaration.MaybeResetStyle<CommonAnimationStyle>(StyleTag::COMMON_ANIMATION_STYLE);
1472 if (!animationStyle.IsValid()) {
1473 LOGD("don't support animation style");
1474 return;
1475 }
1476
1477 std::vector<std::string> offsets;
1478 StringUtils::StringSplitter(val, ' ', offsets);
1479 if (offsets.size() == TRANSFORM_SINGLE) {
1480 Dimension originDimensionX = TRANSFORM_ORIGIN_DEFAULT;
1481 Dimension originDimensionY = TRANSFORM_ORIGIN_DEFAULT;
1482 // for Enum
1483 if (CheckTransformEnum(val)) {
1484 auto resultX = ConvertStrToTransformOrigin(val, Axis::HORIZONTAL);
1485 if (resultX.first) {
1486 originDimensionX = resultX.second;
1487 }
1488 auto resultY = ConvertStrToTransformOrigin(val, Axis::VERTICAL);
1489 if (resultY.first) {
1490 originDimensionY = resultY.second;
1491 }
1492 } else {
1493 // for Dimension
1494 originDimensionX = declaration.ParseDimension(val);
1495 }
1496 animationStyle.tweenOption.SetTransformOrigin(originDimensionX, originDimensionY);
1497 animationStyle.transformOriginX = originDimensionX;
1498 animationStyle.transformOriginY = originDimensionY;
1499 } else if (offsets.size() == TRANSFORM_DUAL) {
1500 Dimension originDimensionX = TRANSFORM_ORIGIN_DEFAULT;
1501 Dimension originDimensionY = TRANSFORM_ORIGIN_DEFAULT;
1502 if (CheckTransformEnum(offsets[0])) {
1503 auto result = ConvertStrToTransformOrigin(offsets[0], Axis::HORIZONTAL);
1504 if (result.first) {
1505 originDimensionX = result.second;
1506 }
1507 } else {
1508 originDimensionX = declaration.ParseDimension(offsets[0]);
1509 }
1510
1511 if (CheckTransformEnum(offsets[1])) {
1512 auto result = ConvertStrToTransformOrigin(offsets[1], Axis::VERTICAL);
1513 if (result.first) {
1514 originDimensionY = result.second;
1515 }
1516 } else {
1517 originDimensionY = declaration.ParseDimension(offsets[1]);
1518 }
1519 animationStyle.tweenOption.SetTransformOrigin(originDimensionX, originDimensionY);
1520 animationStyle.transformOriginX = originDimensionX;
1521 animationStyle.transformOriginY = originDimensionY;
1522 }
1523 declaration.hasTransformOriginStyle_ = true;
1524 } },
1525 { DOM_TRANSITION_DURATION,
1526 [](const std::string& value, Declaration& declaration) {
1527 auto& pageTransitionStyle =
1528 declaration.MaybeResetStyle<CommonPageTransitionStyle>(StyleTag::COMMON_PAGE_TRANSITION_STYLE);
1529 if (pageTransitionStyle.IsValid()) {
1530 if (value.find("ms") != std::string::npos) {
1531 pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value);
1532 } else if (value.find('s') != std::string::npos) {
1533 pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value) * MS_TO_S;
1534 } else {
1535 // default unit is ms
1536 pageTransitionStyle.transitionDuration = StringUtils::StringToInt(value);
1537 }
1538 pageTransitionStyle.transitionEnterOption.SetDuration(pageTransitionStyle.transitionDuration);
1539 pageTransitionStyle.transitionExitOption.SetDuration(pageTransitionStyle.transitionDuration);
1540 }
1541 } },
1542 // card transition
1543 { DOM_TRANSITION_EFFECT,
1544 [](const std::string& value, Declaration& declaration) {
1545 declaration.hasTransitionAnimation_ = true;
1546 auto& cardTransitionStyle =
1547 declaration.MaybeResetStyle<CommonCardTransitionStyle>(StyleTag::COMMON_CARD_TRANSITION_STYLE);
1548 if (cardTransitionStyle.IsValid()) {
1549 declaration.hasTransformStyle_ = true;
1550 cardTransitionStyle.transitionEffect = ParseTransitionEffect(value);
1551 }
1552 } },
1553 { DOM_TRANSITION_TIMING_FUNCTION,
1554 [](const std::string& value, Declaration& declaration) {
1555 auto& pageTransitionStyle =
1556 declaration.MaybeResetStyle<CommonPageTransitionStyle>(StyleTag::COMMON_PAGE_TRANSITION_STYLE);
1557 if (pageTransitionStyle.IsValid()) {
1558 pageTransitionStyle.curve = CreateCurve(value);
1559 pageTransitionStyle.transitionEnterOption.SetCurve(pageTransitionStyle.curve);
1560 pageTransitionStyle.transitionExitOption.SetCurve(pageTransitionStyle.curve);
1561 }
1562 } },
1563 { DOM_VISIBILITY,
1564 [](const std::string& value, Declaration& declaration) {
1565 auto& visibilityStyle =
1566 declaration.MaybeResetStyle<CommonVisibilityStyle>(StyleTag::COMMON_VISIBILITY_STYLE);
1567 if (visibilityStyle.IsValid()) {
1568 visibilityStyle.visibility =
1569 (value == DOM_VISIBILITY_HIDDEN) ? VisibilityType::HIDDEN : VisibilityType::VISIBLE;
1570 declaration.hasDisplayStyle_ = true;
1571 }
1572 } },
1573 { DOM_WIDTH,
1574 [](const std::string& value, Declaration& declaration) {
1575 auto& sizeStyle = declaration.MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
1576 if (sizeStyle.IsValid()) {
1577 sizeStyle.width = declaration.ParseCalcDimension(value);
1578 declaration.hasBoxStyle_ = true;
1579 }
1580 } },
1581 { DOM_WINDOW_FILTER,
1582 [](const std::string& value, Declaration& declaration) {
1583 declaration.hasDecorationStyle_ = true;
1584 std::vector<std::string> offsets;
1585 StringUtils::StringSplitter(value, ' ', offsets);
1586 // progress
1587 if (offsets.size() >= 1) {
1588 auto parseValue = ParseFunctionValue<Dimension>(offsets[0], DOM_BLUR, StringToDimension);
1589 if (parseValue.Unit() == DimensionUnit::PERCENT) {
1590 auto progress = parseValue.Value();
1591 if (GreatNotEqual(progress, 0.0) && LessOrEqual(progress, 1.0)) {
1592 declaration.backDecoration_->SetWindowBlurProgress(static_cast<float>(progress));
1593 }
1594 } else {
1595 declaration.backDecoration_->SetWindowBlurProgress(static_cast<float>(0.0f));
1596 }
1597 }
1598 // style
1599 if (offsets.size() >= 2) {
1600 auto windowBlurStyle = StrToWindowBlurStyle(offsets[1]);
1601 declaration.backDecoration_->SetWindowBlurStyle(windowBlurStyle);
1602 }
1603 } },
1604 { DOM_ZINDEX,
1605 [](const std::string& value, Declaration& declaration) {
1606 auto& commonStyle = declaration.MaybeResetStyle<CommonStyle>(StyleTag::COMMON_STYLE);
1607 if (commonStyle.IsValid()) {
1608 commonStyle.zIndex = StringToInt(value);
1609 }
1610 } },
1611 };
1612
1613 auto operatorIter = BinarySearchFindIndex(styleSetter, ArraySize(styleSetter), style.first.c_str());
1614 if (operatorIter != -1) {
1615 styleSetter[operatorIter].value(style.second, *this);
1616 }
1617
1618 auto& renderAttr = static_cast<CommonRenderAttribute&>(GetAttribute(AttributeTag::COMMON_RENDER_ATTR));
1619 static const std::unordered_set<std::string> displayStyleSet = { DOM_OPACITY, DOM_DISPLAY, DOM_VISIBILITY };
1620 if (displayStyleSet.find(style.first) != displayStyleSet.end() &&
1621 AceApplicationInfo::GetInstance().GetIsCardType() && renderAttr.show == "false") {
1622 SetShowAttr(renderAttr.show);
1623 }
1624 }
1625
AddEvent(int32_t pageId,const std::string & eventId,const std::vector<std::string> & events)1626 void Declaration::AddEvent(int32_t pageId, const std::string& eventId, const std::vector<std::string>& events)
1627 {
1628 ACE_SCOPED_TRACE("Declaration::AddEvent");
1629 static const LinearMapNode<void (*)(int32_t, const std::string&, Declaration&)> eventSetters[] = {
1630 { DOM_BLUR,
1631 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1632 auto& focusableEvent = declaration.MaybeResetEvent<CommonFocusEvent>(EventTag::COMMON_FOCUS_EVENT);
1633 if (focusableEvent.IsValid()) {
1634 focusableEvent.blur.eventMarker = EventMarker(eventId, DOM_BLUR, pageId);
1635 focusableEvent.blur.isRefreshed = true;
1636 }
1637 } },
1638 { DOM_CAPTURE_TOUCH_CANCEL,
1639 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1640 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1641 if (rawEvent.IsValid()) {
1642 rawEvent.captureTouchCancel.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_CANCEL, pageId);
1643 rawEvent.captureTouchCancel.isRefreshed = true;
1644 }
1645 } },
1646 { DOM_CAPTURE_TOUCH_END,
1647 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1648 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1649 if (rawEvent.IsValid()) {
1650 rawEvent.captureTouchEnd.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_END, pageId);
1651 rawEvent.captureTouchEnd.isRefreshed = true;
1652 }
1653 } },
1654 { DOM_CAPTURE_TOUCH_MOVE,
1655 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1656 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1657 if (rawEvent.IsValid()) {
1658 rawEvent.captureTouchMove.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_MOVE, pageId);
1659 rawEvent.captureTouchMove.isRefreshed = true;
1660 }
1661 } },
1662 { DOM_CAPTURE_TOUCH_START,
1663 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1664 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1665 if (rawEvent.IsValid()) {
1666 rawEvent.captureTouchStart.eventMarker = EventMarker(eventId, DOM_CAPTURE_TOUCH_START, pageId);
1667 rawEvent.captureTouchStart.isRefreshed = true;
1668 }
1669 } },
1670 { DOM_CATCH_BUBBLE_CLICK,
1671 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1672 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1673 if (gestureEvent.IsValid()) {
1674 gestureEvent.click.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_CLICK, pageId);
1675 gestureEvent.click.eventMarker.SetCatchMode(true);
1676 gestureEvent.click.isRefreshed = true;
1677 }
1678 } },
1679 { DOM_CATCH_BUBBLE_DOUBLE_CLICK,
1680 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1681 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1682 if (gestureEvent.IsValid()) {
1683 gestureEvent.doubleClick.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_DOUBLE_CLICK, pageId);
1684 gestureEvent.doubleClick.eventMarker.SetCatchMode(true);
1685 gestureEvent.doubleClick.isRefreshed = true;
1686 }
1687 } },
1688 { DOM_CATCH_BUBBLE_LONG_PRESS,
1689 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1690 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1691 if (gestureEvent.IsValid()) {
1692 gestureEvent.longPress.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_LONG_PRESS, pageId);
1693 gestureEvent.longPress.eventMarker.SetCatchMode(true);
1694 gestureEvent.longPress.isRefreshed = true;
1695 }
1696 } },
1697 { DOM_CATCH_BUBBLE_SWIPE,
1698 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1699 auto& swipeEvent = declaration.MaybeResetEvent<CommonSwipeEvent>(EventTag::COMMON_SWIPE_EVENT);
1700 if (swipeEvent.IsValid()) {
1701 swipeEvent.catchBubbleSwipe.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_SWIPE, pageId);
1702 swipeEvent.catchBubbleSwipe.isRefreshed = true;
1703 }
1704 } },
1705 { DOM_CATCH_BUBBLE_TOUCH_CANCEL,
1706 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1707 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1708 if (rawEvent.IsValid()) {
1709 rawEvent.catchBubbleTouchCancel.eventMarker =
1710 EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_CANCEL, pageId);
1711 rawEvent.catchBubbleTouchCancel.isRefreshed = true;
1712 }
1713 } },
1714 { DOM_CATCH_BUBBLE_TOUCH_END,
1715 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1716 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1717 if (rawEvent.IsValid()) {
1718 rawEvent.catchBubbleTouchEnd.eventMarker = EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_END, pageId);
1719 rawEvent.catchBubbleTouchEnd.isRefreshed = true;
1720 }
1721 } },
1722 { DOM_CATCH_BUBBLE_TOUCH_MOVE,
1723 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1724 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1725 if (rawEvent.IsValid()) {
1726 rawEvent.catchBubbleTouchMove.eventMarker =
1727 EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_MOVE, pageId);
1728 rawEvent.catchBubbleTouchMove.isRefreshed = true;
1729 }
1730 } },
1731 { DOM_CATCH_BUBBLE_TOUCH_START,
1732 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1733 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1734 if (rawEvent.IsValid()) {
1735 rawEvent.catchBubbleTouchStart.eventMarker =
1736 EventMarker(eventId, DOM_CATCH_BUBBLE_TOUCH_START, pageId);
1737 rawEvent.catchBubbleTouchStart.isRefreshed = true;
1738 }
1739 } },
1740 { DOM_CATCH_CAPTURE_TOUCH_CANCEL,
1741 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1742 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1743 if (rawEvent.IsValid()) {
1744 rawEvent.catchCaptureTouchCancel.eventMarker =
1745 EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_CANCEL, pageId);
1746 rawEvent.catchCaptureTouchCancel.isRefreshed = true;
1747 }
1748 } },
1749 { DOM_CATCH_CAPTURE_TOUCH_END,
1750 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1751 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1752 if (rawEvent.IsValid()) {
1753 rawEvent.catchCaptureTouchEnd.eventMarker =
1754 EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_END, pageId);
1755 rawEvent.catchCaptureTouchEnd.isRefreshed = true;
1756 }
1757 } },
1758 { DOM_CATCH_CAPTURE_TOUCH_MOVE,
1759 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1760 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1761 if (rawEvent.IsValid()) {
1762 rawEvent.catchCaptureTouchMove.eventMarker =
1763 EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_MOVE, pageId);
1764 rawEvent.catchCaptureTouchMove.isRefreshed = true;
1765 }
1766 } },
1767 { DOM_CATCH_CAPTURE_TOUCH_START,
1768 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1769 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1770 if (rawEvent.IsValid()) {
1771 rawEvent.catchCaptureTouchStart.eventMarker =
1772 EventMarker(eventId, DOM_CATCH_CAPTURE_TOUCH_START, pageId);
1773 rawEvent.catchCaptureTouchStart.isRefreshed = true;
1774 }
1775 } },
1776 { DOM_CLICK,
1777 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1778 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1779 if (gestureEvent.IsValid()) {
1780 gestureEvent.click.eventMarker = EventMarker(eventId, DOM_CLICK, pageId);
1781 gestureEvent.click.eventMarker.SetCatchMode(false);
1782 gestureEvent.click.isRefreshed = true;
1783 }
1784 } },
1785 { DOM_DOUBLE_CLICK,
1786 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1787 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1788 if (gestureEvent.IsValid()) {
1789 gestureEvent.doubleClick.eventMarker = EventMarker(eventId, DOM_DOUBLE_CLICK, pageId);
1790 gestureEvent.doubleClick.eventMarker.SetCatchMode(false);
1791 gestureEvent.doubleClick.isRefreshed = true;
1792 }
1793 } },
1794 { DOM_DRAG,
1795 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1796 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1797 if (gestureEvent.IsValid()) {
1798 gestureEvent.drag.eventMarker = EventMarker(eventId, DOM_DRAG, pageId);
1799 gestureEvent.drag.isRefreshed = true;
1800 }
1801 } },
1802 { DOM_DRAG_END,
1803 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1804 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1805 if (gestureEvent.IsValid()) {
1806 gestureEvent.dragEnd.eventMarker = EventMarker(eventId, DOM_DRAG_END, pageId);
1807 gestureEvent.dragEnd.isRefreshed = true;
1808 }
1809 } },
1810 { DOM_DRAG_ENTER,
1811 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1812 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1813 if (gestureEvent.IsValid()) {
1814 gestureEvent.dragEnter.eventMarker = EventMarker(eventId, DOM_DRAG_ENTER, pageId);
1815 gestureEvent.dragEnter.isRefreshed = true;
1816 }
1817 } },
1818 { DOM_DRAG_LEAVE,
1819 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1820 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1821 if (gestureEvent.IsValid()) {
1822 gestureEvent.dragLeave.eventMarker = EventMarker(eventId, DOM_DRAG_LEAVE, pageId);
1823 gestureEvent.dragLeave.isRefreshed = true;
1824 }
1825 } },
1826 { DOM_DRAG_OVER,
1827 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1828 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1829 if (gestureEvent.IsValid()) {
1830 gestureEvent.dragOver.eventMarker = EventMarker(eventId, DOM_DRAG_OVER, pageId);
1831 gestureEvent.dragOver.isRefreshed = true;
1832 }
1833 } },
1834 { DOM_DRAG_START,
1835 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1836 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1837 if (gestureEvent.IsValid()) {
1838 gestureEvent.dragStart.eventMarker = EventMarker(eventId, DOM_DRAG_START, pageId);
1839 gestureEvent.dragStart.isRefreshed = true;
1840 }
1841 } },
1842 { DOM_DRAG_DROP,
1843 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1844 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1845 if (gestureEvent.IsValid()) {
1846 gestureEvent.dragDrop.eventMarker = EventMarker(eventId, DOM_DRAG_DROP, pageId);
1847 gestureEvent.dragDrop.isRefreshed = true;
1848 }
1849 } },
1850 { DOM_FOCUS,
1851 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1852 auto& focusEvent = declaration.MaybeResetEvent<CommonFocusEvent>(EventTag::COMMON_FOCUS_EVENT);
1853 if (focusEvent.IsValid()) {
1854 focusEvent.focus.eventMarker = EventMarker(eventId, DOM_FOCUS, pageId);
1855 focusEvent.focus.isRefreshed = true;
1856 }
1857 } },
1858 { DOM_HOVER,
1859 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1860 auto& mouseEvent = declaration.MaybeResetEvent<CommonMouseEvent>(EventTag::COMMON_MOUSE_EVENT);
1861 if (mouseEvent.IsValid()) {
1862 mouseEvent.mouseHover.eventMarker = EventMarker(eventId, DOM_HOVER, pageId);
1863 mouseEvent.mouseHover.isRefreshed = true;
1864 }
1865 } },
1866 { DOM_KEY,
1867 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1868 auto& keyEvent = declaration.MaybeResetEvent<CommonKeyEvent>(EventTag::COMMON_KEY_EVENT);
1869 if (keyEvent.IsValid()) {
1870 keyEvent.key.eventMarker = EventMarker(eventId, DOM_KEY, pageId);
1871 keyEvent.key.isRefreshed = true;
1872 }
1873 } },
1874 { DOM_LONG_PRESS,
1875 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1876 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1877 if (gestureEvent.IsValid()) {
1878 gestureEvent.longPress.eventMarker = EventMarker(eventId, DOM_LONG_PRESS, pageId);
1879 gestureEvent.longPress.eventMarker.SetCatchMode(false);
1880 gestureEvent.longPress.isRefreshed = true;
1881 }
1882 } },
1883 { DOM_MOUSE,
1884 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1885 auto& mouseEvent = declaration.MaybeResetEvent<CommonMouseEvent>(EventTag::COMMON_MOUSE_EVENT);
1886 if (mouseEvent.IsValid()) {
1887 mouseEvent.mouse.eventMarker = EventMarker(eventId, DOM_MOUSE, pageId);
1888 mouseEvent.mouse.isRefreshed = true;
1889 }
1890 } },
1891 { DOM_PINCH_CANCEL,
1892 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1893 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1894 if (gestureEvent.IsValid()) {
1895 gestureEvent.pinchCancel.eventMarker = EventMarker(eventId, DOM_PINCH_CANCEL, pageId);
1896 gestureEvent.pinchCancel.isRefreshed = true;
1897 }
1898 } },
1899 { DOM_PINCH_END,
1900 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1901 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1902 if (gestureEvent.IsValid()) {
1903 gestureEvent.pinchEnd.eventMarker = EventMarker(eventId, DOM_PINCH_END, pageId);
1904 gestureEvent.pinchEnd.isRefreshed = true;
1905 }
1906 } },
1907 { DOM_PINCH_START,
1908 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1909 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1910 if (gestureEvent.IsValid()) {
1911 gestureEvent.pinchStart.eventMarker = EventMarker(eventId, DOM_PINCH_START, pageId);
1912 gestureEvent.pinchStart.isRefreshed = true;
1913 }
1914 } },
1915 { DOM_PINCH_UPDATE,
1916 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1917 auto& gestureEvent = declaration.MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
1918 if (gestureEvent.IsValid()) {
1919 gestureEvent.pinchUpdate.eventMarker = EventMarker(eventId, DOM_PINCH_UPDATE, pageId);
1920 gestureEvent.pinchUpdate.isRefreshed = true;
1921 }
1922 } },
1923 { DOM_CROWN_ROTATE,
1924 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1925 auto& crownEvent = declaration.MaybeResetEvent<CommonCrownEvent>(EventTag::COMMON_CROWN_EVENT);
1926 if (crownEvent.IsValid()) {
1927 crownEvent.rotate.eventMarker = EventMarker(eventId, DOM_CROWN_ROTATE, pageId);
1928 crownEvent.rotate.isRefreshed = true;
1929 }
1930 } },
1931 { DOM_SWIPE,
1932 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1933 auto& swipeEvent = declaration.MaybeResetEvent<CommonSwipeEvent>(EventTag::COMMON_SWIPE_EVENT);
1934 if (swipeEvent.IsValid()) {
1935 swipeEvent.swipe.eventMarker = EventMarker(eventId, DOM_SWIPE, pageId);
1936 swipeEvent.swipe.eventMarker.SetCatchMode(false);
1937 swipeEvent.swipe.isRefreshed = true;
1938 }
1939 } },
1940 { DOM_TOUCH_CANCEL,
1941 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1942 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1943 if (rawEvent.IsValid()) {
1944 rawEvent.touchCancel.eventMarker = EventMarker(eventId, DOM_TOUCH_CANCEL, pageId);
1945 rawEvent.touchCancel.isRefreshed = true;
1946 }
1947 } },
1948 { DOM_TOUCH_END,
1949 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1950 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1951 if (rawEvent.IsValid()) {
1952 rawEvent.touchEnd.eventMarker = EventMarker(eventId, DOM_TOUCH_END, pageId);
1953 rawEvent.touchEnd.isRefreshed = true;
1954 }
1955 } },
1956 { DOM_TOUCH_MOVE,
1957 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1958 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1959 if (rawEvent.IsValid()) {
1960 rawEvent.touchMove.eventMarker = EventMarker(eventId, DOM_TOUCH_MOVE, pageId);
1961 rawEvent.touchMove.isRefreshed = true;
1962 }
1963 } },
1964 { DOM_TOUCH_START,
1965 [](int32_t pageId, const std::string& eventId, Declaration& declaration) {
1966 auto& rawEvent = declaration.MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
1967 if (rawEvent.IsValid()) {
1968 rawEvent.touchStart.eventMarker = EventMarker(eventId, DOM_TOUCH_START, pageId);
1969 rawEvent.touchStart.isRefreshed = true;
1970 }
1971 } },
1972 };
1973 for (const auto& event : events) {
1974 if (SetSpecializedEvent(pageId, eventId, event)) {
1975 continue;
1976 }
1977 auto setterIter = BinarySearchFindIndex(eventSetters, ArraySize(eventSetters), event.c_str());
1978 if (setterIter != -1) {
1979 eventSetters[setterIter].value(pageId, eventId, *this);
1980 }
1981 }
1982 }
1983
CallMethod(const std::string & method,const std::string & args)1984 void Declaration::CallMethod(const std::string& method, const std::string& args)
1985 {
1986 if (method == COMMON_METHOD_FOCUS) {
1987 if (!focusableController_) {
1988 LOGE("CallMethod: call focus method failed, focusableController_ is null");
1989 return;
1990 }
1991
1992 bool shouldFocus = true;
1993 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
1994 if (argsValue && argsValue->IsArray() && argsValue->GetArraySize() == COMMON_METHOD_FOCUS_ARGS_SIZE) {
1995 std::unique_ptr<JsonValue> focusValue = argsValue->GetArrayItem(0)->GetValue(COMMON_METHOD_FOCUS);
1996 if (focusValue && focusValue->IsBool()) {
1997 shouldFocus = focusValue->GetBool();
1998 }
1999 }
2000 OnRequestFocus(shouldFocus);
2001 } else if (method == DOM_LIST_METHOD_SCROLL_BY) {
2002 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
2003 if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() != 1) {
2004 LOGE("parse args error");
2005 return;
2006 }
2007 std::unique_ptr<JsonValue> scrollByPara = argsValue->GetArrayItem(0);
2008 double x = scrollByPara->GetDouble("dx", 0.0);
2009 double y = scrollByPara->GetDouble("dy", 0.0);
2010 bool isSmooth = scrollByPara->GetBool("smooth", true);
2011 OnScrollBy(x, y, isSmooth);
2012 } else {
2013 CallSpecializedMethod(method, args);
2014 }
2015 }
2016
OnRequestFocus(bool shouldFocus)2017 void Declaration::OnRequestFocus(bool shouldFocus)
2018 {
2019 auto& commonMethod = MaybeResetMethod<CommonMethod>(MethodTag::COMMON_METHOD);
2020 if (commonMethod.IsValid()) {
2021 commonMethod.Focus(focusableController_, shouldFocus);
2022 }
2023 }
2024
OnScrollBy(double dx,double dy,bool isSmooth)2025 void Declaration::OnScrollBy(double dx, double dy, bool isSmooth)
2026 {
2027 auto& commonMethod = MaybeResetMethod<CommonMethod>(MethodTag::COMMON_METHOD);
2028 if (commonMethod.IsValid()) {
2029 commonMethod.ScrollBy(positionController_, dx, dy, isSmooth);
2030 }
2031 }
2032
SetPaddingOverall(const std::string & value,Declaration & declaration)2033 void Declaration::SetPaddingOverall(const std::string& value, Declaration& declaration)
2034 {
2035 auto& paddingStyle = declaration.MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
2036 if (!paddingStyle.IsValid()) {
2037 LOGD("don't support padding");
2038 return;
2039 }
2040
2041 std::vector<std::string> offsets;
2042 StringUtils::StringSplitter(value, ' ', offsets);
2043 switch (offsets.size()) {
2044 case 1:
2045 paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[0]));
2046 paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[0]));
2047 paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2048 paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[0]));
2049 break;
2050 case 2:
2051 paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[1]));
2052 paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2053 paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2054 paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[0]));
2055 break;
2056 case 3:
2057 paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[1]));
2058 paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2059 paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2060 paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[2]));
2061 break;
2062 case 4:
2063 paddingStyle.padding.SetLeft(declaration.ParseDimension(offsets[3]));
2064 paddingStyle.padding.SetRight(declaration.ParseDimension(offsets[1]));
2065 paddingStyle.padding.SetTop(declaration.ParseDimension(offsets[0]));
2066 paddingStyle.padding.SetBottom(declaration.ParseDimension(offsets[2]));
2067 break;
2068 default:
2069 break;
2070 }
2071 declaration.hasBoxStyle_ = true;
2072 }
2073
SetMarginOverall(const std::string & value,Declaration & declaration)2074 void Declaration::SetMarginOverall(const std::string& value, Declaration& declaration)
2075 {
2076 auto& marginStyle = declaration.MaybeResetStyle<CommonMarginStyle>(StyleTag::COMMON_MARGIN_STYLE);
2077 if (!marginStyle.IsValid()) {
2078 LOGD("don't support margin");
2079 return;
2080 }
2081
2082 std::vector<std::string> offsets;
2083 StringUtils::StringSplitter(value, ' ', offsets);
2084 switch (offsets.size()) {
2085 case 1:
2086 marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[0]));
2087 marginStyle.margin.SetRight(declaration.ParseDimension(offsets[0]));
2088 marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2089 marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[0]));
2090 break;
2091 case 2:
2092 marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[1]));
2093 marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2094 marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2095 marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[0]));
2096 break;
2097 case 3:
2098 marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[1]));
2099 marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2100 marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2101 marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[2]));
2102 break;
2103 case 4:
2104 marginStyle.margin.SetLeft(declaration.ParseDimension(offsets[3]));
2105 marginStyle.margin.SetRight(declaration.ParseDimension(offsets[1]));
2106 marginStyle.margin.SetTop(declaration.ParseDimension(offsets[0]));
2107 marginStyle.margin.SetBottom(declaration.ParseDimension(offsets[2]));
2108 break;
2109 default:
2110 break;
2111 }
2112 declaration.hasBoxStyle_ = true;
2113 }
2114
SetBorderImageWidthForFourEdges(const std::string & value,Declaration & declaration)2115 void Declaration::SetBorderImageWidthForFourEdges(const std::string& value, Declaration& declaration)
2116 {
2117 auto& bgStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2118 if (!bgStyle.IsValid()) {
2119 return;
2120 }
2121 std::vector<std::string> offsets;
2122 StringUtils::StringSplitter(value, ' ', offsets);
2123 switch (offsets.size()) {
2124 case 1:
2125 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2126 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2127 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[0]));
2128 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[0]));
2129 declaration.backDecoration_->SetHasBorderImageWidth(true);
2130 break;
2131 case 2:
2132 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2133 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2134 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2135 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2136 declaration.backDecoration_->SetHasBorderImageWidth(true);
2137 break;
2138 case 3:
2139 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2140 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2141 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2142 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2143 declaration.backDecoration_->SetHasBorderImageWidth(true);
2144 break;
2145 case 4:
2146 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2147 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2148 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2149 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[3]));
2150 declaration.backDecoration_->SetHasBorderImageWidth(true);
2151 break;
2152 default:
2153 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::TOP, declaration.ParseDimension(value));
2154 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::RIGHT, declaration.ParseDimension(value));
2155 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::BOTTOM, declaration.ParseDimension(value));
2156 bgStyle.borderImage->SetEdgeWidth(BorderImageDirection::LEFT, declaration.ParseDimension(value));
2157 declaration.backDecoration_->SetHasBorderImageWidth(false);
2158 break;
2159 }
2160 declaration.backDecoration_->SetBorderImage(bgStyle.borderImage);
2161 declaration.hasDecorationStyle_ = true;
2162 declaration.hasBorderStyle_ = true;
2163 }
2164
SetBorderImageSliceForFourEdges(const std::string & value,Declaration & declaration)2165 void Declaration::SetBorderImageSliceForFourEdges(const std::string& value, Declaration& declaration)
2166 {
2167 auto& bgStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2168 if (!bgStyle.IsValid()) {
2169 return;
2170 }
2171 std::vector<std::string> offsets;
2172 StringUtils::StringSplitter(value, ' ', offsets);
2173 switch (offsets.size()) {
2174 case 1:
2175 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[0]));
2176 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2177 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[0]));
2178 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2179 declaration.backDecoration_->SetHasBorderImageSlice(true);
2180 break;
2181 case 2:
2182 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2183 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2184 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2185 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2186 declaration.backDecoration_->SetHasBorderImageSlice(true);
2187 break;
2188 case 3:
2189 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2190 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2191 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2192 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2193 declaration.backDecoration_->SetHasBorderImageSlice(true);
2194 break;
2195 case 4:
2196 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[3]));
2197 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2198 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2199 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2200 declaration.backDecoration_->SetHasBorderImageSlice(true);
2201 break;
2202 default:
2203 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::LEFT, declaration.ParseDimension(value));
2204 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::TOP, declaration.ParseDimension(value));
2205 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::RIGHT, declaration.ParseDimension(value));
2206 bgStyle.borderImage->SetEdgeSlice(BorderImageDirection::BOTTOM, declaration.ParseDimension(value));
2207 declaration.backDecoration_->SetHasBorderImageSlice(false);
2208 break;
2209 }
2210 declaration.backDecoration_->SetBorderImage(bgStyle.borderImage);
2211 declaration.hasDecorationStyle_ = true;
2212 declaration.hasBorderStyle_ = true;
2213 }
2214
SetBorderImageOutSetForFourEdges(const std::string & value,Declaration & declaration)2215 void Declaration::SetBorderImageOutSetForFourEdges(const std::string& value, Declaration& declaration)
2216 {
2217 auto& bgStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2218 if (!bgStyle.IsValid()) {
2219 return;
2220 }
2221 std::vector<std::string> offsets;
2222 StringUtils::StringSplitter(value, ' ', offsets);
2223 switch (offsets.size()) {
2224 case 1:
2225 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2226 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[0]));
2227 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2228 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[0]));
2229 declaration.backDecoration_->SetHasBorderImageOutset(true);
2230 break;
2231 case 2:
2232 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2233 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2234 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[0]));
2235 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2236 declaration.backDecoration_->SetHasBorderImageOutset(true);
2237 break;
2238 case 3:
2239 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2240 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2241 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2242 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[1]));
2243 declaration.backDecoration_->SetHasBorderImageOutset(true);
2244 break;
2245 case 4:
2246 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::TOP, declaration.ParseDimension(offsets[0]));
2247 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::RIGHT, declaration.ParseDimension(offsets[1]));
2248 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::BOTTOM, declaration.ParseDimension(offsets[2]));
2249 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::LEFT, declaration.ParseDimension(offsets[3]));
2250 declaration.backDecoration_->SetHasBorderImageOutset(true);
2251 break;
2252 default:
2253 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::TOP, declaration.ParseDimension(value));
2254 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::RIGHT, declaration.ParseDimension(value));
2255 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::BOTTOM, declaration.ParseDimension(value));
2256 bgStyle.borderImage->SetEdgeOutset(BorderImageDirection::LEFT, declaration.ParseDimension(value));
2257 declaration.backDecoration_->SetHasBorderImageOutset(false);
2258 break;
2259 }
2260 declaration.backDecoration_->SetBorderImage(bgStyle.borderImage);
2261 declaration.hasDecorationStyle_ = true;
2262 declaration.hasBorderStyle_ = true;
2263 }
2264
SetBorderImageRepeatForFourEdges(const std::string & value,Declaration & declaration)2265 void Declaration::SetBorderImageRepeatForFourEdges(const std::string& value, Declaration& declaration)
2266 {
2267 auto& bgStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2268 if (bgStyle.IsValid()) {
2269 bgStyle.borderImage->SetRepeatMode(ConvertStrToBorderImageRepeat(value));
2270 declaration.backDecoration_->SetBorderImage(bgStyle.borderImage);
2271 declaration.backDecoration_->SetHasBorderImageRepeat(true);
2272 declaration.hasDecorationStyle_ = true;
2273 declaration.hasBorderStyle_ = true;
2274 }
2275 }
2276
SetBorderImage(const std::string & value,Declaration & declaration)2277 void Declaration::SetBorderImage(const std::string& value, Declaration& declaration)
2278 {
2279 declaration.backDecoration_->SetHasBorderImageSource(false);
2280 declaration.backDecoration_->SetHasBorderImageGradient(false);
2281
2282 auto borderImageJson = JsonUtil::ParseJsonString(value);
2283 if (!borderImageJson->IsObject()) {
2284 LOGE("borderImageJson json is not Object");
2285 return;
2286 }
2287 if (borderImageJson->Contains(DOM_VALUES) && borderImageJson->GetValue(DOM_VALUES)->IsArray() &&
2288 borderImageJson->GetValue(DOM_VALUES)->GetArraySize() > 0) {
2289
2290 auto values = borderImageJson->GetValue(DOM_VALUES)->GetArrayItem(0);
2291
2292 if (values->Contains("url")) {
2293 SetBorderImageUrl(values, declaration);
2294 } else {
2295 SetBorderImageGradient(values, declaration);
2296 }
2297 }
2298 }
2299
SetBorderImageGradient(const std::unique_ptr<JsonValue> & values,Declaration & declaration)2300 void Declaration::SetBorderImageGradient(const std::unique_ptr<JsonValue>& values, Declaration& declaration)
2301 {
2302 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2303 backgroundStyle.gradientBorderImage = Gradient();
2304 if (values->Contains(DOM_GRADIENT_TYPE) && values->GetValue(DOM_GRADIENT_TYPE)->IsString()) {
2305 SetBorderImageGradientType(values->GetValue(DOM_GRADIENT_TYPE)->GetString(), declaration);
2306 }
2307 if (values->Contains(DOM_GRADIENT_DIRECTIONS) && values->GetValue(DOM_GRADIENT_DIRECTIONS)->IsArray()) {
2308 SetBorderImageGradientDirections(values->GetValue(DOM_GRADIENT_DIRECTIONS), declaration);
2309 }
2310 if (values->Contains(DOM_GRADIENT_VALUES) && values->GetValue(DOM_GRADIENT_VALUES)->IsArray()) {
2311 SetBorderImageGradientColor(values->GetValue(DOM_GRADIENT_VALUES), declaration);
2312 }
2313 if (values->Contains("slice") && values->GetValue("slice")->IsArray()) {
2314 std::unique_ptr<JsonValue> sliceItem = values->GetValue("slice");
2315 std::string sliceStr;
2316 for (int32_t i = 0; i < sliceItem->GetArraySize(); i++) {
2317 sliceStr += sliceItem->GetArrayItem(i)->GetString() + " ";
2318 }
2319 SetBorderImageSliceForFourEdges(sliceStr, declaration);
2320 }
2321 declaration.backDecoration_->SetHasBorderImageGradient(true);
2322 declaration.hasDecorationStyle_ = true;
2323 declaration.hasBorderStyle_ = true;
2324 }
2325
SetBorderImageUrl(const std::unique_ptr<JsonValue> & values,Declaration & declaration)2326 void Declaration::SetBorderImageUrl(const std::unique_ptr<JsonValue>& values, Declaration& declaration)
2327 {
2328 if (values->Contains("url") && values->GetValue("url")->IsString()) {
2329 SetBorderImageFindUrl(values->GetValue("url")->GetString(), declaration);
2330 }
2331 if (values->Contains("slice") && values->GetValue("slice")->IsArray()) {
2332 std::unique_ptr<JsonValue> sliceItem = values->GetValue("slice");
2333 std::string sliceStr;
2334 for (int32_t i = 0; i < sliceItem->GetArraySize(); i++) {
2335 sliceStr += sliceItem->GetArrayItem(i)->GetString() + " ";
2336 }
2337 SetBorderImageSliceForFourEdges(sliceStr, declaration);
2338 }
2339 if (values->Contains("width") && values->GetValue("width")->IsArray()) {
2340 std::unique_ptr<JsonValue> widthItem = values->GetValue("width");
2341
2342 std::string widthStr;
2343 for (int32_t i = 0; i < widthItem->GetArraySize(); i++) {
2344 widthStr += widthItem->GetArrayItem(i)->GetString() + " ";
2345 }
2346 SetBorderImageWidthForFourEdges(widthStr, declaration);
2347 }
2348 if (values->Contains("outset") && values->GetValue("outset")->IsArray()) {
2349 std::unique_ptr<JsonValue> outsetItem = values->GetValue("outset");
2350
2351 std::string outsetStr;
2352 for (int32_t i = 0; i < outsetItem->GetArraySize(); i++) {
2353 outsetStr += outsetItem->GetArrayItem(i)->GetString() + " ";
2354 }
2355 SetBorderImageOutSetForFourEdges(outsetStr, declaration);
2356 }
2357 if (values->Contains("repeat") && values->GetValue("repeat")->IsString()) {
2358 SetBorderImageRepeatForFourEdges(values->GetValue("repeat")->GetString(), declaration);
2359 }
2360 declaration.hasDecorationStyle_ = true;
2361 declaration.hasBorderStyle_ = true;
2362 }
2363
SetBorderImageFindUrl(const std::string & value,Declaration & declaration)2364 void Declaration::SetBorderImageFindUrl(const std::string& value, Declaration& declaration)
2365 {
2366 auto& backgroundStyle =
2367 declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2368 if (backgroundStyle.IsValid()) {
2369 backgroundStyle.borderImage->SetSrc(value);
2370 declaration.backDecoration_->SetBorderImage(backgroundStyle.borderImage);
2371 declaration.backDecoration_->SetHasBorderImageSource(true);
2372 declaration.hasDecorationStyle_ = true;
2373 }
2374 }
2375
SetBorderImageGradientType(const std::string & gradientType,Declaration & declaration)2376 void Declaration::SetBorderImageGradientType(const std::string& gradientType, Declaration& declaration)
2377 {
2378 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2379 if (!backgroundStyle.IsValid()) {
2380 return;
2381 }
2382 // default: LINEAR
2383 backgroundStyle.gradientBorderImage.SetType(GradientType::LINEAR);
2384 if (gradientType == DOM_RADIAL_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT) {
2385 backgroundStyle.gradientBorderImage.SetType(GradientType::RADIAL);
2386 } else if (gradientType == DOM_SWEEP_GRADIENT || gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2387 backgroundStyle.gradientBorderImage.SetType(GradientType::SWEEP);
2388 }
2389
2390 if (gradientType == DOM_REPEATING_LINEAR_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT ||
2391 gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2392 backgroundStyle.gradientBorderImage.SetRepeat(true);
2393 }
2394 declaration.hasDecorationStyle_ = true;
2395 }
2396
SetBorderImageGradientDirections(const std::unique_ptr<JsonValue> & gradientDirections,Declaration & declaration)2397 void Declaration::SetBorderImageGradientDirections(const std::unique_ptr<JsonValue>& gradientDirections,
2398 Declaration& declaration)
2399 {
2400 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2401 if (!backgroundStyle.IsValid()) {
2402 return;
2403 }
2404
2405 std::unique_ptr<JsonValue> angleItem;
2406 std::unique_ptr<JsonValue> sideItem;
2407 std::unique_ptr<JsonValue> cornerItem;
2408 GradientDirection direction;
2409 switch (gradientDirections->GetArraySize()) {
2410 case DIRECTION_ANGLE:
2411 angleItem = gradientDirections->GetArrayItem(0);
2412 if (angleItem->IsString()) {
2413 LinearGradient linearGradient;
2414 linearGradient.angle = AnimatableDimension(StringToDouble(angleItem->GetString()));
2415 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2416 declaration.hasDecorationStyle_ = true;
2417 }
2418 break;
2419 case DIRECTION_SIDE:
2420 sideItem = gradientDirections->GetArrayItem(1);
2421 if (sideItem->IsString()) {
2422 direction = StrToGradientDirection(sideItem->GetString());
2423 LinearGradient linearGradient;
2424 if (LinearGradient::IsXAxis(direction)) {
2425 linearGradient.linearX = direction;
2426 } else {
2427 linearGradient.linearY = direction;
2428 }
2429 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2430 declaration.hasDecorationStyle_ = true;
2431 }
2432 break;
2433 case DIRECTION_CORNER:
2434 sideItem = gradientDirections->GetArrayItem(1);
2435 cornerItem = gradientDirections->GetArrayItem(2);
2436 if (sideItem->IsString() && cornerItem->IsString()) {
2437 LinearGradient linearGradient;
2438 auto direction1 = StrToGradientDirection(sideItem->GetString());
2439 auto direction2 = StrToGradientDirection(cornerItem->GetString());
2440 if ((LinearGradient::IsXAxis(direction1) && LinearGradient::IsXAxis(direction2)) ||
2441 (!LinearGradient::IsXAxis(direction1) && !LinearGradient::IsXAxis(direction2))) {
2442 linearGradient.linearY = GradientDirection::BOTTOM;
2443 break;
2444 } else {
2445 if (LinearGradient::IsXAxis(direction1)) {
2446 linearGradient.linearX = direction1;
2447 linearGradient.linearY = direction2;
2448 } else {
2449 linearGradient.linearY = direction1;
2450 linearGradient.linearX = direction2;
2451 }
2452 }
2453 backgroundStyle.gradientBorderImage.SetLinearGradient(linearGradient);
2454 declaration.hasDecorationStyle_ = true;
2455 }
2456 break;
2457 default:
2458 LOGE("gradientDirectionsLength error");
2459 break;
2460 }
2461 }
2462
SetBorderImageGradientColor(const std::unique_ptr<JsonValue> & gradientColorValues,Declaration & declaration)2463 void Declaration::SetBorderImageGradientColor(const std::unique_ptr<JsonValue>& gradientColorValues,
2464 Declaration& declaration)
2465 {
2466 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2467 if (!backgroundStyle.IsValid()) {
2468 return;
2469 }
2470 backgroundStyle.gradientBorderImage.ClearColors();
2471 int32_t gradientColorValuesLength = gradientColorValues->GetArraySize();
2472 for (int32_t i = 0; i < gradientColorValuesLength; i++) {
2473 std::string gradientColorValue = gradientColorValues->GetArrayItem(i)->GetString();
2474 GradientColor gradientColor;
2475 RemoveHeadTailSpace(gradientColorValue);
2476 auto index = gradientColorValue.find(' ');
2477 if (index != std::string::npos && index != 0) {
2478 std::string color = gradientColorValue.substr(0, index);
2479 std::string area = gradientColorValue.substr(index + 1, gradientColorValue.size() - index - 1);
2480 gradientColor.SetColor(declaration.ParseColor(color));
2481 gradientColor.SetHasValue(true);
2482 if (area.find("px") != std::string::npos) {
2483 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PX);
2484 } else if (area.find('%') != std::string::npos) {
2485 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PERCENT);
2486 } else {
2487 LOGW("gradientColor DimensionUnit is incorrect)");
2488 gradientColor.SetHasValue(false);
2489 }
2490 } else {
2491 gradientColor.SetHasValue(false);
2492 gradientColor.SetColor(declaration.ParseColor(gradientColorValue));
2493 }
2494 backgroundStyle.gradientBorderImage.AddColor(gradientColor);
2495 declaration.hasDecorationStyle_ = true;
2496 }
2497 }
2498
SetBorderOverall(const std::string & value,Declaration & declaration)2499 void Declaration::SetBorderOverall(const std::string& value, Declaration& declaration)
2500 {
2501 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2502 if (!borderStyle.IsValid()) {
2503 LOGD("don't support border");
2504 return;
2505 }
2506
2507 std::vector<std::string> offsets;
2508 StringUtils::StringSplitter(value, ' ', offsets);
2509 switch (offsets.size()) {
2510 case 1:
2511 if (offsets[0].find("px") != std::string::npos) {
2512 SetBorderWidthForFourEdges(offsets[0], declaration);
2513 } else if (offsets[0] == "solid" || offsets[0] == "dotted" || offsets[0] == "dashed") {
2514 SetBorderStyleForFourEdges(offsets[0], declaration);
2515 } else {
2516 SetBorderColorForFourEdges(offsets[0], declaration);
2517 }
2518 break;
2519 case 2:
2520 SetBorderWidthForFourEdges(offsets[0], declaration);
2521 SetBorderStyleForFourEdges(offsets[1], declaration);
2522 break;
2523 case 3:
2524 SetBorderWidthForFourEdges(offsets[0], declaration);
2525 SetBorderStyleForFourEdges(offsets[1], declaration);
2526 SetBorderColorForFourEdges(offsets[2], declaration);
2527 break;
2528 default:
2529 break;
2530 }
2531 }
2532
SetBorderWidthForFourEdges(const std::string & value,Declaration & declaration)2533 void Declaration::SetBorderWidthForFourEdges(const std::string& value, Declaration& declaration)
2534 {
2535 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2536 if (borderStyle.IsValid()) {
2537 borderStyle.border.SetWidth(declaration.ParseDimension(value));
2538 declaration.hasDecorationStyle_ = true;
2539 declaration.hasBorderStyle_ = true;
2540 }
2541 }
2542
SetBorderColorForFourEdges(const std::string & value,Declaration & declaration)2543 void Declaration::SetBorderColorForFourEdges(const std::string& value, Declaration& declaration)
2544 {
2545 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2546 if (borderStyle.IsValid()) {
2547 borderStyle.border.SetColor(declaration.ParseColor(value));
2548 declaration.hasDecorationStyle_ = true;
2549 declaration.hasBorderStyle_ = true;
2550 }
2551 }
2552
SetBorderStyleForFourEdges(const std::string & value,Declaration & declaration)2553 void Declaration::SetBorderStyleForFourEdges(const std::string& value, Declaration& declaration)
2554 {
2555 auto& borderStyle = declaration.MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
2556 if (borderStyle.IsValid()) {
2557 borderStyle.border.SetStyle(ConvertStrToBorderStyle(value));
2558 declaration.hasDecorationStyle_ = true;
2559 declaration.hasBorderStyle_ = true;
2560 }
2561 }
2562
SetMaskGradient(const std::string & value,Declaration & declaration)2563 void Declaration::SetMaskGradient(const std::string& value, Declaration& declaration)
2564 {
2565 Declaration::SetBackground(value, declaration);
2566 }
2567
SetBackground(const std::string & value,Declaration & declaration)2568 void Declaration::SetBackground(const std::string& value, Declaration& declaration)
2569 {
2570 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2571 if (!backgroundStyle.IsValid()) {
2572 LOGD("don't support background style");
2573 return;
2574 }
2575
2576 LOGD("Declaration::SetBackground value:%{private}s", value.c_str());
2577 auto backgroundJson = JsonUtil::ParseJsonString(value);
2578 if (!backgroundJson->IsObject()) {
2579 LOGE("background json is not Object");
2580 return;
2581 }
2582 if (backgroundJson->Contains(DOM_VALUES) && backgroundJson->GetValue(DOM_VALUES)->IsArray() &&
2583 backgroundJson->GetValue(DOM_VALUES)->GetArraySize() > 0) {
2584 backgroundStyle.gradient = Gradient();
2585 auto values = backgroundJson->GetValue(DOM_VALUES)->GetArrayItem(0);
2586 // gradient type and repeating
2587 if (values->Contains(DOM_GRADIENT_TYPE) && values->GetValue(DOM_GRADIENT_TYPE)->IsString()) {
2588 SetGradientType(values->GetValue(DOM_GRADIENT_TYPE)->GetString(), declaration);
2589 }
2590 // linearGradient direction
2591 if (values->Contains(DOM_GRADIENT_DIRECTIONS) && values->GetValue(DOM_GRADIENT_DIRECTIONS)->IsArray()) {
2592 SetGradientDirections(values->GetValue(DOM_GRADIENT_DIRECTIONS), declaration);
2593 }
2594 // radialGradient shape
2595 if (values->Contains(DOM_GRADIENT_SHAPE) && values->GetValue(DOM_GRADIENT_SHAPE)->IsString()) {
2596 SetGradientShape(values->GetValue(DOM_GRADIENT_SHAPE)->GetString(), declaration);
2597 }
2598 // radialGradient size
2599 if (values->Contains(DOM_GRADIENT_SIZE) && values->GetValue(DOM_GRADIENT_SIZE)->IsString()) {
2600 SetGradientSize(values->GetValue(DOM_GRADIENT_SIZE)->GetString(), declaration);
2601 }
2602 // radialGradient or sweepGradient position
2603 if (values->Contains(DOM_GRADIENT_POSITION) && values->GetValue(DOM_GRADIENT_POSITION)->IsString()) {
2604 SetGradientPosition(values->GetValue(DOM_GRADIENT_POSITION)->GetString(), declaration);
2605 }
2606 // sweepGradient startAngle and endAngle
2607 if (values->Contains(DOM_GRADIENT_ANGLE) && values->GetValue(DOM_GRADIENT_ANGLE)->IsString()) {
2608 SetGradientAngle(values->GetValue(DOM_GRADIENT_ANGLE)->GetString(), declaration);
2609 }
2610 // sweepGradient rotation
2611 if (values->Contains(DOM_GRADIENT_ROTATION) && values->GetValue(DOM_GRADIENT_ROTATION)->IsString()) {
2612 SetGradientRotation(values->GetValue(DOM_GRADIENT_ROTATION)->GetString(), declaration);
2613 }
2614 // gradient color stops
2615 if (values->Contains(DOM_GRADIENT_VALUES) && values->GetValue(DOM_GRADIENT_VALUES)->IsArray()) {
2616 SetGradientColor(values->GetValue(DOM_GRADIENT_VALUES), declaration);
2617 }
2618 }
2619 declaration.hasDecorationStyle_ = true;
2620 declaration.hasBackGroundColor_ = true;
2621 }
2622
SetGradientType(const std::string & gradientType,Declaration & declaration)2623 void Declaration::SetGradientType(const std::string& gradientType, Declaration& declaration)
2624 {
2625 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2626 if (!backgroundStyle.IsValid()) {
2627 return;
2628 }
2629 // default: LINEAR
2630 backgroundStyle.gradient.SetType(GradientType::LINEAR);
2631 if (gradientType == DOM_RADIAL_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT) {
2632 backgroundStyle.gradient.SetType(GradientType::RADIAL);
2633 } else if (gradientType == DOM_SWEEP_GRADIENT || gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2634 backgroundStyle.gradient.SetType(GradientType::SWEEP);
2635 }
2636
2637 if (gradientType == DOM_REPEATING_LINEAR_GRADIENT || gradientType == DOM_REPEATING_RADIAL_GRADIENT ||
2638 gradientType == DOM_REPEATING_SWEEP_GRADIENT) {
2639 backgroundStyle.gradient.SetRepeat(true);
2640 }
2641 declaration.hasDecorationStyle_ = true;
2642 }
2643
SetGradientDirections(const std::unique_ptr<JsonValue> & gradientDirections,Declaration & declaration)2644 void Declaration::SetGradientDirections(const std::unique_ptr<JsonValue>& gradientDirections, Declaration& declaration)
2645 {
2646 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2647 if (!backgroundStyle.IsValid()) {
2648 return;
2649 }
2650
2651 std::unique_ptr<JsonValue> angleItem;
2652 std::unique_ptr<JsonValue> sideItem;
2653 std::unique_ptr<JsonValue> cornerItem;
2654 GradientDirection direction;
2655 switch (gradientDirections->GetArraySize()) {
2656 case DIRECTION_ANGLE:
2657 angleItem = gradientDirections->GetArrayItem(0);
2658 if (angleItem->IsString()) {
2659 LinearGradient linearGradient;
2660 linearGradient.angle = AnimatableDimension(StringToDouble(angleItem->GetString()));
2661 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2662 declaration.hasDecorationStyle_ = true;
2663 }
2664 break;
2665 case DIRECTION_SIDE:
2666 sideItem = gradientDirections->GetArrayItem(1);
2667 if (sideItem->IsString()) {
2668 direction = StrToGradientDirection(sideItem->GetString());
2669 LinearGradient linearGradient;
2670 if (LinearGradient::IsXAxis(direction)) {
2671 linearGradient.linearX = direction;
2672 } else {
2673 linearGradient.linearY = direction;
2674 }
2675 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2676 declaration.hasDecorationStyle_ = true;
2677 }
2678 break;
2679 case DIRECTION_CORNER:
2680 sideItem = gradientDirections->GetArrayItem(1);
2681 cornerItem = gradientDirections->GetArrayItem(2);
2682 if (sideItem->IsString() && cornerItem->IsString()) {
2683 LinearGradient linearGradient;
2684 auto direction1 = StrToGradientDirection(sideItem->GetString());
2685 auto direction2 = StrToGradientDirection(cornerItem->GetString());
2686 if ((LinearGradient::IsXAxis(direction1) && LinearGradient::IsXAxis(direction2)) ||
2687 (!LinearGradient::IsXAxis(direction1) && !LinearGradient::IsXAxis(direction2))) {
2688 linearGradient.linearY = GradientDirection::BOTTOM;
2689 break;
2690 } else {
2691 if (LinearGradient::IsXAxis(direction1)) {
2692 linearGradient.linearX = direction1;
2693 linearGradient.linearY = direction2;
2694 } else {
2695 linearGradient.linearY = direction1;
2696 linearGradient.linearX = direction2;
2697 }
2698 }
2699 backgroundStyle.gradient.SetLinearGradient(linearGradient);
2700 declaration.hasDecorationStyle_ = true;
2701 }
2702 break;
2703 default:
2704 LOGE("gradientDirectionsLength error");
2705 break;
2706 }
2707 }
2708
SetGradientColor(const std::unique_ptr<JsonValue> & gradientColorValues,Declaration & declaration)2709 void Declaration::SetGradientColor(const std::unique_ptr<JsonValue>& gradientColorValues, Declaration& declaration)
2710 {
2711 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2712 if (!backgroundStyle.IsValid()) {
2713 return;
2714 }
2715
2716 backgroundStyle.gradient.ClearColors();
2717 int32_t gradientColorValuesLength = gradientColorValues->GetArraySize();
2718 for (int32_t i = 0; i < gradientColorValuesLength; i++) {
2719 std::string gradientColorValue = gradientColorValues->GetArrayItem(i)->GetString();
2720 GradientColor gradientColor;
2721 RemoveHeadTailSpace(gradientColorValue);
2722 auto index = gradientColorValue.find(' ');
2723 if (index != std::string::npos && index != 0) {
2724 std::string color = gradientColorValue.substr(0, index);
2725 std::string area = gradientColorValue.substr(index + 1, gradientColorValue.size() - index - 1);
2726 gradientColor.SetColor(declaration.ParseColor(color));
2727 gradientColor.SetHasValue(true);
2728 if (area.find("px") != std::string::npos) {
2729 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PX);
2730 } else if (area.find('%') != std::string::npos) {
2731 gradientColor.SetDimension(StringToDouble(area), DimensionUnit::PERCENT);
2732 } else {
2733 LOGW("gradientColor DimensionUnit is incorrect)");
2734 gradientColor.SetHasValue(false);
2735 }
2736 } else {
2737 gradientColor.SetHasValue(false);
2738 gradientColor.SetColor(declaration.ParseColor(gradientColorValue));
2739 }
2740 backgroundStyle.gradient.AddColor(gradientColor);
2741 declaration.hasDecorationStyle_ = true;
2742 }
2743 }
2744
SetGradientShape(const std::string & gradientShape,Declaration & declaration)2745 void Declaration::SetGradientShape(const std::string& gradientShape, Declaration& declaration)
2746 {
2747 // if empty do nothing, If shape is omitted, the ending shape defaults to a circle if the <size> is a single
2748 // <length>, and to an ellipse otherwise.
2749 if (gradientShape.empty()) {
2750 return;
2751 }
2752 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2753 if (!backgroundStyle.IsValid()) {
2754 return;
2755 }
2756
2757 if (gradientShape == DOM_GRADIENT_SHAPE_ELLIPSE) {
2758 backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::ELLIPSE;
2759 } else {
2760 backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::CIRCLE;
2761 }
2762 declaration.hasDecorationStyle_ = true;
2763 }
2764
SetGradientSize(const std::string & gradientSize,Declaration & declaration)2765 void Declaration::SetGradientSize(const std::string& gradientSize, Declaration& declaration)
2766 {
2767 if (gradientSize.empty()) {
2768 return;
2769 }
2770 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2771 if (!backgroundStyle.IsValid()) {
2772 return;
2773 }
2774 // 1. closest-corner | closest-side | farthest-corner | farthest-side
2775 auto extent = ParseRadialGradientSize(gradientSize);
2776 if (extent) {
2777 backgroundStyle.gradient.GetRadialGradient().radialSizeType = extent;
2778 declaration.hasDecorationStyle_ = true;
2779 return;
2780 }
2781
2782 std::vector<std::string> offsets;
2783 StringUtils::StringSplitter(gradientSize, ' ', offsets);
2784 if (offsets.size() == 1) {
2785 // 2. if circle: <length>
2786 auto circleSize = StringToDimension(offsets[0]);
2787
2788 if (circleSize.Unit() != DimensionUnit::PX) {
2789 LOGE("circle only support length");
2790 return;
2791 }
2792 if (backgroundStyle.gradient.GetRadialGradient().radialShape &&
2793 backgroundStyle.gradient.GetRadialGradient().radialShape != RadialShapeType::CIRCLE) {
2794 LOGE("only circle support one size");
2795 return;
2796 }
2797 backgroundStyle.gradient.GetRadialGradient().radialVerticalSize = circleSize;
2798 backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::CIRCLE;
2799 declaration.hasDecorationStyle_ = true;
2800 } else if (offsets.size() == 2) {
2801 // 3. if ellipse: <length-percentage>{2}
2802 auto horizontalSize = StringToDimension(offsets[0]);
2803 auto verticalSize = StringToDimension(offsets[1]);
2804
2805 if (backgroundStyle.gradient.GetRadialGradient().radialShape &&
2806 backgroundStyle.gradient.GetRadialGradient().radialShape != RadialShapeType::ELLIPSE) {
2807 LOGE("only ellipse support two size");
2808 return;
2809 }
2810 backgroundStyle.gradient.GetRadialGradient().radialHorizontalSize = horizontalSize;
2811 backgroundStyle.gradient.GetRadialGradient().radialVerticalSize = verticalSize;
2812 backgroundStyle.gradient.GetRadialGradient().radialShape = RadialShapeType::ELLIPSE;
2813 declaration.hasDecorationStyle_ = true;
2814 } else {
2815 LOGE("unsupported offset size");
2816 }
2817 }
2818
SetGradientPosition(const std::string & gradientPosition,Declaration & declaration)2819 void Declaration::SetGradientPosition(const std::string& gradientPosition, Declaration& declaration)
2820 {
2821 if (gradientPosition.empty()) {
2822 return;
2823 }
2824 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2825 if (!backgroundStyle.IsValid()) {
2826 return;
2827 }
2828 // position determines the center of gradient default is center
2829 BackgroundImagePosition position;
2830 if (ParseBackgroundImagePosition(gradientPosition, position)) {
2831 auto xAxisPosition = Dimension(position.GetSizeValueX(),
2832 position.GetSizeTypeX() == BackgroundImagePositionType::PX ? DimensionUnit::PX : DimensionUnit::PERCENT);
2833 auto yAxisPosition = Dimension(position.GetSizeValueY(),
2834 position.GetSizeTypeY() == BackgroundImagePositionType::PX ? DimensionUnit::PX : DimensionUnit::PERCENT);
2835 if (backgroundStyle.gradient.GetType() == GradientType::RADIAL) {
2836 backgroundStyle.gradient.GetRadialGradient().radialCenterX = xAxisPosition;
2837 backgroundStyle.gradient.GetRadialGradient().radialCenterY = yAxisPosition;
2838 declaration.hasDecorationStyle_ = true;
2839 } else if (backgroundStyle.gradient.GetType() == GradientType::SWEEP) {
2840 backgroundStyle.gradient.GetSweepGradient().centerX = xAxisPosition;
2841 backgroundStyle.gradient.GetSweepGradient().centerY = yAxisPosition;
2842 declaration.hasDecorationStyle_ = true;
2843 }
2844 } else {
2845 LOGE("ParseBackgroundImagePosition failed");
2846 }
2847 }
2848
SetGradientAngle(const std::string & gradientAngle,Declaration & declaration)2849 void Declaration::SetGradientAngle(const std::string& gradientAngle, Declaration& declaration)
2850 {
2851 if (gradientAngle.empty()) {
2852 return;
2853 }
2854 auto backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2855 if (!backgroundStyle.IsValid()) {
2856 LOGE("backgroundStyle is invalid");
2857 return;
2858 }
2859 std::vector<std::string> offsets;
2860 StringUtils::StringSplitter(gradientAngle, ' ', offsets);
2861 if (!offsets.empty()) {
2862 auto startAngle = StringUtils::StringToDegree(offsets[0]);
2863 backgroundStyle.gradient.GetSweepGradient().startAngle = AnimatableDimension(startAngle);
2864 if (offsets.size() > 1) {
2865 auto endAngle = StringUtils::StringToDegree(offsets[1]);
2866 backgroundStyle.gradient.GetSweepGradient().endAngle = AnimatableDimension(endAngle);
2867 }
2868 declaration.hasDecorationStyle_ = true;
2869 }
2870 }
2871
SetGradientRotation(const std::string & gradientRotation,Declaration & declaration)2872 void Declaration::SetGradientRotation(const std::string& gradientRotation, Declaration& declaration)
2873 {
2874 if (gradientRotation.empty()) {
2875 return;
2876 }
2877 auto backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2878 if (!backgroundStyle.IsValid()) {
2879 return;
2880 }
2881 std::vector<std::string> offsets;
2882 StringUtils::StringSplitter(gradientRotation, ' ', offsets);
2883 if (!offsets.empty()) {
2884 auto rotationAngle = StringUtils::StringToDegree(offsets[0]);
2885 backgroundStyle.gradient.GetSweepGradient().rotation = AnimatableDimension(rotationAngle);
2886 declaration.hasDecorationStyle_ = true;
2887 }
2888 }
2889
SetBgImgSizeX(const BackgroundImageSizeType type,const double value,BackgroundImageSize & bgImgSize)2890 void SetBgImgSizeX(const BackgroundImageSizeType type, const double value, BackgroundImageSize& bgImgSize)
2891 {
2892 bgImgSize.SetSizeTypeX(type);
2893 bgImgSize.SetSizeValueX(value);
2894 }
2895
SetBgImgSizeY(const BackgroundImageSizeType type,const double value,BackgroundImageSize & bgImgSize)2896 void SetBgImgSizeY(const BackgroundImageSizeType type, const double value, BackgroundImageSize& bgImgSize)
2897 {
2898 bgImgSize.SetSizeTypeY(type);
2899 bgImgSize.SetSizeValueY(value);
2900 }
2901
SetBackgroundImageSize(const std::string & value,Declaration & declaration)2902 void Declaration::SetBackgroundImageSize(const std::string& value, Declaration& declaration)
2903 {
2904 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
2905 if (!backgroundStyle.IsValid()) {
2906 LOGD("don't support background");
2907 return;
2908 }
2909
2910 static const LinearMapNode<BackgroundImageSizeType> bgImageSizeType[] = {
2911 { DOM_BACKGROUND_IMAGE_SIZE_AUTO, BackgroundImageSizeType::AUTO },
2912 { DOM_BACKGROUND_IMAGE_SIZE_CONTAIN, BackgroundImageSizeType::CONTAIN },
2913 { DOM_BACKGROUND_IMAGE_SIZE_COVER, BackgroundImageSizeType::COVER },
2914 };
2915 BackgroundImageSize bgImgSize;
2916 auto spaceIndex = value.find(' ', 0);
2917 if (spaceIndex != std::string::npos) {
2918 std::string valueX = value.substr(0, spaceIndex);
2919 std::string valueY = value.substr(spaceIndex + 1, value.size() - spaceIndex - 1);
2920 if (valueX.find("px") != std::string::npos) {
2921 SetBgImgSizeX(BackgroundImageSizeType::LENGTH, StringToDouble(valueX), bgImgSize);
2922 } else if (valueX.find('%') != std::string::npos) {
2923 SetBgImgSizeX(BackgroundImageSizeType::PERCENT, StringToDouble(valueX), bgImgSize);
2924 } else {
2925 bgImgSize.SetSizeTypeX(BackgroundImageSizeType::AUTO);
2926 }
2927 if (valueY.find("px") != std::string::npos) {
2928 SetBgImgSizeY(BackgroundImageSizeType::LENGTH, StringToDouble(valueY), bgImgSize);
2929 } else if (valueY.find('%') != std::string::npos) {
2930 SetBgImgSizeY(BackgroundImageSizeType::PERCENT, StringToDouble(valueY), bgImgSize);
2931 } else {
2932 bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2933 }
2934 } else {
2935 auto sizeTypeIter = BinarySearchFindIndex(bgImageSizeType, ArraySize(bgImageSizeType), value.c_str());
2936 if (sizeTypeIter != -1) {
2937 bgImgSize.SetSizeTypeX(bgImageSizeType[sizeTypeIter].value);
2938 bgImgSize.SetSizeTypeY(bgImageSizeType[sizeTypeIter].value);
2939 } else if (value.find("px") != std::string::npos) {
2940 SetBgImgSizeX(BackgroundImageSizeType::LENGTH, StringToDouble(value), bgImgSize);
2941 bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2942 } else if (value.find('%') != std::string::npos) {
2943 SetBgImgSizeX(BackgroundImageSizeType::PERCENT, StringToDouble(value), bgImgSize);
2944 bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2945 } else {
2946 bgImgSize.SetSizeTypeX(BackgroundImageSizeType::AUTO);
2947 bgImgSize.SetSizeTypeY(BackgroundImageSizeType::AUTO);
2948 }
2949 }
2950 backgroundStyle.backgroundImage->SetImageSize(
2951 bgImgSize.GetSizeTypeX(), bgImgSize.GetSizeValueX(), bgImgSize.GetSizeTypeY(), bgImgSize.GetSizeValueY());
2952 declaration.hasDecorationStyle_ = true;
2953 }
2954
SetBgImgPositionX(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2955 void SetBgImgPositionX(
2956 const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2957 {
2958 bgImgPosition.SetSizeTypeX(type);
2959 bgImgPosition.SetSizeValueX(value);
2960 }
2961
SetBgImgPositionY(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2962 void SetBgImgPositionY(
2963 const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2964 {
2965 bgImgPosition.SetSizeTypeY(type);
2966 bgImgPosition.SetSizeValueY(value);
2967 }
2968
SetBgImgPosition(const BackgroundImagePositionType type,const double value,BackgroundImagePosition & bgImgPosition)2969 void SetBgImgPosition(
2970 const BackgroundImagePositionType type, const double value, BackgroundImagePosition& bgImgPosition)
2971 {
2972 SetBgImgPositionX(type, value, bgImgPosition);
2973 SetBgImgPositionY(type, value, bgImgPosition);
2974 }
2975
BgImgPositionIsValid(const std::string & posX,const std::string & posY)2976 bool BgImgPositionIsValid(const std::string& posX, const std::string& posY)
2977 {
2978 if ((std::strcmp(posX.c_str(), DOM_BACKGROUND_IMAGE_POSITION_CENTER) == 0) ||
2979 (std::strcmp(posY.c_str(), DOM_BACKGROUND_IMAGE_POSITION_CENTER) == 0)) {
2980 return true;
2981 }
2982
2983 static const std::unordered_set<std::string> horizonSet = {
2984 DOM_BACKGROUND_IMAGE_POSITION_LEFT,
2985 DOM_BACKGROUND_IMAGE_POSITION_RIGHT,
2986 };
2987 static const std::unordered_set<std::string> verticalSet = {
2988 DOM_BACKGROUND_IMAGE_POSITION_TOP,
2989 DOM_BACKGROUND_IMAGE_POSITION_BOTTOM,
2990 };
2991
2992 // posX and posY are not strictly corresponding to horizontal or vertical, but they must not conflict,
2993 // for example both of them are "top" is invalid.
2994 if (posX.find("px") != std::string::npos || posX.find('%') != std::string::npos ||
2995 horizonSet.find(posX) != horizonSet.end()) {
2996 if (posY.find("px") != std::string::npos || posY.find('%') != std::string::npos ||
2997 verticalSet.find(posY) != verticalSet.end()) {
2998 return true;
2999 }
3000 }
3001
3002 return verticalSet.find(posX) != verticalSet.end() && horizonSet.find(posY) != horizonSet.end();
3003 }
3004
SetBackgroundImagePosition(const std::string & value,Declaration & declaration)3005 void Declaration::SetBackgroundImagePosition(const std::string& value, Declaration& declaration)
3006 {
3007 auto& backgroundStyle = declaration.MaybeResetStyle<CommonBackgroundStyle>(StyleTag::COMMON_BACKGROUND_STYLE);
3008 if (!backgroundStyle.IsValid()) {
3009 LOGD("don't support background");
3010 return;
3011 }
3012
3013 static const LinearMapNode<void (*)(BackgroundImagePosition&)> backGroundPositionOperators[] = {
3014 { DOM_BACKGROUND_IMAGE_POSITION_BOTTOM,
3015 [](BackgroundImagePosition& backgroundImagePosition) {
3016 SetBgImgPositionY(BackgroundImagePositionType::PERCENT, 100.0, backgroundImagePosition);
3017 } },
3018 { DOM_BACKGROUND_IMAGE_POSITION_LEFT,
3019 [](BackgroundImagePosition& backgroundImagePosition) {
3020 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, 0.0, backgroundImagePosition);
3021 } },
3022 { DOM_BACKGROUND_IMAGE_POSITION_RIGHT,
3023 [](BackgroundImagePosition& backgroundImagePosition) {
3024 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, 100.0, backgroundImagePosition);
3025 } },
3026 { DOM_BACKGROUND_IMAGE_POSITION_TOP,
3027 [](BackgroundImagePosition& backgroundImagePosition) {
3028 SetBgImgPositionY(BackgroundImagePositionType::PERCENT, 0.0, backgroundImagePosition);
3029 } },
3030 };
3031 BackgroundImagePosition backgroundImagePosition;
3032
3033 auto index = value.find(' ', 0);
3034 if (index != std::string::npos) {
3035 std::string valueX = value.substr(0, index);
3036 std::string valueY = value.substr(index + 1, value.size() - index - 1);
3037 if (!BgImgPositionIsValid(valueX, valueY)) {
3038 return;
3039 }
3040 // The input is valid,so set the default is (center,center),
3041 // if the value is different, the default value is overwritten.
3042 SetBgImgPosition(BackgroundImagePositionType::PERCENT, 50.0, backgroundImagePosition);
3043 if (valueX.find("px") != std::string::npos) {
3044 SetBgImgPositionX(BackgroundImagePositionType::PX, StringToDouble(valueX), backgroundImagePosition);
3045 } else if (valueX.find('%') != std::string::npos) {
3046 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, StringToDouble(valueX), backgroundImagePosition);
3047 } else {
3048 auto operatorIterX = BinarySearchFindIndex(
3049 backGroundPositionOperators, ArraySize(backGroundPositionOperators), valueX.c_str());
3050 if (operatorIterX != -1) {
3051 backGroundPositionOperators[operatorIterX].value(backgroundImagePosition);
3052 }
3053 }
3054 if (valueY.find("px") != std::string::npos) {
3055 SetBgImgPositionY(BackgroundImagePositionType::PX, StringToDouble(valueY), backgroundImagePosition);
3056 } else if (valueY.find('%') != std::string::npos) {
3057 SetBgImgPositionY(BackgroundImagePositionType::PERCENT, StringToDouble(valueY), backgroundImagePosition);
3058 } else {
3059 auto operatorIterY = BinarySearchFindIndex(
3060 backGroundPositionOperators, ArraySize(backGroundPositionOperators), valueY.c_str());
3061 if (operatorIterY != -1) {
3062 backGroundPositionOperators[operatorIterY].value(backgroundImagePosition);
3063 }
3064 }
3065 } else {
3066 SetBgImgPosition(BackgroundImagePositionType::PERCENT, 50.0, backgroundImagePosition);
3067 if (value.find("px") != std::string::npos) {
3068 SetBgImgPositionX(BackgroundImagePositionType::PX, StringToDouble(value), backgroundImagePosition);
3069 } else if (value.find('%') != std::string::npos) {
3070 SetBgImgPositionX(BackgroundImagePositionType::PERCENT, StringToDouble(value), backgroundImagePosition);
3071 } else {
3072 auto operatorIter = BinarySearchFindIndex(
3073 backGroundPositionOperators, ArraySize(backGroundPositionOperators), value.c_str());
3074 if (operatorIter != -1) {
3075 backGroundPositionOperators[operatorIter].value(backgroundImagePosition);
3076 }
3077 }
3078 }
3079 backgroundStyle.backgroundImage->SetImagePosition(backgroundImagePosition.GetSizeTypeX(),
3080 backgroundImagePosition.GetSizeValueX(), backgroundImagePosition.GetSizeTypeY(),
3081 backgroundImagePosition.GetSizeValueY());
3082 declaration.hasDecorationStyle_ = true;
3083 }
3084
BindPipelineContext(const WeakPtr<PipelineContext> & pipelineContext)3085 void Declaration::BindPipelineContext(const WeakPtr<PipelineContext>& pipelineContext)
3086 {
3087 pipelineContext_ = pipelineContext;
3088 }
3089
ResetDefaultStyles()3090 void Declaration::ResetDefaultStyles()
3091 {
3092 auto& sizeStyle = static_cast<CommonSizeStyle&>(GetStyle(StyleTag::COMMON_SIZE_STYLE));
3093 if (sizeStyle.IsValid() && !sizeStyle.IsShared()) {
3094 sizeStyle.width = Dimension(-1.0, DimensionUnit::PX);
3095 sizeStyle.height = Dimension(-1.0, DimensionUnit::PX);
3096 sizeStyle.minWidth = Dimension(0.0);
3097 sizeStyle.minHeight = Dimension(0.0);
3098 sizeStyle.maxWidth = Dimension(Size::INFINITE_SIZE);
3099 sizeStyle.maxHeight = Dimension(Size::INFINITE_SIZE);
3100 sizeStyle.aspectRatio = -1.0;
3101 }
3102
3103 auto& paddingStyle = static_cast<CommonPaddingStyle&>(GetStyle(StyleTag::COMMON_PADDING_STYLE));
3104 if (paddingStyle.IsValid() && !paddingStyle.IsShared()) {
3105 paddingStyle.padding = Edge(Dimension(0.0));
3106 }
3107
3108 auto& marginStyle = static_cast<CommonMarginStyle&>(GetStyle(StyleTag::COMMON_MARGIN_STYLE));
3109 if (marginStyle.IsValid() && !marginStyle.IsShared()) {
3110 marginStyle.margin = Edge(Dimension(0.0));
3111 }
3112
3113 auto& flexStyle = static_cast<CommonFlexStyle&>(GetStyle(StyleTag::COMMON_FLEX_STYLE));
3114 if (flexStyle.IsValid() && !flexStyle.IsShared()) {
3115 flexStyle.flexGrow = 0.0;
3116 flexStyle.flexShrink = 1.0;
3117 flexStyle.flexBasis = 0.0_px;
3118 flexStyle.flexWeight = 0.0;
3119 flexStyle.displayIndex = 1;
3120 }
3121
3122 auto& opacityStyle = static_cast<CommonOpacityStyle&>(GetStyle(StyleTag::COMMON_OPACITY_STYLE));
3123 if (opacityStyle.IsValid() && !opacityStyle.IsShared()) {
3124 opacityStyle.opacity = 1.0;
3125 }
3126
3127 auto& displayStyle = static_cast<CommonDisplayStyle&>(GetStyle(StyleTag::COMMON_DISPLAY_STYLE));
3128 if (displayStyle.IsValid() && !displayStyle.IsShared()) {
3129 displayStyle.display = DisplayType::NO_SETTING;
3130 }
3131 hasDisplayStyle_ = false;
3132
3133 auto& visibilityStyle = static_cast<CommonVisibilityStyle&>(GetStyle(StyleTag::COMMON_VISIBILITY_STYLE));
3134 if (visibilityStyle.IsValid() && !visibilityStyle.IsShared()) {
3135 visibilityStyle.visibility = VisibilityType::NO_SETTING;
3136 }
3137
3138 auto& borderStyle = static_cast<CommonBorderStyle&>(GetStyle(StyleTag::COMMON_BORDER_STYLE));
3139 if (borderStyle.IsValid() && !borderStyle.IsShared()) {
3140 borderStyle.border.SetBorderEdge(BorderEdge(Color::BLACK, Dimension(), BorderStyle::SOLID));
3141 }
3142
3143 auto& borderImageStyle = static_cast<CommonBorderStyle&>(GetStyle(StyleTag::COMMON_BORDER_STYLE));
3144 if (borderImageStyle.IsValid() && !borderImageStyle.IsShared()) {
3145 borderImageStyle.border.SetBorderImageEdge(
3146 BorderImageEdge(Dimension(), Dimension(), Dimension(), BorderImageRepeat::STRETCH));
3147 }
3148
3149 auto& background = static_cast<CommonBackgroundStyle&>(GetStyle(StyleTag::COMMON_BACKGROUND_STYLE));
3150 if (background.IsValid() && !background.IsShared()) {
3151 background.gradient = Gradient();
3152 background.gradientBorderImage = Gradient();
3153 background.backgroundImage = AceType::MakeRefPtr<BackgroundImage>();
3154 background.borderImage = AceType::MakeRefPtr<BorderImage>();
3155 }
3156
3157 auto& renderAttr = static_cast<CommonRenderAttribute&>(GetAttribute(AttributeTag::COMMON_RENDER_ATTR));
3158 if (renderAttr.IsValid() && !renderAttr.IsShared() && !renderAttr.show.empty()) {
3159 hasDisplayStyle_ = true;
3160 SetShowAttr(renderAttr.show);
3161 }
3162
3163 backDecoration_ = AceType::MakeRefPtr<Decoration>();
3164 frontDecoration_ = AceType::MakeRefPtr<Decoration>();
3165 }
3166
3167 // Convert transform style to json format, such as rotate(50deg) to {"rotate":"50deg"}
GetTransformJsonValue(const std::string & value)3168 std::string Declaration::GetTransformJsonValue(const std::string& value)
3169 {
3170 auto rightIndex = value.find('(');
3171 auto leftIndex = value.find(')');
3172 std::string jsonValue = value;
3173
3174 if (rightIndex != std::string::npos && leftIndex != std::string::npos && (leftIndex - 1 - rightIndex > 0)) {
3175 std::string transformType = value.substr(0, rightIndex);
3176 std::string transformValue = value.substr(rightIndex + 1, leftIndex - 1 - rightIndex);
3177 jsonValue = "{\"" + transformType + "\":\"" + transformValue + "\"}";
3178 }
3179
3180 return jsonValue;
3181 }
3182
GetTransformType(const std::unique_ptr<JsonValue> & transformJson)3183 std::string Declaration::GetTransformType(const std::unique_ptr<JsonValue>& transformJson)
3184 {
3185 if (transformJson->IsNull()) {
3186 LOGE("transformJson is null");
3187 return "";
3188 }
3189 return transformJson->GetKey();
3190 }
3191
GetTransformTypeValue(const std::unique_ptr<JsonValue> & transformJson)3192 std::string Declaration::GetTransformTypeValue(const std::unique_ptr<JsonValue>& transformJson)
3193 {
3194 if (transformJson->IsNull()) {
3195 LOGE("transformJson is null");
3196 return "";
3197 }
3198 std::string jsonValue = transformJson->GetString();
3199 if (jsonValue.empty()) {
3200 double jsonDouble = transformJson->GetDouble();
3201 return std::to_string(jsonDouble);
3202 }
3203 return jsonValue;
3204 }
3205
GetThemeManager() const3206 RefPtr<ThemeManager> Declaration::GetThemeManager() const
3207 {
3208 auto context = pipelineContext_.Upgrade();
3209 if (!context) {
3210 return nullptr;
3211 }
3212 return context->GetThemeManager();
3213 }
3214
GetThemeConstants() const3215 RefPtr<ThemeConstants> Declaration::GetThemeConstants() const
3216 {
3217 auto themeManager = GetThemeManager();
3218 if (!themeManager) {
3219 return nullptr;
3220 }
3221 return themeManager->GetThemeConstants();
3222 }
3223
ParseColor(const std::string & value,uint32_t maskAlpha) const3224 Color Declaration::ParseColor(const std::string& value, uint32_t maskAlpha) const
3225 {
3226 auto themeConstants = GetThemeConstants();
3227 auto&& noRefFunc = [&value, maskAlpha = maskAlpha]() { return Color::FromString(value, maskAlpha); };
3228 auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetColor(refId); };
3229 return ParseThemeReference<Color>(value, noRefFunc, idRefFunc, Color::TRANSPARENT);
3230 }
3231
ParseDouble(const std::string & value) const3232 double Declaration::ParseDouble(const std::string& value) const
3233 {
3234 auto themeConstants = GetThemeConstants();
3235 auto&& noRefFunc = [&value]() { return StringUtils::StringToDouble(value); };
3236 auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDouble(refId); };
3237 return ParseThemeReference<double>(value, noRefFunc, idRefFunc, 0.0);
3238 }
3239
ParseDimension(const std::string & value,bool useVp) const3240 Dimension Declaration::ParseDimension(const std::string& value, bool useVp) const
3241 {
3242 auto themeConstants = GetThemeConstants();
3243 auto&& noRefFunc = [&value, useVp]() { return StringUtils::StringToDimension(value, useVp); };
3244 auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDimension(refId); };
3245 return ParseThemeReference<Dimension>(value, noRefFunc, idRefFunc, Dimension());
3246 }
3247
ParseCalcDimension(const std::string & value,bool useVp) const3248 CalcDimension Declaration::ParseCalcDimension(const std::string& value, bool useVp) const
3249 {
3250 if (value.find("calc") != std::string::npos) {
3251 return StringUtils::StringToCalcDimension(value, useVp);
3252 } else {
3253 return ParseDimension(value, useVp);
3254 }
3255 }
3256
ParseLineHeight(const std::string & value) const3257 Dimension Declaration::ParseLineHeight(const std::string& value) const
3258 {
3259 auto themeConstants = GetThemeConstants();
3260 const auto& parseResult = ThemeUtils::ParseThemeIdReference(value, themeConstants);
3261 if (!parseResult.parseSuccess) {
3262 return StringUtils::StringToDimension(value);
3263 }
3264 auto&& noRefFunc = [&value]() { return StringUtils::StringToDouble(value); };
3265 auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetDouble(refId); };
3266 auto lineHeightScale = ParseThemeReference<double>(value, noRefFunc, idRefFunc, 1.0);
3267 // If got 0.0 from ThemeConstants, use default 1.0
3268 lineHeightScale = NearZero(lineHeightScale) ? 1.0 : lineHeightScale;
3269 return Dimension(lineHeightScale, DimensionUnit::PERCENT);
3270 }
3271
ParseFontFamilies(const std::string & value) const3272 std::vector<std::string> Declaration::ParseFontFamilies(const std::string& value) const
3273 {
3274 std::vector<std::string> fontFamilies;
3275 std::stringstream stream(value);
3276 std::string fontFamily;
3277
3278 auto themeConstants = GetThemeConstants();
3279 auto&& idRefFunc = [constants = themeConstants](uint32_t refId) { return constants->GetString(refId); };
3280
3281 while (getline(stream, fontFamily, ',')) {
3282 auto&& noRefFunc = [&fontFamily]() { return fontFamily; };
3283 fontFamilies.emplace_back(ParseThemeReference<std::string>(fontFamily, noRefFunc, idRefFunc, fontFamily));
3284 }
3285 return fontFamilies;
3286 }
3287
ParsePreferFontSizes(const std::string & value) const3288 std::vector<Dimension> Declaration::ParsePreferFontSizes(const std::string& value) const
3289 {
3290 std::vector<Dimension> prefers;
3291 std::stringstream stream(value);
3292 std::string fontSize;
3293 while (getline(stream, fontSize, ',')) {
3294 prefers.emplace_back(ParseDimension(fontSize));
3295 }
3296 std::sort(prefers.begin(), prefers.end(),
3297 [](const Dimension& left, const Dimension& right) { return left.Value() > right.Value(); });
3298 return prefers;
3299 }
3300
ParseImageSrc(const std::string & imgSrc) const3301 std::string Declaration::ParseImageSrc(const std::string& imgSrc) const
3302 {
3303 return ThemeUtils::ProcessImageSource(imgSrc, GetThemeConstants());
3304 }
3305
IsRightToLeft() const3306 bool Declaration::IsRightToLeft() const
3307 {
3308 bool isRightToLeft = false;
3309 auto& commonAttr = static_cast<CommonAttribute&>(GetAttribute(AttributeTag::COMMON_ATTR));
3310 if (commonAttr.IsValid()) {
3311 isRightToLeft = commonAttr.isRightToLeft;
3312 }
3313 return isRightToLeft;
3314 }
3315
SetClickEvent(const EventMarker & onClick)3316 void Declaration::SetClickEvent(const EventMarker& onClick)
3317 {
3318 auto& gestureEvent = MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_GESTURE_EVENT);
3319 if (gestureEvent.IsValid()) {
3320 gestureEvent.click.eventMarker = onClick;
3321 gestureEvent.click.eventMarker.SetCatchMode(false);
3322 gestureEvent.click.isRefreshed = true;
3323 }
3324 }
3325
SetRemoteMessageEvent(const EventMarker & remoteMessage)3326 void Declaration::SetRemoteMessageEvent(const EventMarker& remoteMessage)
3327 {
3328 LOGI("Declaration::SetRemoteMessageEvent");
3329 auto& gestureEvent = MaybeResetEvent<CommonGestureEvent>(EventTag::COMMON_REMOTE_MESSAGE_GESTURE_EVENT);
3330 if (gestureEvent.IsValid()) {
3331 LOGI("Declaration::SetRemoteMessageEvent IsValid");
3332 gestureEvent.click.eventMarker = remoteMessage;
3333 gestureEvent.click.eventMarker.SetCatchMode(false);
3334 gestureEvent.click.isRefreshed = true;
3335 }
3336 }
3337
3338 } // namespace OHOS::Ace
3339