Просмотр исходного кода

Improve browser support for the NebOS docs site

The documentation uses media colour schemes, CSS variables, flex rows,
auto-fit card grids, inline-block file tags, tables and inline SVG diagrams.
Render the page's base (dark) palette, support repeat(auto-fit, minmax(...))
grids and inline-block runs, lay out basic table columns, and draw an SVG
subset (rect, line, path with M/L/H/V and text, with viewBox, fill/stroke and
text-anchor).

Background shorthands fall back to their final solid colour instead of the
first gradient stop.  Fractional SVG path coordinates and unsupported tokens
are consumed safely.  Sticky headers stay in the flow so they cannot cover
the first content block, and common UTF-8 punctuation is mapped to readable
ASCII.

Compile the browser graph with function sections and link it with
--gc-sections; the command pack drops to 245042 bytes, leaving 16 KB under
the legacy low-memory cap.

Verified with the local docs site (system-stack and boot-chain diagrams, the
card grid and the repository table), live cloudron/nd-aktuell/heise smoke
tests, HTTPS, and make test-images / test-crypto / test-uefi.
Johannes Zellner 1 неделя назад
Родитель
Сommit
b54f7b2dea
6 измененных файлов с 855 добавлено и 90 удалено
  1. 49 38
      AGENTS.md
  2. 19 15
      Makefile
  3. 538 10
      browser.c
  4. 239 26
      browser_html.c
  5. 8 0
      browser_html.h
  6. 2 1
      docs/graphics.html

+ 49 - 38
AGENTS.md

@@ -1178,9 +1178,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    compressed pack through the `0x60000..0x9FBFF` gap before copying it to
    `0x900000`, so `PACK_LOW_MAX` (0x3FBFF, ~255 KB compressed) still caps the
    legacy path; a larger pack needs the UEFI/kexec path or a chunked
-   high-memory loader.  The current pack is within a few KB of that cap
-   with `libdeflate-gzip -12` (plain `gzip -9` is already over), so command
-   growth needs care.
+   high-memory loader.  The browser and its ring-3 dependencies are compiled
+   with function sections and linked with `--gc-sections`; the current pack
+   is ~245 KB, leaving ~16 KB under the cap.  Command growth still needs
+   care because `libdeflate-gzip -12` is already required (plain `gzip -9`
+   is over the cap).
 10. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
     board that exposes only MP tables stays UP (deliberate; there is no test
     hardware).
@@ -1235,41 +1237,50 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     pack is out of the kernel now, so `_end` keeps ~130 KB of headroom below
     the `0x7F000` wall; the remaining limit is kernel code/buffers only.
 
-23. Browser limits: no JavaScript, forms, flex/grid, CSS custom properties
-    (`var(--...)`), tables with column widths, absolute/fixed positioning,
-    inline-block sizing or standalone image documents.  Float support is the
-    basic Bootstrap-grid case: consecutive floated blocks are laid side by
-    side with percentage/px widths, `float:right` is treated like left, there
-    is no `clear`, percentage margins are approximated as pixels, and a float
-    without a width uses a text-width estimate.  `@media` blocks are evaluated
-    against a fixed 1000 px desktop viewport (so narrow/adaptive pages get the
-    desktop rules, not their mobile layout), and pseudo-classes are never
-    matched.  `font-size` maps to 1x/2x only, `em`/`rem` both use a 16 px
-    root, and block `width`/`max-width` are not used for centring.  Images
-    are decoded into a 24 MB shared arena and downscaled to a 1000 px display
-    bitmap (16 images, 512 KB files / 8 MP decodes); a large image-heavy page
-    can still exhaust the 32 MB heap, and the first `<img>` after a big
-    screenshot can transiently need a new heap growth.  `img_draw` rescales
-    the cached bitmap on every repaint.  Lazy image loading uses the last
-    measured positions; adding an image can reflow the content above it, and
-    images below the viewport stay as `[image]` until scrolled into range.
-    Each network fetch starts a one-shot helper process and opens a fresh
-    TCP/TLS connection (the `uhttp` keep-alive path exists but the browser
-    does not use it across helper jobs), so many tiny resources have extra
-    process/connection 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 caps.  Video/audio elements are placeholders, and
-    pages that depend on `display:grid`/flex or CSS variables
-    (tagesschau's player and ad slots, for example) can show empty bordered
-    boxes or miss colours.  Bare `&` characters are now 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 full background and
-    colour rules 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 seconds; the
-    8 KB user stack means the layout state is deliberately static.
+23. Browser limits: no JavaScript, forms, pseudo-classes/elements,
+    border-radius clipping of descendant content, gradients/box-shadow/
+    transitions (a gradient background falls back to its last solid colour),
+    or CSS `color-mix`.  CSS custom properties (`var()`), flex rows with
+    wrap/grow/gap, two-track and `repeat(auto-fit, minmax(...))` grids,
+    inline-block runs, basic proportional table columns and inline SVG
+    (`rect`, `line`, `path` with M/L/H/V, and `text`) are supported.
+    `position:fixed` boxes are deferred and painted above the flow;
+    `position:sticky` is treated as static.  `prefers-color-scheme` media
+    queries are ignored so the page's base palette is used.  `width`/
+    `max-width` are not applied to block flow (they collapsed nested
+    percentage columns); flex/grid items still use them.  Float support is
+    the basic Bootstrap-grid case: consecutive floated blocks are laid side
+    by side with percentage/px widths, `float:right` is treated like left,
+    there is no `clear`, percentage margins are approximated as pixels, and
+    a float without a width uses a text-width estimate.  `@media` width
+    blocks are evaluated against a fixed 1000 px desktop viewport (so
+    narrow/adaptive pages get the desktop rules, not their mobile layout),
+    and pseudo-classes are never matched.  `font-size` maps to 1x/2x only,
+    `em`/`rem` both use a 16 px root, and `line-height` is approximate.
+    Pages that need flex alignment/justification, multi-row grid templates
+    or CSS variables inside `calc()`/`color-mix()` can still lay out
+    imperfectly.  Images are decoded into a 24 MB shared arena and
+    downscaled to a 1000 px display bitmap (16 images, 512 KB files / 8 MP
+    decodes); a large image-heavy page can still exhaust the 32 MB heap, and
+    the first `<img>` after a big screenshot can transiently need a new heap
+    growth.  `img_draw` rescales the cached bitmap on every repaint.  Lazy
+    image loading uses the last measured positions; adding an image can
+    reflow the content above it, and images below the viewport stay as
+    `[image]` until scrolled into range.  Each network fetch starts a
+    one-shot helper process and opens a fresh TCP/TLS connection (the
+    `uhttp` keep-alive path exists but the browser does not use it across
+    helper jobs), so many tiny resources have extra process/connection
+    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
+    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
+    is loaded from FAT at spawn time, so a cold floppy start takes a few
+    seconds; the 8 KB user stack means the layout state is deliberately
+    static.
 
 24. UEFI boot is x86-64 firmware only (no `BOOTIA32.EFI` yet) and the loader
     is unsigned, so Secure Boot must be off.  The loader needs a GOP mode with

+ 19 - 15
Makefile

@@ -9,6 +9,9 @@ OBJCOPY = objcopy
 CFLAGS   = -ffreestanding -fno-pie -m32 -march=i586 -nostdlib -fno-stack-protector \
            -fno-asynchronous-unwind-tables -mno-sse -mno-sse2 -mno-mmx \
            -Wall -Wextra -O2 -c
+# Ring-3 objects are compiled with function sections so the browser link can
+# drop code it never reaches; the legacy rootfs pack is close to its cap.
+SECTION_CFLAGS = -ffunction-sections -fdata-sections
 ASFLAGS  = --32 -I.
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
@@ -54,7 +57,7 @@ font_8x8.o: font_8x8.c font_8x8.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 font_8x16.o: font_8x16.c font_8x16.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -330,7 +333,7 @@ sys_mem.o: sys_mem.c sys_mem.h mm.h paging.h process.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 inflate.o: inflate.c inflate.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h it87.h mm.h mouse.h thermal.h tpec.h usbhid.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -590,7 +593,7 @@ ucmd_term.elf: ucmd_term.o uwm_lib.o font_8x16.o ucmd.ld
 # -Os: every GUI app links the whole toolkit, and the legacy rootfs pack has
 # a hard 255 KB cap; -Os saves ~10 KB compressed across the pack.
 ngui.o: ngui.c ngui.h ucmd_lib.h syscall.h wm_abi.h font_8x16.h
-	$(CC) $(CFLAGS) -Os -o $@ $<
+	$(CC) $(CFLAGS) -Os $(SECTION_CFLAGS) -o $@ $<
 
 ucmd_files.o: ucmd_files.c ucmd_lib.h syscall.h wm_abi.h ngui.h img.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -631,7 +634,7 @@ IMGLIB_OBJS = img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o \
               img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o
 
 $(IMGLIB_OBJS): %.o: %.c img.h img_internal.h inflate.h ucmd_lib.h syscall.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 # The integer IDCT deliberately relies on 32-bit wrap-around.
 img_jpeg.o: CFLAGS += -fwrapv
@@ -650,7 +653,7 @@ TLSLIB_OBJS = tls_sha256.o tls_chacha20.o tls_x25519.o tls_rng.o tls.o
 
 # Shared ring-3 HTTP/TLS client (curl and the browser).
 uhttp.o: uhttp.c uhttp.h ucmd_lib.h tls.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 ucmd_curl.elf: ucmd_curl.o uhttp.o $(TLSLIB_OBJS) ucmd.ld
 	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
@@ -658,34 +661,35 @@ ucmd_curl.elf: ucmd_curl.o uhttp.o $(TLSLIB_OBJS) ucmd.ld
 
 # browser: the real app is a disk image (BROWSER) because it links ngui, the
 # image library, TLS and uhttp.  ucmd_browser.c is the tiny PATH launcher.
-# Compile the two large browser-specific objects for size: the legacy rootfs
-# pack has a hard low-memory cap and is already close to it.
+# Compile this graph with function sections and link it with --gc-sections:
+# unreachable image, TLS and UI paths are dropped, leaving room under the
+# legacy rootfs pack's low-memory cap.
 browser_html.o: browser_html.c browser_html.h
-	$(CC) $(CFLAGS) -Os -o $@ $<
+	$(CC) $(CFLAGS) -Os $(SECTION_CFLAGS) -o $@ $<
 
 browser.o: browser.c ucmd_lib.h syscall.h wm_abi.h ngui.h browser_html.h uhttp.h img.h inflate.h
-	$(CC) $(CFLAGS) -Os -o $@ $<
+	$(CC) $(CFLAGS) -Os $(SECTION_CFLAGS) -o $@ $<
 
 browser.elf: browser.o browser_html.o uhttp.o ngui.o font_8x16.o $(IMGLIB_OBJS) inflate.o $(TLSLIB_OBJS) ucmd.ld
-	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
+	$(LD) -T ucmd.ld -z max-page-size=0x100 --gc-sections -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
 	$(OBJCOPY) --strip-all $@ $@
 
 
 tls_sha256.o: tls_sha256.c tls_sha256.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 tls_chacha20.o: tls_chacha20.c tls_chacha20.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 tls_x25519.o: tls_x25519.c tls_x25519.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 tls_rng.o: tls_rng.c tls_rng.h tls_sha256.h ucmd_lib.h syscall.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 tls.o: tls.c tls.h tls_sha256.h tls_chacha20.h tls_x25519.h tls_rng.h \
        ucmd_lib.h syscall.h
-	$(CC) $(CFLAGS) -o $@ $<
+	$(CC) $(CFLAGS) $(SECTION_CFLAGS) -o $@ $<
 
 tls_test: tls_test.c tls_sha256.c tls_chacha20.c tls_x25519.c \
 	  tls_sha256.h tls_chacha20.h tls_x25519.h

+ 538 - 10
browser.c

@@ -2264,6 +2264,17 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 static void layout_grid_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 unsigned short link);
+static void layout_grid_auto(struct lay *l, unsigned int parent,
+			     const struct br_style *pst, int depth,
+			     unsigned short link);
+static int flex_item_width(struct lay *l, unsigned int n,
+			   const struct br_style *cs, int avail);
+static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
+					 const struct br_style *pst, int depth,
+					 unsigned short link);
+static void layout_table_row(struct lay *l, unsigned int row,
+			     const struct br_style *pst, int depth,
+			     unsigned short link);
 
 /* A flex container whose items are only inline spans is usually just a
  * heading/icon pair.  Our flex row positions such items without emitting
@@ -2283,7 +2294,8 @@ static int flex_wants_row(unsigned int parent, const struct br_style *pst)
 		if (cs.display == BR_DISPLAY_NONE)
 			continue;
 		if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow ||
-		    cs.width_kind || nd->tag == BR_TAG_IMG)
+		    cs.width_kind || cs.has_bg || cs.border_w ||
+		    nd->tag == BR_TAG_IMG)
 			return 1;
 	}
 	return 0;
@@ -2303,6 +2315,223 @@ static int css_radius(const struct br_style *st, int corner,
 	return r;
 }
 
+static void svg_viewbox(unsigned node, int *vw, int *vh)
+{
+	struct br_node *nd = &doc.nodes[node];
+
+	if (nd->width_attr && nd->height_attr) {
+		*vw = nd->width_attr;
+		*vh = nd->height_attr;
+	} else {
+		*vw = 300;
+		*vh = 150;
+	}
+	if (*vw < 1)
+		*vw = 1;
+	if (*vh < 1)
+		*vh = 1;
+}
+
+static int svg_num(const char **pp)
+{
+	const char *p = *pp;
+	int neg = 0, v = 0, frac = 0, scale = 1;
+
+	while (*p == ' ' || *p == ',' || *p == '\t' ||
+	       *p == '\n' || *p == '\r')
+		p++;
+	if (*p == '-') {
+		neg = 1;
+		p++;
+	} else if (*p == '+') {
+		p++;
+	}
+	while (*p >= '0' && *p <= '9') {
+		v = v * 10 + (*p - '0');
+		p++;
+	}
+	if (*p == '.') {
+		p++;
+		while (*p >= '0' && *p <= '9' && scale < 1000) {
+			frac = frac * 10 + (*p - '0');
+			scale *= 10;
+			p++;
+		}
+	}
+	v += frac / scale;
+	*pp = p;
+	return neg ? -v : v;
+}
+
+static void svg_path(struct ngui *ui, const char *d,
+		     int ox, int oy, int dw, int dh, int vw, int vh,
+		     unsigned short color)
+{
+	int cx = 0, cy = 0, sx = 0, sy = 0;
+	char cmd = 0;
+	const char *p = d;
+
+	while (p && *p) {
+		int rel, nx, ny;
+		char c;
+
+		while (*p == ' ' || *p == ',' || *p == '\t' ||
+		       *p == '\n' || *p == '\r')
+			p++;
+		if (!*p)
+			break;
+		if ((*p >= 'A' && *p <= 'Z') ||
+		    (*p >= 'a' && *p <= 'z'))
+			cmd = *p++;
+		else if (!cmd) {
+			p++;
+			continue;
+		}
+		rel = cmd >= 'a' && cmd <= 'z';
+		c = rel ? (char)(cmd - 'a' + 'A') : cmd;
+		if (c == 'Z') {
+			ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
+				  ox + sx * dw / vw, oy + sy * dh / vh,
+				  color);
+			cx = sx;
+			cy = sy;
+			cmd = 0;
+			continue;
+		}
+		if (c == 'H' || c == 'V') {
+			const char *save = p;
+			int a = svg_num(&p);
+
+			if (p == save) {
+				p++;
+				continue;
+			}
+			nx = cx;
+			ny = cy;
+			if (c == 'H')
+				nx = rel ? cx + a : a;
+			else
+				ny = rel ? cy + a : a;
+		} else {
+			const char *save = p;
+			int a = svg_num(&p);
+			int b = svg_num(&p);
+
+			if (p == save) {
+				p++;
+				continue;
+			}
+			if (c == 'M') {
+				nx = rel ? cx + a : a;
+				ny = rel ? cy + b : b;
+				cx = sx = nx;
+				cy = sy = ny;
+				cmd = rel ? 'l' : 'L';
+				continue;
+			}
+			if (c != 'L')
+				break;
+			nx = rel ? cx + a : a;
+			ny = rel ? cy + b : b;
+		}
+		ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
+			  ox + nx * dw / vw, oy + ny * dh / vh, color);
+		cx = nx;
+		cy = ny;
+	}
+}
+
+static void draw_svg(struct ngui *ui, unsigned node,
+		     int ox, int oy, int dw, int dh, int vw, int vh,
+		     const struct br_style *pst)
+{
+	unsigned int c;
+
+	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
+		struct br_node *nd = &doc.nodes[c];
+		struct br_style st;
+		const char *nm;
+
+		if (nd->type != BR_NODE_ELEM || !nd->name_off)
+			continue;
+		nm = doc.attrs + nd->name_off;
+		br_style_for(&doc, c, pst, &st);
+		if (s_eq(nm, "rect")) {
+			struct ngui_rect r;
+			int rx = (int)nd->svg_rx * dw / vw;
+
+			r.x = ox + (int)nd->svg_x * dw / vw;
+			r.y = oy + (int)nd->svg_y * dh / vh;
+			r.w = (int)nd->width_attr * dw / vw;
+			r.h = (int)nd->height_attr * dh / vh;
+			if (r.w < 1)
+				r.w = 1;
+			if (r.h < 1)
+				r.h = 1;
+			if (rx > r.w / 2)
+				rx = r.w / 2;
+			if (rx > r.h / 2)
+				rx = r.h / 2;
+			if (st.has_bg) {
+				if (rx)
+					ngui_fill_round_rect(ui, r, rx, rx,
+							     rx, rx, st.bg);
+				else
+					ngui_fill_rect(ui, r, st.bg);
+			}
+			if (st.border_w) {
+				if (rx)
+					ngui_frame_round(ui, r, rx, rx, rx, rx,
+							 st.border_w,
+							 st.border_color);
+				else
+					ngui_frame(ui, r, st.border_color);
+			}
+		} else if (s_eq(nm, "line")) {
+			if (st.border_w)
+				ngui_line(ui,
+					  ox + (int)nd->svg_x * dw / vw,
+					  oy + (int)nd->svg_y * dh / vh,
+					  ox + (int)nd->width_attr * dw / vw,
+					  oy + (int)nd->height_attr * dh / vh,
+					  st.border_color);
+		} else if (s_eq(nm, "path")) {
+			if (st.border_w && nd->src_off)
+				svg_path(ui, doc.attrs + nd->src_off,
+					 ox, oy, dw, dh, vw, vh,
+					 st.border_color);
+		} else if (s_eq(nm, "text")) {
+			unsigned int t = nd->first;
+			const char *s;
+			unsigned int n, tw;
+			int scale = st.scale ? st.scale : 1;
+			int tx, ty, anchor;
+
+			if (!t || doc.nodes[t].type != BR_NODE_TEXT)
+				continue;
+			s = doc.text + doc.nodes[t].text_off;
+			n = doc.nodes[t].text_len;
+			tw = ngui_text_width_n(s, n, scale);
+			if (st.bold)
+				tw += n;
+			tx = ox + (int)nd->svg_x * dw / vw;
+			ty = oy + (int)nd->svg_y * dh / vh -
+			     12 * scale;
+			anchor = st.align;
+			if (nd->attr_flags & BR_ATTR_ANCHOR_MIDDLE)
+				anchor = BR_ALIGN_CENTER;
+			else if (nd->attr_flags & BR_ATTR_ANCHOR_END)
+				anchor = BR_ALIGN_RIGHT;
+			if (anchor == BR_ALIGN_CENTER)
+				tx -= (int)tw / 2;
+			else if (anchor == BR_ALIGN_RIGHT)
+				tx -= (int)tw;
+			ngui_text_styled(ui, tx, ty, s, n, st.color,
+					 scale, st.bold);
+		}
+	}
+}
+
 static void layout_block(struct lay *l, unsigned int node,
 			 const struct br_style *st, int depth,
 			 unsigned short link)
@@ -2431,6 +2660,27 @@ static void layout_block(struct lay *l, unsigned int node,
 				  0x4208);
 		}
 		l->y += vh + 4;
+	} else if (doc.nodes[node].tag == BR_TAG_SVG) {
+		int vw, vh, dw = l->right - l->left;
+		int dh;
+
+		svg_viewbox(node, &vw, &vh);
+		if (dw < 8)
+			dw = 8;
+		dh = dw * vh / vw;
+		if (dh < 8)
+			dh = 8;
+		if (dh > 1400)
+			dh = 1400;
+		if (l->draw) {
+			struct ngui_rect clip = {l->left, l->y, dw, dh};
+
+			ngui_push_clip(l->ui, clip);
+			draw_svg(l->ui, node, l->left, l->y, dw, dh,
+				 vw, vh, st);
+			ngui_pop_clip(l->ui);
+		}
+		l->y += dh;
 	} else {
 		if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) {
 			char *m = li_marker[node];
@@ -2452,8 +2702,12 @@ static void layout_block(struct lay *l, unsigned int node,
 					  st->color);
 			}
 		}
-		if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
-		    st->grid_gap > 0)
+		if (doc.nodes[node].tag == BR_TAG_TR)
+			layout_table_row(l, node, st, depth + 1, link);
+		else if (st->grid_auto)
+			layout_grid_auto(l, node, st, depth + 1, link);
+		else if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
+			 st->grid_gap > 0)
 			layout_grid_children(l, node, st, depth + 1, link);
 		else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
 			 flex_wants_row(node, st))
@@ -2482,8 +2736,7 @@ static void layout_block(struct lay *l, unsigned int node,
 		if (h > 65535)
 			h = 65535;
 		block_h[node] = (unsigned short)h;
-
-	}
+}
 	if (positioned) {
 		l->y = flow_y;
 		l->left = flow_left;
@@ -2676,6 +2929,91 @@ static int flex_item_width(struct lay *l, unsigned int n,
 	return w;
 }
 
+/* A run of display:inline-block siblings (file tags, small buttons) belongs
+ * on one line, not one full-width block each.  Place intrinsic widths from
+ * left to right and wrap when the row is full. */
+static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
+					 const struct br_style *pst, int depth,
+					 unsigned short link)
+{
+	unsigned int n = first;
+	int row_left = l->left, row_right = l->right;
+	int avail = row_right - row_left;
+	int x = row_left, y = l->y, row_h = 0, any = 0;
+
+	flush_line(l);
+	y = l->y;
+	while (n) {
+		struct br_node *nd = &doc.nodes[n];
+		struct br_style cs;
+		unsigned short lk = link;
+		int colw, h;
+
+		if (nd->type == BR_NODE_TEXT) {
+			if (!text_node_blank(nd))
+				break;
+			if (any)
+				x += 4;
+			n = nd->next;
+			continue;
+		}
+		br_style_for(&doc, n, pst, &cs);
+		if (cs.display == BR_DISPLAY_NONE) {
+			n = nd->next;
+			continue;
+		}
+		if (cs.display != BR_DISPLAY_INLINE_BLOCK)
+			break;
+		if (nd->tag == BR_TAG_A && nd->href_off)
+			lk = (unsigned short)n;
+		colw = flex_item_width(l, n, &cs, avail);
+		/* A few pixels of slack keep a single long token (boot.S)
+		 * from wrapping at the exact content width. */
+		colw += 8;
+		if (colw > avail)
+			colw = avail;
+		if (any && x + colw > row_right) {
+			y += row_h + 2;
+			x = row_left;
+			row_h = 0;
+			any = 0;
+		}
+		if (any && x > row_left)
+			x += 4;
+		l->left = x;
+		l->right = x + colw;
+		if (l->right < l->left + 8)
+			l->right = l->left + 8;
+		l->y = y;
+		l->line_x = l->left;
+		l->run_x = l->left;
+		l->line_y = l->y;
+		l->line_h = 0;
+		l->nruns = 0;
+		l->space_pending = 0;
+		l->align = cs.align;
+		layout_block(l, n, &cs, depth + 1, lk);
+		h = l->y - y;
+		if (h > row_h)
+			row_h = h;
+		x += colw;
+		any = 1;
+		n = nd->next;
+	}
+	if (any)
+		y += row_h;
+	l->left = row_left;
+	l->right = row_right;
+	l->y = y;
+	l->line_x = l->left;
+	l->run_x = l->left;
+	l->line_y = l->y;
+	l->line_h = 0;
+	l->nruns = 0;
+	l->space_pending = 0;
+	return n;
+}
+
 static void layout_flex_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 unsigned short link)
@@ -2683,7 +3021,7 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	unsigned int n, c;
 	int row_left = l->left, row_right = l->right;
 	int avail = row_right - row_left;
-	int grow_count = 0, fixed = 0, grow_w;
+	int grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
 	int x = row_left, row_top = l->y, row_bottom = l->y, any = 0;
 
 	if (avail < 160 || depth > 24) {
@@ -2744,11 +3082,18 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 			colw = flex_item_width(l, n, &cs, avail);
 		if (colw > row_right - row_left)
 			colw = row_right - row_left;
-		if (pst->flex_wrap != 2 && any && x + colw > row_right) {
-			row_top = row_bottom;
-			row_bottom = row_top;
-			x = row_left;
+		if (any && x + colw > row_right) {
+			if (pst->flex_wrap == 2) {
+				if (colw > row_right - x)
+					colw = row_right - x;
+			} else {
+				row_top = row_bottom;
+				row_bottom = row_top;
+				x = row_left;
+			}
 		}
+		if (any && gap && x > row_left)
+			x += gap;
 		l->left = x;
 		l->right = x + colw;
 		if (l->right < l->left + 8)
@@ -2932,6 +3277,185 @@ static void layout_grid_children(struct lay *l, unsigned int parent,
 	l->space_pending = 0;
 }
 
+/* repeat(auto-fit, minmax(<min>, 1fr)): pick as many equal columns as fit,
+ * capped at four.  Used by the project docs' card grids. */
+static void layout_grid_auto(struct lay *l, unsigned int parent,
+			     const struct br_style *pst, int depth,
+			     unsigned short link)
+{
+	unsigned int n, c;
+	int row_left = l->left, row_right = l->right;
+	int avail = row_right - row_left;
+	int gap = pst->grid_gap;
+	int minw = pst->width_kind == 1 ? pst->width : 200;
+	int ncols, colw, x, y = l->y, start_y = l->y, row_h = 0, col = 0;
+
+	if (avail < 32 || depth > 24) {
+		layout_children(l, parent, pst, depth, link);
+		return;
+	}
+	for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
+		struct br_node *nd = &doc.nodes[c];
+
+		if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
+			layout_children(l, parent, pst, depth, link);
+			return;
+		}
+	}
+	if (minw < 80)
+		minw = 80;
+	ncols = (avail + gap) / (minw + gap);
+	if (ncols < 1)
+		ncols = 1;
+	if (ncols > 4)
+		ncols = 4;
+	colw = (avail - gap * (ncols - 1)) / ncols;
+	if (colw < 24)
+		colw = 24;
+	x = row_left;
+	n = doc.nodes[parent].first;
+	while (n) {
+		struct br_node *nd = &doc.nodes[n];
+		struct br_style cs;
+		unsigned short lk = link;
+		int h;
+
+		if (nd->type == BR_NODE_TEXT) {
+			n = nd->next;
+			continue;
+		}
+		br_style_for(&doc, n, pst, &cs);
+		if (cs.display == BR_DISPLAY_NONE) {
+			n = nd->next;
+			continue;
+		}
+		if (nd->tag == BR_TAG_A && nd->href_off)
+			lk = (unsigned short)n;
+		l->left = x;
+		l->right = x + colw;
+		if (l->right < l->left + 8)
+			l->right = l->left + 8;
+		l->y = y;
+		l->line_x = l->left;
+		l->run_x = l->left;
+		l->line_y = l->y;
+		l->line_h = 0;
+		l->nruns = 0;
+		l->space_pending = 0;
+		l->align = cs.align;
+		layout_block(l, n, &cs, depth + 1, lk);
+		h = l->y - y;
+		if (h > row_h)
+			row_h = h;
+		col++;
+		n = nd->next;
+		if (col == ncols) {
+			y += row_h + gap;
+			x = row_left;
+			row_h = 0;
+			col = 0;
+		} else {
+			x += colw + gap;
+		}
+	}
+	if (col)
+		y += row_h;
+	else if (y > start_y)
+		y -= gap;
+	l->left = row_left;
+	l->right = row_right;
+	l->y = y;
+	l->line_x = l->left;
+	l->run_x = l->left;
+	l->line_y = l->y;
+	l->line_h = 0;
+	l->nruns = 0;
+	l->space_pending = 0;
+}
+
+/* Table rows: cells share the row line, with widths proportional to their
+ * intrinsic content.  Border-collapse and colspans are not modelled, but a
+ * two-column reference table reads as a table instead of a vertical list. */
+static void layout_table_row(struct lay *l, unsigned int row,
+			     const struct br_style *pst, int depth,
+			     unsigned short link)
+{
+	unsigned int n, cell[12];
+	int raw[12], ncell = 0, total = 0;
+	int row_left = l->left, row_right = l->right;
+	int avail = row_right - row_left;
+	int x = row_left, y = l->y, bottom = l->y;
+
+	for (n = doc.nodes[row].first; n; n = doc.nodes[n].next) {
+		struct br_node *nd = &doc.nodes[n];
+		struct br_style cs;
+
+		if (nd->type == BR_NODE_TEXT) {
+			if (text_node_blank(nd))
+				continue;
+			layout_children(l, row, pst, depth, link);
+			return;
+		}
+		if (nd->tag != BR_TAG_TD && nd->tag != BR_TAG_TH) {
+			layout_children(l, row, pst, depth, link);
+			return;
+		}
+		br_style_for(&doc, n, pst, &cs);
+		if (cs.display == BR_DISPLAY_NONE)
+			continue;
+		if (ncell >= 12) {
+			layout_children(l, row, pst, depth, link);
+			return;
+		}
+		cell[ncell] = n;
+		raw[ncell] = intrinsic_width(n, &cs, 0) + cs.pl + cs.pr +
+			     cs.ml + cs.mr;
+		if (raw[ncell] < 24)
+			raw[ncell] = 24;
+		total += raw[ncell];
+		ncell++;
+	}
+	if (ncell < 2 || total <= 0 || avail < 32) {
+		layout_children(l, row, pst, depth, link);
+		return;
+	}
+	for (n = 0; n < (unsigned int)ncell; n++) {
+		struct br_style cs;
+		int w = (int)((long)raw[n] * avail / total);
+
+		br_style_for(&doc, cell[n], pst, &cs);
+		if (w < 24)
+			w = 24;
+		if (n == (unsigned int)ncell - 1)
+			w = row_right - x;
+		if (w < 8)
+			w = 8;
+		l->left = x;
+		l->right = x + w;
+		l->y = y;
+		l->line_x = l->left;
+		l->run_x = l->left;
+		l->line_y = l->y;
+		l->line_h = 0;
+		l->nruns = 0;
+		l->space_pending = 0;
+		l->align = cs.align;
+		layout_block(l, cell[n], &cs, depth + 1, link);
+		if (l->y > bottom)
+			bottom = l->y;
+		x += w;
+	}
+	l->left = row_left;
+	l->right = row_right;
+	l->y = bottom;
+	l->line_x = l->left;
+	l->run_x = l->left;
+	l->line_y = l->y;
+	l->line_h = 0;
+	l->nruns = 0;
+	l->space_pending = 0;
+}
+
 /* Positioned elements (fixed headers, overlays) are drawn after the normal
  * flow so they sit above it, like CSS painting order.  Their flow origin is
  * saved for absolute boxes; fixed boxes only need the viewport rect. */
@@ -3031,6 +3555,10 @@ static void layout_children(struct lay *l, unsigned int parent,
 			n = layout_float_group(l, n, pst, depth, link);
 			continue;
 		}
+		if (cs.display == BR_DISPLAY_INLINE_BLOCK) {
+			n = layout_inline_blocks(l, n, pst, depth, link);
+			continue;
+		}
 		if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
 			layout_block(l, n, &cs, depth + 1, lk);
 		else {

+ 239 - 26
browser_html.c

@@ -389,6 +389,12 @@ static unsigned int decode_text(const char *s, unsigned int len, char *out,
 			i += 2;
 			if (cp == 0xA0)
 				cp = ' ';
+			else if (cp == 0xB7)
+				cp = '-';
+			else if (cp == 0xD7)
+				cp = 'x';
+			else if (cp == 0xB0)
+				cp = 'o';
 			if (cp <= 0xFF)
 				emit(out, cap, &n, (char)cp);
 			else
@@ -407,6 +413,10 @@ static unsigned int decode_text(const char *s, unsigned int len, char *out,
 			} else if (cp == 0x2026) {
 				emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
 				emit(out, cap, &n, '.');
+			} else if (cp == 0x2192) {
+				emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
+			} else if (cp == 0x2264) {
+				emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
 			} else {
 				emit(out, cap, &n, '?');
 			}
@@ -766,6 +776,8 @@ static void doc_reset(struct br_doc *d)
 		d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
 		d->nodes[i].name_off = 0;
 		d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
+		d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
+		d->nodes[i].svg_rx = 0;
 		d->nodes[i].img_index = -1;
 		d->nodes[i].style_valid = 0;
 	}
@@ -848,6 +860,39 @@ static unsigned int parse_uint(const char *s, unsigned int len)
 	return v;
 }
 
+static int contains_ci_len(const char *s, unsigned int len,
+			   const char *needle);
+
+static int parse_svg_viewbox(const char *s, unsigned int len,
+			     unsigned short *w, unsigned short *h)
+{
+	unsigned int i = 0, v[4], n = 0;
+
+	while (i < len && n < 4) {
+		unsigned int x = 0;
+		int any = 0;
+
+		while (i < len && (is_space(s[i]) || s[i] == ','))
+			i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9') {
+			x = x * 10 + (unsigned int)(s[i] - '0');
+			i++;
+			any = 1;
+		}
+		if (!any) {
+			if (i < len)
+				i++;
+			continue;
+		}
+		v[n++] = x;
+	}
+	if (n < 4)
+		return 0;
+	*w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
+	*h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
+	return *w && *h;
+}
+
 static void note_attr_flag(struct br_node *n, const char *name)
 {
 	if (str_eq(name, "data-collapse-target"))
@@ -952,6 +997,31 @@ static void parse_attrs(struct br_doc *d, struct br_node *n,
 			n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
 			if (vlen && s[vstart + vlen - 1] == '%')
 				n->height_pct = 1;
+		} else if (str_eq(name, "x") || str_eq(name, "x1")) {
+			n->svg_x = (signed short)parse_uint(s + vstart, vlen);
+		} else if (str_eq(name, "y") || str_eq(name, "y1")) {
+			n->svg_y = (signed short)parse_uint(s + vstart, vlen);
+		} else if (str_eq(name, "x2")) {
+			n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
+		} else if (str_eq(name, "y2")) {
+			n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
+		} else if (str_eq(name, "rx")) {
+			n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
+		} else if (str_eq(name, "d")) {
+			n->src_off = store_attr(d, s + vstart, vlen, 300);
+		} else if (str_eq(name, "viewbox")) {
+			unsigned short vw = 0, vh = 0;
+
+			if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
+				n->width_attr = vw;
+				n->height_attr = vh;
+				n->flags |= BR_NODE_VIEWBOX;
+			}
+		} else if (str_eq(name, "text-anchor")) {
+			if (contains_ci_len(s + vstart, vlen, "middle"))
+				n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
+			else if (contains_ci_len(s + vstart, vlen, "end"))
+				n->attr_flags |= BR_ATTR_ANCHOR_END;
 		}
 	}
 }
@@ -977,7 +1047,7 @@ enum {
 	PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
 	PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
 	PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
-	PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT,
+	PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
 	PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
 	PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
 	PROP_COUNT
@@ -1475,6 +1545,41 @@ static int find_color(const char *s, unsigned int len, unsigned short *c)
 	return 0;
 }
 
+/* Background shorthands can hold several layers before the final solid
+ * colour.  The colour is conventionally the last token (e.g. a stack of
+ * gradients followed by var(--bg)), so take the rightmost colour rather
+ * than the first gradient stop. */
+static int find_color_last(const char *s, unsigned int len, unsigned short *c)
+{
+	int last = -1;
+	unsigned int i;
+
+	for (i = 0; i < len; i++) {
+		if (s[i] == '#')
+			last = (int)i;
+		else if ((s[i] == 'r' || s[i] == 'R') &&
+			 i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
+			last = (int)i;
+	}
+	if (last >= 0)
+		return parse_color(s + last, len - (unsigned int)last, c);
+	while (i > 0) {
+		unsigned int st, e;
+
+		while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
+				 s[i - 1] == ')' || s[i - 1] == '('))
+			i--;
+		e = i;
+		while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
+		       s[i - 1] != ')' && s[i - 1] != '(')
+			i--;
+		st = i;
+		if (e > st && parse_color(s + st, e - st, c))
+			return 1;
+	}
+	return 0;
+}
+
 static int parse_display(const char *s, unsigned int len)
 {
 	unsigned int i;
@@ -1489,12 +1594,12 @@ static int parse_display(const char *s, unsigned int len)
 	if (i == 6 && ci_eq_n(s, "inline", 6))
 		return BR_DISPLAY_INLINE;
 	if (i == 12 && ci_eq_n(s, "inline-block", 12))
-		return BR_DISPLAY_BLOCK;
+		return BR_DISPLAY_INLINE_BLOCK;
 	if (i == 10 && ci_eq_n(s, "list-item", 10))
 		return BR_DISPLAY_BLOCK;
 	if (i == 4 && ci_eq_n(s, "flex", 4))
 		return BR_DISPLAY_FLEX;
-	if (i == 10 && ci_eq_n(s, "inline-flex", 10))
+	if (i == 11 && ci_eq_n(s, "inline-flex", 11))
 		return BR_DISPLAY_FLEX;
 	if (i == 5 && ci_eq_n(s, "table", 5))
 		return BR_DISPLAY_BLOCK;
@@ -2046,13 +2151,50 @@ static void parse_decls(struct br_doc *d, const char *s,
 			unsigned short c;
 			if (parse_color(s + vstart, vend - vstart, &c))
 				decl_add(out, nout, PROP_COLOR, c, important);
+		} else if (str_eq(prop, "fill")) {
+			unsigned short c;
+
+			if (parse_color(s + vstart, vend - vstart, &c)) {
+				decl_add(out, nout, PROP_BG, c, important);
+				decl_add(out, nout, PROP_COLOR, c, important);
+			}
+		} else if (str_eq(prop, "stroke")) {
+			unsigned short c;
+
+			if (parse_color(s + vstart, vend - vstart, &c)) {
+				decl_add(out, nout, PROP_BORDER_COLOR, c,
+					 important);
+				decl_add(out, nout, PROP_BORDER_W, 1, important);
+			}
+		} else if (str_eq(prop, "stroke-width")) {
+			unsigned int j = vstart;
+			int sw = 0;
+
+			while (j < vend && s[j] >= '0' && s[j] <= '9') {
+				sw = sw * 10 + (s[j] - '0');
+				j++;
+			}
+			if (sw < 1)
+				sw = 1;
+			if (sw > 3)
+				sw = 3;
+			decl_add(out, nout, PROP_BORDER_W, sw, important);
+		} else if (str_eq(prop, "text-anchor")) {
+			int a = BR_ALIGN_LEFT;
+
+			if (contains_ci_len(s + vstart, vend - vstart, "middle"))
+				a = BR_ALIGN_CENTER;
+			else if (contains_ci_len(s + vstart, vend - vstart,
+						 "end"))
+				a = BR_ALIGN_RIGHT;
+			decl_add(out, nout, PROP_ALIGN, a, important);
 		} else if (str_eq(prop, "background-color") ||
 			   str_eq(prop, "background")) {
 			unsigned short c;
 			if (parse_color(s + vstart, vend - vstart, &c))
 				decl_add(out, nout, PROP_BG, c, important);
 			else if (str_eq(prop, "background") &&
-				 find_color(s + vstart, vend - vstart, &c))
+				 find_color_last(s + vstart, vend - vstart, &c))
 				decl_add(out, nout, PROP_BG, c, important);
 		} else if (str_eq(prop, "font-size")) {
 			int px = parse_font_px(s + vstart, vend - vstart);
@@ -2172,10 +2314,11 @@ static void parse_decls(struct br_doc *d, const char *s,
 					    "absolute"))
 				p = 1;
 			else if (contains_ci_len(s + vstart, vend - vstart,
-						 "fixed") ||
-				 contains_ci_len(s + vstart, vend - vstart,
-						 "sticky"))
+						 "fixed"))
 				p = 2;
+			/* position:sticky stays in the flow.  Treating it as
+			 * fixed painted the header over the first content
+			 * block; a static header is safer than hidden text. */
 			decl_add(out, nout, PROP_POS, p, important);
 		} else if (str_eq(prop, "left") || str_eq(prop, "top")) {
 			int is_left = prop[0] == 'l';
@@ -2214,15 +2357,44 @@ static void parse_decls(struct br_doc *d, const char *s,
 						break;
 					}
 			}
-			n = parse_grid_cols(v, vl, val, kind);
-			if (n > 0) {
-				decl_add(out, nout, PROP_WIDTH, val[0], important);
-				decl_add(out, nout, PROP_WIDTH_KIND, kind[0], important);
-				decl_add(out, nout, PROP_GRID_NCOLS, n, important);
-			}
-			if (n > 1) {
-				decl_add(out, nout, PROP_MAXW, val[1], important);
-				decl_add(out, nout, PROP_MAXW_KIND, kind[1], important);
+			if (contains_ci_len(v, vl, "auto-fit") ||
+			    contains_ci_len(v, vl, "auto-fill")) {
+				unsigned int mp = 0;
+				int found = 0;
+				unsigned short mw = 200;
+				unsigned char mk = 1;
+
+				for (q = 0; q + 7 <= (int)vl; q++)
+					if (ci_eq_n(v + q, "minmax(", 7)) {
+						mp = (unsigned int)q + 7;
+						found = 1;
+						break;
+					}
+				if (found)
+					parse_grid_track(v, vl, &mp, &mw, &mk);
+				if (mk != 1 || mw < 80)
+					mw = 200;
+				if (mw > 480)
+					mw = 480;
+				decl_add(out, nout, PROP_GRID_AUTO, 1, important);
+				decl_add(out, nout, PROP_WIDTH, mw, important);
+				decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
+			} else {
+				n = parse_grid_cols(v, vl, val, kind);
+				if (n > 0) {
+					decl_add(out, nout, PROP_WIDTH, val[0],
+						 important);
+					decl_add(out, nout, PROP_WIDTH_KIND,
+						 kind[0], important);
+					decl_add(out, nout, PROP_GRID_NCOLS, n,
+						 important);
+				}
+				if (n > 1) {
+					decl_add(out, nout, PROP_MAXW, val[1],
+						 important);
+					decl_add(out, nout, PROP_MAXW_KIND,
+						 kind[1], important);
+				}
 			}
 		} else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
 			unsigned int gp = 0;
@@ -2536,10 +2708,11 @@ static int media_feature_matches(const char *s, unsigned int len)
 	while (len && is_space(*s)) { s++; len--; }
 	while (len && is_space(s[len - 1])) len--;
 	if (contains_ci_len(s, len, "prefers-color-scheme")) {
-		/* The browser canvas is dark, but sites express their light
-		 * palette in the base rules; the dark overrides are usually
-		 * invisible against our default surfaces.  Pick the light set. */
-		return !contains_ci_len(s, len, "dark");
+		/* There is no user-selectable colour scheme.  Ignore both
+		 * variants so a page's base palette is used; sites whose base
+		 * is dark (like the project docs) then stay dark instead of
+		 * flipping to their light media override. */
+		return 0;
 	}
 	if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
 		for (i = 9; i < len && s[i] != ':'; i++)
@@ -3442,6 +3615,9 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_GRID_SLOT:
 		st->grid_slot = (unsigned char)dcl->val;
 		break;
+	case PROP_GRID_AUTO:
+		st->grid_auto = (unsigned char)(dcl->val != 0);
+		break;
 	case PROP_POS:
 		st->pos = (unsigned char)dcl->val;
 		break;
@@ -3505,6 +3681,7 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	out->pt = out->pr = out->pb = out->pl = 0;
 	out->flex_grow = out->flex_col = out->flex_wrap = 0;
 	out->grid_ncols = out->grid_gap = out->grid_slot = 0;
+	out->grid_auto = 0;
 	out->pos = out->left_set = out->top_set = 0;
 	out->left = out->top = 0;
 	out->vis_hidden = out->opacity_hidden = 0;
@@ -3673,6 +3850,42 @@ static int has_attr_token(const char *s, unsigned int len, const char *name)
 	return 0;
 }
 
+static int stack_has_svg(const struct br_doc *d,
+			 const unsigned short *stack, unsigned int sp)
+{
+	unsigned int k;
+
+	for (k = 0; k <= sp && k < 64; k++)
+		if (d->nodes[stack[k]].tag == BR_TAG_SVG)
+			return 1;
+	return 0;
+}
+
+/* Skip an element subtree by finding its closing tag.  Used for SVG
+ * <title>/<desc>/<metadata> metadata, which must not enter the flow or
+ * overwrite the document title. */
+static unsigned int skip_element(const char *html, unsigned int len,
+				 unsigned int i, const char *name,
+				 unsigned int n)
+{
+	char close[40];
+	unsigned int k = 0, p;
+
+	close[k++] = '<';
+	close[k++] = '/';
+	while (k < sizeof(close) - 1 && n--)
+		close[k++] = *name++;
+	p = (unsigned int)find_ci(html, len, close, k, i);
+	if ((int)p < 0)
+		return len;
+	p += k;
+	while (p < len && html[p] != '>')
+		p++;
+	if (p < len)
+		p++;
+	return p;
+}
+
 int br_parse(struct br_doc *d, const char *html, unsigned int len)
 {
 	unsigned short stack[64];
@@ -3816,12 +4029,12 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 					i++;
 				continue;
 			}
-			if (tag == BR_TAG_SVG && !selfclose) {
-				/* Inline SVG is not rendered; skip its subtree so
-				 * logos and icon sprites do not consume the node
-				 * arena. */
-				int p = find_ci(html, len, "</svg>", 6, i);
-				i = p >= 0 ? (unsigned int)p + 6 : len;
+			if (stack_has_svg(d, stack, sp) &&
+			    (tag == BR_TAG_TITLE ||
+			     (tag == BR_TAG_UNKNOWN &&
+			      (ci_eq_n(name, "desc", 4) ||
+			       ci_eq_n(name, "metadata", 8))))) {
+				i = skip_element(html, len, i, name, n);
 				continue;
 			}
 			if (tag == BR_TAG_DETAILS && !selfclose &&

+ 8 - 0
browser_html.h

@@ -26,6 +26,8 @@
 #define BR_NODE_PREFORMAT 1
 /* Element flag: the HTML hidden attribute is present. */
 #define BR_NODE_HIDDEN   2
+/* Element flag: width_attr/height_attr hold an SVG viewBox. */
+#define BR_NODE_VIEWBOX  4
 
 /* Extra attribute presence flags (data-collapse-*, state attributes).
  * Stored separately because struct br_style/selector matching needs them
@@ -36,6 +38,8 @@
 #define BR_ATTR_DISABLED         0x08
 #define BR_ATTR_OPEN             0x10
 #define BR_ATTR_INERT            0x20
+#define BR_ATTR_ANCHOR_MIDDLE    0x40
+#define BR_ATTR_ANCHOR_END       0x80
 
 /* Display values in struct br_style. */
 #define BR_DISPLAY_INLINE 0
@@ -43,6 +47,7 @@
 #define BR_DISPLAY_NONE   2
 #define BR_DISPLAY_FLEX   3
 #define BR_DISPLAY_GRID   4
+#define BR_DISPLAY_INLINE_BLOCK 5
 
 /* Text alignment. */
 #define BR_ALIGN_LEFT   0
@@ -90,6 +95,7 @@ struct br_style {
 	unsigned char grid_ncols;   /* 1 or 2 supported */
 	unsigned char grid_gap;     /* px */
 	unsigned char grid_slot;    /* 0 auto, 1 full, 2 left, 3 right */
+	unsigned char grid_auto;    /* repeat(auto-fit, minmax(width, 1fr)) */
 	unsigned char pos;          /* 0 static, 1 absolute, 2 fixed */
 	unsigned char left_set, top_set;
 	signed short left, top;
@@ -136,6 +142,8 @@ struct br_node {
 	unsigned char width_pct;
 	unsigned char height_pct;
 	short img_index;            /* -1 until the image is loaded */
+	signed short svg_x, svg_y;  /* inline SVG x/y or line x1/y1 */
+	unsigned char svg_rx;       /* inline SVG rect corner radius */
 	struct br_style style;      /* computed-style cache */
 	unsigned char style_valid;
 };

+ 2 - 1
docs/graphics.html

@@ -266,7 +266,7 @@
 			</div>
 			<div class="card">
 				<h3>browser</h3>
-				<p>HTML renderer with CSS subset, inline images and a headless fetch helper. The largest ring-3 application in the tree.</p>
+				<p>HTML/CSS renderer with flex rows, auto-fit card grids, inline-block runs, basic tables and inline SVG diagrams. It decodes inline images and fetches through a headless helper; there is no JavaScript or form submission. The largest ring-3 application in the tree.</p>
 			</div>
 		</div>
 	</section>
@@ -308,6 +308,7 @@
 			<li><code>ucmd_wm.c</code>, <code>uwm_lib.c</code> — ring-3 WM</li>
 			<li><code>gfx.c</code>, <code>gfx.h</code> — RGB565 window canvases</li>
 			<li><code>sys_wm.c</code>, <code>sys_wm.h</code> — WM/window ABI</li>
+			<li><code>browser.c</code>, <code>browser_html.c</code> — HTML/CSS renderer and inline SVG</li>
 			<li><code>ngui.c</code>, <code>ngui.h</code> — toolkit</li>
 			<li><code>ucmd_files.c</code>, <code>gallery.c</code>, <code>ucmd_solitaire.c</code></li>
 			<li><code>img*.c</code> — image codecs</li>