Pārlūkot izejas kodu

LibWeb: Keep track of the parent of each formatting context

This will allow us to find the containing block formatting context
when needed later on.
Andreas Kling 4 gadi atpakaļ
vecāks
revīzija
b1e75437c9

+ 1 - 1
Libraries/LibWeb/DOM/Document.cpp

@@ -352,7 +352,7 @@ void Document::layout()
         m_layout_root = static_ptr_cast<Layout::InitialContainingBlockBox>(tree_builder.build(*this));
     }
 
-    Layout::BlockFormattingContext root_formatting_context(*m_layout_root);
+    Layout::BlockFormattingContext root_formatting_context(*m_layout_root, nullptr);
     root_formatting_context.run(Layout::LayoutMode::Default);
 
     m_layout_root->set_needs_display();

+ 3 - 3
Libraries/LibWeb/Layout/BlockFormattingContext.cpp

@@ -37,8 +37,8 @@
 
 namespace Web::Layout {
 
-BlockFormattingContext::BlockFormattingContext(Box& context_box)
-    : FormattingContext(context_box)
+BlockFormattingContext::BlockFormattingContext(Box& context_box, FormattingContext* parent)
+    : FormattingContext(context_box, parent)
 {
 }
 
@@ -393,7 +393,7 @@ void BlockFormattingContext::compute_height(Box& box)
 
 void BlockFormattingContext::layout_inline_children(LayoutMode layout_mode)
 {
-    InlineFormattingContext context(context_box());
+    InlineFormattingContext context(context_box(), this);
     context.run(layout_mode);
 }
 

+ 1 - 1
Libraries/LibWeb/Layout/BlockFormattingContext.h

@@ -33,7 +33,7 @@ namespace Web::Layout {
 
 class BlockFormattingContext : public FormattingContext {
 public:
-    explicit BlockFormattingContext(Box& containing_block);
+    explicit BlockFormattingContext(Box&, FormattingContext* parent);
     ~BlockFormattingContext();
 
     virtual void run(LayoutMode) override;

+ 6 - 5
Libraries/LibWeb/Layout/FormattingContext.cpp

@@ -32,8 +32,9 @@
 
 namespace Web::Layout {
 
-FormattingContext::FormattingContext(Box& context_box)
-    : m_context_box(context_box)
+FormattingContext::FormattingContext(Box& context_box, FormattingContext* parent)
+    : m_parent(parent)
+    , m_context_box(context_box)
 {
 }
 
@@ -44,13 +45,13 @@ FormattingContext::~FormattingContext()
 void FormattingContext::layout_inside(Box& box, LayoutMode layout_mode)
 {
     if (box.is_table()) {
-        TableFormattingContext context(box);
+        TableFormattingContext context(box, this);
         context.run(layout_mode);
     } else if (box.children_are_inline()) {
-        InlineFormattingContext context(box);
+        InlineFormattingContext context(box, this);
         context.run(layout_mode);
     } else {
-        BlockFormattingContext context(box);
+        BlockFormattingContext context(box, this);
         context.run(layout_mode);
     }
 }

+ 6 - 2
Libraries/LibWeb/Layout/FormattingContext.h

@@ -37,11 +37,14 @@ public:
     Box& context_box() { return m_context_box; }
     const Box& context_box() const { return m_context_box; }
 
+    FormattingContext* parent() { return m_parent; }
+    const FormattingContext* parent() const { return m_parent; }
+
 protected:
-    FormattingContext(Box&);
+    FormattingContext(Box&, FormattingContext* parent = nullptr);
     virtual ~FormattingContext();
 
-    static void layout_inside(Box&, LayoutMode);
+    void layout_inside(Box&, LayoutMode);
 
     struct ShrinkToFitResult {
         float preferred_width { 0 };
@@ -50,6 +53,7 @@ protected:
 
     ShrinkToFitResult calculate_shrink_to_fit_widths(Box&);
 
+    FormattingContext* m_parent { nullptr };
     Box& m_context_box;
 };
 

+ 4 - 3
Libraries/LibWeb/Layout/InlineFormattingContext.cpp

@@ -28,6 +28,7 @@
 #include <LibWeb/DOM/Node.h>
 #include <LibWeb/Dump.h>
 #include <LibWeb/Layout/BlockBox.h>
+#include <LibWeb/Layout/BlockFormattingContext.h>
 #include <LibWeb/Layout/Box.h>
 #include <LibWeb/Layout/InlineFormattingContext.h>
 #include <LibWeb/Layout/InlineNode.h>
@@ -35,8 +36,8 @@
 
 namespace Web::Layout {
 
-InlineFormattingContext::InlineFormattingContext(Box& containing_block)
-    : FormattingContext(containing_block)
+InlineFormattingContext::InlineFormattingContext(Box& containing_block, FormattingContext* parent)
+    : FormattingContext(containing_block, parent)
 {
 }
 
@@ -178,7 +179,7 @@ void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_
             inline_block.set_width(inline_block.style().width().to_px(inline_block));
         }
 
-        FormattingContext::layout_inside(inline_block, layout_mode);
+        layout_inside(inline_block, layout_mode);
 
         if (inline_block.style().height().is_undefined_or_auto()) {
             // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.

+ 1 - 1
Libraries/LibWeb/Layout/InlineFormattingContext.h

@@ -33,7 +33,7 @@ namespace Web::Layout {
 
 class InlineFormattingContext final : public FormattingContext {
 public:
-    InlineFormattingContext(Box& containing_block);
+    InlineFormattingContext(Box& containing_block, FormattingContext* parent);
     ~InlineFormattingContext();
 
     virtual void run(LayoutMode) override;

+ 2 - 2
Libraries/LibWeb/Layout/TableFormattingContext.cpp

@@ -38,8 +38,8 @@
 
 namespace Web::Layout {
 
-TableFormattingContext::TableFormattingContext(Box& context_box)
-    : BlockFormattingContext(context_box)
+TableFormattingContext::TableFormattingContext(Box& context_box, FormattingContext* parent)
+    : BlockFormattingContext(context_box, parent)
 {
 }
 

+ 1 - 1
Libraries/LibWeb/Layout/TableFormattingContext.h

@@ -33,7 +33,7 @@ namespace Web::Layout {
 
 class TableFormattingContext final : public BlockFormattingContext {
 public:
-    explicit TableFormattingContext(Box& containing_block);
+    explicit TableFormattingContext(Box&, FormattingContext* parent);
     ~TableFormattingContext();
 
     virtual void run(LayoutMode) override;