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
Notes: sideshowbarker 2024-07-17 19:45:34 +09:00
31 changed files with 48 additions and 91 deletions

View file

@ -451,11 +451,6 @@ void Document::update_style()
set_needs_layout();
}
RefPtr<Layout::Node> Document::create_layout_node()
{
return adopt_ref(*new Layout::InitialContainingBlock(*this, style_computer().create_document_style()));
}
void Document::set_link_color(Color color)
{
m_link_color = color;

View file

@ -309,9 +309,6 @@ public:
private:
explicit Document(const AK::URL&);
// ^DOM::Node
virtual RefPtr<Layout::Node> create_layout_node() override;
// ^HTML::GlobalEventHandlers
virtual EventTarget& global_event_handlers_to_event_target() final { return *this; }

View file

@ -130,15 +130,10 @@ bool Element::has_class(const FlyString& class_name, CaseSensitivity case_sensit
});
}
RefPtr<Layout::Node> Element::create_layout_node()
RefPtr<Layout::Node> Element::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
auto style = document().style_computer().compute_style(*this);
const_cast<Element&>(*this).m_specified_css_values = style;
auto display = style->display();
if (display.is_none())
return nullptr;
if (local_name() == "noscript" && document().is_scripting_enabled())
return nullptr;

View file

@ -90,7 +90,8 @@ public:
String name() const { return attribute(HTML::AttributeNames::name); }
const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
CSS::StyleProperties const* specified_css_values() const { return m_specified_css_values.ptr(); }
void set_specified_css_values(RefPtr<CSS::StyleProperties> style) { m_specified_css_values = move(style); }
NonnullRefPtr<CSS::StyleProperties> computed_style();
const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
@ -128,8 +129,9 @@ public:
NonnullRefPtr<Geometry::DOMRect> get_bounding_client_rect() const;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
protected:
RefPtr<Layout::Node> create_layout_node() override;
virtual void children_changed() override;
private:

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
@ -145,11 +145,6 @@ void Node::set_text_content(String const& content)
set_needs_style_update(true);
}
RefPtr<Layout::Node> Node::create_layout_node()
{
return nullptr;
}
void Node::invalidate_style()
{
for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -105,8 +105,6 @@ public:
NonnullRefPtr<NodeList> child_nodes();
NonnullRefPtrVector<Node> children_as_vector() const;
virtual RefPtr<Layout::Node> create_layout_node();
virtual FlyString node_name() const = 0;
String descendant_text_content() const;

View file

@ -30,11 +30,6 @@ EventTarget* ShadowRoot::get_parent(const Event& event)
return host();
}
RefPtr<Layout::Node> ShadowRoot::create_layout_node()
{
return adopt_ref(*new Layout::BlockContainer(document(), this, CSS::ComputedValues {}));
}
// https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
String ShadowRoot::inner_html() const
{

View file

@ -34,7 +34,6 @@ public:
private:
// ^Node
virtual FlyString node_name() const override { return "#shadow-root"; }
virtual RefPtr<Layout::Node> create_layout_node() override;
// NOTE: The specification doesn't seem to specify a default value for closed. Assuming false for now.
bool m_closed { false };

View file

@ -19,11 +19,6 @@ Text::~Text()
{
}
RefPtr<Layout::Node> Text::create_layout_node()
{
return adopt_ref(*new Layout::TextNode(document(), *this));
}
// https://dom.spec.whatwg.org/#dom-text-text
NonnullRefPtr<Text> Text::create_with_global_object(Bindings::WindowObject& window, String const& data)
{

View file

@ -28,8 +28,6 @@ public:
void set_always_editable(bool b) { m_always_editable = b; }
private:
virtual RefPtr<Layout::Node> create_layout_node() override;
bool m_always_editable { false };
};

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 };

View file

@ -90,7 +90,26 @@ void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context&
return;
}
auto layout_node = dom_node.create_layout_node();
auto& document = dom_node.document();
auto& style_computer = document.style_computer();
RefPtr<Layout::Node> layout_node;
if (is<DOM::Element>(dom_node)) {
auto& element = static_cast<DOM::Element&>(dom_node);
auto style = style_computer.compute_style(element);
if (style->display().is_none())
return;
element.set_specified_css_values(style);
layout_node = element.create_layout_node(move(style));
} else if (is<DOM::Document>(dom_node)) {
auto style = style_computer.create_document_style();
layout_node = adopt_ref(*new Layout::InitialContainingBlock(static_cast<DOM::Document&>(dom_node), move(style)));
} else if (is<DOM::Text>(dom_node)) {
layout_node = adopt_ref(*new Layout::TextNode(document, static_cast<DOM::Text&>(dom_node)));
} else if (is<DOM::ShadowRoot>(dom_node)) {
layout_node = adopt_ref(*new Layout::BlockContainer(document, &static_cast<DOM::ShadowRoot&>(dom_node), CSS::ComputedValues {}));
}
if (!layout_node)
return;

View file

@ -16,11 +16,8 @@ SVGGElement::SVGGElement(DOM::Document& document, QualifiedName qualified_name)
{
}
RefPtr<Layout::Node> SVGGElement::create_layout_node()
RefPtr<Layout::Node> SVGGElement::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::SVGGraphicsBox(document(), *this, move(style)));
}

View file

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

View file

@ -443,11 +443,8 @@ SVGPathElement::SVGPathElement(DOM::Document& document, QualifiedName qualified_
{
}
RefPtr<Layout::Node> SVGPathElement::create_layout_node()
RefPtr<Layout::Node> SVGPathElement::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::SVGPathBox(document(), *this, move(style)));
}

View file

@ -89,7 +89,7 @@ public:
SVGPathElement(DOM::Document&, QualifiedName);
virtual ~SVGPathElement() override = default;
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual void parse_attribute(const FlyString& name, const String& value) override;

View file

@ -19,11 +19,8 @@ SVGSVGElement::SVGSVGElement(DOM::Document& document, QualifiedName qualified_na
{
}
RefPtr<Layout::Node> SVGSVGElement::create_layout_node()
RefPtr<Layout::Node> SVGSVGElement::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::SVGSVGBox(document(), *this, move(style)));
}

View file

@ -18,7 +18,7 @@ public:
SVGSVGElement(DOM::Document&, QualifiedName);
virtual RefPtr<Layout::Node> create_layout_node() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
unsigned width() const;
unsigned height() const;