Explorar o código

LibWeb: Fix absolute positioning issues

Make sure the insets and margins calculated according to the spec are
not later ignored and ad-hoc recomputed in
layout_absolutely_positioned_element.

Use the static position calculation in a couple of places where the
spec (and comment) was indicating it should be used.

Fixes #19362
Sebastian Zaha %!s(int64=2) %!d(string=hai) anos
pai
achega
fd37ad3a84

+ 10 - 0
Tests/LibWeb/Layout/expected/abspos-not-replaced-multiple-auto-variants.txt

@@ -0,0 +1,10 @@
+Viewport <#document> at (0,0) content-size 800x600 children: not-inline
+  BlockContainer <html> at (1,1) content-size 798x600 [BFC] children: not-inline
+    BlockContainer <body> at (10,10) content-size 780x0 children: inline
+      TextNode <#text>
+      BlockContainer <div.only-t-l> at (6,6) content-size 10x10 positioned [BFC] children: not-inline
+      TextNode <#text>
+      BlockContainer <div.only-mt-ml> at (16,16) content-size 10x10 positioned [BFC] children: not-inline
+      TextNode <#text>
+      BlockContainer <div.both> at (11,11) content-size 10x10 positioned [BFC] children: not-inline
+      TextNode <#text>

+ 1 - 1
Tests/LibWeb/Layout/expected/flex-abspos-item-with-preceding-whitespace.txt

@@ -4,5 +4,5 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       Box <div.pink> at (38,38) content-size 724x0 flex-container(row) [FFC] children: not-inline
         BlockContainer <(anonymous)> (not painted) [BFC] children: inline
           TextNode <#text>
-        BlockContainer <div.orange> at (38,-12) content-size 100x100 positioned [BFC] children: inline
+        BlockContainer <div.orange> at (48,-12) content-size 100x100 positioned [BFC] children: inline
           TextNode <#text>

+ 18 - 18
Tests/LibWeb/Layout/expected/flex/abspos-flex-child-static-position-with-align-items.txt

@@ -4,73 +4,73 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.normal> at (38,38) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (38,48) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (48,48) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 54.578125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 6, rect: [38,48 54.578125x17.46875]
+            frag 0 from TextNode start: 0, length: 6, rect: [48,48 54.578125x17.46875]
               "normal"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.stretch> at (208,38) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (208,48) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (218,48) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 58.796875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 7, rect: [208,48 58.796875x17.46875]
+            frag 0 from TextNode start: 0, length: 7, rect: [218,48 58.796875x17.46875]
               "stretch"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.start> at (378,38) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (378,48) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (388,48) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 41.234375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 5, rect: [378,48 41.234375x17.46875]
+            frag 0 from TextNode start: 0, length: 5, rect: [388,48 41.234375x17.46875]
               "start"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.flex-start> at (548,38) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (548,48) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (558,48) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.8125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [548,48 76.8125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [558,48 76.8125x17.46875]
               "flex-start"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.end> at (38,208) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (38,298) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (48,308) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 26.1875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 3, rect: [38,298 26.1875x17.46875]
+            frag 0 from TextNode start: 0, length: 3, rect: [48,308 26.1875x17.46875]
               "end"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.flex-end> at (208,208) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (208,298) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (218,308) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.765625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [208,298 61.765625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [218,308 61.765625x17.46875]
               "flex-end"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.center> at (378,208) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (378,258) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (388,258) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 51.625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 6, rect: [378,258 51.625x17.46875]
+            frag 0 from TextNode start: 0, length: 6, rect: [388,258 51.625x17.46875]
               "center"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.self-start> at (548,208) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (548,218) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (558,218) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.453125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [548,218 76.453125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [558,218 76.453125x17.46875]
               "self-start"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline
         TextNode <#text>
       Box <div.outer.self-end> at (38,378) content-size 150x150 flex-container(row) flex-item [FFC] children: not-inline
-        BlockContainer <div> at (38,468) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (48,478) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.40625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [38,468 61.40625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [48,478 61.40625x17.46875]
               "self-end"
           TextNode <#text>
       BlockContainer <(anonymous)> (not painted) [BFC] children: inline

+ 40 - 40
Tests/LibWeb/Layout/expected/flex/abspos-flex-child-static-position-with-justify-content.txt

@@ -4,33 +4,33 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,38) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.outer.start> at (53,53) content-size 300x60 flex-container(row) [FFC] children: not-inline
-        BlockContainer <div> at (53,68) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,68) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 41.234375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 5, rect: [53,68 41.234375x17.46875]
+            frag 0 from TextNode start: 0, length: 5, rect: [68,68 41.234375x17.46875]
               "start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,128) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.outer.flex-start> at (53,143) content-size 300x60 flex-container(row) [FFC] children: not-inline
-        BlockContainer <div> at (53,158) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,158) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.8125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [53,158 76.8125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [68,158 76.8125x17.46875]
               "flex-start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,218) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.outer.end> at (53,233) content-size 300x60 flex-container(row) [FFC] children: not-inline
-        BlockContainer <div> at (203,248) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (188,248) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 26.1875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 3, rect: [203,248 26.1875x17.46875]
+            frag 0 from TextNode start: 0, length: 3, rect: [188,248 26.1875x17.46875]
               "end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,308) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.outer.flex-end> at (53,323) content-size 300x60 flex-container(row) [FFC] children: not-inline
-        BlockContainer <div> at (203,338) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (188,338) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.765625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [203,338 61.765625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [188,338 61.765625x17.46875]
               "flex-end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,398) content-size 724x0 children: inline
@@ -52,9 +52,9 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,578) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.outer.space-between> at (53,593) content-size 300x60 flex-container(row) [FFC] children: not-inline
-        BlockContainer <div> at (53,608) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,608) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 115.515625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 13, rect: [53,608 115.515625x17.46875]
+            frag 0 from TextNode start: 0, length: 13, rect: [68,608 115.515625x17.46875]
               "space-between"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,668) content-size 724x0 children: inline
@@ -68,33 +68,33 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,758) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.reverse.outer.start> at (53,773) content-size 300x60 flex-container(row-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (203,788) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (218,788) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 41.234375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 5, rect: [203,788 41.234375x17.46875]
+            frag 0 from TextNode start: 0, length: 5, rect: [218,788 41.234375x17.46875]
               "start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,848) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.reverse.outer.flex-start> at (53,863) content-size 300x60 flex-container(row-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (203,878) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (218,878) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.8125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [203,878 76.8125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [218,878 76.8125x17.46875]
               "flex-start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,938) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.reverse.outer.end> at (53,953) content-size 300x60 flex-container(row-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (53,968) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (38,968) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 26.1875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 3, rect: [53,968 26.1875x17.46875]
+            frag 0 from TextNode start: 0, length: 3, rect: [38,968 26.1875x17.46875]
               "end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1028) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.reverse.outer.flex-end> at (53,1043) content-size 300x60 flex-container(row-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (53,1058) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (38,1058) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.765625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [53,1058 61.765625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [38,1058 61.765625x17.46875]
               "flex-end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1118) content-size 724x0 children: inline
@@ -116,9 +116,9 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,1298) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.row.reverse.outer.space-between> at (53,1313) content-size 300x60 flex-container(row-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (53,1328) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,1328) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 115.515625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 13, rect: [53,1328 115.515625x17.46875]
+            frag 0 from TextNode start: 0, length: 13, rect: [68,1328 115.515625x17.46875]
               "space-between"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1388) content-size 724x0 children: inline
@@ -132,33 +132,33 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,1478) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.outer.start> at (53,1493) content-size 300x60 flex-container(column) [FFC] children: not-inline
-        BlockContainer <div> at (68,1493) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,1508) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 41.234375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 5, rect: [68,1493 41.234375x17.46875]
+            frag 0 from TextNode start: 0, length: 5, rect: [68,1508 41.234375x17.46875]
               "start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1568) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.outer.flex-start> at (53,1583) content-size 300x60 flex-container(column) [FFC] children: not-inline
-        BlockContainer <div> at (68,1583) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,1598) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.8125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [68,1583 76.8125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [68,1598 76.8125x17.46875]
               "flex-start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1658) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.outer.end> at (53,1673) content-size 300x60 flex-container(column) [FFC] children: not-inline
-        BlockContainer <div> at (68,1683) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,1668) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 26.1875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 3, rect: [68,1683 26.1875x17.46875]
+            frag 0 from TextNode start: 0, length: 3, rect: [68,1668 26.1875x17.46875]
               "end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1748) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.outer.flex-end> at (53,1763) content-size 300x60 flex-container(column) [FFC] children: not-inline
-        BlockContainer <div> at (68,1773) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,1758) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.765625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [68,1773 61.765625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [68,1758 61.765625x17.46875]
               "flex-end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,1838) content-size 724x0 children: inline
@@ -180,9 +180,9 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,2018) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.outer.space-between> at (53,2033) content-size 300x60 flex-container(column) [FFC] children: not-inline
-        BlockContainer <div> at (68,2033) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2048) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 115.515625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 13, rect: [68,2033 115.515625x17.46875]
+            frag 0 from TextNode start: 0, length: 13, rect: [68,2048 115.515625x17.46875]
               "space-between"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2108) content-size 724x0 children: inline
@@ -196,33 +196,33 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,2198) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.reverse.outer.start> at (53,2213) content-size 300x60 flex-container(column-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (68,2223) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2238) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 41.234375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 5, rect: [68,2223 41.234375x17.46875]
+            frag 0 from TextNode start: 0, length: 5, rect: [68,2238 41.234375x17.46875]
               "start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2288) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.reverse.outer.flex-start> at (53,2303) content-size 300x60 flex-container(column-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (68,2313) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2328) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 76.8125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 10, rect: [68,2313 76.8125x17.46875]
+            frag 0 from TextNode start: 0, length: 10, rect: [68,2328 76.8125x17.46875]
               "flex-start"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2378) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.reverse.outer.end> at (53,2393) content-size 300x60 flex-container(column-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (68,2393) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2378) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 26.1875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 3, rect: [68,2393 26.1875x17.46875]
+            frag 0 from TextNode start: 0, length: 3, rect: [68,2378 26.1875x17.46875]
               "end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2468) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.reverse.outer.flex-end> at (53,2483) content-size 300x60 flex-container(column-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (68,2483) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2468) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 61.765625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 8, rect: [68,2483 61.765625x17.46875]
+            frag 0 from TextNode start: 0, length: 8, rect: [68,2468 61.765625x17.46875]
               "flex-end"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2558) content-size 724x0 children: inline
@@ -244,9 +244,9 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       BlockContainer <(anonymous)> at (38,2738) content-size 724x0 children: inline
         TextNode <#text>
       Box <div.column.reverse.outer.space-between> at (53,2753) content-size 300x60 flex-container(column-reverse) [FFC] children: not-inline
-        BlockContainer <div> at (68,2753) content-size 150x50 positioned [BFC] children: inline
+        BlockContainer <div> at (68,2768) content-size 150x50 positioned [BFC] children: inline
           line 0 width: 115.515625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
-            frag 0 from TextNode start: 0, length: 13, rect: [68,2753 115.515625x17.46875]
+            frag 0 from TextNode start: 0, length: 13, rect: [68,2768 115.515625x17.46875]
               "space-between"
           TextNode <#text>
       BlockContainer <(anonymous)> at (38,2828) content-size 724x0 children: inline

+ 5 - 5
Tests/LibWeb/Layout/expected/position-absolute-from-edges.txt

@@ -1,14 +1,14 @@
 Viewport <#document> at (0,0) content-size 800x600 children: not-inline
   BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
     BlockContainer <body> at (8,8) content-size 784x0 children: inline
-      BlockContainer <div#container> at (8,8) content-size 500x400 positioned [BFC] children: inline
+      BlockContainer <div#container> at (28,28) content-size 500x400 positioned [BFC] children: inline
         TextNode <#text>
-        BlockContainer <div#red> at (38,38) content-size 100x100 positioned [BFC] children: not-inline
+        BlockContainer <div#red> at (58,58) content-size 100x100 positioned [BFC] children: not-inline
         TextNode <#text>
-        BlockContainer <div#green> at (338,78) content-size 100x100 positioned [BFC] children: not-inline
+        BlockContainer <div#green> at (358,98) content-size 100x100 positioned [BFC] children: not-inline
         TextNode <#text>
-        BlockContainer <div#blue> at (28,288) content-size 100x100 positioned [BFC] children: not-inline
+        BlockContainer <div#blue> at (48,308) content-size 100x100 positioned [BFC] children: not-inline
         TextNode <#text>
-        BlockContainer <div#yellow> at (388,288) content-size 100x100 positioned [BFC] children: not-inline
+        BlockContainer <div#yellow> at (408,308) content-size 100x100 positioned [BFC] children: not-inline
         TextNode <#text>
       TextNode <#text>

+ 27 - 0
Tests/LibWeb/Layout/input/abspos-not-replaced-multiple-auto-variants.html

@@ -0,0 +1,27 @@
+<style>
+*  { border: 1px solid black; }
+div {
+    position: absolute;
+    width: 10px;
+    height: 10px;
+}
+.only-t-l {
+    top: 5px;
+    left: 5px;
+    background-color: red;
+}
+.only-mt-ml {
+    margin: 5px;
+    background-color: orange;
+}
+.both {
+    top: 5px;
+    left: 5px;
+    margin: 5px;
+    background-color: green;
+}
+</style>
+<body>
+    <div class="only-t-l"></div>
+    <div class="only-mt-ml"></div>
+    <div class="both"></div>

+ 6 - 5
Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp

@@ -2155,8 +2155,9 @@ CSSPixelPoint FlexFormattingContext::calculate_static_position(Box const& box) c
     CSSPixels cross_margin_after = is_row_layout() ? box_state.margin_bottom : box_state.margin_right;
     CSSPixels cross_border_before = is_row_layout() ? box_state.border_top : box_state.border_left;
     CSSPixels cross_border_after = is_row_layout() ? box_state.border_bottom : box_state.border_right;
-    CSSPixels cross_padding_before = is_row_layout() ? box_state.padding_top : box_state.padding_left;
     CSSPixels cross_padding_after = is_row_layout() ? box_state.padding_bottom : box_state.padding_right;
+    CSSPixels main_border_before = is_row_layout() ? box_state.border_left : box_state.border_top;
+    CSSPixels main_border_after = is_row_layout() ? box_state.border_right : box_state.border_bottom;
 
     switch (alignment_for_item(box)) {
     case CSS::AlignItems::Baseline:
@@ -2167,7 +2168,7 @@ CSSPixelPoint FlexFormattingContext::calculate_static_position(Box const& box) c
     case CSS::AlignItems::SelfStart:
     case CSS::AlignItems::Stretch:
     case CSS::AlignItems::Normal:
-        cross_offset = -half_line_size + cross_margin_before + cross_border_before + cross_padding_before;
+        cross_offset = -half_line_size + cross_margin_before;
         break;
     case CSS::AlignItems::End:
     case CSS::AlignItems::SelfEnd:
@@ -2175,7 +2176,7 @@ CSSPixelPoint FlexFormattingContext::calculate_static_position(Box const& box) c
         cross_offset = half_line_size - inner_cross_size(box) - cross_margin_after - cross_border_after - cross_padding_after;
         break;
     case CSS::AlignItems::Center:
-        cross_offset = -(inner_cross_size(box) / 2.0);
+        cross_offset = -((inner_cross_size(box) + cross_border_before + cross_border_after) / 2.0);
         break;
     default:
         break;
@@ -2198,7 +2199,7 @@ CSSPixelPoint FlexFormattingContext::calculate_static_position(Box const& box) c
         break;
     case CSS::JustifyContent::End:
     case CSS::JustifyContent::FlexEnd:
-        main_offset = 0;
+        main_offset = -main_border_before - main_border_after;
         pack_from_end = !is_direction_reverse();
         break;
     case CSS::JustifyContent::SpaceBetween:
@@ -2209,7 +2210,7 @@ CSSPixelPoint FlexFormattingContext::calculate_static_position(Box const& box) c
     case CSS::JustifyContent::SpaceAround:
     case CSS::JustifyContent::SpaceEvenly:
         pack_from_end = false;
-        main_offset = inner_main_size(flex_container()) / 2.0 - inner_main_size(box) / 2.0;
+        main_offset = (inner_main_size(flex_container()) - inner_main_size(box) - main_border_before - main_border_after) / 2.0;
         break;
     }
 

+ 27 - 65
Userland/Libraries/LibWeb/Layout/FormattingContext.cpp

@@ -578,13 +578,13 @@ void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_ele
 
     auto computed_left = computed_values.inset().left();
     auto computed_right = computed_values.inset().right();
+    auto left = computed_values.inset().left().to_px(box, width_of_containing_block);
+    auto right = computed_values.inset().right().to_px(box, width_of_containing_block);
 
     auto try_compute_width = [&](auto const& a_width) {
         margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length);
         margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length);
 
-        auto left = computed_values.inset().left().to_px(box, width_of_containing_block);
-        auto right = computed_values.inset().right().to_px(box, width_of_containing_block);
         auto width = a_width;
 
         auto solve_for_left = [&] {
@@ -610,7 +610,8 @@ void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_ele
             // is 'ltr' set 'left' to the static position and apply rule number three below;
             // otherwise, set 'right' to the static position and apply rule number one below.
             // FIXME: This is very hackish.
-            left = 0;
+            auto static_position = calculate_static_position(box);
+            left = static_position.x();
             goto Rule3;
         }
 
@@ -666,8 +667,8 @@ void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_ele
         //    Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
         else if (computed_left.is_auto() && computed_right.is_auto() && !width.is_auto()) {
             // FIXME: Check direction
-            // FIXME: Use the static-position containing block
-            left = 0;
+            auto static_position = calculate_static_position(box);
+            left = static_position.x();
             right = solve_for_right();
         }
 
@@ -722,11 +723,10 @@ void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_ele
 
     auto& box_state = m_state.get_mutable(box);
     box_state.set_content_width(used_width.to_px(box));
-
+    box_state.inset_left = left;
+    box_state.inset_right = right;
     box_state.margin_left = margin_left.to_px(box);
     box_state.margin_right = margin_right.to_px(box);
-    box_state.border_left = border_left;
-    box_state.border_right = border_right;
     box_state.padding_left = padding_left;
     box_state.padding_right = padding_right;
 }
@@ -1014,15 +1014,18 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el
     //       the final used values for vertical margin/border/padding.
 
     auto& box_state = m_state.get_mutable(box);
+    box_state.set_content_height(used_height);
+
+    // do not set calculated insets or margins on the first pass, there will be a second pass
+    if (before_or_after_inside_layout == BeforeOrAfterInsideLayout::Before)
+        return;
+
+    box_state.inset_top = top.to_px(box, width_of_containing_block);
+    box_state.inset_bottom = bottom.to_px(box, width_of_containing_block);
     box_state.margin_top = margin_top.to_px(box, width_of_containing_block);
     box_state.margin_bottom = margin_bottom.to_px(box, width_of_containing_block);
-    box_state.border_top = box.computed_values().border_top().width;
-    box_state.border_bottom = box.computed_values().border_bottom().width;
     box_state.padding_top = box.computed_values().padding().top().to_px(box, width_of_containing_block);
     box_state.padding_bottom = box.computed_values().padding().bottom().to_px(box, width_of_containing_block);
-
-    // And here is where we assign the box's content height.
-    box_state.set_content_height(used_height);
 }
 
 // NOTE: This is different from content_box_rect_in_ancestor_coordinate_space() as this does *not* follow the containing block chain up, but rather the parent() chain.
@@ -1077,9 +1080,9 @@ CSSPixelPoint FormattingContext::calculate_static_position(Box const& box) const
             // Easy case: no previous sibling, we're at the top of the containing block.
         }
     } else {
-        x = m_state.get(box).margin_box_left();
+        x = m_state.get(box).margin_left;
         // We're among block siblings, Y can be calculated easily.
-        y = m_state.get(box).margin_box_top();
+        y = m_state.get(box).margin_top;
     }
     auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
     return offset_to_static_parent.location().translated(x, y);
@@ -1095,6 +1098,13 @@ void FormattingContext::layout_absolutely_positioned_element(Box const& box, Ava
     auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
     auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
 
+    // The border computed values are not changed by the compute_height & width calculations below.
+    // The spec only adjusts and computes sizes, insets and margins.
+    box_state.border_left = box.computed_values().border_left().width;
+    box_state.border_right = box.computed_values().border_right().width;
+    box_state.border_top = box.computed_values().border_top().width;
+    box_state.border_bottom = box.computed_values().border_bottom().width;
+
     compute_width_for_absolutely_positioned_element(box, available_space);
 
     // NOTE: We compute height before *and* after doing inside layout.
@@ -1106,59 +1116,11 @@ void FormattingContext::layout_absolutely_positioned_element(Box const& box, Ava
 
     compute_height_for_absolutely_positioned_element(box, available_space, BeforeOrAfterInsideLayout::After);
 
-    box_state.margin_top = box.computed_values().margin().top().to_px(box, width_of_containing_block);
-    box_state.margin_bottom = box.computed_values().margin().bottom().to_px(box, width_of_containing_block);
-
-    box_state.border_left = box.computed_values().border_left().width;
-    box_state.border_right = box.computed_values().border_right().width;
-    box_state.border_top = box.computed_values().border_top().width;
-    box_state.border_bottom = box.computed_values().border_bottom().width;
-
-    auto const& computed_left = box.computed_values().inset().left();
-    auto const& computed_right = box.computed_values().inset().right();
-    auto const& computed_top = box.computed_values().inset().top();
-    auto const& computed_bottom = box.computed_values().inset().bottom();
-
-    box_state.inset_left = computed_left.to_px(box, width_of_containing_block);
-    box_state.inset_top = computed_top.to_px(box, height_of_containing_block);
-    box_state.inset_right = computed_right.to_px(box, width_of_containing_block);
-    box_state.inset_bottom = computed_bottom.to_px(box, height_of_containing_block);
-
-    auto static_position = calculate_static_position(box);
-
     CSSPixelPoint used_offset;
-
-    if (!computed_left.is_auto()) {
-        CSSPixels x_offset = box_state.inset_left
-            + box_state.border_box_left();
-        used_offset.set_x(x_offset + box_state.margin_left);
-    } else if (!computed_right.is_auto()) {
-        CSSPixels x_offset = CSSPixels(0)
-            - box_state.inset_right
-            - box_state.border_box_right();
-        used_offset.set_x(width_of_containing_block + x_offset - box_state.content_width() - box_state.margin_right);
-    } else {
-        // NOTE: static position is content box position so border_box and margin should not be added
-        used_offset.set_x(static_position.x());
-    }
-
-    if (!computed_top.is_auto()) {
-        CSSPixels y_offset = box_state.inset_top
-            + box_state.border_box_top();
-        used_offset.set_y(y_offset + box_state.margin_top);
-    } else if (!computed_bottom.is_auto()) {
-        CSSPixels y_offset = CSSPixels(0)
-            - box_state.inset_bottom
-            - box_state.border_box_bottom();
-        used_offset.set_y(height_of_containing_block + y_offset - box_state.content_height() - box_state.margin_bottom);
-    } else {
-        // NOTE: static position is content box position so border_box and margin should not be added
-        used_offset.set_y(static_position.y());
-    }
-
+    used_offset.set_x(box_state.inset_left + box_state.margin_box_left());
+    used_offset.set_y(box_state.inset_top + box_state.margin_box_top());
     // NOTE: Absolutely positioned boxes are relative to the *padding edge* of the containing block.
     used_offset.translate_by(-containing_block_state.padding_left, -containing_block_state.padding_top);
-
     box_state.set_content_offset(used_offset);
 
     if (independent_formatting_context)