LayoutBlock.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <LibHTML/CSS/StyledNode.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/Layout/LayoutBlock.h>
  4. LayoutBlock::LayoutBlock(const Node* node, const StyledNode* styled_node)
  5. : LayoutNode(node, styled_node)
  6. {
  7. }
  8. LayoutBlock::~LayoutBlock()
  9. {
  10. }
  11. LayoutNode& LayoutBlock::inline_wrapper()
  12. {
  13. if (!last_child() || !last_child()->is_block()) {
  14. append_child(adopt(*new LayoutBlock(nullptr, nullptr)));
  15. }
  16. return *last_child();
  17. }
  18. void LayoutBlock::layout()
  19. {
  20. compute_width();
  21. LayoutNode::layout();
  22. compute_height();
  23. }
  24. void LayoutBlock::compute_width()
  25. {
  26. if (!styled_node()) {
  27. // I guess the size is "auto" in this case.
  28. return;
  29. }
  30. auto& styled_node = *this->styled_node();
  31. auto auto_value = Length();
  32. auto zero_value = Length(0, Length::Type::Absolute);
  33. auto length_or_fallback = [&](const StringView& property_name, const Length& fallback) {
  34. auto value = styled_node.property(property_name);
  35. if (!value.has_value())
  36. return fallback;
  37. return value.value()->to_length();
  38. };
  39. auto width = length_or_fallback("width", auto_value);
  40. auto margin_left = length_or_fallback("margin-left", zero_value);
  41. auto margin_right = length_or_fallback("margin-right", zero_value);
  42. auto border_left = length_or_fallback("border-left", zero_value);
  43. auto border_right = length_or_fallback("border-right", zero_value);
  44. auto padding_left = length_or_fallback("padding-left", zero_value);
  45. auto padding_right = length_or_fallback("padding-right", zero_value);
  46. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  47. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  48. int total_px = 0;
  49. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  50. total_px += value.to_px();
  51. }
  52. dbg() << "Total: " << total_px;
  53. // 10.3.3 Block-level, non-replaced elements in normal flow
  54. // 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.
  55. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  56. if (margin_left.is_auto())
  57. margin_left = zero_value;
  58. if (margin_right.is_auto())
  59. margin_right = zero_value;
  60. }
  61. // 10.3.3 cont'd.
  62. auto underflow_px = containing_block()->rect().width() - total_px;
  63. if (width.is_auto()) {
  64. if (margin_left.is_auto())
  65. margin_left = zero_value;
  66. if (margin_right.is_auto())
  67. margin_right = zero_value;
  68. if (underflow_px >= 0) {
  69. width = Length(underflow_px, Length::Type::Absolute);
  70. } else {
  71. width = zero_value;
  72. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  73. }
  74. } else {
  75. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  76. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  77. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  78. margin_right = Length(underflow_px, Length::Type::Absolute);
  79. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  80. margin_left = Length(underflow_px, Length::Type::Absolute);
  81. } else { // margin_left.is_auto() && margin_right.is_auto()
  82. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  83. margin_left = half_of_the_underflow;
  84. margin_right = half_of_the_underflow;
  85. }
  86. }
  87. rect().set_width(width.to_px());
  88. style().margin().left = margin_left;
  89. style().margin().right = margin_right;
  90. style().border().left = border_left;
  91. style().border().right = border_right;
  92. style().padding().left = padding_left;
  93. style().padding().right = padding_right;
  94. }
  95. void LayoutBlock::compute_height()
  96. {
  97. }