Jelajahi Sumber

Add browser relative z-index and overflow clipping

Johannes Zellner 1 Minggu lalu
induk
melakukan
9f71f20446

+ 4 - 3
AGENTS.md

@@ -820,7 +820,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `text-transform: uppercase`, `display`, explicit/percentage block
   `width`/`height` with `min`/`max` and `box-sizing: border-box`,
   `margin: 0 auto`, `calc(percent ± px)` in widths, margins/padding,
-  heights and min/max, `white-space: nowrap`,
+  heights and min/max, `overflow: hidden` box clipping, `white-space: nowrap`,
   `position: relative`/`absolute`/`fixed`/`sticky`, `right`/`bottom`,
   `z-index`, `transform: translate`/`scale`/`rotate` with
   `transform-origin` (paint-time box and inline-image transforms), flex rows
@@ -831,7 +831,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–042 and 030a pass (036a needs the
+  (`make compat-serve`); pages 010–044 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
@@ -1562,7 +1562,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     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` supports top and
+    painting without moving flow (and joins the deferred stack when it has a
+    z-index), 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`,

+ 37 - 2
browser.c

@@ -4742,6 +4742,8 @@ static void layout_block(struct lay *l, unsigned int node,
 	int definite_w = 0;
 	int normal_top = 0;
 	int rel_dx = 0, rel_dy = 0;
+	int clip_pushed = 0;
+	struct ngui_rect clip_rect;
 
 	if (depth > 24 || st->display == BR_DISPLAY_NONE)
 		return;
@@ -4980,6 +4982,17 @@ static void layout_block(struct lay *l, unsigned int node,
 				draw_border_box(l->ui, box, st);
 		}
 	}
+	if (l->draw && st->overflow_hidden && block_h[node] > 0 &&
+	    l->ui && l->ui->nclip < 4) {
+		clip_rect.x = l0 + bl;
+		clip_rect.y = top + bt;
+		clip_rect.w = (r0 - l0) - bl - br;
+		clip_rect.h = (int)block_h[node] - bt - bb;
+		if (clip_rect.w > 0 && clip_rect.h > 0) {
+			ngui_push_clip(l->ui, clip_rect);
+			clip_pushed = 1;
+		}
+	}
 	l->left = l0 + bl + pl;
 	l->right = l->left + content_w;
 	if (l->right < l->left + 8)
@@ -5080,6 +5093,8 @@ static void layout_block(struct lay *l, unsigned int node,
 			layout_children(l, node, st, depth + 1, link);
 	}
 	flush_line(l);
+	if (clip_pushed)
+		ngui_pop_clip(l->ui);
 	if (l->float_active) {
 		if (l->y < l->float_bottom)
 			l->y = l->float_bottom;
@@ -6697,8 +6712,28 @@ static void layout_children(struct lay *l, unsigned int parent,
 		}
 		if (nd->tag == BR_TAG_A && nd->href_off)
 			lk = (unsigned short)n;
-		if (cs.pos == 1 || cs.pos == 2) {
-			if (l->draw && ndeferred < 16) {
+		if (cs.pos == 1 || cs.pos == 2 ||
+		    (cs.pos == 3 && cs.z_set)) {
+			if (cs.pos == 3 && l->draw && ndeferred < 16) {
+				/* Relative z-index: measure and advance the flow
+				 * without painting, then paint it in the
+				 * deferred (stacked) pass. */
+				int fy = l->y, fl = l->left, fr = l->right;
+				int save_draw = l->draw;
+
+				l->draw = 0;
+				layout_block(l, n, &cs, depth + 1, lk);
+				l->draw = save_draw;
+				deferred[ndeferred].node = n;
+				deferred[ndeferred].flow_y = fy;
+				deferred[ndeferred].flow_left = fl;
+				deferred[ndeferred].flow_right = fr;
+				deferred[ndeferred].link = lk;
+				deferred[ndeferred].z_index = cs.z_index;
+				deferred[ndeferred].z_set = cs.z_set;
+				deferred[ndeferred].order = (unsigned int)ndeferred;
+				ndeferred++;
+			} else if (l->draw && ndeferred < 16) {
 				deferred[ndeferred].node = n;
 				deferred[ndeferred].flow_y = l->y;
 				deferred[ndeferred].flow_left = l->left;

File diff ditekan karena terlalu besar
+ 1 - 1
docs/userland.html


+ 41 - 0
testdata/browser/compat/043-relative-zindex.html

@@ -0,0 +1,41 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>043 relative z-index</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 10px 0; }
+.pane { float: left; width: 440px; margin-left: 10px; }
+.label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
+.stage { position: relative; width: 260px; height: 220px;
+         background: #f4f4f4; border: 1px solid #999999; }
+.red { position: relative; left: 20px; top: 20px; width: 120px; height: 100px;
+       background: #cc3333; }
+.blue { position: relative; left: 60px; top: -60px; width: 120px; height: 100px;
+        background: #3366cc; }
+.z2 { z-index: 2; }
+.z1 { z-index: 1; }
+</style>
+</head>
+<body>
+<h1>043 &middot; relative z-index</h1>
+<p>A: red has z-index 2, blue has z-index 1, so red wins the overlap. B: the same DOM order without z-index paints blue last.</p>
+<div class="pane">
+  <div class="label">A &middot; z-index: red 2, blue 1</div>
+  <div class="stage">
+    <div class="red z2"></div>
+    <div class="blue z1"></div>
+  </div>
+</div>
+<div class="pane">
+  <div class="label">B &middot; no z-index (DOM order)</div>
+  <div class="stage">
+    <div class="red"></div>
+    <div class="blue"></div>
+  </div>
+</div>
+</body>
+</html>

+ 33 - 0
testdata/browser/compat/044-overflow-hidden.html

@@ -0,0 +1,33 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>044 overflow hidden</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 10px 0; }
+.pane { float: left; width: 440px; margin-left: 10px; }
+.label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
+.stage { width: 200px; height: 100px; overflow: hidden;
+         background: #eeeeee; border: 1px solid #999999; }
+.big { position: relative; left: 30px; top: 30px; width: 300px;
+       height: 200px; background: #cc3333; }
+.small { position: relative; left: 30px; top: 30px; width: 170px;
+         height: 70px; background: #cc3333; }
+</style>
+</head>
+<body>
+<h1>044 &middot; overflow: hidden</h1>
+<p>The large red child must be clipped to the visible 170&times;70 rectangle in both panes.</p>
+<div class="pane">
+  <div class="label">A &middot; overflow: hidden</div>
+  <div class="stage"><div class="big"></div></div>
+</div>
+<div class="pane">
+  <div class="label">B &middot; clipped reference</div>
+  <div class="stage"><div class="small"></div></div>
+</div>
+</body>
+</html>

+ 1 - 1
testdata/browser/compat/README

@@ -17,7 +17,7 @@ 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-042 and 030a pass.  036a needs the POST echo server:
+Status: 010-044 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

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

@@ -65,6 +65,8 @@ a { color: #0645ad; }
 <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>
 <tr><td>041</td><td><a href="041-calc-boxes.html">calc boxes</a></td><td>calc() in margins/padding, heights and min/max</td><td class="pass">PASS</td></tr>
 <tr><td>042</td><td><a href="042-computed-style.html">computed style</a></td><td>getComputedStyle values and the display:none case</td><td class="pass">PASS</td></tr>
+<tr><td>043</td><td><a href="043-relative-zindex.html">relative z-index</a></td><td>z-index on position: relative overrides DOM paint order</td><td class="pass">PASS</td></tr>
+<tr><td>044</td><td><a href="044-overflow-hidden.html">overflow hidden</a></td><td>a block larger than its parent is clipped</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini