Ver código fonte

LibWeb: Move position and text-align to LayoutStyle

Andreas Kling 5 anos atrás
pai
commit
959464fce4

+ 10 - 10
Libraries/LibWeb/Layout/LayoutBlock.cpp

@@ -187,7 +187,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
         line_box.trim_trailing_whitespace();
     }
 
-    auto text_align = this->text_align();
+    auto text_align = style().text_align();
     float min_line_height = specified_style().line_height(*this);
     float line_spacing = min_line_height - specified_style().font().glyph_height();
     float content_height = 0;
@@ -616,24 +616,24 @@ LayoutBlock::ShrinkToFitResult LayoutBlock::calculate_shrink_to_fit_width()
 
 void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBlock& block)
 {
-    auto& style = block.specified_style();
+    auto& specified_style = block.specified_style();
     auto zero_value = Length::make_px(0);
     auto& containing_block = *this;
     auto& box = block.box_model();
 
-    box.margin.top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
-    box.margin.bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
-    box.border.top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
-    box.border.bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
-    box.padding.top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
-    box.padding.bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
+    box.margin.top = specified_style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
+    box.margin.bottom = specified_style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
+    box.border.top = specified_style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
+    box.border.bottom = specified_style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
+    box.padding.top = specified_style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
+    box.padding.bottom = specified_style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
 
     float x = box.margin.left.to_px(*this)
         + box.border.left.to_px(*this)
         + box.padding.left.to_px(*this)
         + box.offset.left.to_px(*this);
 
-    if (text_align() == CSS::TextAlign::VendorSpecificCenter) {
+    if (this->style().text_align() == CSS::TextAlign::VendorSpecificCenter) {
         x = (containing_block.width() / 2) - block.width() / 2;
     }
 
@@ -642,7 +642,7 @@ void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBl
 
     auto* relevant_sibling = block.previous_sibling();
     while (relevant_sibling != nullptr) {
-        if (relevant_sibling->position() != CSS::Position::Absolute)
+        if (relevant_sibling->style().position() != CSS::Position::Absolute)
             break;
         relevant_sibling = relevant_sibling->previous_sibling();
     }

+ 1 - 1
Libraries/LibWeb/Layout/LayoutBox.cpp

@@ -323,7 +323,7 @@ bool LayoutBox::establishes_stacking_context() const
         return false;
     if (node() == document().root())
         return true;
-    auto position = this->position();
+    auto position = style().position();
     auto z_index = style().z_index();
     if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
         if (z_index.has_value())

+ 6 - 6
Libraries/LibWeb/Layout/LayoutNode.cpp

@@ -57,7 +57,7 @@ void LayoutNode::layout(LayoutMode layout_mode)
 
 bool LayoutNode::can_contain_boxes_with_position_absolute() const
 {
-    return position() != CSS::Position::Static || is_root();
+    return style().position() != CSS::Position::Static || is_root();
 }
 
 const LayoutBlock* LayoutNode::containing_block() const
@@ -72,7 +72,7 @@ const LayoutBlock* LayoutNode::containing_block() const
     if (is_text())
         return nearest_block_ancestor();
 
-    auto position = this->position();
+    auto position = style().position();
 
     if (position == CSS::Position::Absolute) {
         auto* ancestor = parent();
@@ -199,7 +199,7 @@ bool LayoutNode::is_absolutely_positioned() const
 {
     if (!has_style())
         return false;
-    auto position = this->position();
+    auto position = style().position();
     return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
 }
 
@@ -207,7 +207,7 @@ bool LayoutNode::is_fixed_position() const
 {
     if (!has_style())
         return false;
-    auto position = this->position();
+    auto position = style().position();
     return position == CSS::Position::Fixed;
 }
 
@@ -223,8 +223,8 @@ void LayoutNodeWithStyle::apply_style(const StyleProperties& specified_style)
 {
     auto& style = static_cast<MutableLayoutStyle&>(m_style);
 
-    m_position = specified_style.position();
-    m_text_align = specified_style.text_align();
+    style.set_position(specified_style.position());
+    style.set_text_align(specified_style.text_align());
     style.set_z_index(specified_style.z_index());
 }
 

+ 0 - 19
Libraries/LibWeb/Layout/LayoutNode.h

@@ -192,8 +192,6 @@ public:
 
     const StyleProperties& specified_style() const;
     const ImmutableLayoutStyle& style() const;
-    CSS::Position position() const;
-    CSS::TextAlign text_align() const;
 
     LayoutNodeWithStyle* parent();
     const LayoutNodeWithStyle* parent() const;
@@ -263,9 +261,6 @@ public:
 
     const ImmutableLayoutStyle& style() const { return static_cast<const ImmutableLayoutStyle&>(m_style); }
 
-    CSS::Position position() const { return m_position; }
-    CSS::TextAlign text_align() const { return m_text_align; }
-
 protected:
     explicit LayoutNodeWithStyle(const Node*, NonnullRefPtr<StyleProperties>);
 
@@ -308,20 +303,6 @@ inline const ImmutableLayoutStyle& LayoutNode::style() const
     return parent()->style();
 }
 
-inline CSS::Position LayoutNode::position() const
-{
-    if (m_has_style)
-        return static_cast<const LayoutNodeWithStyle*>(this)->position();
-    return parent()->position();
-}
-
-inline CSS::TextAlign LayoutNode::text_align() const
-{
-    if (m_has_style)
-        return static_cast<const LayoutNodeWithStyle*>(this)->text_align();
-    return parent()->text_align();
-}
-
 inline const LayoutNodeWithStyle* LayoutNode::parent() const
 {
     return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());

+ 7 - 0
Libraries/LibWeb/Layout/LayoutStyle.h

@@ -27,15 +27,20 @@
 #pragma once
 
 #include <AK/Optional.h>
+#include <LibWeb/CSS/StyleValue.h>
 
 namespace Web {
 
 class LayoutStyle {
 public:
     Optional<int> z_index() const { return m_z_index; }
+    CSS::TextAlign text_align() const { return m_text_align; }
+    CSS::Position position() const { return m_position; }
 
 protected:
     Optional<int> m_z_index;
+    CSS::TextAlign m_text_align;
+    CSS::Position m_position;
 };
 
 class ImmutableLayoutStyle final : public LayoutStyle {
@@ -44,6 +49,8 @@ class ImmutableLayoutStyle final : public LayoutStyle {
 class MutableLayoutStyle final : public LayoutStyle {
 public:
     void set_z_index(Optional<int> value) { m_z_index = value; }
+    void set_text_align(CSS::TextAlign text_align) { m_text_align = text_align; }
+    void set_position(CSS::Position position) { m_position = position; }
 };
 
 }

+ 1 - 1
Libraries/LibWeb/Layout/LineBox.cpp

@@ -35,7 +35,7 @@ namespace Web {
 
 void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length, int width, int height)
 {
-    bool text_align_is_justify = layout_node.text_align() == CSS::TextAlign::Justify;
+    bool text_align_is_justify = layout_node.style().text_align() == CSS::TextAlign::Justify;
     if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
         // The fragment we're adding is from the last LayoutNode on the line.
         // Expand the last fragment instead of adding a new one with the same LayoutNode.