LayoutState.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (c) 2022-2024, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Debug.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/Layout/AvailableSpace.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/InlineNode.h>
  13. #include <LibWeb/Layout/LayoutState.h>
  14. #include <LibWeb/Layout/Viewport.h>
  15. #include <LibWeb/Painting/SVGPathPaintable.h>
  16. #include <LibWeb/Painting/SVGSVGPaintable.h>
  17. #include <LibWeb/Painting/TextPaintable.h>
  18. namespace Web::Layout {
  19. LayoutState::LayoutState(LayoutState const* parent)
  20. : m_parent(parent)
  21. , m_root(find_root())
  22. {
  23. }
  24. LayoutState::~LayoutState()
  25. {
  26. }
  27. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node)
  28. {
  29. if (auto* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
  30. return *used_values;
  31. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  32. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr)) {
  33. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  34. auto* cow_used_values_ptr = cow_used_values.ptr();
  35. used_values_per_layout_node.set(node, move(cow_used_values));
  36. return *cow_used_values_ptr;
  37. }
  38. }
  39. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  40. auto new_used_values = adopt_own(*new UsedValues);
  41. auto* new_used_values_ptr = new_used_values.ptr();
  42. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  43. used_values_per_layout_node.set(node, move(new_used_values));
  44. return *new_used_values_ptr;
  45. }
  46. LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const
  47. {
  48. if (auto const* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
  49. return *used_values;
  50. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  51. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr))
  52. return *ancestor_used_values;
  53. }
  54. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  55. auto new_used_values = adopt_own(*new UsedValues);
  56. auto* new_used_values_ptr = new_used_values.ptr();
  57. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  58. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(node, move(new_used_values));
  59. return *new_used_values_ptr;
  60. }
  61. // https://www.w3.org/TR/css-overflow-3/#scrollable-overflow
  62. static CSSPixelRect measure_scrollable_overflow(Box const& box)
  63. {
  64. if (!box.paintable_box())
  65. return {};
  66. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  67. if (paintable_box.scrollable_overflow_rect().has_value())
  68. return paintable_box.scrollable_overflow_rect().value();
  69. // The scrollable overflow area is the union of:
  70. // - The scroll container’s own padding box.
  71. auto scrollable_overflow_rect = paintable_box.absolute_padding_box_rect();
  72. // - All line boxes directly contained by the scroll container.
  73. if (is<Painting::PaintableWithLines>(box.first_paintable())) {
  74. for (auto const& fragment : static_cast<Painting::PaintableWithLines const&>(*box.first_paintable()).fragments()) {
  75. scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect());
  76. }
  77. }
  78. auto content_overflow_rect = scrollable_overflow_rect;
  79. // - The border boxes of all boxes for which it is the containing block
  80. // and whose border boxes are positioned not wholly in the negative scrollable overflow region,
  81. // FIXME: accounting for transforms by projecting each box onto the plane of the element that establishes its 3D rendering context. [CSS3-TRANSFORMS]
  82. if (!box.children_are_inline()) {
  83. box.for_each_in_subtree_of_type<Box>([&box, &scrollable_overflow_rect, &content_overflow_rect](Box const& child) {
  84. if (!child.paintable_box())
  85. return TraversalDecision::Continue;
  86. if (child.containing_block() != &box)
  87. return TraversalDecision::Continue;
  88. auto child_border_box = child.paintable_box()->absolute_border_box_rect();
  89. // Border boxes with zero area do not affect the scrollable overflow area.
  90. if (child_border_box.is_empty())
  91. return TraversalDecision::Continue;
  92. // NOTE: Here we check that the child is not wholly in the negative scrollable overflow region.
  93. if (child_border_box.bottom() < 0 || child_border_box.right() < 0)
  94. return TraversalDecision::Continue;
  95. scrollable_overflow_rect = scrollable_overflow_rect.united(child_border_box);
  96. content_overflow_rect = content_overflow_rect.united(child_border_box);
  97. // - The scrollable overflow areas of all of the above boxes
  98. // (including zero-area boxes and accounting for transforms as described above),
  99. // provided they themselves have overflow: visible (i.e. do not themselves trap the overflow)
  100. // and that scrollable overflow is not already clipped (e.g. by the clip property or the contain property).
  101. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible || child.computed_values().overflow_y() == CSS::Overflow::Visible) {
  102. auto child_scrollable_overflow = measure_scrollable_overflow(child);
  103. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible)
  104. scrollable_overflow_rect.unite_horizontally(child_scrollable_overflow);
  105. if (is<Viewport>(box) || child.computed_values().overflow_y() == CSS::Overflow::Visible)
  106. scrollable_overflow_rect.unite_vertically(child_scrollable_overflow);
  107. }
  108. return TraversalDecision::Continue;
  109. });
  110. }
  111. // FIXME: - The margin areas of grid item and flex item boxes for which the box establishes a containing block.
  112. // - Additional padding added to the end-side of the scrollable overflow rectangle as necessary
  113. // to enable a scroll position that satisfies the requirements of place-content: end alignment.
  114. auto has_scrollable_overflow = !paintable_box.absolute_padding_box_rect().contains(scrollable_overflow_rect);
  115. if (has_scrollable_overflow) {
  116. scrollable_overflow_rect.set_height(max(scrollable_overflow_rect.height(), content_overflow_rect.height() + box.box_model().padding.bottom));
  117. }
  118. paintable_box.set_overflow_data(Painting::PaintableBox::OverflowData {
  119. .scrollable_overflow_rect = scrollable_overflow_rect,
  120. .has_scrollable_overflow = has_scrollable_overflow,
  121. });
  122. return scrollable_overflow_rect;
  123. }
  124. void LayoutState::resolve_relative_positions()
  125. {
  126. // This function resolves relative position offsets of fragments that belong to inline paintables.
  127. // It runs *after* the paint tree has been constructed, so it modifies paintable node & fragment offsets directly.
  128. for (auto& it : used_values_per_layout_node) {
  129. auto& used_values = *it.value;
  130. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  131. for (auto& paintable : node.paintables()) {
  132. if (!(is<Painting::PaintableWithLines>(paintable) && is<Layout::InlineNode>(paintable.layout_node())))
  133. continue;
  134. auto const& inline_paintable = static_cast<Painting::PaintableWithLines&>(paintable);
  135. for (auto& fragment : inline_paintable.fragments()) {
  136. auto const& fragment_node = fragment.layout_node();
  137. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
  138. continue;
  139. // Collect effective relative position offset from inline-flow parent chain.
  140. CSSPixelPoint offset;
  141. for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
  142. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
  143. break;
  144. if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
  145. break;
  146. if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
  147. auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
  148. auto const& inset = ancestor_node.box_model().inset;
  149. offset.translate_by(inset.left, inset.top);
  150. }
  151. }
  152. const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset));
  153. }
  154. }
  155. }
  156. }
  157. static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr)
  158. {
  159. for (auto& paintable : node.paintables()) {
  160. if (parent_paintable && !paintable.forms_unconnected_subtree()) {
  161. VERIFY(!paintable.parent());
  162. parent_paintable->append_child(paintable);
  163. }
  164. paintable.set_dom_node(node.dom_node());
  165. if (node.dom_node())
  166. node.dom_node()->set_paintable(paintable);
  167. }
  168. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  169. build_paint_tree(*child, node.first_paintable());
  170. }
  171. }
  172. void LayoutState::commit(Box& root)
  173. {
  174. // Only the top-level LayoutState should ever be committed.
  175. VERIFY(!m_parent);
  176. // NOTE: In case this is a relayout of an existing tree, we start by detaching the old paint tree
  177. // from the layout tree. This is done to ensure that we don't end up with any old-tree pointers
  178. // when text paintables shift around in the tree.
  179. root.for_each_in_inclusive_subtree([&](Layout::Node& node) {
  180. node.clear_paintables();
  181. return TraversalDecision::Continue;
  182. });
  183. HashTable<Layout::InlineNode*> inline_nodes;
  184. root.document().for_each_shadow_including_inclusive_descendant([&](DOM::Node& node) {
  185. node.clear_paintable();
  186. if (node.layout_node() && is<InlineNode>(node.layout_node())) {
  187. inline_nodes.set(static_cast<InlineNode*>(node.layout_node()));
  188. }
  189. return TraversalDecision::Continue;
  190. });
  191. HashTable<Layout::TextNode*> text_nodes;
  192. HashTable<Painting::PaintableWithLines*> inline_node_paintables;
  193. auto try_to_relocate_fragment_in_inline_node = [&](auto& fragment, size_t line_index) -> bool {
  194. for (auto const* parent = fragment.layout_node().parent(); parent; parent = parent->parent()) {
  195. if (is<InlineNode>(*parent)) {
  196. auto& inline_node = const_cast<InlineNode&>(static_cast<InlineNode const&>(*parent));
  197. auto line_paintable = inline_node.create_paintable_for_line_with_index(line_index);
  198. line_paintable->add_fragment(fragment);
  199. if (!inline_node_paintables.contains(line_paintable.ptr())) {
  200. inline_node_paintables.set(line_paintable.ptr());
  201. inline_node.add_paintable(line_paintable);
  202. }
  203. return true;
  204. }
  205. }
  206. return false;
  207. };
  208. for (auto& it : used_values_per_layout_node) {
  209. auto& used_values = *it.value;
  210. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  211. if (is<NodeWithStyleAndBoxModelMetrics>(node)) {
  212. // Transfer box model metrics.
  213. auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model();
  214. box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  215. box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  216. box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  217. box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  218. }
  219. auto paintable = node.create_paintable();
  220. node.add_paintable(paintable);
  221. // For boxes, transfer all the state needed for painting.
  222. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  223. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  224. paintable_box.set_offset(used_values.offset);
  225. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  226. if (used_values.override_borders_data().has_value()) {
  227. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  228. }
  229. if (used_values.table_cell_coordinates().has_value()) {
  230. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  231. }
  232. if (is<Painting::PaintableWithLines>(paintable_box)) {
  233. auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
  234. for (size_t line_index = 0; line_index < used_values.line_boxes.size(); ++line_index) {
  235. auto& line_box = used_values.line_boxes[line_index];
  236. for (auto& fragment : line_box.fragments()) {
  237. if (fragment.layout_node().is_text_node())
  238. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  239. auto did_relocate_fragment = try_to_relocate_fragment_in_inline_node(fragment, line_index);
  240. if (!did_relocate_fragment) {
  241. paintable_with_lines.add_fragment(fragment);
  242. }
  243. }
  244. }
  245. }
  246. if (used_values.computed_svg_transforms().has_value() && is<Painting::SVGGraphicsPaintable>(paintable_box)) {
  247. auto& svg_graphics_paintable = static_cast<Painting::SVGGraphicsPaintable&>(paintable_box);
  248. svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms());
  249. }
  250. if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) {
  251. auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box);
  252. svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path()));
  253. }
  254. if (node.display().is_grid_inside()) {
  255. paintable_box.set_used_values_for_grid_template_columns(used_values.grid_template_columns());
  256. paintable_box.set_used_values_for_grid_template_rows(used_values.grid_template_rows());
  257. }
  258. }
  259. }
  260. // Create paintables for inline nodes without fragments to make possible querying their geometry.
  261. for (auto& inline_node : inline_nodes) {
  262. auto* paintable = inline_node->first_paintable();
  263. if (paintable)
  264. continue;
  265. auto line_paintable = inline_node->create_paintable_for_line_with_index(0);
  266. inline_node->add_paintable(line_paintable);
  267. inline_node_paintables.set(line_paintable.ptr());
  268. }
  269. // Resolve relative positions for regular boxes (not line box fragments):
  270. // NOTE: This needs to occur before fragments are transferred into the corresponding inline paintables, because
  271. // after this transfer, the containing_line_box_fragment will no longer be valid.
  272. for (auto& it : used_values_per_layout_node) {
  273. auto& used_values = *it.value;
  274. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  275. if (!node.is_box())
  276. continue;
  277. auto& paintable = static_cast<Painting::PaintableBox&>(*node.first_paintable());
  278. CSSPixelPoint offset;
  279. if (used_values.containing_line_box_fragment.has_value()) {
  280. // Atomic inline case:
  281. // We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the
  282. // line box fragment in the parent block container that contains it.
  283. auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
  284. auto const& containing_block = *node.containing_block();
  285. auto const& containing_block_used_values = get(containing_block);
  286. auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
  287. // The fragment has the final offset for the atomic inline, so we just need to copy it from there.
  288. offset = fragment.offset();
  289. } else {
  290. // Not an atomic inline, much simpler case.
  291. offset = used_values.offset;
  292. }
  293. // Apply relative position inset if appropriate.
  294. if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
  295. auto const& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset;
  296. offset.translate_by(inset.left, inset.top);
  297. }
  298. paintable.set_offset(offset);
  299. }
  300. for (auto* text_node : text_nodes) {
  301. text_node->add_paintable(text_node->create_paintable());
  302. auto* paintable = text_node->first_paintable();
  303. auto const& font = text_node->first_available_font();
  304. auto const glyph_height = CSSPixels::nearest_value_for(font.pixel_size());
  305. auto const css_line_thickness = [&] {
  306. auto computed_thickness = text_node->computed_values().text_decoration_thickness().resolved(*text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(*text_node));
  307. if (computed_thickness.is_auto())
  308. return max(glyph_height.scaled(0.1), 1);
  309. return computed_thickness.to_px(*text_node);
  310. }();
  311. auto& text_paintable = static_cast<Painting::TextPaintable&>(*paintable);
  312. text_paintable.set_text_decoration_thickness(css_line_thickness);
  313. }
  314. build_paint_tree(root);
  315. resolve_relative_positions();
  316. // Measure size of paintables created for inline nodes.
  317. for (auto& paintable_with_lines : inline_node_paintables) {
  318. if (!is<InlineNode>(paintable_with_lines->layout_node())) {
  319. continue;
  320. }
  321. Optional<CSSPixelPoint> offset;
  322. CSSPixelSize size;
  323. auto line_index = paintable_with_lines->line_index();
  324. paintable_with_lines->for_each_in_inclusive_subtree_of_type<Painting::PaintableWithLines>([&offset, &size, &line_index](auto& paintable) {
  325. if (paintable.line_index() == line_index) {
  326. auto const& fragments = paintable.fragments();
  327. if (!fragments.is_empty()) {
  328. if (!offset.has_value() || (fragments.first().offset().x() < offset.value().x())) {
  329. offset = fragments.first().offset();
  330. }
  331. }
  332. for (auto const& fragment : fragments) {
  333. // FIXME: Padding and margin of nested inlines not included in fragment width
  334. size.set_width(size.width() + fragment.width());
  335. }
  336. }
  337. return TraversalDecision::Continue;
  338. });
  339. if (offset.has_value()) {
  340. if (!paintable_with_lines->fragments().is_empty()) {
  341. offset.value().set_y(paintable_with_lines->fragments().first().offset().y());
  342. }
  343. // FIXME: If this paintable does not have any fragment we do no know the y offset. It should be where text should
  344. // start if there had been any for this node. Pick y offset of the leftmost fragment in the inclusive subtree in the meantime.
  345. paintable_with_lines->set_offset(offset.value());
  346. }
  347. if (!paintable_with_lines->fragments().is_empty()) {
  348. for (auto const& fragment : paintable_with_lines->fragments()) {
  349. size.set_height(max(size.height(), fragment.height()));
  350. }
  351. } else {
  352. size.set_height(paintable_with_lines->layout_node().computed_values().line_height());
  353. }
  354. paintable_with_lines->set_content_size(size.width(), size.height());
  355. }
  356. // Measure overflow in scroll containers.
  357. for (auto& it : used_values_per_layout_node) {
  358. auto& used_values = *it.value;
  359. if (!used_values.node().is_box())
  360. continue;
  361. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  362. measure_scrollable_overflow(box);
  363. // The scroll offset can become invalid if the scrollable overflow rectangle has changed after layout.
  364. // For example, if the scroll container has been scrolled to the very end and is then resized to become larger
  365. // (scrollable overflow rect become smaller), the scroll offset would be out of bounds.
  366. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  367. if (!paintable_box.scroll_offset().is_zero())
  368. paintable_box.set_scroll_offset(paintable_box.scroll_offset());
  369. }
  370. for (auto& it : used_values_per_layout_node) {
  371. auto& used_values = *it.value;
  372. if (!used_values.node().is_box())
  373. continue;
  374. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  375. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  376. if (box.is_sticky_position()) {
  377. // https://drafts.csswg.org/css-position/#insets
  378. // For sticky positioned boxes, the inset is instead relative to the relevant scrollport’s size. Negative values are allowed.
  379. auto sticky_insets = make<Painting::PaintableBox::StickyInsets>();
  380. auto const& inset = box.computed_values().inset();
  381. auto const* nearest_scrollable_ancestor = paintable_box.nearest_scrollable_ancestor();
  382. CSSPixelSize scrollport_size;
  383. if (nearest_scrollable_ancestor) {
  384. scrollport_size = nearest_scrollable_ancestor->absolute_rect().size();
  385. }
  386. if (!inset.top().is_auto()) {
  387. sticky_insets->top = inset.top().to_px(box, scrollport_size.height());
  388. }
  389. if (!inset.right().is_auto()) {
  390. sticky_insets->right = inset.right().to_px(box, scrollport_size.width());
  391. }
  392. if (!inset.bottom().is_auto()) {
  393. sticky_insets->bottom = inset.bottom().to_px(box, scrollport_size.height());
  394. }
  395. if (!inset.left().is_auto()) {
  396. sticky_insets->left = inset.left().to_px(box, scrollport_size.width());
  397. }
  398. paintable_box.set_sticky_insets(move(sticky_insets));
  399. }
  400. }
  401. }
  402. void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)
  403. {
  404. m_node = &node;
  405. m_containing_block_used_values = containing_block_used_values;
  406. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  407. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  408. // If `node` has definite values for min/max-width or min/max-height and a definite
  409. // preferred size in the same axis, we clamp the preferred size here as well.
  410. //
  411. // There are additional cases where CSS considers values to be definite. We model all of
  412. // those by having our engine consider sizes to be definite *once they are assigned to
  413. // the UsedValues by calling set_content_width() or set_content_height().
  414. auto const& computed_values = node.computed_values();
  415. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  416. // box-sizing: content-box and/or automatic size don't require any adjustment.
  417. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  418. return unadjusted_pixels;
  419. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  420. CSSPixels border_and_padding;
  421. if (width) {
  422. border_and_padding = computed_values.border_left().width
  423. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  424. + computed_values.border_right().width
  425. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  426. } else {
  427. border_and_padding = computed_values.border_top().width
  428. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  429. + computed_values.border_bottom().width
  430. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  431. }
  432. return unadjusted_pixels - border_and_padding;
  433. };
  434. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  435. // A size that can be determined without performing layout; that is,
  436. // a <length>,
  437. // a measure of text (without consideration of line-wrapping),
  438. // a size of the initial containing block,
  439. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  440. auto containing_block_has_definite_size = containing_block_used_values ? (width ? containing_block_used_values->has_definite_width() : containing_block_used_values->has_definite_height()) : false;
  441. if (size.is_auto()) {
  442. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  443. if (width
  444. && !node.is_floating()
  445. && !node.is_absolutely_positioned()
  446. && node.display().is_block_outside()
  447. && node.parent()
  448. && !node.parent()->is_floating()
  449. && (node.parent()->display().is_flow_root_inside()
  450. || node.parent()->display().is_flow_inside())) {
  451. if (containing_block_has_definite_size) {
  452. CSSPixels available_width = containing_block_used_values->content_width();
  453. resolved_definite_size = available_width
  454. - margin_left
  455. - margin_right
  456. - padding_left
  457. - padding_right
  458. - border_left
  459. - border_right;
  460. return true;
  461. }
  462. return false;
  463. }
  464. return false;
  465. }
  466. if (size.is_calculated()) {
  467. if (size.calculated().contains_percentage()) {
  468. if (!containing_block_has_definite_size)
  469. return false;
  470. auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  471. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node), size, width);
  472. return true;
  473. }
  474. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  475. return true;
  476. }
  477. if (size.is_length()) {
  478. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  479. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  480. return true;
  481. }
  482. if (size.is_percentage()) {
  483. if (containing_block_has_definite_size) {
  484. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  485. resolved_definite_size = adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width);
  486. return true;
  487. }
  488. return false;
  489. }
  490. return false;
  491. };
  492. CSSPixels min_width = 0;
  493. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  494. CSSPixels max_width = 0;
  495. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  496. CSSPixels min_height = 0;
  497. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  498. CSSPixels max_height = 0;
  499. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  500. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  501. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  502. // For boxes with a preferred aspect ratio and one definite size, we can infer the other size
  503. // and consider it definite since this did not require performing layout.
  504. if (is<Box>(node)) {
  505. auto const& box = static_cast<Box const&>(node);
  506. if (auto aspect_ratio = box.preferred_aspect_ratio(); aspect_ratio.has_value()) {
  507. if (m_has_definite_width && m_has_definite_height) {
  508. // Both width and height are definite.
  509. } else if (m_has_definite_width) {
  510. m_content_height = m_content_width / *aspect_ratio;
  511. m_has_definite_height = true;
  512. } else if (m_has_definite_height) {
  513. m_content_width = m_content_height * *aspect_ratio;
  514. m_has_definite_width = true;
  515. }
  516. }
  517. }
  518. if (m_has_definite_width) {
  519. if (has_definite_min_width)
  520. m_content_width = max(min_width, m_content_width);
  521. if (has_definite_max_width)
  522. m_content_width = min(max_width, m_content_width);
  523. }
  524. if (m_has_definite_height) {
  525. if (has_definite_min_height)
  526. m_content_height = max(min_height, m_content_height);
  527. if (has_definite_max_height)
  528. m_content_height = min(max_height, m_content_height);
  529. }
  530. }
  531. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  532. {
  533. VERIFY(!width.might_be_saturated());
  534. if (width < 0) {
  535. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  536. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  537. width = 0;
  538. }
  539. m_content_width = width;
  540. // FIXME: We should not do this! Definiteness of widths should be determined early,
  541. // and not changed later (except for some special cases in flex layout..)
  542. m_has_definite_width = true;
  543. }
  544. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  545. {
  546. VERIFY(!height.might_be_saturated());
  547. if (height < 0) {
  548. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  549. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  550. height = 0;
  551. }
  552. m_content_height = height;
  553. }
  554. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  555. {
  556. m_content_width = width;
  557. }
  558. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  559. {
  560. m_content_height = height;
  561. }
  562. AvailableSize LayoutState::UsedValues::available_width_inside() const
  563. {
  564. if (width_constraint == SizeConstraint::MinContent)
  565. return AvailableSize::make_min_content();
  566. if (width_constraint == SizeConstraint::MaxContent)
  567. return AvailableSize::make_max_content();
  568. if (has_definite_width())
  569. return AvailableSize::make_definite(m_content_width);
  570. return AvailableSize::make_indefinite();
  571. }
  572. AvailableSize LayoutState::UsedValues::available_height_inside() const
  573. {
  574. if (height_constraint == SizeConstraint::MinContent)
  575. return AvailableSize::make_min_content();
  576. if (height_constraint == SizeConstraint::MaxContent)
  577. return AvailableSize::make_max_content();
  578. if (has_definite_height())
  579. return AvailableSize::make_definite(m_content_height);
  580. return AvailableSize::make_indefinite();
  581. }
  582. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  583. {
  584. auto inner_width = available_width_inside();
  585. auto inner_height = available_height_inside();
  586. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  587. inner_width = outer_space.width;
  588. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  589. inner_height = outer_space.height;
  590. return AvailableSpace(inner_width, inner_height);
  591. }
  592. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  593. {
  594. set_content_x(new_offset.x());
  595. set_content_y(new_offset.y());
  596. }
  597. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  598. {
  599. offset.set_x(x);
  600. }
  601. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  602. {
  603. offset.set_y(y);
  604. }
  605. void LayoutState::UsedValues::set_indefinite_content_width()
  606. {
  607. m_has_definite_width = false;
  608. }
  609. void LayoutState::UsedValues::set_indefinite_content_height()
  610. {
  611. m_has_definite_height = false;
  612. }
  613. }