LayoutReplaced.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/Element.h>
  27. #include <LibWeb/Layout/LayoutBlock.h>
  28. #include <LibWeb/Layout/LayoutReplaced.h>
  29. namespace Web {
  30. LayoutReplaced::LayoutReplaced(const Element& element, NonnullRefPtr<StyleProperties> style)
  31. : LayoutBox(&element, move(style))
  32. {
  33. // FIXME: Allow non-inline replaced elements.
  34. set_inline(true);
  35. }
  36. LayoutReplaced::~LayoutReplaced()
  37. {
  38. }
  39. float LayoutReplaced::calculate_width() const
  40. {
  41. // 10.3.2 [Inline,] replaced elements
  42. auto& style = this->style();
  43. auto auto_value = Length();
  44. auto zero_value = Length(0, Length::Type::Px);
  45. auto& containing_block = *this->containing_block();
  46. auto margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value, containing_block.width());
  47. auto margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value, containing_block.width());
  48. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  49. if (margin_left.is_auto())
  50. margin_left = zero_value;
  51. if (margin_right.is_auto())
  52. margin_right = zero_value;
  53. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value, containing_block.width());
  54. auto specified_height = style.length_or_fallback(CSS::PropertyID::Height, auto_value, containing_block.height());
  55. // FIXME: Actually compute 'width'
  56. auto computed_width = specified_width;
  57. float used_width = specified_width.to_px(*this);
  58. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  59. // then that intrinsic width is the used value of 'width'.
  60. if (specified_height.is_auto() && specified_width.is_auto() && has_intrinsic_width()) {
  61. used_width = intrinsic_width();
  62. }
  63. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  64. // but does have an intrinsic height and intrinsic ratio;
  65. // or if 'width' has a computed value of 'auto',
  66. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  67. //
  68. // (used height) * (intrinsic ratio)
  69. else if ((specified_height.is_auto() && specified_width.is_auto() && !has_intrinsic_width() && has_intrinsic_height() && has_intrinsic_ratio()) || computed_width.is_auto()) {
  70. used_width = calculate_height() * intrinsic_ratio();
  71. }
  72. else if (computed_width.is_auto() && has_intrinsic_width()) {
  73. used_width = intrinsic_width();
  74. }
  75. else if (computed_width.is_auto()) {
  76. used_width = 300;
  77. }
  78. return used_width;
  79. }
  80. float LayoutReplaced::calculate_height() const
  81. {
  82. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  83. // 'inline-block' replaced elements in normal flow and floating replaced elements
  84. auto& style = this->style();
  85. auto auto_value = Length();
  86. auto& containing_block = *this->containing_block();
  87. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value, containing_block.width());
  88. auto specified_height = style.length_or_fallback(CSS::PropertyID::Height, auto_value, containing_block.height());
  89. float used_height = specified_height.to_px(*this);
  90. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  91. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  92. if (specified_width.is_auto() && specified_height.is_auto() && has_intrinsic_height())
  93. used_height = intrinsic_height();
  94. else if (specified_height.is_auto() && has_intrinsic_ratio())
  95. used_height = calculate_width() * intrinsic_ratio();
  96. else if (specified_height.is_auto() && has_intrinsic_height())
  97. used_height = intrinsic_height();
  98. else if (specified_height.is_auto())
  99. used_height = 150;
  100. return used_height;
  101. }
  102. Gfx::FloatPoint LayoutReplaced::calculate_position()
  103. {
  104. auto& style = this->style();
  105. auto zero_value = Length(0, Length::Type::Px);
  106. auto& containing_block = *this->containing_block();
  107. if (style.position() == CSS::Position::Absolute) {
  108. box_model().offset().top = style.length_or_fallback(CSS::PropertyID::Top, zero_value, containing_block.height());
  109. box_model().offset().right = style.length_or_fallback(CSS::PropertyID::Right, zero_value, containing_block.width());
  110. box_model().offset().bottom = style.length_or_fallback(CSS::PropertyID::Bottom, zero_value, containing_block.height());
  111. box_model().offset().left = style.length_or_fallback(CSS::PropertyID::Left, zero_value, containing_block.width());
  112. }
  113. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
  114. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
  115. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  116. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  117. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
  118. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
  119. float x = box_model().margin().left.to_px(*this)
  120. + box_model().border().left.to_px(*this)
  121. + box_model().padding().left.to_px(*this)
  122. + box_model().offset().left.to_px(*this);
  123. float y = box_model().full_margin(*this).top + box_model().offset().top.to_px(*this);
  124. return { x, y };
  125. }
  126. void LayoutReplaced::layout(LayoutMode layout_mode)
  127. {
  128. set_width(calculate_width());
  129. set_height(calculate_height());
  130. LayoutBox::layout(layout_mode);
  131. set_offset(calculate_position());
  132. }
  133. void LayoutReplaced::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  134. {
  135. layout(layout_mode);
  136. auto* line_box = &container.ensure_last_line_box();
  137. if (line_box->width() > 0 && line_box->width() + width() > container.width())
  138. line_box = &container.add_line_box();
  139. line_box->add_fragment(*this, 0, 0, width(), height());
  140. }
  141. }