Skip to content

Commit 3170873

Browse files
ShouruiSongjianliang00
authored andcommitted
[Optimize][Element] Promote shared FiberElement helpers to Element base
This change promotes common helper APIs from FiberElement into the Element base class so they can be reused by other element types and keeps FiberElement focused on Fiber-specific behavior. - Add virtual parent traversal, async resolve state, CSS scope, and attribute/config accessors on Element and reuse them in FiberElement. - Move root_virtual_parent to Element and update BlockElement to work with the generic virtual_parent pointer on the base class. - Remove duplicated helpers from FiberElement now covered by the Element base, reducing maintenance overhead and API divergence. AutoSubmit:True
1 parent 30b40b4 commit 3170873

File tree

5 files changed

+171
-121
lines changed

5 files changed

+171
-121
lines changed

core/renderer/dom/element.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,5 +2194,13 @@ void Element::UpdateGlobalInsertionOrder() {
21942194
global_insertion_order_ = element_manager()->GenerateGlobalInsertionOrder();
21952195
}
21962196

2197+
Element* Element::root_virtual_parent() {
2198+
Element* root_virtual = virtual_parent_;
2199+
while (root_virtual && root_virtual->virtual_parent() != nullptr) {
2200+
root_virtual = root_virtual->virtual_parent();
2201+
}
2202+
return root_virtual;
2203+
}
2204+
21972205
} // namespace tasm
21982206
} // namespace lynx

core/renderer/dom/element.h

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,70 @@ class Element : public lepus::RefCounted,
308308
virtual void SetEventHandler(const base::String& name, EventHandler* handler);
309309
virtual void ResetEventHandlers();
310310

311-
// For js/lepus/worklet event handlers
311+
/**
312+
* Element API for adding js event
313+
* @param name the binding event's name
314+
* @param type the binding event's type
315+
* @param callback the binding event's corresponding js function name
316+
*/
312317
void SetJSEventHandler(const base::String& name, const base::String& type,
313318
const base::String& callback);
319+
320+
/**
321+
* Element API for adding lepus event
322+
* @param name the binding event's name
323+
* @param type the binding event's type
324+
* @param script the binding event's corresponding lepus script
325+
* @param callback the binding event's corresponding lepus function
326+
*/
314327
void SetLepusEventHandler(const base::String& name, const base::String& type,
315328
const lepus::Value& script,
316329
const lepus::Value& callback);
330+
331+
/**
332+
* Element API for adding worklet event
333+
* @param name the binding worklet event's name
334+
* @param type the binding worklet event's type
335+
* @param worklet_info the binding worklet info, passed to the front-end
336+
* @param ctx the context of Lepus / LepusNg
337+
* framework
338+
*/
317339
void SetWorkletEventHandler(const base::String& name,
318340
const base::String& type,
319341
const lepus::Value& worklet_info,
320342
lepus::Context* ctx);
343+
344+
/**
345+
* Element API for removing specific event
346+
* @param name the removed event's name
347+
* @param type the removed event's type
348+
*/
321349
void RemoveEvent(const base::String& name, const base::String& type);
350+
351+
/**
352+
* Element API for removing all events
353+
*/
322354
void RemoveAllEvents();
323355

324-
// For config op
356+
/**
357+
* Element API for adding config.
358+
* @param key the config key,
359+
* @param value the config value.
360+
*/
325361
void AddConfig(const base::String& key, const lepus::Value& value);
362+
363+
/**
364+
* Element API for setting config.
365+
* @param config the config will be setted,
366+
*/
326367
void SetConfig(const lepus::Value& config);
368+
369+
/**
370+
* A key function to get element's config.
371+
* The returned value is constant. You should not get Table() from
372+
* the value and change configs. Use AddConfig() instead which will
373+
* guarantee this element creates a writable config table.
374+
*/
327375
const lepus::Value config() const;
328376

329377
// For gesture handler
@@ -414,6 +462,115 @@ class Element : public lepus::RefCounted,
414462
bool IsRadonArch() const { return arch_type_ == RadonArch; }
415463
bool IsFiberArch() const { return arch_type_ == FiberArch; }
416464

465+
// Element type checking methods
466+
virtual bool is_none() const { return false; }
467+
virtual bool is_block() const { return false; }
468+
virtual bool is_if() const { return false; }
469+
virtual bool is_for() const { return false; }
470+
bool is_inline_element() const { return is_inline_element_; }
471+
472+
// Virtual parent node access methods (for AirModeFiber)
473+
void set_virtual_parent(Element* virtual_parent) {
474+
virtual_parent_ = virtual_parent;
475+
}
476+
Element* virtual_parent() { return virtual_parent_; }
477+
Element* root_virtual_parent();
478+
479+
// Parent component unique ID access methods
480+
int64_t GetParentComponentUniqueIdForFiber() {
481+
return parent_component_unique_id_;
482+
}
483+
484+
void SetParentComponentUniqueIdForFiber(int64_t id) {
485+
if (id != parent_component_unique_id_) {
486+
parent_component_element_ = nullptr;
487+
}
488+
parent_component_unique_id_ = id;
489+
}
490+
491+
bool IsInSameCSSScope(Element* element) {
492+
return css_id_ == element->css_id_;
493+
}
494+
495+
// This interface is currently only used by the inspector. The inspector
496+
// determines whether an element is created by the itself by checking whether
497+
// element has a data model. Since the data model of a fiber element is not
498+
// empty by default, this interface is provided to the inspector to reset the
499+
// data model and mark the element as created by the inspector.
500+
void ResetDataModel() { data_model_ = nullptr; }
501+
502+
void MarkCanBeLayoutOnly(bool flag) { can_be_layout_only_ = flag; }
503+
504+
// Async resolve status query methods
505+
void UpdateResolveStatus(AsyncResolveStatus value) {
506+
resolve_status_ = value;
507+
}
508+
509+
bool IsAsyncResolveInvoked() {
510+
return resolve_status_ != AsyncResolveStatus::kCreated &&
511+
resolve_status_ != AsyncResolveStatus::kUpdated;
512+
}
513+
514+
bool IsAsyncResolveResolving() {
515+
return resolve_status_ == AsyncResolveStatus::kResolving ||
516+
resolve_status_ == AsyncResolveStatus::kResolved ||
517+
resolve_status_ == AsyncResolveStatus::kPreparing ||
518+
resolve_status_ == AsyncResolveStatus::kSyncResolving;
519+
}
520+
521+
bool flush_required() { return flush_required_; }
522+
523+
inline bool ShouldProcessParallelTasks() {
524+
return is_parallel_flush() ||
525+
resolve_status_ == AsyncResolveStatus::kSyncResolving;
526+
}
527+
528+
inline bool ShouldResolveStyle() {
529+
return !IsAsyncResolveResolving() && ((dirty_ & ~kDirtyTree) != 0);
530+
}
531+
532+
inline void EnqueueReduceTask(base::MoveOnlyClosure<void> operation) {
533+
parallel_reduce_tasks_->emplace_back(std::move(operation));
534+
}
535+
536+
inline bool IsAsyncFlushRoot() const { return is_async_flush_root_; }
537+
inline void MarkAsyncFlushRoot(bool value) { is_async_flush_root_ = value; }
538+
539+
// Data model accessor methods
540+
const ClassList& classes() { return data_model_->classes(); }
541+
542+
ClassList ReleaseClasses() { return data_model_->ReleaseClasses(); }
543+
544+
const base::String& GetIdSelector() { return data_model_->idSelector(); }
545+
546+
const DataMap& dataset() { return data_model_->dataset(); }
547+
548+
// Check has_value() before usage to avoid unintentional construction.
549+
const auto& builtin_attr_map() const { return builtin_attr_map_; }
550+
551+
// Check has_value() before usage to avoid unintentional construction.
552+
const auto& updated_attr_map() const { return updated_attr_map_; }
553+
554+
void set_style_sheet_manager(
555+
const std::shared_ptr<CSSStyleSheetManager>& manager) {
556+
css_style_sheet_manager_ = manager;
557+
}
558+
559+
const std::shared_ptr<CSSStyleSheetManager>& style_sheet_manager() {
560+
return css_style_sheet_manager_;
561+
}
562+
563+
void set_attached_to_layout_parent(bool has) {
564+
attached_to_layout_parent_ = has;
565+
}
566+
bool attached_to_layout_parent() const { return attached_to_layout_parent_; }
567+
568+
void UpdateAttrMap(const base::String& key, const lepus::Value& value) {
569+
updated_attr_map_[key] = value;
570+
}
571+
572+
void MarkAttrDirtyForPseudoElement() { dirty_ |= kDirtyAttr; }
573+
417574
void UpdateLayout(float left, float top, float width, float height,
418575
const std::array<float, 4>& paddings,
419576
const std::array<float, 4>& margins,

core/renderer/dom/fiber/block_element.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void BlockElement::RemoveAllBlockNodes() {
5454

5555
size_t BlockElement::FindInsertIndex(const fml::RefPtr<FiberElement> &child) {
5656
size_t offset = 0;
57-
FiberElement *virtual_parent = this->virtual_parent();
57+
FiberElement *virtual_parent =
58+
static_cast<FiberElement *>(this->virtual_parent());
5859
FiberElement *current = this;
5960

6061
// If there are multiple virtual nodes from child to the parent node, it is
@@ -77,7 +78,8 @@ size_t BlockElement::FindInsertIndex(const fml::RefPtr<FiberElement> &child) {
7778
}
7879
}
7980
current = virtual_parent;
80-
virtual_parent = virtual_parent->virtual_parent();
81+
virtual_parent =
82+
static_cast<FiberElement *>(virtual_parent->virtual_parent());
8183
offset = offset + idx;
8284
}
8385

core/renderer/dom/fiber/fiber_element.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,14 +3869,6 @@ void FiberElement::SetCSSID(int32_t id) {
38693869
}
38703870
}
38713871

3872-
FiberElement *FiberElement::root_virtual_parent() {
3873-
FiberElement *root_virtual = static_cast<FiberElement *>(virtual_parent_);
3874-
while (root_virtual && root_virtual->virtual_parent() != nullptr) {
3875-
root_virtual = root_virtual->virtual_parent();
3876-
}
3877-
return root_virtual;
3878-
}
3879-
38803872
void FiberElement::ResetSheetRecursively(
38813873
const std::shared_ptr<CSSStyleSheetManager> &manager) {
38823874
if (is_page() || is_component() || css_id_ != kInvalidCssId) {

0 commit comments

Comments
 (0)