LayoutReplaced.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(DOM::Document& document, const DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  31. : LayoutBox(document, &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 zero_value = CSS::Length::make_px(0);
  43. auto& containing_block = *this->containing_block();
  44. auto margin_left = style().margin().left.resolved_or_zero(*this, containing_block.width());
  45. auto margin_right = style().margin().right.resolved_or_zero(*this, containing_block.width());
  46. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  47. if (margin_left.is_auto())
  48. margin_left = zero_value;
  49. if (margin_right.is_auto())
  50. margin_right = zero_value;
  51. auto specified_width = style().width().resolved_or_auto(*this, containing_block.width());
  52. auto specified_height = style().height().resolved_or_auto(*this, containing_block.height());
  53. // FIXME: Actually compute 'width'
  54. auto computed_width = specified_width;
  55. float used_width = specified_width.to_px(*this);
  56. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  57. // then that intrinsic width is the used value of 'width'.
  58. if (specified_height.is_auto() && specified_width.is_auto() && has_intrinsic_width()) {
  59. used_width = intrinsic_width();
  60. }
  61. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  62. // but does have an intrinsic height and intrinsic ratio;
  63. // or if 'width' has a computed value of 'auto',
  64. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  65. //
  66. // (used height) * (intrinsic ratio)
  67. else if ((specified_height.is_auto() && specified_width.is_auto() && !has_intrinsic_width() && has_intrinsic_height() && has_intrinsic_ratio()) || computed_width.is_auto()) {
  68. used_width = calculate_height() * intrinsic_ratio();
  69. }
  70. else if (computed_width.is_auto() && has_intrinsic_width()) {
  71. used_width = intrinsic_width();
  72. }
  73. else if (computed_width.is_auto()) {
  74. used_width = 300;
  75. }
  76. return used_width;
  77. }
  78. float LayoutReplaced::calculate_height() const
  79. {
  80. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  81. // 'inline-block' replaced elements in normal flow and floating replaced elements
  82. auto& containing_block = *this->containing_block();
  83. auto specified_width = style().width().resolved_or_auto(*this, containing_block.width());
  84. auto specified_height = style().height().resolved_or_auto(*this, containing_block.height());
  85. float used_height = specified_height.to_px(*this);
  86. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  87. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  88. if (specified_width.is_auto() && specified_height.is_auto() && has_intrinsic_height())
  89. used_height = intrinsic_height();
  90. else if (specified_height.is_auto() && has_intrinsic_ratio())
  91. used_height = calculate_width() / intrinsic_ratio();
  92. else if (specified_height.is_auto() && has_intrinsic_height())
  93. used_height = intrinsic_height();
  94. else if (specified_height.is_auto())
  95. used_height = 150;
  96. return used_height;
  97. }
  98. void LayoutReplaced::layout(LayoutMode)
  99. {
  100. set_width(calculate_width());
  101. set_height(calculate_height());
  102. }
  103. void LayoutReplaced::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  104. {
  105. layout(layout_mode);
  106. auto* line_box = &container.ensure_last_line_box();
  107. if (line_box->width() > 0 && line_box->width() + width() > container.width())
  108. line_box = &container.add_line_box();
  109. line_box->add_fragment(*this, 0, 0, width(), height());
  110. }
  111. }