1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:37:36 +00:00

LibWeb: Compute element style in Layout::TreeBuilder

Instead of making each Layout::Node compute style for itself, we now
compute it in TreeBuilder before even calling create_layout_node().

For non-element DOM nodes, we create the style and layout tree node
in TreeBuilder. This allows us to move create_layout_node() from
DOM::Node to DOM::Element.
This commit is contained in:
Andreas Kling 2022-02-05 13:17:01 +01:00
parent 3451673ac8
commit 7e1bf4d300
31 changed files with 48 additions and 91 deletions

View file

@ -19,11 +19,8 @@ HTMLBRElement::~HTMLBRElement()
{
}
RefPtr<Layout::Node> HTMLBRElement::create_layout_node()
RefPtr<Layout::Node> HTMLBRElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
return adopt_ref(*new Layout::BreakNode(document(), *this, move(style)));
}

View file

@ -17,7 +17,7 @@ public:
HTMLBRElement(DOM::Document&, QualifiedName);
virtual ~HTMLBRElement() override;
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
};
}

View file

@ -47,11 +47,8 @@ void HTMLCanvasElement::set_height(unsigned value)
set_attribute(HTML::AttributeNames::height, String::number(value));
}
RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node()
RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
return adopt_ref(*new Layout::CanvasBox(document(), *this, move(style)));
}

View file

@ -34,7 +34,7 @@ public:
String to_data_url(const String& type, Optional<double> quality) const;
private:
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
RefPtr<Gfx::Bitmap> m_bitmap;
RefPtr<CanvasRenderingContext2D> m_context;

View file

@ -22,9 +22,8 @@ HTMLIFrameElement::~HTMLIFrameElement()
{
}
RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node()
RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
return adopt_ref(*new Layout::FrameBox(document(), *this, move(style)));
}

View file

@ -17,7 +17,7 @@ public:
HTMLIFrameElement(DOM::Document&, QualifiedName);
virtual ~HTMLIFrameElement() override;
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
private:
virtual void inserted() override;

View file

@ -68,11 +68,8 @@ void HTMLImageElement::parse_attribute(const FlyString& name, const String& valu
m_image_loader.load(document().parse_url(value));
}
RefPtr<Layout::Node> HTMLImageElement::create_layout_node()
RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
}

View file

@ -33,7 +33,7 @@ private:
void animate();
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
ImageLoader m_image_loader;
};

View file

@ -41,15 +41,11 @@ void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
}
}
RefPtr<Layout::Node> HTMLInputElement::create_layout_node()
RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
if (type() == "hidden")
return nullptr;
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
if (type().equals_ignoring_case("submit") || type().equals_ignoring_case("button"))
return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));

View file

@ -20,7 +20,7 @@ public:
HTMLInputElement(DOM::Document&, QualifiedName);
virtual ~HTMLInputElement() override;
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
String type() const { return attribute(HTML::AttributeNames::type); }
String default_value() const { return attribute(HTML::AttributeNames::value); }

View file

@ -19,12 +19,8 @@ HTMLLabelElement::~HTMLLabelElement()
{
}
RefPtr<Layout::Node> HTMLLabelElement::create_layout_node()
RefPtr<Layout::Node> HTMLLabelElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
auto layout_node = adopt_ref(*new Layout::Label(document(), this, move(style)));
layout_node->set_inline(true);
return layout_node;

View file

@ -17,7 +17,7 @@ public:
HTMLLabelElement(DOM::Document&, QualifiedName);
virtual ~HTMLLabelElement() override;
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
String for_() const { return attribute(HTML::AttributeNames::for_); }
};

View file

@ -41,14 +41,10 @@ void HTMLObjectElement::parse_attribute(const FlyString& name, const String& val
m_image_loader.load(document().parse_url(value));
}
RefPtr<Layout::Node> HTMLObjectElement::create_layout_node()
RefPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
if (m_should_show_fallback_content)
return HTMLElement::create_layout_node();
auto style = document().style_computer().compute_style(*this);
if (style->display().is_none())
return nullptr;
return HTMLElement::create_layout_node(move(style));
if (m_image_loader.has_image())
return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
return nullptr;

View file

@ -26,7 +26,7 @@ public:
String type() const { return attribute(HTML::AttributeNames::type); }
private:
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
ImageLoader m_image_loader;
bool m_should_show_fallback_content { false };