Przeglądaj źródła

Load external CSS and improve browser layout

Johannes Zellner 2 tygodni temu
rodzic
commit
52229c454e
4 zmienionych plików z 1021 dodań i 166 usunięć
  1. 26 11
      AGENTS.md
  2. 347 48
      browser.c
  3. 616 103
      browser_html.c
  4. 32 4
      browser_html.h

+ 26 - 11
AGENTS.md

@@ -438,12 +438,20 @@ run area), so adding code no longer moves any address.
   `uhttp.c/h` is the shared ring-3 HTTP client extracted from curl: URL
   parsing/resolution, redirects, chunked decoding, timeouts and TLS; curl and
   the browser both use it.  `browser_html.c/h` is a tolerant HTML tree + the
-  CSS subset (tag/class/id selectors, colours, backgrounds, font size/weight,
-  alignment, margins, padding, display); `browser.c` owns the layout/render,
-  URL bar, history, status bar and inline `<img>` decoding.  Page and image
-  buffers come from `SYS_MEM_ALLOC`; the DOM uses fixed static arenas.
-  There is no JavaScript, forms, external CSS or standalone image viewer
-  mode.  `SYS_SPAWN` loads FAT ELFs through `kmalloc` now (the old 16 KB
+  CSS subset: external `<link rel=stylesheet>` sheets (in document order,
+  inline `<style>` last), tag/class/id and descendant/child selectors,
+  colours/`rgba()`, backgrounds, font size (`px`/`%`/`em`/`rem`), numeric
+  weights, `!important`, line-height, alignment, margins, padding, borders,
+  `list-style: none`, `text-transform: uppercase`, `display`, and percentage
+  `width`/`height`/`max-width`; selectors with `:`/`[`/`+`/`~` are skipped
+  individually, and `@media`/`@font-face`/`@import` blocks are ignored.
+  `browser.c` owns the layout/render, URL bar, history, status bar and inline
+  `<img>` decoding.  Page and image buffers come from `SYS_MEM_ALLOC`; the DOM
+  uses fixed static arenas.  Images are decoded once into a shared 24 MB
+  arena and copied to a display bitmap at most 1000 px wide (up to 16 images,
+  512 KB files / 8 MP decodes), so 4K screenshots fit the 32 MB heap.  There
+  is no JavaScript, forms, floats, flex/grid or standalone image viewer mode.
+  `SYS_SPAWN` loads FAT ELFs through `kmalloc` now (the old 16 KB
   `fat_load_buf` is gone) and wraps the load in `sys_fs_enter/exit`, since the
   floppy needs IRQ 6.
 - **Image library + viewer**: `img.h`, `img.c`, `img_mem.c`, `img_blit.c`,
@@ -959,11 +967,18 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     within roughly 30 KB of the `0x7F000` wall.  Adding more kernel buffers
     or commands should be paired with reclaiming space elsewhere.
 
-23. Browser limits: no JavaScript, forms, external stylesheets, floats,
-    flex/grid, tables with column widths or standalone image documents.  CSS
-    `font-size` maps to 1x/2x only, `vh`/`vw`/`em` units are approximated as
-    pixels, and images are decoded eagerly (up to 6, 4 MB/4 MP each) and
-    redrawn by `img_draw` on every repaint.  TLS is phase A (no certificate
+23. Browser limits: no JavaScript, forms, floats, flex/grid, tables with
+    column widths, absolute/fixed positioning, inline-block sizing or
+    standalone image documents.  `@media`/`@font-face`/`@import` are ignored,
+    so only the base CSS applies on a narrow window, and pseudo-classes are
+    never matched (`:hover` styles only via their non-pseudo siblings).
+    `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
+    eagerly 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.  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.

+ 347 - 48
browser.c

@@ -20,11 +20,15 @@
 #define BR_STATUS_H     18
 
 #define BR_HEAP_INIT    (6u * 1024u * 1024u)
-#define BR_HEAP_MAX     (28u * 1024u * 1024u)
+#define BR_HEAP_MAX     (32u * 1024u * 1024u)
 #define BR_PAGE_MAX     (512u * 1024u)
-#define BR_IMG_FILE_MAX (4u * 1024u * 1024u)
-#define BR_IMG_PIX_MAX  (4u * 1024u * 1024u)
-#define BR_MAX_IMAGES   6
+#define BR_IMG_FILE_MAX (512u * 1024u)
+#define BR_IMG_PIX_MAX  (8u * 1024u * 1024u)
+#define BR_MAX_IMAGES   16
+#define BR_IMG_ARENA    (24u * 1024u * 1024u)
+#define BR_IMG_LAYOUT_W 1000
+#define BR_CSS_MAX      (128u * 1024u)
+#define BR_MAX_SHEETS   6
 #define BR_HISTORY      16
 #define BR_URL_MAX      320
 #define BR_STATUS_MAX   200
@@ -54,7 +58,8 @@ enum {
 
 struct br_image {
 	int state;                  /* 0 empty, 1 ready */
-	struct img_image img;
+	struct img_image img;       /* scaled bitmap at <= BR_IMG_LAYOUT_W */
+	int native_w, native_h;
 	char err[32];
 };
 
@@ -70,6 +75,15 @@ static unsigned char *heap;
 static unsigned int heap_size = 0;
 static unsigned int heap_used = 0;
 
+/* One reusable file buffer and one shared decode arena: decoded pixels are
+ * scaled down to a display bitmap, then the arena is reused for the next
+ * image.  This keeps 4K screenshots inside the 32 MB heap. */
+static unsigned char image_file_buf[BR_IMG_FILE_MAX];
+static unsigned char image_comp_buf[BR_IMG_FILE_MAX];
+static struct inflate image_inflate_ws;
+static unsigned char *image_arena;
+static unsigned int image_arena_size;
+
 static char page_url[BR_URL_MAX];
 static char page_title[BR_TITLE_MAX];
 static char cwd[64];
@@ -249,6 +263,8 @@ static void heap_clear(void)
 	u_mem_free();
 	heap = 0;
 	heap_size = heap_used = 0;
+	image_arena = 0;
+	image_arena_size = 0;
 }
 
 static void *heap_alloc(unsigned int bytes, unsigned int align)
@@ -301,43 +317,103 @@ static void image_reset(void)
 
 	for (i = 0; i < BR_MAX_IMAGES; i++) {
 		images[i].state = 0;
+		images[i].native_w = images[i].native_h = 0;
 		images[i].err[0] = '\0';
 	}
 	image_count = 0;
 }
 
+/* Decode into the shared arena, then copy/scale the result into a display
+ * bitmap of at most BR_IMG_LAYOUT_W pixels wide.  The arena is reused by the
+ * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
+ * pixels.  Transparent edges are composited over white. */
 static int decode_image_data(const unsigned char *data, unsigned int len,
 			     struct br_image *bi)
 {
 	struct img_info info;
 	struct img_options opt;
 	struct img_mem mem;
-	void *arena;
-	unsigned int est;
-	int r;
+	struct img_image decoded;
+	unsigned int est, bytes, i;
+	unsigned short *px;
+	int dw, dh, r;
 
 	if (img_sniff(data, len, &info) != 0) {
 		s_copy(bi->err, "unknown image format", sizeof(bi->err));
 		return -1;
 	}
 	est = img_estimate(&info);
-	if (est == 0xFFFFFFFFu || est > BR_HEAP_MAX / 2) {
+	if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA) {
 		s_copy(bi->err, "image too large", sizeof(bi->err));
 		return -1;
 	}
-	est += 64u * 1024u;
-	arena = heap_alloc(est, 16);
-	if (!arena) {
-		s_copy(bi->err, "out of memory", sizeof(bi->err));
-		return -1;
+	if (!image_arena) {
+		image_arena = (unsigned char *)heap_alloc(BR_IMG_ARENA, 16);
+		if (!image_arena) {
+			s_copy(bi->err, "out of memory", sizeof(bi->err));
+			return -1;
+		}
+		image_arena_size = BR_IMG_ARENA;
 	}
-	img_mem_init(&mem, arena, est);
+	img_mem_init(&mem, image_arena, image_arena_size);
 	opt.max_pixels = BR_IMG_PIX_MAX;
-	r = img_decode(data, len, &opt, &mem, &bi->img);
+	r = img_decode(data, len, &opt, &mem, &decoded);
 	if (r != 0) {
 		s_copy(bi->err, img_error_string(r), sizeof(bi->err));
 		return -1;
 	}
+	if (decoded.w == 0 || decoded.h == 0) {
+		s_copy(bi->err, "empty image", sizeof(bi->err));
+		return -1;
+	}
+	bi->native_w = (int)decoded.w;
+	bi->native_h = (int)decoded.h;
+
+	dw = (int)decoded.w;
+	dh = (int)decoded.h;
+	if (dw > BR_IMG_LAYOUT_W) {
+		dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
+			   decoded.w);
+		dw = BR_IMG_LAYOUT_W;
+	}
+	if (dh < 1)
+		dh = 1;
+	if (dw < 1)
+		dw = 1;
+	bytes = (unsigned int)dw * (unsigned int)dh * 2u;
+	px = (unsigned short *)heap_alloc(bytes, 16);
+	if (!px) {
+		s_copy(bi->err, "out of memory", sizeof(bi->err));
+		return -1;
+	}
+	for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
+		px[i] = BR_WHITE;
+
+	{
+		struct img_blit b;
+
+		b.dst = px;
+		b.dst_w = dw;
+		b.dst_h = dh;
+		b.clip_x = 0;
+		b.clip_y = 0;
+		b.clip_w = dw;
+		b.clip_h = dh;
+		b.x = 0;
+		b.y = 0;
+		b.scale = ((unsigned int)dw << 16) / decoded.w;
+		b.bg = BR_WHITE;
+		b.bg2 = BR_WHITE;
+		b.flags = IMG_BLIT_OVER_DST;
+		img_draw(&decoded, &b);
+	}
+
+	bi->img.format = decoded.format;
+	bi->img.w = (unsigned int)dw;
+	bi->img.h = (unsigned int)dh;
+	bi->img.px = px;
+	bi->img.alpha = 0;
+	bi->img.flags = 0;
 	bi->state = 1;
 	return 0;
 }
@@ -661,6 +737,111 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 	return 0;
 }
 
+/* Fetch an image into the fixed scratch buffers.  The page heap is already
+ * carrying the 24 MB decode arena and every scaled bitmap, so images must not
+ * allocate their file body from it as well. */
+static int fetch_image(const char *url, unsigned int cap,
+		       unsigned char **out, unsigned int *outlen,
+		       char *err, unsigned int errcap)
+{
+	unsigned int total = 0;
+	int rv;
+
+	*out = 0;
+	*outlen = 0;
+	if (cap > BR_IMG_FILE_MAX)
+		cap = BR_IMG_FILE_MAX;
+	if (url[0] == '/') {
+		rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
+				(int)cap);
+		if (rv < 0) {
+			s_copy(err, "file not found", errcap);
+			return -1;
+		}
+		*out = image_file_buf;
+		*outlen = (unsigned int)rv;
+		return 0;
+	}
+
+	uhttp_init(&http);
+	http.user_agent = "nebos-browser/0.1";
+	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
+	if (rv != 0) {
+		s_copy(err, uhttp_error_string(rv), errcap);
+		return -1;
+	}
+	if (http.resp.gzip ||
+	    contains_ci(http.resp.content_encoding, "gzip") ||
+	    contains_ci(http.resp.content_encoding, "deflate")) {
+		unsigned int plen = 0;
+		int zr;
+
+		total = 0;
+		for (;;) {
+			rv = uhttp_read(&http, image_comp_buf + total,
+					BR_IMG_FILE_MAX - total,
+					UHTTP_IDLE_TIMEOUT_MS);
+			if (rv > 0) {
+				total += (unsigned int)rv;
+				if (total >= BR_IMG_FILE_MAX)
+					break;
+				continue;
+			}
+			if (rv < 0) {
+				s_copy(err, uhttp_error_string(rv), errcap);
+				uhttp_close(&http);
+				return -1;
+			}
+			break;
+		}
+		uhttp_close(&http);
+		if (!total) {
+			s_copy(err, "empty image", errcap);
+			return -1;
+		}
+		if (contains_ci(http.resp.content_encoding, "deflate"))
+			zr = inflate_zlib_mem(image_comp_buf, total,
+					      image_file_buf, cap, &plen,
+					      &image_inflate_ws);
+		else
+			zr = inflate_gzip_mem(image_comp_buf, total,
+					      image_file_buf, cap, &plen,
+					      &image_inflate_ws);
+		if (zr != 0) {
+			s_copy(err, "compressed image is corrupt", errcap);
+			return -1;
+		}
+		*out = image_file_buf;
+		*outlen = plen;
+		return 0;
+	}
+
+	for (;;) {
+		rv = uhttp_read(&http, image_file_buf + total, cap - total,
+				UHTTP_IDLE_TIMEOUT_MS);
+		if (rv > 0) {
+			total += (unsigned int)rv;
+			if (total >= cap)
+				break;
+			continue;
+		}
+		if (rv < 0) {
+			s_copy(err, uhttp_error_string(rv), errcap);
+			uhttp_close(&http);
+			return -1;
+		}
+		break;
+	}
+	uhttp_close(&http);
+	if (!total) {
+		s_copy(err, "empty image", errcap);
+		return -1;
+	}
+	*out = image_file_buf;
+	*outlen = total;
+	return 0;
+}
+
 /* ---- page loading ---- */
 
 static void redraw_now(void);
@@ -709,6 +890,55 @@ static int decode_direct_image(const unsigned char *data, unsigned int len,
 	return 0;
 }
 
+static int ancestor_is(unsigned int node, unsigned int tag)
+{
+	unsigned int p = doc.nodes[node].parent;
+
+	while (p) {
+		if (doc.nodes[p].tag == tag)
+			return 1;
+		p = doc.nodes[p].parent;
+	}
+	return 0;
+}
+
+static void load_stylesheets(void)
+{
+	unsigned int i, sheets = 0;
+
+	for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
+		struct br_node *nd = &doc.nodes[i];
+		const char *rel, *href;
+		char url[BR_URL_MAX];
+		unsigned char *data;
+		unsigned int len;
+		int local, trunc;
+		char ctype[64];
+		char final[BR_URL_MAX];
+		char err[64];
+
+		if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
+			continue;
+		rel = br_rel(&doc, i);
+		if (!contains_ci(rel, "stylesheet"))
+			continue;
+		href = br_href(&doc, i);
+		if (!href[0])
+			continue;
+		if (resolve_link(page_url, href, url, sizeof(url)) != 0)
+			continue;
+		if (read_url(url, BR_CSS_MAX, &data, &len, &local, ctype,
+			     sizeof(ctype), final, sizeof(final), err,
+			     sizeof(err), &trunc) != 0)
+			continue;
+		if (len)
+			br_parse_css_text(&doc, (const char *)data, len);
+		sheets++;
+	}
+	br_parse_inline_css(&doc);
+	br_reset_styles(&doc);
+}
+
 static void preload_images(void)
 {
 	unsigned int i;
@@ -720,9 +950,6 @@ static void preload_images(void)
 		char url[BR_URL_MAX];
 		unsigned char *data;
 		unsigned int len;
-		int local, trunc;
-		char ctype[64];
-		char final[BR_URL_MAX];
 		char err[64];
 		char msg[64];
 		unsigned int n = 0;
@@ -730,8 +957,11 @@ static void preload_images(void)
 		if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
 		    nd->img_index >= 0)
 			continue;
+		if (ancestor_is(i, BR_TAG_NOSCRIPT))
+			continue;
 		src = br_src(&doc, i);
-		if (!src[0])
+		if (!src[0] || contains_ci(src, ".svg") ||
+		    starts_with(src, "data:"))
 			continue;
 		if (resolve_link(page_url, src, url, sizeof(url)) != 0)
 			continue;
@@ -743,13 +973,9 @@ static void preload_images(void)
 		status_error = 0;
 		redraw_now();
 
-		if (read_url(url, BR_IMG_FILE_MAX, &data, &len, &local, ctype,
-			     sizeof(ctype), final, sizeof(final), err,
-			     sizeof(err), &trunc) != 0) {
+		if (fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
+				sizeof(err)) != 0)
 			continue;
-		}
-		(void)local;
-		(void)trunc;
 		if (decode_image_data(data, len, &images[image_count]) != 0)
 			continue;
 		nd->img_index = (short)image_count;
@@ -795,6 +1021,8 @@ static void load_page(const char *url, int local, int push)
 
 	heap_clear();
 	image_reset();
+	page_scroll.offset = 0;
+	page_scroll.dragging = 0;
 	br_parse_plain(&doc, "", 0);    /* blank while loading */
 	page_title[0] = '\0';
 	page_loading = 1;
@@ -840,6 +1068,7 @@ static void load_page(const char *url, int local, int push)
 		br_parse(&doc, (const char *)body, len);
 		s_copy(page_title, doc.title[0] ? doc.title : page_url,
 		       sizeof(page_title));
+		load_stylesheets();
 		preload_images();
 	}
 	if (truncated)
@@ -863,6 +1092,8 @@ static void load_home(int push)
 {
 	heap_clear();
 	image_reset();
+	page_scroll.offset = 0;
+	page_scroll.dragging = 0;
 	br_parse_plain(&doc, "", 0);
 	page_loading = 1;
 	page_is_local = 0;
@@ -901,6 +1132,8 @@ static void load_home(int push)
 			"\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
 			"</body></html>";
 		br_parse(&doc, home, sizeof(home) - 1);
+		br_parse_inline_css(&doc);
+		br_reset_styles(&doc);
 		s_copy(page_title, doc.title, sizeof(page_title));
 	}
 	page_loading = 0;
@@ -959,16 +1192,24 @@ static void navigate(const char *input, int push)
 
 static struct br_style base_style(void)
 {
-	struct br_style s;
+	struct br_style s = { 0 };
 
 	s.color = BR_BLACK;
 	s.bg = BR_WHITE;
 	s.has_bg = 0;
+	s.border_color = 0;
 	s.scale = 1;
 	s.bold = 0;
 	s.underline = 0;
 	s.align = BR_ALIGN_LEFT;
 	s.display = BR_DISPLAY_BLOCK;
+	s.no_bullet = 0;
+	s.uppercase = 0;
+	s.border_w = 0;
+	s.width_kind = s.max_width_kind = s.height_kind = 0;
+	s.width = s.max_width = s.height = 0;
+	s.line_h_pct = 0;
+	s.line_h_px = 0;
 	s.mt = s.mb = s.ml = s.mr = 0;
 	s.pt = s.pb = s.pl = s.pr = 0;
 	return s;
@@ -1119,8 +1360,18 @@ static void add_run(struct lay *l, unsigned short off, unsigned short len,
 	}
 	l->line_x += w;
 	l->run_x = l->line_x;
-	if (l->line_h < 16 * st->scale)
-		l->line_h = 16 * st->scale;
+	{
+		int lh = 16 * st->scale;
+
+		if (st->line_h_px)
+			lh = st->line_h_px;
+		else if (st->line_h_pct)
+			lh = (16 * st->scale) * st->line_h_pct / 100;
+		if (lh < 16 * st->scale)
+			lh = 16 * st->scale;
+		if (l->line_h < lh)
+			l->line_h = lh;
+	}
 }
 
 static void add_image_run(struct lay *l, unsigned int node,
@@ -1156,30 +1407,66 @@ static void add_image_run(struct lay *l, unsigned int node,
 		add_run(l, 0, 0, &gs, href, tmp);
 		return;
 	}
-	if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
-		if (l->line_x + 8 <= l->right)
-			l->line_x += 8;
-	}
 	maxw = l->right - l->left;
-	dw = (int)bi->img.w;
-	dh = (int)bi->img.h;
-	if (doc.nodes[node].width_attr) {
-		int want = doc.nodes[node].width_attr;
-		dh = dh * want / dw;
-		dw = want;
-	}
-	if (doc.nodes[node].height_attr) {
-		int want = doc.nodes[node].height_attr;
-		if (dw > 0)
-			dw = dw * want / dh;
-		dh = want;
+	if (maxw < 8)
+		maxw = 8;
+	{
+		int iw = bi->native_w, ih = bi->native_h;
+		int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
+
+		if (iw <= 0 || ih <= 0) {
+			iw = (int)bi->img.w;
+			ih = (int)bi->img.h;
+		}
+
+		dw = iw;
+		dh = ih;
+		if (doc.nodes[node].width_attr) {
+			int want = doc.nodes[node].width_attr;
+			dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
+			have_w = 1;
+		}
+		if (doc.nodes[node].height_attr) {
+			int want = doc.nodes[node].height_attr;
+			dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
+			have_h = 1;
+		}
+		if (have_w && !have_h)
+			dh = ih ? ih * dw / iw : 0;
+		if (have_h && !have_w)
+			dw = iw ? iw * dh / ih : 0;
+
+		if (st->width_kind) {
+			css_w = 1;
+			dw = st->width_kind == 2 ? maxw * (int)st->width / 100 :
+						   (int)st->width;
+		}
+		if (st->height_kind) {
+			css_h = 1;
+			dh = st->height_kind == 2 ? maxw * (int)st->height / 100 :
+						    (int)st->height;
+		}
+		if (css_w && !css_h)
+			dh = ih ? ih * dw / iw : 0;
+		if (css_h && !css_w)
+			dw = iw ? iw * dh / ih : 0;
+
+		if (st->max_width_kind) {
+			int mw = st->max_width_kind == 2 ?
+				 maxw * (int)st->max_width / 100 :
+				 (int)st->max_width;
+			if (mw > 0 && dw > mw) {
+				dh = iw ? ih * mw / iw : 0;
+				dw = mw;
+			}
+		}
 	}
 	if (dw < 8)
 		dw = 8;
 	if (dh < 8)
 		dh = 8;
-	if (dw > maxw && maxw > 0) {
-		dh = dh * maxw / dw;
+	if (dw > maxw) {
+		dh = dw ? dh * maxw / dw : dh;
 		dw = maxw;
 	}
 	if (l->line_x + dw > l->right && (l->nruns > 0 ||
@@ -1228,6 +1515,14 @@ static void emit_text(struct lay *l, const char *s, unsigned int len,
 	if (!s || !len)
 		return;
 	base = (unsigned int)(s - doc.text);
+	if (st->uppercase) {
+		char *p = (char *)s;
+		unsigned int j;
+
+		for (j = 0; j < len; j++)
+			if (p[j] >= 'a' && p[j] <= 'z')
+				p[j] = (char)(p[j] - 'a' + 'A');
+	}
 	if (pre) {
 		while (i < len) {
 			unsigned int start;
@@ -1362,6 +1657,10 @@ static void layout_block(struct lay *l, unsigned int node,
 		ngui_fill_rect(l->ui, (struct ngui_rect){l0, top, r0 - l0,
 							 block_h[node]},
 			       st->bg);
+	if (l->draw && st->border_w && block_h[node])
+		ngui_frame(l->ui, (struct ngui_rect){l0, top, r0 - l0,
+						    block_h[node]},
+			   st->border_color);
 	l->left = l0 + st->pl;
 	l->right = r0 - st->pr;
 	if (l->right < l->left + 8)
@@ -1375,7 +1674,7 @@ static void layout_block(struct lay *l, unsigned int node,
 				 l->y + 2, l->right - l->left, 1}, BR_GRAY);
 		l->y += 8;
 	} else {
-		if (doc.nodes[node].tag == BR_TAG_LI) {
+		if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) {
 			char *m = li_marker[node];
 			if (!m[0]) {
 				unsigned int parent = doc.nodes[node].parent;

+ 616 - 103
browser_html.c

@@ -404,6 +404,11 @@ const char *br_alt(struct br_doc *d, unsigned int node)
 	return attr_ptr(d, d->nodes[node].alt_off);
 }
 
+const char *br_rel(struct br_doc *d, unsigned int node)
+{
+	return attr_ptr(d, d->nodes[node].rel_off);
+}
+
 /* ---- document setup / node append ---- */
 
 void br_doc_init(struct br_doc *d)
@@ -431,6 +436,7 @@ static void doc_reset(struct br_doc *d)
 	d->attrs[0] = '\0';
 	d->nrules = 0;
 	d->title[0] = '\0';
+	d->inline_css_len = 0;
 	d->truncated = 0;
 	d->plain = 0;
 	for (i = 0; i < BR_MAX_NODES; i++) {
@@ -444,9 +450,11 @@ static void doc_reset(struct br_doc *d)
 		d->nodes[i].href_off = d->nodes[i].class_off = 0;
 		d->nodes[i].id_off = d->nodes[i].style_off = 0;
 		d->nodes[i].src_off = d->nodes[i].alt_off = 0;
-		d->nodes[i].color_off = 0;
+		d->nodes[i].color_off = d->nodes[i].rel_off = 0;
 		d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
+		d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
 		d->nodes[i].img_index = -1;
+		d->nodes[i].style_valid = 0;
 	}
 }
 
@@ -589,10 +597,17 @@ static void parse_attrs(struct br_doc *d, struct br_node *n,
 			n->alt_off = store_attr(d, s + vstart, vlen, 120);
 		else if (str_eq(name, "color"))
 			n->color_off = store_attr(d, s + vstart, vlen, 32);
-		else if (str_eq(name, "width"))
+		else if (str_eq(name, "rel"))
+			n->rel_off = store_attr(d, s + vstart, vlen, 64);
+		else if (str_eq(name, "width")) {
 			n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
-		else if (str_eq(name, "height"))
+			if (vlen && s[vstart + vlen - 1] == '%')
+				n->width_pct = 1;
+		} else if (str_eq(name, "height")) {
 			n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
+			if (vlen && s[vstart + vlen - 1] == '%')
+				n->height_pct = 1;
+		}
 	}
 }
 
@@ -610,7 +625,11 @@ static int str_eq(const char *a, const char *b)
 enum {
 	PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
 	PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
-	PROP_PT, PROP_PR, PROP_PB, PROP_PL, PROP_COUNT
+	PROP_PT, PROP_PR, PROP_PB, PROP_PL,
+	PROP_BORDER_W, PROP_BORDER_COLOR, PROP_LIST_NONE,
+	PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
+	PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
+	PROP_COUNT
 };
 
 struct color_name {
@@ -666,7 +685,8 @@ static int named_color(const char *s, unsigned int len, unsigned short *out)
 	return 0;
 }
 
-/* Parse #rgb/#rrggbb/rgb(r,g,b)/a named colour. */
+/* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour.  Alpha is composited
+ * over white; the render target is a white page. */
 static int parse_color(const char *s, unsigned int len, unsigned short *out)
 {
 	unsigned int i = 0;
@@ -697,8 +717,9 @@ static int parse_color(const char *s, unsigned int len, unsigned short *out)
 		}
 		return 0;
 	}
-	if (i + 3 < len && ci_eq_n(s + i, "rgb", 3)) {
-		unsigned int v[3], k = 0;
+	if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
+		unsigned int v[3], k = 0, a = 255;
+		int is_rgba = (i + 4 <= len && ci_eq_n(s + i, "rgba", 4));
 
 		while (i < len && s[i] != '(')
 			i++;
@@ -713,6 +734,10 @@ static int parse_color(const char *s, unsigned int len, unsigned short *out)
 				x = x * 10 + (unsigned int)(s[i] - '0');
 				i++;
 			}
+			if (i < len && s[i] == '%') {
+				x = x * 255 / 100;
+				i++;
+			}
 			if (x > 255)
 				x = 255;
 			v[k++] = x;
@@ -721,9 +746,46 @@ static int parse_color(const char *s, unsigned int len, unsigned short *out)
 			if (i < len && s[i] == ',')
 				i++;
 		}
+		if (is_rgba && i < len) {
+			unsigned int whole = 0, frac = 0, scale = 1;
+			int percent = 0;
+
+			while (i < len && is_space(s[i]))
+				i++;
+			while (i < len && s[i] >= '0' && s[i] <= '9') {
+				whole = whole * 10 + (unsigned int)(s[i] - '0');
+				i++;
+			}
+			if (i < len && s[i] == '.') {
+				i++;
+				while (i < len && s[i] >= '0' && s[i] <= '9' && scale < 1000) {
+					frac = frac * 10 + (unsigned int)(s[i] - '0');
+					scale *= 10;
+					i++;
+				}
+			}
+			while (i < len && is_space(s[i]))
+				i++;
+			if (i < len && s[i] == '%')
+				percent = 1;
+			if (percent) {
+				a = whole > 100 ? 255 : whole * 255 / 100;
+			} else {
+				a = whole >= 1 ? 255 : (frac * 255) / scale;
+			}
+			if (a > 255)
+				a = 255;
+		}
 		if (k == 3) {
-			*out = (unsigned short)(((v[0] >> 3) << 11) |
-						((v[1] >> 2) << 5) | (v[2] >> 3));
+			unsigned int r = v[0], g = v[1], b = v[2];
+
+			if (a < 255) {
+				r = (r * a + 255u * (255u - a)) / 255u;
+				g = (g * a + 255u * (255u - a)) / 255u;
+				b = (b * a + 255u * (255u - a)) / 255u;
+			}
+			*out = (unsigned short)(((r >> 3) << 11) |
+						((g >> 2) << 5) | (b >> 3));
 			return 1;
 		}
 		return 0;
@@ -737,6 +799,7 @@ static int parse_color(const char *s, unsigned int len, unsigned short *out)
 	}
 }
 
+/* Length values for margins/padding: px, ignore percentages/units. */
 static int parse_len(const char *s, unsigned int len, unsigned int *pos)
 {
 	unsigned int i = *pos;
@@ -772,6 +835,213 @@ static int parse_len(const char *s, unsigned int len, unsigned int *pos)
 	return v;
 }
 
+/* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
+static int parse_size(const char *s, unsigned int len, unsigned short *val,
+		      unsigned char *kind)
+{
+	unsigned int i = 0;
+	unsigned int v = 0;
+	int digits = 0;
+	int pct = 0;
+
+	*val = 0;
+	*kind = 0;
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
+		return 0;
+	if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
+		return 0;
+	while (i < len && s[i] >= '0' && s[i] <= '9') {
+		v = v * 10 + (unsigned int)(s[i] - '0');
+		digits = 1;
+		i++;
+	}
+	if (i < len && s[i] == '.') {
+		i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9')
+			i++;
+	}
+	if (!digits)
+		return 0;
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i < len && s[i] == '%')
+		pct = 1;
+	if (pct) {
+		if (v > 100)
+			v = 100;
+		*kind = 2;
+	} else {
+		if (v > 2048)
+			v = 2048;
+		*kind = 1;
+	}
+	*val = (unsigned short)v;
+	return 1;
+}
+
+static int parse_font_px(const char *s, unsigned int len)
+{
+	unsigned int i = 0;
+	int whole = 0, frac = 0, fscale = 1, digits = 0;
+	int v1000, px;
+	int is_px = 1, is_rem = 0, is_pct = 0;
+
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
+		return 24;
+	if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
+		return 32;
+	if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
+		return 14;
+	while (i < len && s[i] >= '0' && s[i] <= '9') {
+		whole = whole * 10 + (s[i] - '0');
+		digits = 1;
+		i++;
+	}
+	if (i < len && s[i] == '.') {
+		i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
+			frac = frac * 10 + (s[i] - '0');
+			fscale *= 10;
+			i++;
+		}
+	}
+	if (!digits)
+		return 0;
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
+		is_rem = 1;
+		is_px = 0;
+	} else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
+		is_rem = 1;
+		is_px = 0;
+	} else if (i < len && s[i] == '%') {
+		is_pct = 1;
+		is_px = 0;
+	}
+	v1000 = whole * 1000 + frac * 1000 / fscale;
+	if (is_rem)
+		px = v1000 * 16 / 1000;
+	else if (is_pct)
+		px = v1000 * 16 / 100000;
+	else if (is_px)
+		px = v1000 / 1000;
+	else
+		px = v1000 / 1000;
+	if (px < 8)
+		px = 8;
+	if (px > 72)
+		px = 72;
+	return px;
+}
+
+static int parse_line_height(const char *s, unsigned int len,
+			     unsigned short *px, unsigned char *pct)
+{
+	unsigned int i = 0;
+	int whole = 0, frac = 0, fscale = 1, digits = 0;
+	int v1000;
+	int is_px = 0, is_pct = 0;
+
+	*px = 0;
+	*pct = 0;
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
+		return 0;
+	while (i < len && s[i] >= '0' && s[i] <= '9') {
+		whole = whole * 10 + (s[i] - '0');
+		digits = 1;
+		i++;
+	}
+	if (i < len && s[i] == '.') {
+		i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
+			frac = frac * 10 + (s[i] - '0');
+			fscale *= 10;
+			i++;
+		}
+	}
+	if (!digits)
+		return 0;
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
+		is_px = 1;
+	else if (i < len && s[i] == '%')
+		is_pct = 1;
+	v1000 = whole * 1000 + frac * 1000 / fscale;
+	if (is_px) {
+		int v = v1000 / 1000;
+
+		if (v < 8)
+			v = 8;
+		if (v > 160)
+			v = 160;
+		*px = (unsigned short)v;
+	} else {
+		int v = v1000 / 10;     /* unitless multiplier -> percent */
+
+		if (is_pct)
+			v = v1000 / 1000;
+		if (v < 50)
+			v = 50;
+		if (v > 400)
+			v = 400;
+		*pct = (unsigned char)v;
+	}
+	return 1;
+}
+
+static int contains_ci_len(const char *s, unsigned int len, const char *needle)
+{
+	unsigned int i, j, n = s_len(needle);
+
+	for (i = 0; i + n <= len; i++) {
+		for (j = 0; j < n; j++) {
+			char a = s[i + j];
+			if (a >= 'A' && a <= 'Z')
+				a = (char)(a - 'A' + 'a');
+			if (a != needle[j])
+				break;
+		}
+		if (j == n)
+			return 1;
+	}
+	return 0;
+}
+
+/* Pull a colour out of a border/background shorthand. */
+static int find_color(const char *s, unsigned int len, unsigned short *c)
+{
+	unsigned int i;
+
+	for (i = 0; i < len; i++) {
+		if (s[i] == '#')
+			return parse_color(s + i, len - i, c);
+		if ((s[i] == 'r' || s[i] == 'R') &&
+		    i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
+			return parse_color(s + i, len - i, c);
+	}
+	for (i = 0; i < len; ) {
+		unsigned int st, e;
+
+		while (i < len && (is_space(s[i]) || s[i] == ','))
+			i++;
+		st = i;
+		while (i < len && !is_space(s[i]) && s[i] != ',')
+			i++;
+		e = 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;
@@ -789,18 +1059,78 @@ static int parse_display(const char *s, unsigned int len)
 		return BR_DISPLAY_INLINE;
 	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_BLOCK;
+	if (i == 5 && ci_eq_n(s, "table", 5))
+		return BR_DISPLAY_BLOCK;
+	if (i == 10 && ci_eq_n(s, "table-cell", 10))
+		return BR_DISPLAY_INLINE;
+	if (i == 5 && ci_eq_n(s, "grid", 5))
+		return BR_DISPLAY_BLOCK;
 	return BR_DISPLAY_INLINE;
 }
 
+static void decl_add(struct br_decl *out, unsigned int *nout,
+		     unsigned char prop, int val, int important)
+{
+	if (*nout >= BR_DECLS)
+		return;
+	out[*nout].prop = prop;
+	out[*nout].val = (short)val;
+	out[*nout].important = (unsigned char)(important != 0);
+	(*nout)++;
+}
+
+static int parse_border_width(const char *s, unsigned int len)
+{
+	unsigned int i = 0;
+
+	while (i < len) {
+		while (i < len && is_space(s[i]))
+			i++;
+		if (i >= len)
+			break;
+		if (s[i] == '#') {
+			while (i < len && !is_space(s[i]))
+				i++;
+			continue;
+		}
+		if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
+			while (i < len && s[i] != ')')
+				i++;
+			if (i < len)
+				i++;
+			continue;
+		}
+		if (s[i] >= '0' && s[i] <= '9') {
+			int v = 0;
+
+			while (i < len && s[i] >= '0' && s[i] <= '9') {
+				v = v * 10 + (s[i] - '0');
+				i++;
+			}
+			if (v < 0)
+				v = 0;
+			if (v > 3)
+				v = 3;
+			return v;
+		}
+		while (i < len && !is_space(s[i]))
+			i++;
+	}
+	return -1;
+}
+
 static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
 			unsigned int *nout)
 {
 	unsigned int i = 0;
 
 	*nout = 0;
-	while (i < len && *nout < 8) {
+	while (i < len && *nout < BR_DECLS) {
 		unsigned int pstart, pend, vstart, vend, k;
 		char prop[24];
+		int important = 0;
 
 		while (i < len && (is_space(s[i]) || s[i] == ';'))
 			i++;
@@ -825,6 +1155,13 @@ static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
 			vstart++;
 		while (vend > vstart && is_space(s[vend - 1]))
 			vend--;
+		if (vend - vstart >= 10 &&
+		    ci_eq_n(s + vend - 10, "!important", 10)) {
+			important = 1;
+			vend -= 10;
+			while (vend > vstart && is_space(s[vend - 1]))
+				vend--;
+		}
 		for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
 			char c = s[pstart + k];
 			if (c >= 'A' && c <= 'Z')
@@ -834,73 +1171,131 @@ static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
 		prop[k] = '\0';
 		if (str_eq(prop, "color")) {
 			unsigned short c;
-			if (parse_color(s + vstart, vend - vstart, &c)) {
-				out[*nout].prop = PROP_COLOR;
-				out[*nout].val = (short)c;
-				(*nout)++;
-			}
+			if (parse_color(s + vstart, vend - vstart, &c))
+				decl_add(out, nout, PROP_COLOR, c, important);
 		} else if (str_eq(prop, "background-color") ||
 			   str_eq(prop, "background")) {
 			unsigned short c;
-			if (parse_color(s + vstart, vend - vstart, &c)) {
-				out[*nout].prop = PROP_BG;
-				out[*nout].val = (short)c;
-				(*nout)++;
-			}
+			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))
+				decl_add(out, nout, PROP_BG, c, important);
 		} else if (str_eq(prop, "font-size")) {
-			unsigned int pos = 0;
-			int v = parse_len(s + vstart, vend - vstart, &pos);
-			int scale = 1;
-
-			if (v >= 20)
-				scale = 2;
-			if (vend - vstart >= 5 &&
-			    ci_eq_n(s + vstart, "large", 5))
-				scale = 2;
-			if (vend - vstart >= 6 &&
-			    ci_eq_n(s + vstart, "x-large", 7))
-				scale = 2;
-			out[*nout].prop = PROP_SCALE;
-			out[*nout].val = (short)scale;
-			(*nout)++;
+			int px = parse_font_px(s + vstart, vend - vstart);
+			if (px > 0)
+				decl_add(out, nout, PROP_SCALE,
+					 px >= 24 ? 2 : 1, important);
 		} else if (str_eq(prop, "font-weight")) {
 			int bold = 0;
-			if ((vend - vstart >= 4 &&
-			     ci_eq_n(s + vstart, "bold", 4)) ||
-			    (vend - vstart >= 3 &&
-			     ci_eq_n(s + vstart, "700", 3)) ||
-			    (vend - vstart >= 3 &&
-			     ci_eq_n(s + vstart, "800", 3)) ||
-			    (vend - vstart >= 3 &&
-			     ci_eq_n(s + vstart, "900", 3)))
+			unsigned int j;
+
+			if (vend - vstart >= 4 &&
+			    ci_eq_n(s + vstart, "bold", 4))
 				bold = 1;
-			out[*nout].prop = PROP_BOLD;
-			out[*nout].val = (short)bold;
-			(*nout)++;
+			else {
+				int w = 0;
+				for (j = vstart; j < vend; j++)
+					if (s[j] >= '0' && s[j] <= '9')
+						w = w * 10 + (s[j] - '0');
+				if (w >= 500)
+					bold = 1;
+			}
+			decl_add(out, nout, PROP_BOLD, bold, important);
 		} else if (str_eq(prop, "text-align")) {
 			int a = BR_ALIGN_LEFT;
-			if ((vend - vstart >= 6 &&
-			     ci_eq_n(s + vstart, "center", 6)))
+			if (vend - vstart >= 6 &&
+			    ci_eq_n(s + vstart, "center", 6))
 				a = BR_ALIGN_CENTER;
-			else if ((vend - vstart >= 5 &&
-				  ci_eq_n(s + vstart, "right", 5)))
+			else if (vend - vstart >= 5 &&
+				 ci_eq_n(s + vstart, "right", 5))
 				a = BR_ALIGN_RIGHT;
-			out[*nout].prop = PROP_ALIGN;
-			out[*nout].val = (short)a;
-			(*nout)++;
+			decl_add(out, nout, PROP_ALIGN, a, important);
 		} else if (str_eq(prop, "text-decoration")) {
 			int u = 0;
 			if (vend - vstart >= 9 &&
 			    ci_eq_n(s + vstart, "underline", 9))
 				u = 1;
-			out[*nout].prop = PROP_UNDERLINE;
-			out[*nout].val = (short)u;
-			(*nout)++;
+			decl_add(out, nout, PROP_UNDERLINE, u, important);
+		} else if (str_eq(prop, "text-transform")) {
+			int u = (vend - vstart >= 9 &&
+				 ci_eq_n(s + vstart, "uppercase", 9));
+			decl_add(out, nout, PROP_UPPER, u, important);
 		} else if (str_eq(prop, "display")) {
-			out[*nout].prop = PROP_DISPLAY;
-			out[*nout].val = (short)parse_display(s + vstart,
-							      vend - vstart);
-			(*nout)++;
+			decl_add(out, nout, PROP_DISPLAY,
+				 parse_display(s + vstart, vend - vstart),
+				 important);
+		} else if (str_eq(prop, "width") ||
+			   str_eq(prop, "max-width") ||
+			   str_eq(prop, "height")) {
+			unsigned short v;
+			unsigned char kind;
+			if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
+				if (str_eq(prop, "width")) {
+					decl_add(out, nout, PROP_WIDTH, v, important);
+					decl_add(out, nout, PROP_WIDTH_KIND, kind,
+						 important);
+				} else if (str_eq(prop, "max-width")) {
+					decl_add(out, nout, PROP_MAXW, v, important);
+					decl_add(out, nout, PROP_MAXW_KIND, kind,
+						 important);
+				} else {
+					decl_add(out, nout, PROP_HEIGHT, v, important);
+					decl_add(out, nout, PROP_HEIGHT_KIND, kind,
+						 important);
+				}
+			}
+		} else if (str_eq(prop, "line-height")) {
+			unsigned short px;
+			unsigned char pct;
+
+			if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
+				if (px) {
+					decl_add(out, nout, PROP_LH_PX, px, important);
+					decl_add(out, nout, PROP_LH_PCT, 0, important);
+				} else {
+					decl_add(out, nout, PROP_LH_PX, 0, important);
+					decl_add(out, nout, PROP_LH_PCT, pct, important);
+				}
+			}
+		} else if (str_eq(prop, "list-style") ||
+			   str_eq(prop, "list-style-type")) {
+			int none = (vend - vstart >= 4 &&
+				    contains_ci_len(s + vstart, vend - vstart,
+						    "none"));
+			decl_add(out, nout, PROP_LIST_NONE, none, important);
+		} else if (str_eq(prop, "border") ||
+			   str_eq(prop, "border-top") ||
+			   str_eq(prop, "border-right") ||
+			   str_eq(prop, "border-bottom") ||
+			   str_eq(prop, "border-left") ||
+			   str_eq(prop, "border-color") ||
+			   str_eq(prop, "border-width")) {
+			unsigned short c;
+			int bw;
+			int have_color;
+
+			if (vend - vstart >= 4 &&
+			    ci_eq_n(s + vstart, "none", 4)) {
+				decl_add(out, nout, PROP_BORDER_W, 0, important);
+			} else {
+				bw = parse_border_width(s + vstart,
+							vend - vstart);
+				have_color = find_color(s + vstart,
+							vend - vstart, &c);
+				if (have_color) {
+					decl_add(out, nout, PROP_BORDER_COLOR, c,
+						 important);
+					if (bw < 0)
+						bw = 1;
+				}
+				if (bw >= 0) {
+					if (bw > 3)
+						bw = 3;
+					decl_add(out, nout, PROP_BORDER_W, bw,
+						 important);
+				}
+			}
 		} else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
 			int vals[4], nv = 0;
 			unsigned int pos = 0;
@@ -923,19 +1318,13 @@ static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
 				vals[3] = vals[1];
 			}
 			if (nv > 0) {
-				/* props are T, R, B, L adjacent */
-				out[*nout].prop = base;
-				out[*nout].val = (short)vals[0];
-				(*nout)++;
-				out[*nout].prop = (unsigned char)(base + 1);
-				out[*nout].val = (short)vals[1];
-				(*nout)++;
-				out[*nout].prop = (unsigned char)(base + 2);
-				out[*nout].val = (short)vals[2];
-				(*nout)++;
-				out[*nout].prop = (unsigned char)(base + 3);
-				out[*nout].val = (short)vals[3];
-				(*nout)++;
+				decl_add(out, nout, base, vals[0], important);
+				decl_add(out, nout, (unsigned char)(base + 1),
+					 vals[1], important);
+				decl_add(out, nout, (unsigned char)(base + 2),
+					 vals[2], important);
+				decl_add(out, nout, (unsigned char)(base + 3),
+					 vals[3], important);
 			}
 		} else if (str_eq(prop, "margin-top") ||
 			   str_eq(prop, "margin-right") ||
@@ -948,29 +1337,56 @@ static void parse_decls(const char *s, unsigned int len, struct br_decl *out,
 			unsigned int pos = 0;
 			int v = parse_len(s + vstart, vend - vstart, &pos);
 			unsigned char p = PROP_MT;
+			const char *side;
 
 			if (prop[0] == 'p')
 				p = PROP_PT;
-			if (str_eq(prop + 7, "right"))
+			side = prop + (prop[0] == 'p' ? 8 : 7);
+			if (str_eq(side, "right"))
 				p = (unsigned char)(p + 1);
-			else if (str_eq(prop + 7, "bottom"))
+			else if (str_eq(side, "bottom"))
 				p = (unsigned char)(p + 2);
-			else if (str_eq(prop + 7, "left"))
+			else if (str_eq(side, "left"))
 				p = (unsigned char)(p + 3);
-			out[*nout].prop = p;
-			out[*nout].val = (short)v;
-			(*nout)++;
+			decl_add(out, nout, p, v, important);
 		}
 	}
 }
 
+/* Skip @media/@font-face/@keyframes/@import without descending into their
+ * nested blocks.  This is the mobile/base rules path: media enhancements are
+ * deliberately ignored for now. */
+static unsigned int skip_at_rule(const char *s, unsigned int len,
+				 unsigned int i)
+{
+	unsigned int j = i;
+	int depth;
+
+	while (j < len && s[j] != '{' && s[j] != ';')
+		j++;
+	if (j >= len)
+		return len;
+	if (s[j] == ';')
+		return j + 1;
+	depth = 1;
+	j++;
+	while (j < len && depth) {
+		if (s[j] == '{')
+			depth++;
+		else if (s[j] == '}')
+			depth--;
+		j++;
+	}
+	return j;
+}
+
 static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 {
 	unsigned int i = 0;
 
 	while (i < len && d->nrules < BR_MAX_RULES) {
 		unsigned int sstart, send, bstart, bend, j;
-		struct br_decl decls[8];
+		struct br_decl decls[BR_DECLS];
 		unsigned int nd;
 
 		while (i < len && is_space(s[i]))
@@ -984,6 +1400,10 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 		}
 		if (i >= len || s[i] == '}')
 			break;
+		if (s[i] == '@') {
+			i = skip_at_rule(s, len, i);
+			continue;
+		}
 		sstart = i;
 		while (i < len && s[i] != '{')
 			i++;
@@ -1003,15 +1423,6 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 			send--;
 		if (sstart == send)
 			continue;
-		if (s[sstart] == '@')
-			continue;
-		/* Ignore sibling combinators and pseudo-classes: they need
-		 * state we do not model. */
-		for (j = sstart; j < send; j++)
-			if (s[j] == '+' || s[j] == '~' || s[j] == ':')
-				break;
-		if (j < send)
-			continue;
 		parse_decls(s + bstart, bend - bstart, decls, &nd);
 		if (!nd)
 			continue;
@@ -1030,6 +1441,20 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 			while (e > j && is_space(s[e - 1]))
 				e--;
 			if (e > j) {
+				/* Ignore single selectors with sibling combinators,
+				 * pseudo-classes or attribute tests: they need
+				 * state/attributes we do not model.  The other
+				 * selectors in the list still apply. */
+				for (k = j; k < e; k++)
+					if (s[k] == '+' || s[k] == '~' ||
+					    s[k] == ':' || s[k] == '[')
+						break;
+				if (k < e) {
+					j = q + 1;
+					if (q >= send)
+						break;
+					continue;
+				}
 				for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
 					sel[k] = s[j + k];
 				sel[k] = '\0';
@@ -1049,6 +1474,26 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 	}
 }
 
+void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
+{
+	parse_css(d, css, len);
+}
+
+void br_parse_inline_css(struct br_doc *d)
+{
+	if (d->inline_css_len)
+		parse_css(d, css_store, d->inline_css_len);
+	d->inline_css_len = 0;
+}
+
+void br_reset_styles(struct br_doc *d)
+{
+	unsigned int i;
+
+	for (i = 0; i < d->nnode; i++)
+		d->nodes[i].style_valid = 0;
+}
+
 /* ---- selector matching ---- */
 
 static int class_has(struct br_doc *d, unsigned int node, const char *cls,
@@ -1258,7 +1703,64 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_PR: st->pr = dcl->val; break;
 	case PROP_PB: st->pb = dcl->val; break;
 	case PROP_PL: st->pl = dcl->val; break;
-	default: break;
+	case PROP_BORDER_W:
+		st->border_w = (unsigned char)dcl->val;
+		if (st->border_w && !st->border_color)
+			st->border_color = st->color;
+		break;
+	case PROP_BORDER_COLOR:
+		st->border_color = (unsigned short)dcl->val;
+		break;
+	case PROP_LIST_NONE:
+		st->no_bullet = (unsigned char)(dcl->val != 0);
+		break;
+	case PROP_WIDTH:
+		st->width = (unsigned short)dcl->val;
+		break;
+	case PROP_WIDTH_KIND:
+		st->width_kind = (unsigned char)dcl->val;
+		break;
+	case PROP_MAXW:
+		st->max_width = (unsigned short)dcl->val;
+		break;
+	case PROP_MAXW_KIND:
+		st->max_width_kind = (unsigned char)dcl->val;
+		break;
+	case PROP_HEIGHT:
+		st->height = (unsigned short)dcl->val;
+		break;
+	case PROP_HEIGHT_KIND:
+		st->height_kind = (unsigned char)dcl->val;
+		break;
+	case PROP_LH_PCT:
+		st->line_h_pct = (unsigned char)dcl->val;
+		break;
+	case PROP_LH_PX:
+		st->line_h_px = (unsigned short)dcl->val;
+		break;
+	case PROP_UPPER:
+		st->uppercase = (unsigned char)(dcl->val != 0);
+		break;
+	default:
+		break;
+	}
+}
+
+static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
+			 int spec, short *best, short *best_imp)
+{
+	unsigned char p = dcl->prop;
+
+	if (p >= PROP_COUNT)
+		return;
+	if (dcl->important) {
+		if (spec >= best_imp[p]) {
+			apply_decl(st, dcl);
+			best_imp[p] = (short)spec;
+		}
+	} else if (best_imp[p] < 0 && spec >= best[p]) {
+		apply_decl(st, dcl);
+		best[p] = (short)spec;
 	}
 }
 
@@ -1266,18 +1768,29 @@ void br_style_for(struct br_doc *d, unsigned int node,
 		  const struct br_style *parent, struct br_style *out)
 {
 	unsigned int tag;
-	short best[PROP_COUNT];
+	short best[PROP_COUNT], best_imp[PROP_COUNT];
 	unsigned int i;
 
+	if (d->nodes[node].style_valid) {
+		*out = d->nodes[node].style;
+		return;
+	}
+
 	*out = *parent;
 	out->has_bg = 0;
+	out->border_w = 0;
+	out->border_color = 0;
+	out->width = out->max_width = out->height = 0;
+	out->width_kind = out->max_width_kind = out->height_kind = 0;
 	out->mt = out->mr = out->mb = out->ml = 0;
 	out->pt = out->pr = out->pb = out->pl = 0;
 	tag = d->nodes[node].tag;
 	out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
 					      BR_DISPLAY_INLINE;
-	for (i = 0; i < PROP_COUNT; i++)
+	for (i = 0; i < PROP_COUNT; i++) {
 		best[i] = -1;
+		best_imp[i] = -1;
+	}
 
 	switch (tag) {
 	case BR_TAG_HEAD:
@@ -1334,6 +1847,9 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	case BR_TAG_TD: case BR_TAG_TH:
 		out->pr = 12;
 		break;
+	case BR_TAG_VIDEO:
+		out->display = BR_DISPLAY_BLOCK;
+		break;
 	default:
 		break;
 	}
@@ -1354,24 +1870,22 @@ void br_style_for(struct br_doc *d, unsigned int node,
 
 		if (spec < 0)
 			continue;
-		for (k = 0; k < r->nd; k++) {
-			unsigned char p = r->d[k].prop;
-			if (spec >= best[p]) {
-				apply_decl(out, &r->d[k]);
-				best[p] = (short)spec;
-			}
-		}
+		for (k = 0; k < r->nd; k++)
+			cascade_decl(out, &r->d[k], spec, best, best_imp);
 	}
 
 	if (d->nodes[node].style_off) {
-		struct br_decl decls[8];
+		struct br_decl decls[BR_DECLS];
 		unsigned int nd, k;
 		const char *v = attr_ptr(d, d->nodes[node].style_off);
 
 		parse_decls(v, s_len(v), decls, &nd);
 		for (k = 0; k < nd; k++)
-			apply_decl(out, &decls[k]);
+			cascade_decl(out, &decls[k], 1000, best, best_imp);
 	}
+
+	d->nodes[node].style = *out;
+	d->nodes[node].style_valid = 1;
 }
 
 /* ---- HTML parser ---- */
@@ -1625,8 +2139,7 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 		}
 	}
 
-	if (css_len)
-		parse_css(d, css_store, css_len);
+	d->inline_css_len = css_len;
 	return 0;
 }
 

+ 32 - 4
browser_html.h

@@ -8,9 +8,10 @@
 
 #define BR_MAX_NODES     1536
 #define BR_TEXT_SIZE     24576
-#define BR_ATTR_SIZE     16384
+#define BR_ATTR_SIZE     64000
 #define BR_CSS_SIZE      12288
-#define BR_MAX_RULES     96
+#define BR_MAX_RULES     1536
+#define BR_DECLS         16
 #define BR_TITLE_MAX     96
 
 /* Node types. */
@@ -56,12 +57,24 @@ enum br_tag {
 struct br_style {
 	unsigned short color;
 	unsigned short bg;
+	unsigned short border_color;
 	unsigned char has_bg;
 	unsigned char scale;        /* 1 or 2 */
 	unsigned char bold;
 	unsigned char underline;
 	unsigned char align;        /* BR_ALIGN_* */
 	unsigned char display;      /* BR_DISPLAY_* */
+	unsigned char no_bullet;    /* list-style: none (inherited) */
+	unsigned char uppercase;    /* text-transform: uppercase (inherited) */
+	unsigned char border_w;
+	unsigned char width_kind;   /* 0 none, 1 px, 2 percent */
+	unsigned short width;
+	unsigned char max_width_kind;
+	unsigned short max_width;
+	unsigned char height_kind;
+	unsigned short height;
+	unsigned char line_h_pct;   /* percent of font size, 0 = default */
+	unsigned short line_h_px;   /* absolute px, 0 = default */
 	signed short mt, mb, ml, mr;
 	signed short pt, pb, pl, pr;
 };
@@ -84,20 +97,26 @@ struct br_node {
 	unsigned short src_off;
 	unsigned short alt_off;
 	unsigned short color_off;
+	unsigned short rel_off;
 	unsigned short width_attr;
 	unsigned short height_attr;
+	unsigned char width_pct;
+	unsigned char height_pct;
 	short img_index;            /* -1 until the image is loaded */
+	struct br_style style;      /* computed-style cache */
+	unsigned char style_valid;
 };
 
 struct br_decl {
 	unsigned char prop;
+	unsigned char important;
 	short val;
 };
 
 struct br_rule {
 	unsigned short sel_off;     /* selector text in the attr arena */
 	unsigned char nd;
-	struct br_decl d[8];
+	struct br_decl d[BR_DECLS];
 };
 
 struct br_doc {
@@ -111,6 +130,7 @@ struct br_doc {
 	struct br_rule *rules;
 	unsigned int nrules;
 	char title[BR_TITLE_MAX];
+	unsigned int inline_css_len;
 	int truncated;
 	int plain;                  /* text/plain: one preformatted text node */
 };
@@ -120,8 +140,15 @@ int  br_parse(struct br_doc *d, const char *html, unsigned int len);
 int  br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
 unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
 
+/* External CSS is parsed by the browser after br_parse(); parse the inline
+ * <style> blocks last so the cascade order matches the document. */
+void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
+void br_parse_inline_css(struct br_doc *d);
+void br_reset_styles(struct br_doc *d);
+
 /* A node's style: parent inherited values, UA defaults, matching rules, then
- * the inline style attribute. */
+ * the inline style attribute.  Results are cached in the node until
+ * br_reset_styles(). */
 void br_style_for(struct br_doc *d, unsigned int node,
 		  const struct br_style *parent, struct br_style *out);
 
@@ -133,5 +160,6 @@ unsigned int br_tag_from_name(const char *name);
 const char *br_href(struct br_doc *d, unsigned int node);
 const char *br_src(struct br_doc *d, unsigned int node);
 const char *br_alt(struct br_doc *d, unsigned int node);
+const char *br_rel(struct br_doc *d, unsigned int node);
 
 #endif