Procházet zdrojové kódy

Add browser sticky bottom and containing-block clamp

Johannes Zellner před 1 týdnem
rodič
revize
f502cad228

+ 8 - 10
AGENTS.md

@@ -830,7 +830,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   (the browser fetches remote faces before layout).  The compatibility
   ladder is `testdata/browser/compat/`
-  (`make compat-serve`); pages 010–039 and 030a pass (036a needs the
+  (`make compat-serve`); pages 010–040 and 030a pass (036a needs the
   POST echo server).  Inline
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<video>` gets a placeholder box.  The default page canvas follows
@@ -1549,22 +1549,20 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     errors even though the static page renders.  CSS also lacks multiple
     background layers, `background-attachment`, 3D/perspective transforms
     and transformed text/descendants (`scale`/`rotate` paint the element box
-    and inline images; layout geometry is unchanged), sticky beyond the top
-    edge, border-radius clipping
+    and inline images; layout geometry is unchanged), border-radius clipping
     of descendant content, gradients/box-shadow/transitions (a gradient
     background falls back to its last solid colour), or CSS `color-mix`.
     A non-uniform `background-size` uses the width scale for both axes.
     CSS custom properties (`var()`), flex rows with wrap/grow/gap/justify/
-    align, two-track and
-    `repeat(auto-fit, minmax(...))` grids, inline-block runs, basic
-    proportional table columns, table rowspan/colspan/percentage columns and
-    inline SVG (`rect`, `line`, `path` with M/L/H/V, and `text`) are
+    align, two-track and `repeat(auto-fit, minmax(...))` grids, inline-block
+    runs, basic proportional table columns, table rowspan/colspan/percentage
+    columns and inline SVG (`rect`, `line`, `path` with M/L/H/V, and `text`) are
     supported.
     `position:fixed`/`absolute` boxes are deferred and painted above the
     flow; `z-index` sorts that deferred list; `position:relative` shifts
-    painting without moving flow, and `position:sticky` clamps against the
-    top edge (no bottom/container clamping).  `prefers-color-scheme` media
-    queries are ignored so the page's base palette is used.  Block
+    painting without moving flow, and `position:sticky` supports top and
+    bottom offsets and clamps to the containing block.  `prefers-color-scheme`
+    media queries are ignored so the page's base palette is used.  Block
     `width`/`max-width`/`min-width`, content-box `height` with `min`/`max`,
     `box-sizing: border-box`, `margin: 0 auto` and `calc(percent ± px)` work;
     percentage widths resolve against the containing block.  Float support

+ 33 - 4
browser.c

@@ -193,6 +193,7 @@ struct lay {
 	int y;
 	int line_x, line_y, line_h;
 	int line_left, line_right;
+	int parent_top;
 	int run_x;
 	int align;
 	int depth;
@@ -4387,6 +4388,7 @@ static void layout_block(struct lay *l, unsigned int node,
 			 unsigned short link)
 {
 	int old_left = l->left, old_right = l->right, old_align = l->align;
+	int saved_parent_top = l->parent_top;
 	int top, l0, r0;
 	int flow_y, flow_left, flow_right, positioned, relative, sticky, shifted;
 	int cbw, ml, mr, bl, br, pl, pr, bt, bb, pt, pb;
@@ -4538,16 +4540,40 @@ static void layout_block(struct lay *l, unsigned int node,
 		r0 = l0 + 8;
 	normal_top = top;
 	if (sticky) {
-		int stick = page_rect.y + 4 + (st->top_set ? st->top : 0);
-
-		if (normal_top < stick)
-			rel_dy += stick - normal_top;
+		if (st->top_set) {
+			int stick = page_rect.y + 4 + st->top;
+
+			if (normal_top < stick)
+				rel_dy += stick - normal_top;
+		} else if (st->bottom_set && block_h[node]) {
+			int stick = page_rect.y + page_rect.h - st->bottom -
+				    block_h[node];
+
+			if (normal_top > stick)
+				rel_dy += stick - normal_top;
+		}
+		if (l->draw && doc.nodes[node].parent < BR_MAX_NODES &&
+		    block_h[doc.nodes[node].parent]) {
+			unsigned int p = doc.nodes[node].parent;
+			int ptop = l->parent_top;
+			int pbottom = ptop + block_h[p];
+			int eh = block_h[node];
+			int moved_top = normal_top + rel_dy;
+			int moved_bottom = moved_top + eh;
+
+			if (moved_bottom > pbottom)
+				rel_dy -= moved_bottom - pbottom;
+			moved_top = normal_top + rel_dy;
+			if (moved_top < ptop)
+				rel_dy += ptop - moved_top;
+		}
 	}
 	if (shifted && l->draw) {
 		top += rel_dy;
 		l0 += rel_dx;
 		r0 += rel_dx;
 	}
+	l->parent_top = top;
 	if (l->draw && block_h[node] &&
 	    top < page_rect.y + page_rect.h &&
 	    top + block_h[node] > page_rect.y) {
@@ -4756,6 +4782,7 @@ static void layout_block(struct lay *l, unsigned int node,
 		l->left = flow_left;
 		l->right = flow_right;
 	}
+	l->parent_top = saved_parent_top;
 }
 
 
@@ -6419,6 +6446,7 @@ static int measure_page(int left, int right)
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
 	l->float_active = 0;
+	l->parent_top = 0;
 	l->depth = 0;
 	l->nruns = 0;
 	l->space_pending = 0;
@@ -6472,6 +6500,7 @@ static void render_page(int left, int right, int y0)
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
 	l->float_active = 0;
+	l->parent_top = 0;
 	l->depth = 0;
 	l->nruns = 0;
 	l->space_pending = 0;

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 1
docs/userland.html


+ 127 - 0
testdata/browser/compat/040-sticky-bottom.html

@@ -0,0 +1,127 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>040 sticky edges</title>
+<style>
+html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
+body { font-family: sans-serif; font-size: 13px; padding: 10px; }
+h1 { font-size: 20px; margin: 0 0 6px 0; }
+p { margin: 0 0 8px 0; }
+.pane { float: left; width: 480px; margin-left: 10px; }
+.label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
+.section { position: relative; height: 1500px; background: #f4f4f4;
+           border: 1px solid #999999; padding: 6px; }
+.stickytop { position: sticky; top: 0; height: 26px; background: #3366cc;
+             color: #ffffff; padding-left: 4px; }
+.statictop { height: 26px; background: #888888; color: #ffffff;
+             padding-left: 4px; }
+.stickybot { position: sticky; bottom: 0; height: 26px; background: #cc3333;
+             color: #ffffff; padding-left: 4px; }
+.staticbot { height: 26px; background: #aa7777; color: #ffffff;
+             padding-left: 4px; }
+.line { height: 22px; border-bottom: 1px solid #dddddd; }
+.foot { clear: both; margin-top: 10px; color: #555555; }
+</style>
+</head>
+<body>
+<h1>040 &middot; sticky top, sticky bottom and the containing block</h1>
+<p>Scroll down: the left header sticks to the viewport top and the left footer to its bottom; both stop at the section edges. The right column is static.</p>
+<div class="pane">
+  <div class="label">A &middot; position: sticky</div>
+  <div class="section">
+    <div class="stickytop">sticky top</div>
+    <div class="line">line 1</div>
+    <div class="line">line 2</div>
+    <div class="line">line 3</div>
+    <div class="line">line 4</div>
+    <div class="line">line 5</div>
+    <div class="line">line 6</div>
+    <div class="line">line 7</div>
+    <div class="line">line 8</div>
+    <div class="line">line 9</div>
+    <div class="line">line 10</div>
+    <div class="line">line 11</div>
+    <div class="line">line 12</div>
+    <div class="line">line 13</div>
+    <div class="line">line 14</div>
+    <div class="line">line 15</div>
+    <div class="line">line 16</div>
+    <div class="line">line 17</div>
+    <div class="line">line 18</div>
+    <div class="line">line 19</div>
+    <div class="line">line 20</div>
+    <div class="line">line 21</div>
+    <div class="line">line 22</div>
+    <div class="line">line 23</div>
+    <div class="line">line 24</div>
+    <div class="line">line 25</div>
+    <div class="line">line 26</div>
+    <div class="line">line 27</div>
+    <div class="line">line 28</div>
+    <div class="line">line 29</div>
+    <div class="line">line 30</div>
+    <div class="line">line 31</div>
+    <div class="line">line 32</div>
+    <div class="line">line 33</div>
+    <div class="line">line 34</div>
+    <div class="line">line 35</div>
+    <div class="line">line 36</div>
+    <div class="line">line 37</div>
+    <div class="line">line 38</div>
+    <div class="line">line 39</div>
+    <div class="line">line 40</div>
+    <div class="stickybot">sticky bottom</div>
+  </div>
+</div>
+<div class="pane">
+  <div class="label">B &middot; static reference</div>
+  <div class="section">
+    <div class="statictop">static top</div>
+    <div class="line">line 1</div>
+    <div class="line">line 2</div>
+    <div class="line">line 3</div>
+    <div class="line">line 4</div>
+    <div class="line">line 5</div>
+    <div class="line">line 6</div>
+    <div class="line">line 7</div>
+    <div class="line">line 8</div>
+    <div class="line">line 9</div>
+    <div class="line">line 10</div>
+    <div class="line">line 11</div>
+    <div class="line">line 12</div>
+    <div class="line">line 13</div>
+    <div class="line">line 14</div>
+    <div class="line">line 15</div>
+    <div class="line">line 16</div>
+    <div class="line">line 17</div>
+    <div class="line">line 18</div>
+    <div class="line">line 19</div>
+    <div class="line">line 20</div>
+    <div class="line">line 21</div>
+    <div class="line">line 22</div>
+    <div class="line">line 23</div>
+    <div class="line">line 24</div>
+    <div class="line">line 25</div>
+    <div class="line">line 26</div>
+    <div class="line">line 27</div>
+    <div class="line">line 28</div>
+    <div class="line">line 29</div>
+    <div class="line">line 30</div>
+    <div class="line">line 31</div>
+    <div class="line">line 32</div>
+    <div class="line">line 33</div>
+    <div class="line">line 34</div>
+    <div class="line">line 35</div>
+    <div class="line">line 36</div>
+    <div class="line">line 37</div>
+    <div class="line">line 38</div>
+    <div class="line">line 39</div>
+    <div class="line">line 40</div>
+    <div class="staticbot">static bottom</div>
+  </div>
+</div>
+<div class="foot">A: sticky header/footer, bounded by the section. B: same content without sticky.</div>
+<div style="clear:both;height:600px;background:#ffffff">after the sections</div>
+</body>
+</html>

+ 2 - 3
testdata/browser/compat/README

@@ -17,14 +17,13 @@ Serve this directory from the host and open it on the board:
   browser http://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
-Status: 010-039 and 030a pass.  036a needs the POST echo server:
+Status: 010-040 and 030a pass.  036a needs the POST echo server:
 
   python3 testdata/browser/compat/post_server.py
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo
 
 Still missing from the ladder: 3D/perspective transforms, transformed
-text/descendants, sticky bottom/container clamping, and calc() beyond
-widths.
+text/descendants, and calc() beyond widths.
 
 Rules for new pages:
   - one behaviour per page;

+ 1 - 0
testdata/browser/compat/index.html

@@ -62,6 +62,7 @@ a { color: #0645ad; }
 <tr><td>037</td><td><a href="037-font-face.html">@font-face</a></td><td>custom TTF family and missing-URL fallback</td><td class="pass">PASS</td></tr>
 <tr><td>038</td><td><a href="038-background-props.html">background props</a></td><td>repeat, position, contain/cover and the shorthand URL</td><td class="pass">PASS</td></tr>
 <tr><td>039</td><td><a href="039-transform-scale-rotate.html">transform</a></td><td>scale, rotate, transform-origin on images and boxes</td><td class="pass">PASS</td></tr>
+<tr><td>040</td><td><a href="040-sticky-bottom.html">sticky edges</a></td><td>sticky top/bottom clamped to the containing block</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů