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  #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_NODE_H
17  #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_NODE_H
18  
19  #include <string>
20  #include <unordered_map>
21  
22  #include "base/geometry/transform_util.h"
23  #include "base/json/json_util.h"
24  #include "base/memory/ace_type.h"
25  #include "base/utils/macros.h"
26  #include "core/animation/animation_util.h"
27  #include "core/animation/keyframe_animation.h"
28  #include "core/animation/property_animation.h"
29  #include "core/components/box/box_component.h"
30  #include "core/components/common/properties/shadow.h"
31  #include "core/components/declaration/common/declaration_creator_manager.h"
32  #include "core/components/display/display_component.h"
33  #include "core/components/flex/flex_component.h"
34  #include "core/components/flex/flex_item_component.h"
35  #include "core/components/focusable/focusable_component.h"
36  #include "core/components/gesture_listener/gesture_listener_component.h"
37  #include "core/components/list/list_component.h"
38  #include "core/components/mouse_listener/mouse_listener_component.h"
39  #include "core/components/page_transition/page_transition_component.h"
40  #include "core/components/positioned/positioned_component.h"
41  #include "core/components/shared_transition/shared_transition_component.h"
42  #include "core/components/stack/stack_component.h"
43  #include "core/components/swiper/swiper_component.h"
44  #include "core/components/theme/theme_utils.h"
45  #include "core/components/touch_listener/touch_listener_component.h"
46  #include "core/components/transform/transform_component.h"
47  #include "core/components/transition/transition_component.h"
48  #include "core/components/tween/tween_component.h"
49  #include "core/pipeline/pipeline_context.h"
50  #include "frameworks/bridge/common/dom/dom_type.h"
51  #include "frameworks/bridge/common/utils/transform_convertor.h"
52  #include "frameworks/core/animation/animatable_data.h"
53  
54  #ifndef WEARABLE_PRODUCT
55  #include "core/components/multimodal/multimodal_component.h"
56  #endif
57  
58  namespace OHOS::Ace::Framework {
59  
60  // If no insertion location is specified, new child will be added to the end of children list by default.
61  inline constexpr int32_t DEFAULT_ITEM_INDEX = -1;
62  
63  class ACE_EXPORT DOMNode : public virtual AceType {
64      DECLARE_ACE_TYPE(DOMNode, AceType);
65  
66  public:
67      DOMNode(NodeId nodeId, const std::string& nodeName);
68      ~DOMNode() override;
69  
70      void SetAnimationStyle(const std::vector<std::unordered_map<std::string, std::string>>& animationKeyframes);
71      bool ParseAnimationStyle(const std::vector<std::unordered_map<std::string, std::string>>& animationKeyframes);
72      void SetSharedTransitionStyle(const std::vector<std::unordered_map<std::string, std::string>>& animationKeyframes);
73      bool ParseTransitionPropertyStyle(const std::string& transitionProperty);
74      bool ParseTransitionNameStyle(const std::string& transitionName);
75      void CreateDeclaration(const std::string& tag);
76      void TweenOptionSetKeyframes(TweenOption& tweenOption);
77      void SetCustomAnimationStyleUpdate(bool enable);
78      void AddNode(const RefPtr<DOMNode>& node, int32_t slot = DEFAULT_ITEM_INDEX);
79      void RemoveNode(const RefPtr<DOMNode>& node);
80      virtual void Mount(int32_t slot);
81      void GenerateComponentNode();
OnPageLoadFinish()82      virtual void OnPageLoadFinish() {}
83  
84      virtual void SetShowAttr(const std::string& showValue);
85  
GetScrollComponent()86      virtual const RefPtr<ScrollComponent>& GetScrollComponent() const
87      {
88          return scrollComponent_;
89      }
90  
91      void MarkNeedUpdate();
92  
SetTweenComponent(const RefPtr<TweenComponent> & tweenComponent)93      void SetTweenComponent(const RefPtr<TweenComponent>& tweenComponent)
94      {
95          tweenComponent_ = tweenComponent;
96          if (!displayComponent_) {
97              displayComponent_ = AceType::MakeRefPtr<DisplayComponent>(rootComponent_->GetChild());
98          }
99          if (!transformComponent_) {
100              transformComponent_ = AceType::MakeRefPtr<TransformComponent>();
101          }
102      }
103  
GetTweenComponent()104      RefPtr<TweenComponent> GetTweenComponent() const
105      {
106          return tweenComponent_;
107      }
108  
GetChildList()109      const std::list<RefPtr<DOMNode>>& GetChildList() const
110      {
111          return children_;
112      }
113  
GetDeclaration()114      const RefPtr<Declaration>& GetDeclaration() const
115      {
116          return declaration_;
117      }
118  
GetRootComponent()119      const RefPtr<ComposedComponent>& GetRootComponent() const
120      {
121          return rootComponent_;
122      }
123  
GetBoxComponent()124      const RefPtr<BoxComponent>& GetBoxComponent() const
125      {
126          return boxComponent_;
127      }
128  
GetDisplayComponent()129      const RefPtr<DisplayComponent>& GetDisplayComponent() const
130      {
131          return displayComponent_;
132      }
133  
GetTouchListenerComponent()134      const RefPtr<TouchListenerComponent>& GetTouchListenerComponent() const
135      {
136          return touchEventComponent_;
137      }
138  
GetMouseListenerComponent()139      const RefPtr<MouseListenerComponent>& GetMouseListenerComponent() const
140      {
141          return mouseEventComponent_;
142      }
143  
GetGestureListenerComponent()144      const RefPtr<GestureListenerComponent>& GetGestureListenerComponent() const
145      {
146          return gestureEventComponent_;
147      }
148  
GetFocusableComponent()149      const RefPtr<FocusableComponent>& GetFocusableComponent() const
150      {
151          return focusableEventComponent_;
152      }
153  
GetTransformComponent()154      const RefPtr<TransformComponent>& GetTransformComponent() const
155      {
156          return transformComponent_;
157      }
158  
SetShareId(const std::string & shareId)159      void SetShareId(const std::string& shareId)
160      {
161          shareId_ = shareId;
162          if (declaration_) {
163              declaration_->SetShareId(shareId);
164          }
165      }
166  
GetShareId()167      const std::string& GetShareId() const
168      {
169          return shareId_;
170      }
171  
172      const RefPtr<PageTransitionComponent>& BuildTransitionComponent();
173  
174      // Ensure DOMPopup can response onClick event even without defining it.
SetOnClick(const EventMarker & eventMarker)175      void SetOnClick(const EventMarker& eventMarker)
176      {
177          ACE_DCHECK(gestureEventComponent_);
178          gestureEventComponent_->SetOnClickId(eventMarker);
179      }
180  
SetOnDoubleClick(const EventMarker & eventMarker)181      void SetOnDoubleClick(const EventMarker& eventMarker)
182      {
183          ACE_DCHECK(gestureEventComponent_);
184          gestureEventComponent_->SetOnDoubleClickId(eventMarker);
185      }
186  
SetOnDragStart(const EventMarker & eventMarker)187      void SetOnDragStart(const EventMarker& eventMarker)
188      {
189          gestureEventComponent_->SetOnFreeDragStartId(eventMarker);
190      }
191  
SetOnDrag(const EventMarker & eventMarker)192      void SetOnDrag(const EventMarker& eventMarker)
193      {
194          gestureEventComponent_->SetOnFreeDragUpdateId(eventMarker);
195      }
196  
SetOnDragEnd(const EventMarker & eventMarker)197      void SetOnDragEnd(const EventMarker& eventMarker)
198      {
199          gestureEventComponent_->SetOnFreeDragEndId(eventMarker);
200      }
201  
SetOnLongPress(const EventMarker & eventMarker)202      void SetOnLongPress(const EventMarker& eventMarker)
203      {
204          ACE_DCHECK(gestureEventComponent_);
205          gestureEventComponent_->SetOnLongPressId(eventMarker);
206      }
207  
SetOnPinchStart(const EventMarker & eventMarker)208      void SetOnPinchStart(const EventMarker& eventMarker)
209      {
210          gestureEventComponent_->SetOnPinchStartId(eventMarker);
211      }
212  
SetOnPinchUpdate(const EventMarker & eventMarker)213      void SetOnPinchUpdate(const EventMarker& eventMarker)
214      {
215          gestureEventComponent_->SetOnPinchUpdateId(eventMarker);
216      }
217  
SetOnPinchEnd(const EventMarker & eventMarker)218      void SetOnPinchEnd(const EventMarker& eventMarker)
219      {
220          gestureEventComponent_->SetOnPinchEndId(eventMarker);
221      }
222  
SetOnPinchCancel(const EventMarker & eventMarker)223      void SetOnPinchCancel(const EventMarker& eventMarker)
224      {
225          gestureEventComponent_->SetOnPinchCancelId(eventMarker);
226      }
227  
SetIsRootNode(bool isRootNode)228      void SetIsRootNode(bool isRootNode)
229      {
230          isRootNode_ = isRootNode;
231      }
232  
IsRootNode()233      bool IsRootNode() const
234      {
235          return isRootNode_;
236      }
237  
SetIsTransition(bool isTransition)238      void SetIsTransition(bool isTransition)
239      {
240          isTransition_ = isTransition;
241      }
242  
GetIsTransition()243      bool GetIsTransition() const
244      {
245          return isTransition_;
246      }
247  
248      void SetOnFocusClick(const EventMarker& eventMarker);
249  
250      bool IsNodeDisabled() const;
251  
SetIsEnter(bool isEnter)252      void SetIsEnter(bool isEnter)
253      {
254          isEnter_ = isEnter;
255      }
256  
GetIsEnter()257      bool GetIsEnter() const
258      {
259          return isEnter_;
260      }
261  
GetNodeId()262      NodeId GetNodeId() const
263      {
264          return nodeId_;
265      }
266  
GetNodeIdForEvent()267      std::string GetNodeIdForEvent() const
268      {
269          return isRootNode_ ? DOM_DEFAULT_ROOT_NODE_ID : std::to_string(GetNodeId());
270      }
271  
GetTag()272      const std::string& GetTag() const
273      {
274          return tag_;
275      }
276  
SetProxyNode(bool isProxy)277      void SetProxyNode(bool isProxy)
278      {
279          isProxy_ = isProxy;
280      }
281  
IsProxy()282      bool IsProxy() const
283      {
284          return isProxy_;
285      }
286  
IsShow()287      bool IsShow() const
288      {
289          double opacity = 1.0;
290          if (declaration_) {
291              auto& opacityStyle =
292                  static_cast<CommonOpacityStyle&>(declaration_->GetStyle(StyleTag::COMMON_OPACITY_STYLE));
293              if (opacityStyle.IsValid()) {
294                  opacity = opacityStyle.opacity;
295              }
296          }
297          return visible_ == VisibleType::VISIBLE && !NearZero(opacity);
298      }
299  
GetBackGroundColor()300      Color GetBackGroundColor() const
301      {
302          Color color = Color::TRANSPARENT;
303          if (declaration_ && isTransitionColor_) {
304              auto& style =
305                  static_cast<CommonBackgroundStyle&>(declaration_->GetStyle(StyleTag::COMMON_BACKGROUND_STYLE));
306              if (style.IsValid()) {
307                  color = style.backgroundColor;
308              }
309          }
310          return color;
311      }
312  
GetOpacity()313      double GetOpacity() const
314      {
315          double opacity = 1.0;
316          if (declaration_) {
317              auto& style = static_cast<CommonOpacityStyle&>(declaration_->GetStyle(StyleTag::COMMON_OPACITY_STYLE));
318              if (style.IsValid()) {
319                  opacity = style.opacity;
320              }
321          }
322          return opacity;
323      }
324  
GetParentId()325      NodeId GetParentId() const
326      {
327          return parentId_;
328      }
329  
GetParentNode()330      RefPtr<DOMNode> GetParentNode() const
331      {
332          return parentNode_.Upgrade();
333      }
334  
335      bool IsTabbarSubNode() const;
336  
SetParentNode(const RefPtr<DOMNode> & parentNode)337      void SetParentNode(const RefPtr<DOMNode>& parentNode)
338      {
339          if (!parentNode) {
340              return;
341          }
342          parentNode_ = parentNode;
343          parentId_ = parentNode->GetNodeId();
344          parentTag_ = parentNode->GetTag();
345  
346          if (tag_ == DOM_NODE_TAG_SVG || parentNode->HasSvgTag()) {
347              hasSvgTag_ = true;
348          }
349      }
350  
GetPageId()351      int32_t GetPageId() const
352      {
353          return pageId_;
354      }
SetPageId(int32_t pageId)355      void SetPageId(int32_t pageId)
356      {
357          pageId_ = pageId;
358      }
359  
360      // Subclasses need to implement this interface to return their specialized components, which will be added to the
361      // last node of the component tree.
362      virtual RefPtr<Component> GetSpecializedComponent() = 0;
363  
SetPipelineContext(const WeakPtr<PipelineContext> & pipelineContext)364      void SetPipelineContext(const WeakPtr<PipelineContext>& pipelineContext)
365      {
366          pipelineContext_ = pipelineContext;
367      }
368  
GetPipelineContext()369      const WeakPtr<PipelineContext>& GetPipelineContext() const
370      {
371          return pipelineContext_;
372      }
373  
374      // Initialize node theme style when created.
InitializeStyle()375      virtual void InitializeStyle() {}
376  
377      void OnActive(bool isActive);
378  
HasPseudo()379      bool HasPseudo() const
380      {
381          return (cachedPseudoType_ != STATE_NORMAL);
382      }
383  
HasDisabledPseudo()384      bool HasDisabledPseudo() const
385      {
386          return ((cachedPseudoType_ & STATE_DISABLED) > 0);
387      }
388  
HasCheckedPseudo()389      bool HasCheckedPseudo() const
390      {
391          return ((cachedPseudoType_ & STATE_CHECKED) > 0);
392      }
393  
HasFocusPseudo()394      bool HasFocusPseudo() const
395      {
396          return ((cachedPseudoType_ & STATE_FOCUS) > 0);
397      }
398  
HasActivePseudo()399      bool HasActivePseudo() const
400      {
401          return ((cachedPseudoType_ & STATE_ACTIVE) > 0);
402      }
403  
HasSvgTag()404      bool HasSvgTag()
405      {
406          return hasSvgTag_;
407      }
408  
409      /*
410       * Parse color from string content and reference for id/attr, including format:
411       * #rrggbb, #aarrggbb, "@id001", "@attr_sys_color".
412       */
413      Color ParseColor(const std::string& value, uint32_t maskAlpha = COLOR_ALPHA_MASK) const;
414  
415      /*
416       * Parse double from string content and reference for id/attr, including format:
417       * 100.01, "@id001", "@attr_sys_alpha".
418       */
419      double ParseDouble(const std::string& value) const;
420  
421      /*
422       * Parse dimension from string content and reference for id/attr, including format:
423       * 10px, "@id001", "@attr_sys_dimension, expression".
424       */
425      CalcDimension ParseDimension(const std::string& value) const;
426  
427      /*
428       * Parse line height from string content and reference for id/attr, including format:
429       * 1.5, "@id001", "@attr_sys_line_height".
430       */
431      Dimension ParseLineHeight(const std::string& value) const;
432  
433      /*
434       * Parse font family list from string content and reference for id/attr, including format:
435       * sans-serif, "@id001", "@attr_sys_font_family".
436       */
437      std::vector<std::string> ParseFontFamilies(const std::string& value) const;
438  
439      /*
440       * Parse dimension list from string content and reference for id/attr, including format:
441       * 10px, "@id001", "@attr_sys_dimension".
442       */
443      std::vector<Dimension> ParsePreferFontSizes(const std::string& value) const;
444  
445      /*
446       * Parse image src from string content and reference for id/attr, including format:
447       * "@app.media.customized_image", "@sys.media.123".
448       */
449      std::string ParseImageSrc(const std::string& imgSrc) const;
450  
GetThemeManager()451      RefPtr<ThemeManager> GetThemeManager() const
452      {
453          auto context = pipelineContext_.Upgrade();
454          if (!context) {
455              return nullptr;
456          }
457          return context->GetThemeManager();
458      }
459  
460      template<typename T>
GetTheme()461      RefPtr<T> GetTheme() const
462      {
463          auto context = pipelineContext_.Upgrade();
464          if (!context) {
465              return nullptr;
466          }
467          auto themeManager = context->GetThemeManager();
468          if (!themeManager) {
469              return nullptr;
470          }
471          return themeManager->GetTheme<T>();
472      }
473  
474      virtual void UpdateStyleWithChildren();
475  
IsCustomComponent()476      bool IsCustomComponent() const
477      {
478          return isCustomComponent_;
479      }
480  
SetIsCustomComponent(bool isCustomComponent)481      void SetIsCustomComponent(bool isCustomComponent)
482      {
483          isCustomComponent_ = isCustomComponent;
484      }
485  
HasBorderRadiusStyle()486      bool HasBorderRadiusStyle() const
487      {
488          return declaration_ ? declaration_->HasBorderRadiusStyle() : false;
489      }
490  
HasOverflowStyle()491      bool HasOverflowStyle() const
492      {
493          return declaration_ ? declaration_->HasOverflowStyle() : false;
494      }
495  
IsBoxWrap()496      bool IsBoxWrap() const
497      {
498          return boxWrap_;
499      }
500  
SetBoxWrap(bool boxWrap)501      void SetBoxWrap(bool boxWrap)
502      {
503          boxWrap_ = boxWrap;
504      }
505  
506      void AdjustParamInLiteMode();
507  
AdjustSpecialParamInLiteMode()508      virtual void AdjustSpecialParamInLiteMode() {}
509  
510      virtual void SetAttr(const std::vector<std::pair<std::string, std::string>>& attrs);
511      virtual void SetStyle(const std::vector<std::pair<std::string, std::string>>& styles);
512      virtual void AddEvent(int32_t pageId, const std::vector<std::string>& events);
513      virtual void CallMethod(const std::string& method, const std::string& args);
514      virtual void OnRequestFocus(bool shouldFocus);
515      virtual void OnScrollBy(double dx, double dy, bool isSmooth);
516  
517      bool SetCurrentStyle(const std::pair<std::string, std::string>& style);
518  
IsRightToLeft()519      bool IsRightToLeft() const
520      {
521          return declaration_ ? declaration_->IsRightToLeft() : false;
522      }
523  
SetHeight(const CalcDimension & height)524      void SetHeight(const CalcDimension& height)
525      {
526          if (declaration_) {
527              auto& sizeStyle = declaration_->MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
528              if (sizeStyle.IsValid()) {
529                  sizeStyle.height = height;
530              }
531          }
532      }
533  
SetWidth(const CalcDimension & width)534      void SetWidth(const CalcDimension& width)
535      {
536          if (declaration_) {
537              auto& sizeStyle = declaration_->MaybeResetStyle<CommonSizeStyle>(StyleTag::COMMON_SIZE_STYLE);
538              if (sizeStyle.IsValid()) {
539                  sizeStyle.width = width;
540              }
541          }
542      }
543  
GetHeight()544      virtual Dimension GetHeight() const
545      {
546          Dimension height = Dimension(-1.0, DimensionUnit::PX);
547          if (declaration_) {
548              auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
549              if (sizeStyle.IsValid()) {
550                  height = sizeStyle.height;
551              }
552          }
553          return height;
554      }
555  
GetCalcHeight()556      virtual CalcDimension GetCalcHeight() const
557      {
558          CalcDimension height = Dimension(-1.0, DimensionUnit::PX);
559          if (declaration_) {
560              auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
561              if (sizeStyle.IsValid()) {
562                  height = sizeStyle.height;
563              }
564          }
565          return height;
566      }
567  
GetWidth()568      virtual Dimension GetWidth() const
569      {
570          Dimension width = Dimension(-1.0, DimensionUnit::PX);
571          if (declaration_) {
572              auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
573              if (sizeStyle.IsValid()) {
574                  width = sizeStyle.width;
575              }
576          }
577          return width;
578      }
579  
GetCalcWidth()580      virtual CalcDimension GetCalcWidth() const
581      {
582          CalcDimension width = Dimension(-1.0, DimensionUnit::PX);
583          if (declaration_) {
584              auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
585              if (sizeStyle.IsValid()) {
586                  width = sizeStyle.width;
587              }
588          }
589          return width;
590      }
591  
GetDisplay()592      DisplayType GetDisplay() const
593      {
594          DisplayType display = DisplayType::NO_SETTING;
595          if (declaration_) {
596              auto& displayStyle =
597                  static_cast<CommonDisplayStyle&>(declaration_->GetStyle(StyleTag::COMMON_DISPLAY_STYLE));
598              if (displayStyle.IsValid()) {
599                  display = displayStyle.display;
600              }
601          }
602          return display;
603      }
604  
SetDisplay(DisplayType type)605      void SetDisplay(DisplayType type)
606      {
607          if (declaration_) {
608              auto& displayStyle = declaration_->MaybeResetStyle<CommonDisplayStyle>(StyleTag::COMMON_DISPLAY_STYLE);
609              if (displayStyle.IsValid()) {
610                  displayStyle.display = type;
611              }
612          }
613      }
614  
GetPosition()615      PositionType GetPosition() const
616      {
617          PositionType position = PositionType::PTRELATIVE;
618          if (declaration_) {
619              auto& positionStyle =
620                  static_cast<CommonPositionStyle&>(declaration_->GetStyle(StyleTag::COMMON_POSITION_STYLE));
621              if (positionStyle.IsValid()) {
622                  position = positionStyle.position;
623              }
624          }
625          return position;
626      }
627  
GetImageFill()628      std::optional<Color> GetImageFill() const
629      {
630          std::optional<Color> imageFill = std::nullopt;
631          if (declaration_) {
632              auto& imageStyle = static_cast<CommonImageStyle&>(declaration_->GetStyle(StyleTag::COMMON_IMAGE_STYLE));
633              if (imageStyle.IsValid()) {
634                  imageFill = imageStyle.imageFill;
635              }
636          }
637          return imageFill;
638      }
639  
GetDirtyNodeId()640      virtual NodeId GetDirtyNodeId() const
641      {
642          return nodeId_;
643      }
644  
645  protected:
OnMounted(const RefPtr<DOMNode> & parentNode)646      virtual void OnMounted(const RefPtr<DOMNode>& parentNode) {}
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)647      virtual void OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot) {}
OnChildNodeRemoved(const RefPtr<DOMNode> & child)648      virtual void OnChildNodeRemoved(const RefPtr<DOMNode>& child) {}
OnSetStyleFinished()649      virtual void OnSetStyleFinished() {}
650      // Confirm declaration is exist before call GetClickId and GetLongPressId.
GetClickId()651      virtual const EventMarker& GetClickId()
652      {
653          static EventMarker defaultMarker;
654          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
655          return gestureEvent.IsValid() ? gestureEvent.click.eventMarker : defaultMarker;
656      };
GetDoubleClickId()657      virtual const EventMarker& GetDoubleClickId()
658      {
659          static EventMarker defaultMarker;
660          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
661          return gestureEvent.IsValid() ? gestureEvent.doubleClick.eventMarker : defaultMarker;
662      };
GetDragStartId()663      const EventMarker& GetDragStartId()
664      {
665          static EventMarker defaultMarker;
666          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
667          return gestureEvent.IsValid() ? gestureEvent.dragStart.eventMarker : defaultMarker;
668      };
GetDragId()669      const EventMarker& GetDragId()
670      {
671          static EventMarker defaultMarker;
672          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
673          return gestureEvent.IsValid() ? gestureEvent.drag.eventMarker : defaultMarker;
674      };
GetDragEndId()675      const EventMarker& GetDragEndId()
676      {
677          static EventMarker defaultMarker;
678          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
679          return gestureEvent.IsValid() ? gestureEvent.dragEnd.eventMarker : defaultMarker;
680      };
GetDragEnterId()681      const EventMarker& GetDragEnterId()
682      {
683          static EventMarker defaultMarker;
684          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
685          return gestureEvent.IsValid() ? gestureEvent.dragEnter.eventMarker : defaultMarker;
686      };
GetDragOverId()687      const EventMarker& GetDragOverId()
688      {
689          static EventMarker defaultMarker;
690          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
691          return gestureEvent.IsValid() ? gestureEvent.dragOver.eventMarker : defaultMarker;
692      };
GetDragLeaveId()693      const EventMarker& GetDragLeaveId()
694      {
695          static EventMarker defaultMarker;
696          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
697          return gestureEvent.IsValid() ? gestureEvent.dragLeave.eventMarker : defaultMarker;
698      };
GetDragDropId()699      const EventMarker& GetDragDropId()
700      {
701          static EventMarker defaultMarker;
702          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
703          return gestureEvent.IsValid() ? gestureEvent.dragDrop.eventMarker : defaultMarker;
704      };
GetLongPressId()705      virtual const EventMarker& GetLongPressId()
706      {
707          static EventMarker defaultMarker;
708          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
709          return gestureEvent.IsValid() ? gestureEvent.longPress.eventMarker : defaultMarker;
710      };
GetPinchStartId()711          const EventMarker& GetPinchStartId()
712      {
713          static EventMarker defaultMarker;
714          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
715          return gestureEvent.IsValid() ? gestureEvent.pinchStart.eventMarker : defaultMarker;
716      };
GetPinchUpdateId()717      const EventMarker& GetPinchUpdateId()
718      {
719          static EventMarker defaultMarker;
720          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
721          return gestureEvent.IsValid() ? gestureEvent.pinchUpdate.eventMarker : defaultMarker;
722      };
GetPinchEndId()723      const EventMarker& GetPinchEndId()
724      {
725          static EventMarker defaultMarker;
726          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
727          return gestureEvent.IsValid() ? gestureEvent.pinchEnd.eventMarker : defaultMarker;
728      };
GetPinchCancelId()729      const EventMarker& GetPinchCancelId()
730      {
731          static EventMarker defaultMarker;
732          auto& gestureEvent = static_cast<CommonGestureEvent&>(declaration_->GetEvent(EventTag::COMMON_GESTURE_EVENT));
733          return gestureEvent.IsValid() ? gestureEvent.pinchCancel.eventMarker : defaultMarker;
734      };
735  
GetSwipeId(uint32_t action,uint32_t stage)736      EventMarker& GetSwipeId(uint32_t action, uint32_t stage)
737      {
738          static EventMarker defaultMarker;
739          if (!declaration_) {
740              return defaultMarker;
741          }
742          auto& swipeEvent = declaration_->MaybeResetEvent<CommonSwipeEvent>(EventTag::COMMON_SWIPE_EVENT);
743          if (!swipeEvent.IsValid()) {
744              return defaultMarker;
745          }
746          if (action == EventAction::ON && stage == EventStage::CAPTURE) {
747              return swipeEvent.captureSwipe.eventMarker;
748          } else if (action == EventAction::CATCH && stage == EventStage::BUBBLE) {
749              return swipeEvent.catchBubbleSwipe.eventMarker;
750          } else if (action == EventAction::ON && stage == EventStage::BUBBLE) {
751              return swipeEvent.swipe.eventMarker;
752          }
753          return defaultMarker;
754      }
755  
756      // Confirm declaration exist and support raw event before call GetTouchId.
GetTouchId(uint32_t action,uint32_t stage,uint32_t type)757      EventMarker& GetTouchId(uint32_t action, uint32_t stage, uint32_t type)
758      {
759          static EventMarker defaultMarker;
760          if (!declaration_) {
761              return defaultMarker;
762          }
763  
764          auto& rawEvent = declaration_->MaybeResetEvent<CommonRawEvent>(EventTag::COMMON_RAW_EVENT);
765          if (!rawEvent.IsValid()) {
766              return defaultMarker;
767          }
768          if (action == EventAction::ON && stage == EventStage::CAPTURE && type == EventType::TOUCH_CANCEL) {
769              return rawEvent.captureTouchCancel.eventMarker;
770          } else if (action == EventAction::ON && stage == EventStage::CAPTURE && type == EventType::TOUCH_UP) {
771              return rawEvent.captureTouchEnd.eventMarker;
772          } else if (action == EventAction::ON && stage == EventStage::CAPTURE && type == EventType::TOUCH_MOVE) {
773              return rawEvent.captureTouchMove.eventMarker;
774          } else if (action == EventAction::ON && stage == EventStage::CAPTURE && type == EventType::TOUCH_DOWN) {
775              return rawEvent.captureTouchStart.eventMarker;
776          } else if (action == EventAction::CATCH && stage == EventStage::BUBBLE && type == EventType::TOUCH_CANCEL) {
777              return rawEvent.catchBubbleTouchCancel.eventMarker;
778          } else if (action == EventAction::CATCH && stage == EventStage::BUBBLE && type == EventType::TOUCH_UP) {
779              return rawEvent.catchBubbleTouchEnd.eventMarker;
780          } else if (action == EventAction::CATCH && stage == EventStage::BUBBLE && type == EventType::TOUCH_MOVE) {
781              return rawEvent.catchBubbleTouchMove.eventMarker;
782          } else if (action == EventAction::CATCH && stage == EventStage::BUBBLE && type == EventType::TOUCH_DOWN) {
783              return rawEvent.catchBubbleTouchStart.eventMarker;
784          } else if (action == EventAction::CATCH && stage == EventStage::CAPTURE && type == EventType::TOUCH_CANCEL) {
785              return rawEvent.catchCaptureTouchCancel.eventMarker;
786          } else if (action == EventAction::CATCH && stage == EventStage::CAPTURE && type == EventType::TOUCH_UP) {
787              return rawEvent.catchCaptureTouchEnd.eventMarker;
788          } else if (action == EventAction::CATCH && stage == EventStage::CAPTURE && type == EventType::TOUCH_MOVE) {
789              return rawEvent.catchCaptureTouchMove.eventMarker;
790          } else if (action == EventAction::CATCH && stage == EventStage::CAPTURE && type == EventType::TOUCH_DOWN) {
791              return rawEvent.catchCaptureTouchStart.eventMarker;
792          } else if (action == EventAction::ON && stage == EventStage::BUBBLE && type == EventType::TOUCH_CANCEL) {
793              return rawEvent.touchCancel.eventMarker;
794          } else if (action == EventAction::ON && stage == EventStage::BUBBLE && type == EventType::TOUCH_UP) {
795              return rawEvent.touchEnd.eventMarker;
796          } else if (action == EventAction::ON && stage == EventStage::BUBBLE && type == EventType::TOUCH_MOVE) {
797              return rawEvent.touchMove.eventMarker;
798          } else if (action == EventAction::ON && stage == EventStage::BUBBLE && type == EventType::TOUCH_DOWN) {
799              return rawEvent.touchStart.eventMarker;
800          }
801          return defaultMarker;
802      }
803  
804      // Subclasses need to implement this interface to composit specialized component into common components.
805      virtual RefPtr<Component> CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components);
806  
807      // Subclasses need to override this interface to update component with declaration.
UpdateSpecializedComponentWithDeclaration()808      virtual void UpdateSpecializedComponentWithDeclaration() {}
809  
810      // Subclasses need to override this interface to implement the dynamic creation of subclass specialized components.
PrepareSpecializedComponent()811      virtual void PrepareSpecializedComponent() {}
812  
813      virtual void CompositeComponents();
814  
815      virtual void UpdateBoxSize(const CalcDimension& width, const CalcDimension& height);
816      virtual void UpdateBoxPadding(const Edge& padding);
817      virtual void UpdateBoxBorder(const Border& border);
818      virtual void UpdatePropAnimations(const PropAnimationMap& animations);
819      virtual void UpdatePositionAnimations(const RefPtr<Component> component);
820  
821      // Subclasses need to override this interface to implement reset initialization style before any frontend style set.
ResetInitializedStyle()822      virtual void ResetInitializedStyle() {}
823  
IsLeafNode()824      virtual bool IsLeafNode() const
825      {
826          return false;
827      }
828  
829      void PrepareScrollComponent();
830  
GetLastCommonParent()831      RefPtr<SingleChild> GetLastCommonParent()
832      {
833          if (sharedTransitionComponent_) {
834              return sharedTransitionComponent_;
835          }
836          return boxComponent_;
837      }
838  
SetAlignment(const Alignment & align)839      void SetAlignment(const Alignment& align)
840      {
841          ACE_DCHECK(boxComponent_);
842          boxComponent_->SetAlignment(align);
843      }
844  
845      void OnChecked(bool isChecked);
846  
847      // Each subclass needs to override this function to obtain the properties. If it returns true, it means that the
848      // property has been consumed. If it returns false, it means it is handed over to the parent class.
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)849      virtual bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
850      {
851          return false;
852      }
853  
854      // Each subclass needs to override this function to obtain the style. If it returns true, it means that the
855      // style has been consumed. If it returns false, it means it is handed over to the parent class.
SetSpecializedStyle(const std::pair<std::string,std::string> & style)856      virtual bool SetSpecializedStyle(const std::pair<std::string, std::string>& style)
857      {
858          return false;
859      }
860  
861      // Each subclass needs to override this function to obtain the event. If it returns true, it means that the
862      // event has been consumed. If it returns false, it means it is handed over to the parent class.
AddSpecializedEvent(int32_t pageId,const std::string & event)863      virtual bool AddSpecializedEvent(int32_t pageId, const std::string& event)
864      {
865          return false;
866      }
867  
CallSpecializedMethod(const std::string & method,const std::string & args)868      virtual void CallSpecializedMethod(const std::string& method, const std::string& args) {}
869  
870      // When the multi-mode input subscript is set to auto, need to determine whether the current component has the
871      // ability to support the subscript.
IsSubscriptEnable()872      virtual bool IsSubscriptEnable() const
873      {
874          if (declaration_) {
875              declaration_->SetIsSubscriptEnable(false);
876          }
877          return false;
878      }
879  
880      WeakPtr<DOMNode> parentNode_;
881      NodeId parentId_ = -1;
882      bool isRootNode_ = false;
883      std::string parentTag_;
884      std::list<RefPtr<DOMNode>> children_;
885      // for pseudo class
886      std::vector<std::pair<std::string, std::string>> attributesCache_;
887      std::unordered_map<int32_t, std::unordered_map<std::string, std::string>> pseudoClassStyleMap_;
888      RefPtr<ComposedComponent> rootComponent_;
889      RefPtr<BoxComponent> boxComponent_;
890      RefPtr<ScrollComponent> scrollComponent_;
891      RefPtr<FlexItemComponent> flexItemComponent_;
892      RefPtr<TransformComponent> transformComponent_;
893      WeakPtr<PipelineContext> pipelineContext_;
894      RefPtr<Declaration> declaration_;
895      bool isCustomComponent_ = false;
896      bool boxWrap_ = false;
897  
898  private:
899      static void SetTransform(const std::string& value, DOMNode& node);
900      static void AddKeyframe(
901          double time, const std::string& typeValue, RefPtr<KeyframeAnimation<float>>& transformKeyframes);
902      static void AddKeyframe(double time, double typeValue, RefPtr<KeyframeAnimation<float>>& transformKeyframes);
903      std::string GetTransformJsonValue(const std::string& value);
904      std::string GetTransformType(const std::unique_ptr<JsonValue>& transformJson);
905      std::string GetTransformTypeValue(const std::unique_ptr<JsonValue>& transformJson);
906  
907      void CreatePropertyAnimation(const std::string& property);
908      RefPtr<KeyframeAnimation<float>> SetPropertyFloatAnimationKeyframe(float begin, float end);
909      RefPtr<KeyframeAnimation<Color>> SetPropertyColorAnimationKeyframe(const Color& begin, const Color& end);
910      void TransitionOptionSetKeyframes(TweenOption& tweenOption);
911      void SetDisplayStyle();
912  
913      void SetDisplayType();
914      void UpdateFlexItemComponent();
915      void UpdateUiComponents();
916      void UpdateBoxComponent();
917      void UpdateDisplayComponent();
918      void UpdateTweenComponent();
919      void UpdateTouchEventComponent();
920      void UpdateGestureEventComponent();
921      void UpdateMouseEventComponent();
922      void UpdateFocusableEventComponents();
923      void UpdatePositionComponent();
924      void UpdatePositionProps();
925      void UpdateTweenPosition(const RefPtr<TweenComponent> tweenComponent);
926  #ifndef WEARABLE_PRODUCT
927      void UpdateMultimodalComponent();
928  #endif
929  
930      void CachePseudoClassStyle(const std::pair<std::string, std::string>& pseudoClassStyle);
931      void UpdatePseudoStyle(bool isBackendChange);
932      void PrepareTouchEvent(EventMarker& eventMarker, uint32_t type);
933      void PrepareFocusableEventId();
934      void PrepareMouseHoverEvent();
935      void UpdatePseudoStyleByStatus(int32_t status, bool isBackendChange);
936      void ResetDefaultStyles();
937      uint32_t CalculatePseudoStatus() const;
938      void SetAttrInternal(const std::vector<std::pair<std::string, std::string>>& attrs);
939  
940      // for state update callbacks
941      void OnFocus(bool isFocus);
942      void OnHover(bool isHover);
943  
944      RefPtr<ThemeConstants> GetThemeConstants() const;
945  
946      template<typename T>
ParseThemeReference(const std::string & value,std::function<T ()> && noRefFunc,std::function<T (uint32_t refId)> && idRefFunc,const T & errorValue)947      T ParseThemeReference(const std::string& value, std::function<T()>&& noRefFunc,
948          std::function<T(uint32_t refId)>&& idRefFunc, const T& errorValue) const
949      {
950          const auto& parseResult = ThemeUtils::ParseThemeIdReference(value, GetThemeConstants());
951          if (!parseResult.parseSuccess) {
952              return noRefFunc();
953          }
954          auto themeConstants = GetThemeConstants();
955          if (!themeConstants) {
956              return errorValue;
957          }
958          // Refer to a theme id resource.
959          if (parseResult.isIdRef) {
960              return idRefFunc(parseResult.id);
961          }
962          // Refer to a theme attribute.
963          auto themeStyle = themeConstants->GetThemeStyle();
964          if (!themeStyle) {
965              return errorValue;
966          }
967          return themeStyle->GetAttr<T>(parseResult.refAttr, errorValue);
968      }
969  
970      bool isTransition_ = false;
971      bool isEnter_ = false;
972      bool isProxy_ = false;
973      bool animationStyleUpdated_ = false;
974      bool customAnimationStyleUpdated_ = false;
975      bool hasSvgTag_ = false;
976      VisibleType visible_ = VisibleType::VISIBLE;
977  
978      int32_t pageId_ = -1;
979      NodeId nodeId_ = -1;
980      std::string tag_;
981  
982      RefPtr<DisplayComponent> displayComponent_;
983      RefPtr<TouchListenerComponent> touchEventComponent_;
984      RefPtr<GestureListenerComponent> gestureEventComponent_;
985      RefPtr<FocusableComponent> focusableEventComponent_;
986      RefPtr<MouseListenerComponent> mouseEventComponent_;
987      RefPtr<PositionedComponent> positionComponent_;
988      RefPtr<SharedTransitionComponent> sharedTransitionComponent_;
989      RefPtr<TweenComponent> tweenComponent_;
990      RefPtr<TransitionComponent> propTransitionComponent_;
991      RefPtr<PageTransitionComponent> transitionComponent_;
992  
993      PropAnimationMap propAnimations_;
994      AnimationUtil animationUtil_;
995      RefPtr<KeyframeAnimation<float>> propertyWidthAnimation_;
996      RefPtr<KeyframeAnimation<float>> propertyHeightAnimation_;
997      RefPtr<KeyframeAnimation<Color>> propertyColorAnimation_;
998      std::string animationName_;
999      bool isActive_ = false;
1000      bool isFocus_ = false;
1001      uint32_t cachedPseudoType_ = STATE_NORMAL;
1002  
1003      TweenOption propTransitionOption_;
1004      std::string transitionPropertyName_ = "all";
1005      std::string transitionTimeFunction_ = "ease";
1006      int32_t transitionDuration_ = 0;
1007      int32_t transitionDelay_ = 0;
1008      bool isTransitionNameUpdateFirst_ = true;
1009      bool isTransitionDurationUpdateFirst_ = true;
1010      bool transitionStyleUpdated_ = false;
1011      bool isTransitionColor_ = false;
1012  
1013      // for shared transition
1014      std::string shareId_;
1015  #ifndef WEARABLE_PRODUCT
1016      // for multi modal input.
1017      RefPtr<MultimodalComponent> multimodalComponent_;
1018  #endif
1019  };
1020  
1021  } // namespace OHOS::Ace::Framework
1022  
1023  #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_NODE_H
1024