ReplacedBox.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/BlockBox.h>
  28. #include <LibWeb/Layout/InlineFormattingContext.h>
  29. #include <LibWeb/Layout/ReplacedBox.h>
  30. namespace Web::Layout {
  31. ReplacedBox::ReplacedBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  32. : Box(document, &element, move(style))
  33. {
  34. // FIXME: Allow non-inline replaced elements.
  35. set_inline(true);
  36. }
  37. ReplacedBox::~ReplacedBox()
  38. {
  39. }
  40. float ReplacedBox::calculate_width() const
  41. {
  42. // 10.3.2 [Inline,] replaced elements
  43. auto zero_value = CSS::Length::make_px(0);
  44. auto& containing_block = *this->containing_block();
  45. auto margin_left = style().margin().left.resolved_or_zero(*this, containing_block.width());
  46. auto margin_right = style().margin().right.resolved_or_zero(*this, containing_block.width());
  47. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  48. if (margin_left.is_auto())
  49. margin_left = zero_value;
  50. if (margin_right.is_auto())
  51. margin_right = zero_value;
  52. auto specified_width = style().width().resolved_or_auto(*this, containing_block.width());
  53. auto specified_height = style().height().resolved_or_auto(*this, containing_block.height());
  54. // FIXME: Actually compute 'width'
  55. auto computed_width = specified_width;
  56. float used_width = specified_width.to_px(*this);
  57. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  58. // then that intrinsic width is the used value of 'width'.
  59. if (specified_height.is_auto() && specified_width.is_auto() && has_intrinsic_width()) {
  60. used_width = intrinsic_width();
  61. }
  62. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  63. // but does have an intrinsic height and intrinsic ratio;
  64. // or if 'width' has a computed value of 'auto',
  65. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  66. //
  67. // (used height) * (intrinsic ratio)
  68. else if ((specified_height.is_auto() && specified_width.is_auto() && !has_intrinsic_width() && has_intrinsic_height() && has_intrinsic_ratio()) || (computed_width.is_auto() && has_intrinsic_ratio())) {
  69. used_width = calculate_height() * intrinsic_ratio();
  70. }
  71. else if (computed_width.is_auto() && has_intrinsic_width()) {
  72. used_width = intrinsic_width();
  73. }
  74. else if (computed_width.is_auto()) {
  75. used_width = 300;
  76. }
  77. return used_width;
  78. }
  79. float ReplacedBox::calculate_height() const
  80. {
  81. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  82. // 'inline-block' replaced elements in normal flow and floating replaced elements
  83. auto& containing_block = *this->containing_block();
  84. auto specified_width = style().width().resolved_or_auto(*this, containing_block.width());
  85. auto specified_height = style().height().resolved_or_auto(*this, containing_block.height());
  86. float used_height = specified_height.to_px(*this);
  87. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  88. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  89. if (specified_width.is_auto() && specified_height.is_auto() && has_intrinsic_height())
  90. used_height = intrinsic_height();
  91. else if (specified_height.is_auto() && has_intrinsic_ratio())
  92. used_height = calculate_width() / intrinsic_ratio();
  93. else if (specified_height.is_auto() && has_intrinsic_height())
  94. used_height = intrinsic_height();
  95. else if (specified_height.is_auto())
  96. used_height = 150;
  97. return used_height;
  98. }
  99. void ReplacedBox::split_into_lines(InlineFormattingContext& context, LayoutMode)
  100. {
  101. auto& containing_block = context.context_box();
  102. // FIXME: This feels out of place. It would be nice if someone at a higher level
  103. // made sure we had usable geometry by the time we start splitting.
  104. prepare_for_replaced_layout();
  105. auto width = calculate_width();
  106. auto height = calculate_height();
  107. auto* line_box = &containing_block.ensure_last_line_box();
  108. if (line_box->width() > 0 && line_box->width() + width > context.available_width_at_line(containing_block.line_boxes().size() - 1))
  109. line_box = &containing_block.add_line_box();
  110. line_box->add_fragment(*this, 0, 0, width, height);
  111. }
  112. }