Procházet zdrojové kódy

Fix browser page background, flex footer and query selectors

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

+ 12 - 9
AGENTS.md

@@ -825,19 +825,22 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `position: relative`/`absolute`/`fixed`/`sticky`, `right`/`bottom`,
   `position: relative`/`absolute`/`fixed`/`sticky`, `right`/`bottom`,
   `z-index`, `transform: translate`/`scale`/`rotate` with
   `z-index`, `transform: translate`/`scale`/`rotate` with
   `transform-origin` (paint-time box and inline-image transforms), flex rows
   `transform-origin` (paint-time box and inline-image transforms), flex rows
-  with `justify-content`/`align-items`, two-column grids, table colspan and
+  with `justify-content` (including `space-evenly`)/`align-items`, two-column
+  grids, table colspan and
   percentage columns, and basic `float: left/right` columns.
   percentage columns, and basic `float: left/right` columns.
   `@media (min-width/max-width)` blocks are evaluated against the browser
   `@media (min-width/max-width)` blocks are evaluated against the browser
   canvas width when the page is parsed; `@import`/`@keyframes` are skipped.
   canvas width when the page is parsed; `@import`/`@keyframes` are skipped.
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   (the browser fetches remote faces before layout).  The compatibility
   (the browser fetches remote faces before layout).  The compatibility
   ladder is `testdata/browser/compat/`
   ladder is `testdata/browser/compat/`
-  (`make compat-serve`); pages 010–045 and 030a pass (036a needs the
+  (`make compat-serve`); pages 010–046 and 030a pass (036a needs the
   POST echo server).  Inline
   POST echo server).  Inline
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<video>` gets a placeholder box.  The default page canvas follows
   `<video>` gets a placeholder box.  The default page canvas follows
-  `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`);
-  a site's own `background-color`/`color` CSS still overrides it.  `browser.c`
+  `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`), but
+  the `html`/`body` background fills the whole page canvas, so a site's white
+  page has no dark side gutters; other `background-color`/`color` CSS still
+  overrides the theme.  `browser.c`
   opens at 1100x700 (960x640 and 780x540 fallbacks on smaller screens), the
   opens at 1100x700 (960x640 and 780x540 fallbacks on smaller screens), the
   layout/render, URL bar, history, status bar and inline `<img>` decoding.
   layout/render, URL bar, history, status bar and inline `<img>` decoding.
   A dotted input without a scheme that is not a known local file extension
   A dotted input without a scheme that is not a known local file extension
@@ -1606,11 +1609,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     overhead.  The helper deliberately requests identity encoding, so a page
     overhead.  The helper deliberately requests identity encoding, so a page
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
-    consumed by the entity decoder instead of looping forever.  Because the
-    default canvas is dark, a page that sets dark text but relies on the
-    browser's default white background can be hard to read; pages with their
-    own background and colour rules are unaffected.  TLS is phase A (no
-    certificate verification); `https` is shown as unverified.  The disk app
+    consumed by the entity decoder instead of looping forever.  A page with
+    no `html`/`body` background still gets the dark default canvas, so dark
+    text can be hard to read; pages with their own background are unaffected.
+    TLS is phase A (no certificate verification); `https` is shown as
+    unverified.  The disk app
     is loaded from FAT at spawn time, so a cold floppy start takes a few
     is loaded from FAT at spawn time, so a cold floppy start takes a few
     seconds; the layout state is deliberately static.
     seconds; the layout state is deliberately static.
 
 

+ 67 - 22
browser.c

@@ -4208,6 +4208,13 @@ static void emit_word(struct lay *l, const char *s, unsigned int start,
 				(unsigned short)n, st, href, 0);
 				(unsigned short)n, st, href, 0);
 		return;
 		return;
 	}
 	}
+	/* A padded/bordered one-word button can be a few pixels wider than
+	 * the content box; keep it on one line instead of splitting it. */
+	if (l->nruns == 0 && st->has_bg && w > avail && w <= avail + 8) {
+		add_run(l, (unsigned short)(base + start),
+			(unsigned short)n, st, href, 0);
+		return;
+	}
 	if (l->line_x + w > l->line_right &&
 	if (l->line_x + w > l->line_right &&
 	    (l->nruns > 0 || l->line_x > l->line_left)) {
 	    (l->nruns > 0 || l->line_x > l->line_left)) {
 		flush_line(l);
 		flush_line(l);
@@ -5185,7 +5192,7 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			   const struct br_style *pst, int depth, int base)
 			   const struct br_style *pst, int depth, int base)
 {
 {
 	unsigned int c;
 	unsigned int c;
-	int w = 0;
+	int line_w = 0, max_w = 0;
 
 
 	if (depth > 12)
 	if (depth > 12)
 		return 0;
 		return 0;
@@ -5198,11 +5205,11 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			unsigned int tn = nd->text_len;
 			unsigned int tn = nd->text_len;
 
 
 			if (doc.nodes[node].tag == BR_TAG_PRE) {
 			if (doc.nodes[node].tag == BR_TAG_PRE) {
-				w += (int)face_text_width(ui, pst, t, tn);
+				line_w += (int)face_text_width(ui, pst, t, tn);
 			} else {
 			} else {
 				unsigned int ti = 0;
 				unsigned int ti = 0;
 				int first = 1;
 				int first = 1;
-				int lead = w > 0 && tn > 0 && text_space(t[0]);
+				int lead = line_w > 0 && tn > 0 && text_space(t[0]);
 				int sp = (int)face_text_width(ui, pst, " ", 1);
 				int sp = (int)face_text_width(ui, pst, " ", 1);
 
 
 				while (ti < tn) {
 				while (ti < tn) {
@@ -5216,9 +5223,9 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 					if (ti == ts)
 					if (ti == ts)
 						break;
 						break;
 					if (!first || lead)
 					if (!first || lead)
-						w += sp;
+						line_w += sp;
 					lead = 0;
 					lead = 0;
-					w += (int)face_text_width(ui, pst,
+					line_w += (int)face_text_width(ui, pst,
 						t + ts, ti - ts);
 						t + ts, ti - ts);
 					first = 0;
 					first = 0;
 				}
 				}
@@ -5229,7 +5236,9 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 		if (cs.display == BR_DISPLAY_NONE)
 		if (cs.display == BR_DISPLAY_NONE)
 			continue;
 			continue;
 		if (nd->tag == BR_TAG_BR) {
 		if (nd->tag == BR_TAG_BR) {
-			w += 8;
+			if (line_w > max_w)
+				max_w = line_w;
+			line_w = 0;
 			continue;
 			continue;
 		}
 		}
 		if (nd->tag == BR_TAG_IMG) {
 		if (nd->tag == BR_TAG_IMG) {
@@ -5252,31 +5261,45 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			if (cs.width_kind) {
 			if (cs.width_kind) {
 				int ww = style_width_px(&cs, base);
 				int ww = style_width_px(&cs, base);
 
 
-				w += ww > 0 ? ww : 64;
+				line_w += ww > 0 ? ww : 64;
 			} else if (nd->width_attr) {
 			} else if (nd->width_attr) {
-				w += nd->width_attr;
+				line_w += nd->width_attr;
 			} else if (nd->height_attr && iw && ih) {
 			} else if (nd->height_attr && iw && ih) {
-				w += iw * (int)nd->height_attr / ih;
+				line_w += iw * (int)nd->height_attr / ih;
 			} else if (cs.height_kind && iw && ih) {
 			} else if (cs.height_kind && iw && ih) {
 				int hh = style_height_px(&cs, base);
 				int hh = style_height_px(&cs, base);
 
 
-				w += hh > 0 ? iw * hh / ih : iw;
+				line_w += hh > 0 ? iw * hh / ih : iw;
 			} else if (iw) {
 			} else if (iw) {
-				w += iw;
+				line_w += iw;
 			} else {
 			} else {
-				w += 64;
+				line_w += 64;
 			}
 			}
 			continue;
 			continue;
 		}
 		}
-		w += mp_px(&cs, 7, base) +
-		     mp_px(&cs, 5, base) +
-		     mp_px(&cs, 3, base) +
-		     mp_px(&cs, 1, base);
-		w += intrinsic_width(ui, c, &cs, depth + 1, base);
-		if (w > 4096)
+		{
+			int cw = mp_px(&cs, 7, base) +
+				 mp_px(&cs, 5, base) +
+				 mp_px(&cs, 3, base) +
+				 mp_px(&cs, 1, base) +
+				 cs.border_w[1] + cs.border_w[3] +
+				 intrinsic_width(ui, c, &cs, depth + 1, base);
+
+			if (cs.float_left || cs.display == BR_DISPLAY_INLINE ||
+			    cs.display == BR_DISPLAY_INLINE_BLOCK) {
+				line_w += cw;
+			} else {
+				if (line_w > max_w)
+					max_w = line_w;
+				line_w = 0;
+				if (cw > max_w)
+					max_w = cw;
+			}
+		}
+		if (line_w > 4096 || max_w > 4096)
 			break;
 			break;
 	}
 	}
-	return w;
+	return line_w > max_w ? line_w : max_w;
 }
 }
 
 
 /* Lay consecutive floated block children side by side.  Bootstrap's grid is
 /* Lay consecutive floated block children side by side.  Bootstrap's grid is
@@ -5672,6 +5695,9 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 		} else if (pst->justify == 4) {
 		} else if (pst->justify == 4) {
 			extra = count ? free_w / count : 0;
 			extra = count ? free_w / count : 0;
 			x += extra / 2;
 			x += extra / 2;
+		} else if (pst->justify == 5) {
+			extra = count ? free_w / (count + 1) : 0;
+			x += extra;
 		}
 		}
 		for (k = 0; k < count; k++) {
 		for (k = 0; k < count; k++) {
 			int node = (int)it[start + k].node;
 			int node = (int)it[start + k].node;
@@ -6828,7 +6854,7 @@ static int measure_page(int left, int right)
 		layout_boxes[i].valid = 0;
 		layout_boxes[i].valid = 0;
 	}
 	}
 	l->draw = 0;
 	l->draw = 0;
-	l->ui = 0;
+	l->ui = &ui;
 	l->left = left;
 	l->left = left;
 	l->right = right;
 	l->right = right;
 	l->y = 0;
 	l->y = 0;
@@ -7046,9 +7072,28 @@ static void draw_page(struct ngui_rect page)
 	int left, right, ch;
 	int left, right, ch;
 	int old_panel, old_dim;
 	int old_panel, old_dim;
 	unsigned short *saved_px;
 	unsigned short *saved_px;
+	unsigned short page_bg = ui.theme.bg;
 	int saved_w, saved_h, use_buf;
 	int saved_w, saved_h, use_buf;
 
 
 	page_rect = page;
 	page_rect = page;
+	{
+		unsigned int root = doc.nnode > 1 ?
+			br_find_tag(&doc, BR_TAG_HTML) : 0;
+		unsigned int body = doc.nnode > 1 ?
+			br_find_tag(&doc, BR_TAG_BODY) : 0;
+		struct br_style bs;
+
+		if (root) {
+			style_for_node(root - 1, &bs);
+			if (bs.has_bg)
+				page_bg = bs.bg;
+		}
+		if (body) {
+			style_for_node(body - 1, &bs);
+			if (bs.has_bg)
+				page_bg = bs.bg;
+		}
+	}
 	if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
 	if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
 	    page_cache_valid && layout_valid &&
 	    page_cache_valid && layout_valid &&
 	    !ui.pressed && !(ui.buttons & 1) && !ui.released &&
 	    !ui.pressed && !(ui.buttons & 1) && !ui.released &&
@@ -7059,7 +7104,7 @@ static void draw_page(struct ngui_rect page)
 		blit_page(page, page_backbuf, pagebuf_w);
 		blit_page(page, page_backbuf, pagebuf_w);
 		return;
 		return;
 	}
 	}
-	ngui_fill_rect(&ui, page, ui.theme.bg);
+	ngui_fill_rect(&ui, page, page_bg);
 	if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
 	if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
 		const char *s = page_loading ? "Loading..." : "No page";
 		const char *s = page_loading ? "Loading..." : "No page";
 		int w = (int)ngui_text_width_ui(&ui, s);
 		int w = (int)ngui_text_width_ui(&ui, s);
@@ -7083,7 +7128,7 @@ static void draw_page(struct ngui_rect page)
 		ui.px = page_backbuf;
 		ui.px = page_backbuf;
 		ui.w = pagebuf_w;
 		ui.w = pagebuf_w;
 		ui.h = pagebuf_h;
 		ui.h = pagebuf_h;
-		ngui_fill_rect(&ui, page, ui.theme.bg);
+		ngui_fill_rect(&ui, page, page_bg);
 	}
 	}
 
 
 	left = page.x + 8;
 	left = page.x + 8;

+ 9 - 5
browser_html.c

@@ -1909,8 +1909,9 @@ static void query_walk(struct br_doc *d, unsigned int node,
 {
 {
 	unsigned int c;
 	unsigned int c;
 
 
-	if (match_selector(d, node, sel, len))
-		query_add(out, max, count, node + 1);
+	if (d->nodes[node].type == BR_NODE_ELEM &&
+	    match_selector(d, node, sel, len))
+		query_add(out, max, count, node);
 	for (c = d->nodes[node].first; c; c = d->nodes[c].next)
 	for (c = d->nodes[node].first; c; c = d->nodes[c].next)
 		query_walk(d, c, sel, len, out, max, count);
 		query_walk(d, c, sel, len, out, max, count);
 }
 }
@@ -1965,11 +1966,11 @@ unsigned int br_query_all(struct br_doc *d, unsigned int root,
 				unsigned int n;
 				unsigned int n;
 
 
 				for (n = 1; n < d->nnode; n++)
 				for (n = 1; n < d->nnode; n++)
-					if (!node_detached(d, n) &&
+					if (d->nodes[n].type == BR_NODE_ELEM &&
+					    !node_detached(d, n) &&
 					    match_selector(d, n, selector + start,
 					    match_selector(d, n, selector + start,
 							   i - start))
 							   i - start))
-						query_add(out, max, &count,
-							  n + 1);
+						query_add(out, max, &count, n);
 			} else if (root <= d->nnode) {
 			} else if (root <= d->nnode) {
 				unsigned int c;
 				unsigned int c;
 
 
@@ -4549,6 +4550,9 @@ static void parse_decls(struct br_doc *d, const char *s,
 			else if (contains_ci_len(s + vstart, vend - vstart,
 			else if (contains_ci_len(s + vstart, vend - vstart,
 						 "space-around"))
 						 "space-around"))
 				j = 4;
 				j = 4;
+			else if (contains_ci_len(s + vstart, vend - vstart,
+						 "space-evenly"))
+				j = 5;
 			else if (contains_ci_len(s + vstart, vend - vstart,
 			else if (contains_ci_len(s + vstart, vend - vstart,
 						 "center"))
 						 "center"))
 				j = 1;
 				j = 1;

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


+ 81 - 0
testdata/browser/compat/046-flex-footer-query.html

@@ -0,0 +1,81 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>046 flex footer and query selectors</title>
+<style>
+html, body { margin: 0; padding: 0; background: #f6f6ef; 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; }
+.nav { clear: both; overflow: hidden; margin-bottom: 14px; }
+.nav ul { float: right; margin: 0; padding: 0; list-style: none; }
+.nav li { float: left; margin-left: 8px; }
+.nav a { display: block; color: #404040; padding: 10px 15px; text-decoration: none; }
+.nav a.btn { background: #28cb75; color: #ffffff; font-weight: bold;
+             border: 2px solid #28cb75; border-radius: 20px; }
+.flexfooter { display: flex; flex-direction: row; justify-content: space-evenly;
+              max-width: 1000px; margin: 0 auto; text-align: left;
+              flex-wrap: wrap; background: #494f5a; color: #ffffff;
+              padding: 20px; }
+.list h3 { color: #acacac; margin: 0 0 10px 0; }
+.list a { display: block; color: #ffffff; margin-bottom: 4px; text-decoration: none; }
+.list.wide a { white-space: nowrap; }
+#result { margin-top: 14px; padding: 8px; background: #eeeeee; color: #000000;
+          font-family: monospace; font-weight: bold; }
+</style>
+</head>
+<body>
+<h1>046 &middot; flex footer and query selectors</h1>
+<p>The three footer columns must sit on one row with equal space, and the padded Install button must stay on one line.</p>
+<div class="nav">
+  <ul>
+    <li><a href="#">Demo</a></li>
+    <li><a href="#">Docs</a></li>
+    <li><a class="btn" id="install" href="#">Install</a></li>
+    <li><a href="#">My Account</a></li>
+  </ul>
+</div>
+<div class="flexfooter" id="footer">
+  <div class="list wide" id="l1"><h3>About</h3><a href="#">Story</a><a href="#">Open Source</a><a href="#">Imprint</a><a href="#">Brand Assets</a><a href="#">Data Processing Agreement</a></div>
+  <div class="list" id="l2"><h3>Resources</h3><a href="#">Blog</a><a href="#">Documentation</a><a href="#">Forum</a><a href="#">Pricing</a><a href="#">Community apps</a><a href="#">App Status</a></div>
+  <div class="list" id="l3"><h3>Follow us</h3><a href="#">Forum</a><a href="#">Mastodon</a><a href="#">RSS</a></div>
+</div>
+<div id="result">checking...</div>
+<script>
+setTimeout(function () {
+  var lists = document.querySelectorAll('.list');
+  var b1 = document.getElementById('l1').getBoundingClientRect();
+  var b2 = document.getElementById('l2').getBoundingClientRect();
+  var b3 = document.getElementById('l3').getBoundingClientRect();
+  var ins = document.getElementById('install');
+  var ir = ins.getBoundingClientRect();
+  var q = document.querySelector('a.btn');
+  var details = [];
+  var ok = lists.length === 3;
+  if (!ok) details.push('list count=' + lists.length);
+  if (lists.length && lists[0].tagName !== 'DIV') {
+    ok = false; details.push('query element tag=' + lists[0].tagName);
+  }
+  if (!q || q.tagName !== 'A' || q.textContent !== 'Install') {
+    ok = false; details.push('querySelector anchor');
+  }
+  if (Math.abs(b1.top - b2.top) > 1 || Math.abs(b2.top - b3.top) > 1) {
+    ok = false; details.push('footer rows differ');
+  }
+  var gap1 = b2.left - b1.right;
+  var gap2 = b3.left - b2.right;
+  if (Math.abs(gap1 - gap2) > 3) {
+    ok = false; details.push('gaps ' + Math.round(gap1) + '/' + Math.round(gap2));
+  }
+  if (ir.height > 50 || ir.width < 70) {
+    ok = false; details.push('install ' + Math.round(ir.width) + 'x' + Math.round(ir.height));
+  }
+  var out = document.getElementById('result');
+  out.textContent = ok ? 'PASS: footer columns, padding and query selectors' :
+    ('FAIL: ' + details.join('; '));
+  out.style.background = ok ? '#ccffcc' : '#ffcccc';
+}, 600);
+</script>
+</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://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
 
-Status: 010-045 and 030a pass.  036a needs the POST echo server:
+Status: 010-046 and 030a pass.  036a needs the POST echo server:
 
 
   python3 testdata/browser/compat/post_server.py
   python3 testdata/browser/compat/post_server.py
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo

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

@@ -68,6 +68,7 @@ a { color: #0645ad; }
 <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>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>
 <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>
 <tr><td>045</td><td><a href="045-bgcolor.html">legacy bgcolor</a></td><td>table/cell bgcolor matches CSS backgrounds</td><td class="pass">PASS</td></tr>
 <tr><td>045</td><td><a href="045-bgcolor.html">legacy bgcolor</a></td><td>table/cell bgcolor matches CSS backgrounds</td><td class="pass">PASS</td></tr>
+<tr><td>046</td><td><a href="046-flex-footer-query.html">flex footer and queries</a></td><td>space-evenly columns, padded one-line button, query selectors</td><td class="pass">PASS</td></tr>
 </table>
 </table>
 </body>
 </body>
 </html>
 </html>

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