LayoutBlock.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 auto_value= LengthStyleValue::create({});
  31. auto& styled_node = *this->styled_node();
  32. auto width = styled_node.property("width").value_or(auto_value);
  33. auto zero_value = LengthStyleValue::create(Length(0, Length::Type::Absolute));
  34. auto margin_left = styled_node.property("margin-left").value_or(zero_value);
  35. auto margin_right = styled_node.property("margin-right").value_or(zero_value);
  36. auto border_left = styled_node.property("border-left").value_or(zero_value);
  37. auto border_right = styled_node.property("border-right").value_or(zero_value);
  38. auto padding_left = styled_node.property("padding-left").value_or(zero_value);
  39. auto padding_right = styled_node.property("padding-right").value_or(zero_value);
  40. dbg() << " Left: " << margin_left->to_string() << "+" << border_left->to_string() << "+" << padding_left->to_string();
  41. dbg() << "Right: " << margin_right->to_string() << "+" << border_right->to_string() << "+" << padding_right->to_string();
  42. int total_px = 0;
  43. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  44. total_px += value->to_length().to_px();
  45. }
  46. dbg() << "Total: " << total_px;
  47. // 10.3.3 Block-level, non-replaced elements in normal flow
  48. // 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.
  49. if (width->to_length().is_auto() && total_px > containing_block()->rect().width()) {
  50. if (margin_left->to_length().is_auto())
  51. margin_left = zero_value;
  52. if (margin_right->to_length().is_auto())
  53. margin_right = zero_value;
  54. }
  55. }
  56. void LayoutBlock::compute_height()
  57. {
  58. }