Selaa lähdekoodia

LibHTML: A tiny bit of work towards block layout.

Andreas Kling 6 vuotta sitten
vanhempi
commit
e423bb4901
2 muutettua tiedostoa jossa 21 lisäystä ja 1 poistoa
  1. 5 1
      Libraries/LibHTML/CSS/StyleValue.h
  2. 16 0
      Libraries/LibHTML/Layout/LayoutBlock.cpp

+ 5 - 1
Libraries/LibHTML/CSS/StyleValue.h

@@ -24,6 +24,7 @@ public:
     bool is_initial() const { return type() == Type::Initial; }
 
     virtual String to_string() const = 0;
+    virtual Length to_length() const { return {}; }
 
 protected:
     explicit StyleValue(Type);
@@ -60,7 +61,10 @@ public:
     }
     virtual ~LengthStyleValue() override {}
 
-    String to_string() const override { return m_length.to_string(); }
+    virtual String to_string() const override { return m_length.to_string(); }
+    virtual Length to_length() const override { return m_length; }
+
+    const Length& length() const { return m_length; }
 
 private:
     explicit LengthStyleValue(const Length& length)

+ 16 - 0
Libraries/LibHTML/Layout/LayoutBlock.cpp

@@ -50,6 +50,22 @@ void LayoutBlock::compute_width()
 
     dbg() << " Left: " << margin_left->to_string() << "+" << border_left->to_string() << "+" << padding_left->to_string();
     dbg() << "Right: " << margin_right->to_string() << "+" << border_right->to_string() << "+" << padding_right->to_string();
+
+    int total_px = 0;
+    for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
+        total_px += value->to_length().to_px();
+    }
+
+    dbg() << "Total: " << total_px;
+
+    // 10.3.3 Block-level, non-replaced elements in normal flow
+    // If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.
+    if (width->to_length().is_auto() && total_px > containing_block()->rect().width()) {
+        if (margin_left->to_length().is_auto())
+            margin_left = zero_value;
+        if (margin_right->to_length().is_auto())
+            margin_right = zero_value;
+    }
 }
 
 void LayoutBlock::compute_height()