diff --git a/Libraries/LibHTML/CSS/StyleValue.h b/Libraries/LibHTML/CSS/StyleValue.h
index c5bc72947a..dd42aa7a19 100644
--- a/Libraries/LibHTML/CSS/StyleValue.h
+++ b/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)
diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp
index be97a2597f..5705fafd03 100644
--- a/Libraries/LibHTML/Layout/LayoutBlock.cpp
+++ b/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()