Skip to content

Truly independent web browser

LibWeb: Stop the block size transfer walk at boxes that size themselves

Aliaksandr Kalenik552b8c3b5265
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index d305367e443..06649604f88 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -220,8 +220,6 @@ impl IntrinsicMeasurement for IntrinsicBlockSizeMeasurement {
#[derive(Default)]
struct IntrinsicSizeMaps {
- // This is a subtree fact and shares the intrinsic cache's epoch so descendant changes invalidate it.
- inline_size_depends_on_block_size: Option<bool>,
content_inline_sizes: IntrinsicMeasurements<IntrinsicInlineSizeMeasurement>,
max_content_block_size: IntrinsicMeasurements<IntrinsicBlockSizeMeasurement>,
}
@@ -298,21 +296,6 @@ impl IntrinsicSizeCaches {
self.with_maps(node, |maps| maps.content_inline_sizes.answer(&key))
}
- pub(crate) fn intrinsic_inline_size_depends_on_block_size(
- &self,
- node: NodeSlotId,
- compute: impl FnOnce() -> bool,
- ) -> bool {
- if let Some(value) = self.with_maps(node, |maps| maps.inline_size_depends_on_block_size) {
- return value;
- }
- let value = compute();
- self.with_maps_mut(node, |maps| {
- maps.inline_size_depends_on_block_size = Some(value);
- });
- value
- }
-
pub(crate) fn intrinsic_inline_size_measurement_cache_put(
&self,
node: NodeSlotId,
@@ -6272,7 +6255,6 @@ const _: () = {
#[cfg(test)]
mod tests {
- use std::cell::Cell;
use crate::layout::abspos_inputs::{
AbsposAxisMode, AbsposContainingBlockInfo, AbsposLayoutInputs, StaticPositionAlignment, StaticPositionRect,
@@ -7426,8 +7408,6 @@ mod tests {
depends_on_percentage_inline_basis: false,
depends_on_block_size: false,
};
- let dependency_computations = Cell::new(0);
-
caches.max_content_block_size_cache_put(first.slot, key, value);
caches.intrinsic_inline_size_measurement_cache_put(first.slot, key, inline_measurement);
assert_eq!(caches.max_content_block_size_cache_get(first.slot, key), Some(value));
@@ -7435,12 +7415,6 @@ mod tests {
caches.intrinsic_inline_size_measurement_cache_get(first.slot, key),
Some(inline_measurement)
);
- assert!(caches.intrinsic_inline_size_depends_on_block_size(first.slot, || {
- dependency_computations.set(dependency_computations.get() + 1);
- true
- }));
- assert!(caches.intrinsic_inline_size_depends_on_block_size(first.slot, || false));
- assert_eq!(dependency_computations.get(), 1);
arena.reset_cached_intrinsic_sizes(first.slot);
assert_eq!(
@@ -7454,11 +7428,6 @@ mod tests {
caches.intrinsic_inline_size_measurement_cache_get(first.slot, key),
None
);
- assert!(!caches.intrinsic_inline_size_depends_on_block_size(first.slot, || {
- dependency_computations.set(dependency_computations.get() + 1);
- false
- }));
- assert_eq!(dependency_computations.get(), 2);
caches.intrinsic_inline_size_measurement_cache_put(first.slot, key, inline_measurement);
arena
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index a9cee982fac..5724add561f 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -338,56 +338,77 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
!self.callbacks.first_child(node).is_invalid()
}
+ /// Whether the content inline sizes of `root` may read its block size: the root takes its inline size from it, or
+ /// the block size reaches a box that does, through the boxes that size themselves from their parent's block size.
pub(crate) fn intrinsic_inline_size_may_depend_on_block_size(&self, root: Node) -> bool {
- if self.establishes_orthogonal_flow(root) {
+ if self.establishes_orthogonal_flow(root) || self.transfers_its_block_size_to_its_inline_size(root) {
return true;
}
- // NB: Every frame the root is not orthogonal to transposes like the root's own, and frames that agree on that
- // read the same sizes, so the walk below answers the same for each of them.
- self.callbacks
- .intrinsic_size_caches()
- .intrinsic_inline_size_depends_on_block_size(root, || {
- let mut pending = vec![root];
- while let Some(node) = pending.pop() {
- let facts = self.facts(node);
- if facts.is_text_node() {
- continue;
- }
- let style = self.style(node);
- if node != root && geometry::Frame::of(style.writing_mode()).is_orthogonal_to(self.callbacks.frame)
- {
- return true;
- }
- if style.display().is_flex_inside()
- && matches!(
- style.effective_flex_direction(),
- flex_direction::COLUMN | flex_direction::COLUMN_REVERSE
- )
- && style.flex_wrap() != flex_wrap::NOWRAP
- {
- return true;
- }
- if facts.has_preferred_aspect_ratio()
- && (style.height().contains_percentage()
- || style.min_height().contains_percentage()
- || style.max_height().contains_percentage()
- || (style.height().is_auto()
- && (node_facts::has_flag(facts.data(), NodeFlag::IsFlexItem)
- || node_facts::has_flag(facts.data(), NodeFlag::IsGridItem))))
- {
- return true;
- }
- if node != root && facts.is_svg_svg_box() {
- continue;
- }
- for child in self.callbacks.children(node) {
- if !self.facts(child).is_absolutely_positioned() {
- pending.push(child);
- }
- }
+ let mut boxes_the_block_size_reaches = vec![root];
+ while let Some(container) = boxes_the_block_size_reaches.pop() {
+ if container != root && self.facts(container).is_svg_svg_box() {
+ continue;
+ }
+ for child in self.callbacks.children(container) {
+ let facts = self.facts(child);
+ if facts.is_text_node() || facts.is_absolutely_positioned() {
+ continue;
}
- false
- })
+ if self.establishes_orthogonal_flow(child) {
+ return true;
+ }
+ if !self.block_size_reaches_child(container, child) {
+ continue;
+ }
+ if self.transfers_its_block_size_to_its_inline_size(child) {
+ return true;
+ }
+ boxes_the_block_size_reaches.push(child);
+ }
+ }
+ false
+ }
+
+ fn transfers_its_block_size_to_its_inline_size(&self, node: Node) -> bool {
+ let facts = self.facts(node);
+ let style = self.style(node);
+ let lays_out_its_lines_against_its_block_size = style.display().is_flex_inside()
+ && matches!(
+ style.effective_flex_direction(),
+ flex_direction::COLUMN | flex_direction::COLUMN_REVERSE
+ )
+ && style.flex_wrap() != flex_wrap::NOWRAP;
+ let takes_its_inline_size_from_its_block_size_through_its_aspect_ratio = facts.has_preferred_aspect_ratio()
+ && (style.height().contains_percentage()
+ || style.min_height().contains_percentage()
+ || style.max_height().contains_percentage()
+ || (style.height().is_auto()
+ && (node_facts::has_flag(facts.data(), NodeFlag::IsFlexItem)
+ || node_facts::has_flag(facts.data(), NodeFlag::IsGridItem))));
+ lays_out_its_lines_against_its_block_size || takes_its_inline_size_from_its_block_size_through_its_aspect_ratio
+ }
+
+ /// Whether `child` sizes itself, or the boxes inside it, from the block size of `container`.
+ fn block_size_reaches_child(&self, container: Node, child: Node) -> bool {
+ let container_facts = self.facts(container);
+ let facts = self.facts(child);
+ let container_sizes_its_children_in_its_block_axis = facts.parent_is_flex_or_grid_container()
+ || container_facts.is_table_wrapper()
+ || container_facts.is_table_box()
+ || container_facts.is_table_row_group()
+ || container_facts.is_table_row();
+ if container_sizes_its_children_in_its_block_axis
+ || facts.is_anonymous()
+ || (facts.is_inline() && !facts.is_atomic_inline())
+ {
+ return true;
+ }
+ let style = self.style(child);
+ self.own_style_depends_on_percentage_block_size(child)
+ || self.passes_percentage_block_size_through_to_children(child)
+ || style.height().is_stretch()
+ || style.min_height().is_stretch()
+ || style.max_height().is_stretch()
}
pub(crate) fn content_block_size_from_aspect_ratio(
diff --git a/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-block-size-transfer-chain.txt b/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-block-size-transfer-chain.txt
new file mode 100644
index 00000000000..5118bba998a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-block-size-transfer-chain.txt
@@ -0,0 +1,2 @@
+auto-height: item measured again: false, width matches a fresh layout: true
+percentage-height: item measured again: true, width matches a fresh layout: true
diff --git a/Tests/LibWeb/Text/input/intrinsic-measurement-cache-block-size-transfer-chain.html b/Tests/LibWeb/Text/input/intrinsic-measurement-cache-block-size-transfer-chain.html
new file mode 100644
index 00000000000..26f897fb33a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/intrinsic-measurement-cache-block-size-transfer-chain.html
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../Ref/data/fonts/Ahem.ttf");
+ }
+
+ body {
+ margin: 0;
+ font: 10px/20px Ahem;
+ }
+
+ .row {
+ display: flex;
+ height: 100px;
+ width: max-content;
+ }
+
+ .vertical {
+ writing-mode: vertical-rl;
+ }
+</style>
+<template id="rows">
+ <div class="row" id="auto-height"><div id="auto-height-item"><div><div class="vertical">alpha beta gamma delta</div></div></div></div>
+ <div class="row" id="percentage-height"><div id="percentage-height-item"><div style="height: 100%"><div class="vertical">alpha beta gamma delta</div></div></div></div>
+</template>
+<script>
+ function measuredAgain(work, id) {
+ return work.count({ event: "measure", box: `BlockContainer<DIV>#${id}`, cache: "miss" }) > 0;
+ }
+
+ // The items are measured at the height they stretch to. An orthogonal flow inside an item only reads that height
+ // when the boxes between them take their height from it.
+ asyncTest(async done => {
+ await document.fonts.load("10px Ahem");
+ document.body.appendChild(document.getElementById("rows").content.cloneNode(true));
+ document.body.offsetWidth;
+
+ for (const id of ["auto-height", "percentage-height"]) {
+ const container = document.getElementById(id);
+ const work = layoutWork(() => (container.style.height = "140px"));
+ const fresh = document.getElementById("rows").content.getElementById(id).cloneNode(true);
+ fresh.id = "";
+ fresh.firstElementChild.id = "";
+ fresh.style.height = "140px";
+ document.body.appendChild(fresh);
+ const item = document.getElementById(`${id}-item`);
+ println(
+ `${id}: item measured again: ${measuredAgain(work, `${id}-item`)}, width matches a fresh layout: ${item.getBoundingClientRect().width === fresh.firstElementChild.getBoundingClientRect().width}`
+ );
+ fresh.remove();
+ }
+ done();
+ });
+</script>