From f88c5860df7ac20f8583c6b1be991842d850f808 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 24 Jul 2019 07:34:07 +0200 Subject: [PATCH] LibHTML: Fetch the box edge values needed for block width computation. --- Libraries/LibHTML/CSS/Length.h | 7 +++++++ Libraries/LibHTML/CSS/StyledNode.h | 13 +++++++++++-- Libraries/LibHTML/Layout/LayoutBlock.cpp | 21 +++++++++++++++++++++ Libraries/LibHTML/Layout/LayoutNode.h | 3 +++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Libraries/LibHTML/CSS/Length.h b/Libraries/LibHTML/CSS/Length.h index 0dc8c5a54a..c0dcefb628 100644 --- a/Libraries/LibHTML/CSS/Length.h +++ b/Libraries/LibHTML/CSS/Length.h @@ -29,6 +29,13 @@ public: return String::format("%d [Length/Absolute]", m_value); } + int to_px() const + { + if (is_auto()) + return 0; + return m_value; + } + private: Type m_type { Type::Auto }; int m_value { 0 }; diff --git a/Libraries/LibHTML/CSS/StyledNode.h b/Libraries/LibHTML/CSS/StyledNode.h index 0cef5f3f26..3f5e74a295 100644 --- a/Libraries/LibHTML/CSS/StyledNode.h +++ b/Libraries/LibHTML/CSS/StyledNode.h @@ -1,10 +1,11 @@ #pragma once +#include #include #include -#include -#include +#include #include +#include class Node; @@ -50,6 +51,14 @@ public: m_property_values.set(name, move(value)); } + Optional> property(const String& name) const + { + auto it = m_property_values.find(name); + if (it == m_property_values.end()) + return {}; + return it->value; + } + Display display() const; protected: diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp index 7e76b7d91f..be97a2597f 100644 --- a/Libraries/LibHTML/Layout/LayoutBlock.cpp +++ b/Libraries/LibHTML/Layout/LayoutBlock.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -29,6 +30,26 @@ void LayoutBlock::layout() void LayoutBlock::compute_width() { + if (!styled_node()) { + // I guess the size is "auto" in this case. + return; + } + + auto auto_value= LengthStyleValue::create({}); + auto& styled_node = *this->styled_node(); + auto width = styled_node.property("width").value_or(auto_value); + + auto zero_value = LengthStyleValue::create(Length(0, Length::Type::Absolute)); + + auto margin_left = styled_node.property("margin-left").value_or(zero_value); + auto margin_right = styled_node.property("margin-right").value_or(zero_value); + auto border_left = styled_node.property("border-left").value_or(zero_value); + auto border_right = styled_node.property("border-right").value_or(zero_value); + auto padding_left = styled_node.property("padding-left").value_or(zero_value); + auto padding_right = styled_node.property("padding-right").value_or(zero_value); + + 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(); } void LayoutBlock::compute_height() diff --git a/Libraries/LibHTML/Layout/LayoutNode.h b/Libraries/LibHTML/Layout/LayoutNode.h index a48e11d422..938fd6cd7b 100644 --- a/Libraries/LibHTML/Layout/LayoutNode.h +++ b/Libraries/LibHTML/Layout/LayoutNode.h @@ -49,6 +49,9 @@ public: virtual LayoutNode& inline_wrapper() { return *this; } + StyledNode* styled_node() { return m_styled_node; } + const StyledNode* styled_node() const { return m_styled_node; } + protected: explicit LayoutNode(const Node*, const StyledNode*);