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

LibWeb: Make the layout tree GC-allocated

This removes a set of complex reference cycles between DOM, layout tree
and browsing context.

It also makes lifetimes much easier to reason about, as the DOM and
layout trees are now free to keep each other alive.
This commit is contained in:
Andreas Kling 2022-10-17 14:41:50 +02:00
parent 83c5ff57d8
commit 268b9c5d90
72 changed files with 258 additions and 207 deletions

View file

@ -503,7 +503,7 @@ String BrowsingContext::selected_text() const
auto selection = layout_root->selection().normalized();
if (selection.start().layout_node == selection.end().layout_node) {
if (selection.start().layout_node.ptr() == selection.end().layout_node) {
if (!is<Layout::TextNode>(*selection.start().layout_node))
return "";
return verify_cast<Layout::TextNode>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node);
@ -518,7 +518,7 @@ String BrowsingContext::selected_text() const
// Middle nodes
layout_node = layout_node->next_in_pre_order();
while (layout_node && layout_node != selection.end().layout_node) {
while (layout_node && layout_node.ptr() != selection.end().layout_node) {
if (is<Layout::TextNode>(*layout_node))
builder.append(verify_cast<Layout::TextNode>(*layout_node).text_for_rendering());
else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockContainer>(*layout_node))
@ -528,7 +528,7 @@ String BrowsingContext::selected_text() const
}
// End node
VERIFY(layout_node == selection.end().layout_node);
VERIFY(layout_node.ptr() == selection.end().layout_node);
if (is<Layout::TextNode>(*layout_node)) {
auto& text = verify_cast<Layout::TextNode>(*layout_node).text_for_rendering();
builder.append(text.substring(0, selection.end().index_in_node));
@ -573,7 +573,13 @@ void BrowsingContext::select_all()
last_layout_node_index_in_node = text_for_rendering.length() - 1;
}
layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
auto start = Layout::LayoutPosition {
JS::make_handle(const_cast<Layout::Node*>(first_layout_node)), 0
};
auto end = Layout::LayoutPosition {
JS::make_handle(const_cast<Layout::Node*>(last_layout_node)), last_layout_node_index_in_node
};
layout_root->set_selection({ move(start), move(end) });
}
void BrowsingContext::register_viewport_client(ViewportClient& client)

View file

@ -18,9 +18,9 @@ HTMLBRElement::HTMLBRElement(DOM::Document& document, DOM::QualifiedName qualifi
HTMLBRElement::~HTMLBRElement() = default;
RefPtr<Layout::Node> HTMLBRElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLBRElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
return adopt_ref(*new Layout::BreakNode(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::BreakNode>(document(), *this, move(style));
}
}

View file

@ -16,7 +16,7 @@ class HTMLBRElement final : public HTMLElement {
public:
virtual ~HTMLBRElement() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
private:
HTMLBRElement(DOM::Document&, DOM::QualifiedName);

View file

@ -78,9 +78,9 @@ void HTMLCanvasElement::set_height(unsigned value)
reset_context_to_default_state();
}
RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLCanvasElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
return adopt_ref(*new Layout::CanvasBox(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::CanvasBox>(document(), *this, move(style));
}
HTMLCanvasElement::HasOrCreatedContext HTMLCanvasElement::create_2d_context()

View file

@ -42,7 +42,7 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
enum class HasOrCreatedContext {
No,

View file

@ -22,9 +22,9 @@ HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, DOM::QualifiedName
HTMLIFrameElement::~HTMLIFrameElement() = default;
RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLIFrameElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
return adopt_ref(*new Layout::FrameBox(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::FrameBox>(document(), *this, move(style));
}
void HTMLIFrameElement::parse_attribute(FlyString const& name, String const& value)

View file

@ -16,7 +16,7 @@ class HTMLIFrameElement final : public BrowsingContextContainer {
public:
virtual ~HTMLIFrameElement() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
bool will_lazy_load_element() const;

View file

@ -85,9 +85,9 @@ void HTMLImageElement::parse_attribute(FlyString const& name, String const& valu
}
}
RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
return heap().allocate_without_realm<Layout::ImageBox>(document(), *this, move(style), m_image_loader);
}
Gfx::Bitmap const* HTMLImageElement::bitmap() const

View file

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

View file

@ -51,21 +51,21 @@ void HTMLInputElement::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_selected_files);
}
RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
if (type_state() == TypeAttributeState::Hidden)
return nullptr;
if (type_state() == TypeAttributeState::SubmitButton || type_state() == TypeAttributeState::Button || type_state() == TypeAttributeState::ResetButton || type_state() == TypeAttributeState::FileUpload)
return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::ButtonBox>(document(), *this, move(style));
if (type_state() == TypeAttributeState::Checkbox)
return adopt_ref(*new Layout::CheckBox(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::CheckBox>(document(), *this, move(style));
if (type_state() == TypeAttributeState::RadioButton)
return adopt_ref(*new Layout::RadioButton(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::RadioButton>(document(), *this, move(style));
return adopt_ref(*new Layout::BlockContainer(document(), this, move(style)));
return heap().allocate_without_realm<Layout::BlockContainer>(document(), this, move(style));
}
void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)

View file

@ -48,7 +48,7 @@ class HTMLInputElement final
public:
virtual ~HTMLInputElement() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
enum class TypeAttributeState {
#define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,

View file

@ -18,9 +18,9 @@ HTMLLabelElement::HTMLLabelElement(DOM::Document& document, DOM::QualifiedName q
HTMLLabelElement::~HTMLLabelElement() = default;
RefPtr<Layout::Node> HTMLLabelElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLLabelElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
return adopt_ref(*new Layout::Label(document(), this, move(style)));
return heap().allocate_without_realm<Layout::Label>(document(), this, move(style));
}
}

View file

@ -16,7 +16,7 @@ class HTMLLabelElement final : public HTMLElement {
public:
virtual ~HTMLLabelElement() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
String for_() const { return attribute(HTML::AttributeNames::for_); }

View file

@ -39,7 +39,7 @@ String HTMLObjectElement::data() const
return document().parse_url(data).to_string();
}
RefPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
switch (m_representation) {
case Representation::Children:
@ -49,7 +49,7 @@ RefPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::St
return nullptr;
case Representation::Image:
if (m_image_loader.has_value() && m_image_loader->has_image())
return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), *m_image_loader));
return heap().allocate_without_realm<Layout::ImageBox>(document(), *this, move(style), *m_image_loader);
break;
default:
break;

View file

@ -46,7 +46,7 @@ public:
private:
HTMLObjectElement(DOM::Document&, DOM::QualifiedName);
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
bool has_ancestor_media_element_or_object_element_not_showing_fallback_content() const;

View file

@ -23,11 +23,11 @@ HTMLProgressElement::HTMLProgressElement(DOM::Document& document, DOM::Qualified
HTMLProgressElement::~HTMLProgressElement() = default;
RefPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
if (style->appearance().value_or(CSS::Appearance::Auto) == CSS::Appearance::None)
return HTMLElement::create_layout_node(style);
return adopt_ref(*new Layout::Progress(document(), *this, move(style)));
return heap().allocate_without_realm<Layout::Progress>(document(), *this, move(style));
}
bool HTMLProgressElement::using_system_appearance() const

View file

@ -16,7 +16,7 @@ class HTMLProgressElement final : public HTMLElement {
public:
virtual ~HTMLProgressElement() override;
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
double value() const;
void set_value(double);