Skip to content

Truly independent web browser

LibWeb: Key abspos runs on the static position only where it is read

Aliaksandr Kalenikdb8ce5ca3dc1
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_inputs.rs b/Libraries/LibWeb/Rust/src/layout/abspos_inputs.rs
index d4f606ebd41..1b68f8e81a5 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_inputs.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_inputs.rs
@@ -130,6 +130,18 @@ pub(crate) struct AbsposContainingBlockInfo {
}
impl AbsposContainingBlockInfo {
+ // NB: A grid area, a position area and anchor-center give an axis an alignment, and set its mode whatever the
+ // insets are. They may size or align the box against the static position rect.
+ fn axes_placed_by_own_insets(&self) -> (bool, bool) {
+ let placed_by_own_insets = |mode: AbsposAxisMode, alignment: Option<AbsposAlignment>| {
+ mode == AbsposAxisMode::InsetFromRect && alignment.is_none()
+ };
+ (
+ placed_by_own_insets(self.inline_axis_mode, self.inline_alignment),
+ placed_by_own_insets(self.block_axis_mode, self.block_alignment),
+ )
+ }
+
/// Whether a position area or anchor-center aligns the box in the inline and the block axis.
/// https://drafts.csswg.org/css-anchor-position-1/#position-area
/// https://drafts.csswg.org/css-anchor-position-1/#anchor-center
@@ -178,6 +190,22 @@ pub(crate) struct AbsposLayoutInputs {
pub(crate) position_option: Option<PositionOptionChoice>,
}
+impl AbsposLayoutInputs {
+ pub(crate) fn without_static_position_in_axes_placed_by_own_insets(mut self) -> Self {
+ let (inline_placed_by_insets, block_placed_by_insets) = self.containing_block_info.axes_placed_by_own_insets();
+ let rect = &mut self.static_position_rect.rect;
+ if inline_placed_by_insets {
+ rect.offset.inline_offset = CssPixels::default();
+ rect.size.inline_size = CssPixels::default();
+ }
+ if block_placed_by_insets {
+ rect.offset.block_offset = CssPixels::default();
+ rect.size.block_size = CssPixels::default();
+ }
+ self
+ }
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ContainingBlockSearch {
pub(crate) is_fixed_position: bool,
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 20ba4f04dd3..ac7c3221586 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -113,6 +113,9 @@ impl FcRunCacheKey {
{
*containing_block_position_in_bfc_root = FfiCssPixelPoint::default();
}
+ if let ParticipationInParentFormattingContext::AbsolutelyPositioned(inputs) = &mut input.participation {
+ *inputs = inputs.without_static_position_in_axes_placed_by_own_insets();
+ }
Self {
fc_type,
input,
diff --git a/Tests/LibWeb/Text/expected/abspos-run-reuse-across-static-position-moves.txt b/Tests/LibWeb/Text/expected/abspos-run-reuse-across-static-position-moves.txt
new file mode 100644
index 00000000000..b6a5ba0a849
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-run-reuse-across-static-position-moves.txt
@@ -0,0 +1,6 @@
+fixed box placed by insets, body resized: runs=0 matches fresh layout=true
+box with only left set, inline static position moved: runs=0 matches fresh layout=true
+box with auto inline insets, static position moved: laid out again=true at static position=true matches fresh layout=true
+anchor-centered box, anchor moved: laid out again=true centered=true
+vertical-rl box with top set, moved along its inset axis: runs=0 matches fresh layout=true
+vertical-rl box with top set, moved along its static axis: laid out again=true matches fresh layout=true
diff --git a/Tests/LibWeb/Text/input/abspos-run-reuse-across-static-position-moves.html b/Tests/LibWeb/Text/input/abspos-run-reuse-across-static-position-moves.html
new file mode 100644
index 00000000000..a44c5490ad6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-run-reuse-across-static-position-moves.html
@@ -0,0 +1,103 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; font: 16px/20px monospace; }
+ .page { width: 600px; }
+ .relative { position: relative; width: 300px; height: 200px; }
+ .marker { float: left; width: 10px; }
+ #fixed { position: fixed; top: 10px; left: 20px; bottom: 30px; width: 100px; }
+ #left-only { position: absolute; left: 10px; width: 50px; }
+ #auto-inline { position: absolute; top: 5px; width: 50px; }
+ #anchor { anchor-name: --anchor; width: 40px; height: 10px; margin-left: 20px; }
+ #anchor-centered { position: absolute; position-anchor: --anchor; justify-self: anchor-center; top: 50px; width: 20px; height: 10px; }
+ .vertical { writing-mode: vertical-rl; position: relative; width: 200px; height: 300px; }
+ #vertical-top { position: absolute; top: 10px; width: 20px; height: 30px; }
+</style>
+<div class="page" id="page"><p>text</p><div id="fixed"><span class="marker">1</span>fixed</div></div>
+<div class="relative" id="left-only-container"><div id="left-only-wrapper">before<div id="left-only"><span class="marker">1</span>left</div></div></div>
+<div class="relative" id="auto-inline-container"><div id="auto-inline-wrapper">before<div id="auto-inline">auto</div></div></div>
+<div class="relative" id="anchor-container"><div id="anchor"></div><div id="anchor-centered"></div></div>
+<div class="vertical" id="vertical-container"><div id="vertical-wrapper">before<div id="vertical-top">v</div></div></div>
+<script>
+ // An absolutely positioned box takes its offset from its insets in an axis where one of them is set, and sizes
+ // against them there, so moving its static position along that axis reuses its run. Along an axis with both insets
+ // auto, and for an anchor-centered box, the static position rect is read, so the box is laid out again.
+ test(() => {
+ document.body.offsetHeight;
+
+ const runsOf = (id, change) => {
+ internals.beginLayoutTrace();
+ change();
+ document.body.offsetHeight;
+ return internals.takeLayoutTrace().split("\n").filter(line => line.includes(`#${id}/`) && line.includes(" RUN (")).length;
+ };
+ const geometry = root => {
+ const rootRect = root.getBoundingClientRect();
+ const isInFixedBox = element => {
+ for (let ancestor = element; ancestor !== root; ancestor = ancestor.parentElement) {
+ if (getComputedStyle(ancestor).position === "fixed")
+ return true;
+ }
+ return false;
+ };
+ return Array.from(root.querySelectorAll("*")).map(element => {
+ const rect = element.getBoundingClientRect();
+ const origin = isInFixedBox(element) ? { left: 0, top: 0 } : rootRect;
+ return `${rect.left - origin.left},${rect.top - origin.top},${rect.width},${rect.height}`;
+ }).join(" ");
+ };
+ const matchesFreshLayout = root => {
+ const fresh = root.cloneNode(true);
+ document.body.appendChild(fresh);
+ const matches = geometry(root) === geometry(fresh);
+ fresh.remove();
+ document.body.offsetHeight;
+ return matches;
+ };
+
+ const page = document.getElementById("page");
+ const fixedRuns = runsOf("fixed", () => {
+ document.body.style.width = "500px";
+ document.body.style.paddingLeft = "40px";
+ });
+ println(`fixed box placed by insets, body resized: runs=${fixedRuns} matches fresh layout=${matchesFreshLayout(page)}`);
+ document.body.style.width = "";
+ document.body.style.paddingLeft = "";
+ document.body.offsetHeight;
+
+ const leftOnlyContainer = document.getElementById("left-only-container");
+ const leftOnlyRuns = runsOf("left-only", () => {
+ document.getElementById("left-only-wrapper").style.paddingLeft = "30px";
+ });
+ println(`box with only left set, inline static position moved: runs=${leftOnlyRuns} matches fresh layout=${matchesFreshLayout(leftOnlyContainer)}`);
+
+ const autoInlineContainer = document.getElementById("auto-inline-container");
+ const autoInlineWrapper = document.getElementById("auto-inline-wrapper");
+ const autoInlineRuns = runsOf("auto-inline", () => {
+ autoInlineWrapper.style.paddingLeft = "30px";
+ });
+ const autoInlineLeft = document.getElementById("auto-inline").getBoundingClientRect().left;
+ const wrapperContentLeft = autoInlineWrapper.getBoundingClientRect().left + 30;
+ println(`box with auto inline insets, static position moved: laid out again=${autoInlineRuns > 0} at static position=${autoInlineLeft === wrapperContentLeft} matches fresh layout=${matchesFreshLayout(autoInlineContainer)}`);
+
+ const anchor = document.getElementById("anchor");
+ const anchorCenteredRuns = runsOf("anchor-centered", () => {
+ anchor.style.marginLeft = "100px";
+ });
+ const anchorRect = anchor.getBoundingClientRect();
+ const centeredRect = document.getElementById("anchor-centered").getBoundingClientRect();
+ const centered = anchorRect.left + anchorRect.width / 2 === centeredRect.left + centeredRect.width / 2;
+ println(`anchor-centered box, anchor moved: laid out again=${anchorCenteredRuns > 0} centered=${centered}`);
+
+ const verticalContainer = document.getElementById("vertical-container");
+ const verticalWrapper = document.getElementById("vertical-wrapper");
+ const alongInsetAxisRuns = runsOf("vertical-top", () => {
+ verticalWrapper.style.paddingTop = "25px";
+ });
+ println(`vertical-rl box with top set, moved along its inset axis: runs=${alongInsetAxisRuns} matches fresh layout=${matchesFreshLayout(verticalContainer)}`);
+ const alongStaticAxisRuns = runsOf("vertical-top", () => {
+ verticalWrapper.style.paddingRight = "35px";
+ });
+ println(`vertical-rl box with top set, moved along its static axis: laid out again=${alongStaticAxisRuns > 0} matches fresh layout=${matchesFreshLayout(verticalContainer)}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/layout-trace.html b/Tests/LibWeb/Text/input/layout-trace.html
index 5ee56ffe85b..5165382f0da 100644
--- a/Tests/LibWeb/Text/input/layout-trace.html
+++ b/Tests/LibWeb/Text/input/layout-trace.html
@@ -45,20 +45,17 @@ layoutTest({
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<DIV>#target/block REUSE SUBTREE
- BlockContainer<DIV>#boundary/block RUN (cache=miss)
- BlockContainer<DIV>#inside REUSE IN-FLOW SUBTREE
+ BlockContainer<DIV>#boundary/block REUSE SUBTREE
layout FULL
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<DIV>#target/block REUSE SUBTREE
- BlockContainer<DIV>#boundary/block RUN (cache=miss)
- BlockContainer<DIV>#inside REUSE IN-FLOW SUBTREE
+ BlockContainer<DIV>#boundary/block REUSE SUBTREE
layout FULL
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<DIV>#target/block REUSE SUBTREE
- BlockContainer<DIV>#boundary/block RUN (cache=miss)
- BlockContainer<DIV>#inside REUSE IN-FLOW SUBTREE
+ BlockContainer<DIV>#boundary/block REUSE SUBTREE
`,
check: equal => {
equal(target.getBoundingClientRect().top, 30, "top after flush");
@@ -80,8 +77,7 @@ layoutTest({
BlockContainer<HTML>/block RUN (cache=miss)
BlockContainer<DIV>#spacer REUSE IN-FLOW SUBTREE
BlockContainer<DIV>#target/block RUN (cache=miss)
- BlockContainer<DIV>#boundary/block RUN (cache=miss)
- BlockContainer<DIV>#inside REUSE IN-FLOW SUBTREE
+ BlockContainer<DIV>#boundary/block REUSE SUBTREE
layout FULL
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)