Procházet zdrojové kódy

browser: draw ::before/::after icon boxes and data: images

Docusaurus' sidebar chevrons are empty pseudo-elements with a size and an
inline-SVG background (`content:""; background:url("data:image/svg+xml;
utf8,<svg ...>") 50%/2rem 2rem; transform:rotate(90deg)`), which the
text-only generated content dropped.

- `content: ""` is kept (BR_CONTENT_EMPTY); br_pseudo() reports such a
  box when it has a size and a background.  It becomes one RUN_PBOX run:
  background colour, then the data: image at its background-size and
  position, clipped to the box and rotated by transform.  Flex rows
  reserve their first/last column for these boxes (margin-left:auto puts
  one at the right edge), intrinsic_width() counts them, and an empty
  <button> whose icon is such a box is laid out as an element.
- data: URLs (percent-encoded or base64) are decoded for background
  images, <img> sources and icon boxes (data_url_image() caches by URL
  and size, failures included).
- Declaration values end at the first `;` outside strings and parens,
  url() honours quotes and unescapes CSS string escapes, background URLs
  and custom-property values may be up to 8 KB, and the background
  shorthand reads its keywords/colours with url() bodies blanked (an
  SVG's own rgba() fill no longer becomes the background colour).
- SVG images without a viewBox scale their user units to the up-to-4x
  raster: a 40x20 icon was drawn at a quarter of its size.

The compat ladder renders pixel-identical to the previous commit.
Johannes Zellner před 3 dny
rodič
revize
4996d78353

+ 16 - 4
AGENTS.md

@@ -1412,7 +1412,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   pseudo-classes such as `:hover` never match and their rules are dropped
   at parse time), `::before`/`::after` generated text (`content` strings,
   `attr()`, quotes; cascaded as an anonymous inline box per element by
-  `br_pseudo()` and emitted as literal runs, so scripts never see it),
+  `br_pseudo()` and emitted as literal runs, so scripts never see it;
+  an empty `content: ""` box with a size and a background -- an icon --
+  is one `RUN_PBOX` run that fills the background colour and draws a
+  `data:` background image at its `background-size`/`-position`, clipped
+  to the box and rotated by `transform`; a flex row reserves its first/
+  last column for such boxes (`margin-left: auto` pushes one to the right
+  edge), `intrinsic_width()` counts them, and an empty `<button>` whose
+  icon is such a box is laid out as an element),
+  `data:` URLs (percent-encoded or base64; `data_url_decode()`) for
+  background images, `<img>` sources and those icon boxes,
   colours/`rgba()`, legacy `bgcolor` attributes, solid backgrounds and
   `background-image` with
   `repeat`/`position`/`size` (`auto` keeps the legacy stretch, `cover` and
@@ -1890,7 +1899,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   files in the browser (an SVG body is parsed into a detached subtree of
   the page and drawn by the inline-SVG renderer into an RGB565 + alpha
   bitmap: intrinsic size from width/height/viewBox, drawn at up to 4x for
-  small icons, `currentColor` black, no `<text>`) and
+  small icons (without a viewBox the user units are scaled to that
+  raster), `currentColor` black, no `<text>`) and
   animated GIF frames are supported as well; arithmetic JPEG/CMYK are not.
   The viewer, `ucmd_view.c`, owns a canvas window and reads its file through
   `SYS_READ_FILE`; it uses fit/1:1/zoom/pan, shows format/dimensions/size,
@@ -2636,8 +2646,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     `document.implementation.createDocument`, canvas/2D contexts, user-state
     pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
     pseudo-elements other than `::before`/`::after`; generated content is
-    text only (no box, background, counters or `url()` images, and
-    pseudo-elements inside flex/grid containers are not emitted).
+    text, or an empty icon box with a background colour and a `data:`
+    background image (no borders/padding, counters, `content: url()` or
+    remote background images on pseudo-elements; text pseudo-elements
+    inside flex/grid containers are not emitted).
     `getComputedStyle` returns the engine's
     known properties (display, box metrics, colours, fonts, borders,
     background and transform state), and `style`/`dataset` cover inline state rather

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


+ 400 - 8
user/apps/browser/browser.c

@@ -253,6 +253,12 @@ struct lay_run {
 	unsigned int form;          /* node + 1, 0 = not a control */
 	unsigned char form_kind;
 	unsigned char form_disabled;
+	/* RUN_PBOX: background colour, background image and its placement
+	 * inside the iw x ih box (relative offsets, drawn size). */
+	unsigned char pb_has_bg;
+	unsigned short pb_bg;
+	short pb_img;
+	short pb_x, pb_y, pb_w, pb_h;
 };
 
 struct lay {
@@ -466,6 +472,21 @@ static int button_has_elements(unsigned int node)
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
 		if (doc.nodes[c].type == BR_NODE_ELEM)
 			return 1;
+	/* An empty button whose icon is a ::before/::after box (Docusaurus'
+	 * sidebar caret) is laid out as an element so the box is drawn. */
+	if (!doc.nodes[node].first && doc.npe_rules) {
+		struct br_style st, ps;
+		char buf[8];
+		int pe;
+
+		style_for_node(node, &st);
+		for (pe = 1; pe <= 2; pe++)
+			if (!br_pseudo(&doc, node, pe, &st, &ps, buf,
+				       sizeof(buf)) &&
+			    ps.content == BR_CONTENT_EMPTY &&
+			    ps.display != BR_DISPLAY_NONE)
+				return 1;
+	}
 	return 0;
 }
 
@@ -2341,6 +2362,12 @@ static void run_page_scripts(void)
 
 /* ---- images ---- */
 
+/* data: images that failed to decode (key = data_img_key()), so a layout
+ * pass does not retry them on every frame. */
+#define DATA_IMG_FAIL_MAX 32
+static unsigned int data_img_fail[DATA_IMG_FAIL_MAX];
+static int data_img_nfail;
+
 static void image_reset(void)
 {
 	int i;
@@ -2356,6 +2383,7 @@ static void image_reset(void)
 		images[i].disp_last = 0;
 	}
 	image_count = 0;
+	data_img_nfail = 0;
 	image_pixels = 0;
 	image_disp_pixels = 0;
 	image_disp_clock = 0;
@@ -4837,6 +4865,130 @@ static void preload_decode(unsigned int node, int bg,
 		image_repaint();
 }
 
+
+/* ---- data: URLs ----
+ *
+ * `data:[<type>][;base64],<payload>` decoded to bytes from the page heap
+ * (the caller frees them).  The payload is percent-decoded, then base64-
+ * decoded when the header says so.  Infima's menu chevron and many icon
+ * fonts' fallbacks are inline SVGs written this way. */
+static int b64_val(int c)
+{
+	if (c >= 'A' && c <= 'Z') return c - 'A';
+	if (c >= 'a' && c <= 'z') return c - 'a' + 26;
+	if (c >= '0' && c <= '9') return c - '0' + 52;
+	if (c == '+' || c == '-') return 62;
+	if (c == '/' || c == '_') return 63;
+	return -1;
+}
+
+static int hex_val(int c)
+{
+	if (c >= '0' && c <= '9') return c - '0';
+	c |= 32;
+	if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+	return -1;
+}
+
+static int data_url_decode(const char *url, unsigned char **out,
+			   unsigned int *out_len)
+{
+	const char *comma, *p;
+	unsigned int n = 0, len, i;
+	unsigned char *buf;
+	int base64 = 0;
+
+	*out = 0;
+	*out_len = 0;
+	if (!starts_with(url, "data:"))
+		return -1;
+	comma = url + 5;
+	while (*comma && *comma != ',')
+		comma++;
+	if (!*comma)
+		return -1;
+	for (p = url + 5; p + 7 <= comma; p++)
+		if (!strncmp(p, ";base64", 7))
+			base64 = 1;
+	len = s_len(comma + 1);
+	buf = (unsigned char *)malloc(len + 1);
+	if (!buf)
+		return -1;
+	for (i = 0, p = comma + 1; i < len; i++) {
+		int c = (unsigned char)p[i];
+
+		if (c == '%' && i + 2 < len &&
+		    hex_val(p[i + 1]) >= 0 && hex_val(p[i + 2]) >= 0) {
+			c = hex_val(p[i + 1]) * 16 + hex_val(p[i + 2]);
+			i += 2;
+		}
+		buf[n++] = (unsigned char)c;
+	}
+	if (base64) {
+		unsigned int acc = 0, o = 0;
+		int bits = 0;
+
+		for (i = 0; i < n; i++) {
+			int v = b64_val(buf[i]);
+
+			if (v < 0)
+				continue;       /* padding, whitespace */
+			acc = (acc << 6) | (unsigned int)v;
+			bits += 6;
+			if (bits >= 8) {
+				bits -= 8;
+				buf[o++] = (unsigned char)(acc >> bits);
+			}
+		}
+		n = o;
+	}
+	buf[n] = 0;
+	*out = buf;
+	*out_len = n;
+	return 0;
+}
+
+static unsigned int data_img_key(const char *url, int w, int h)
+{
+	return (script_cache_hash(url) ^ ((unsigned int)w * 2654435761u) ^
+		((unsigned int)h * 40503u)) | 1u;
+}
+
+/* The image index of a data: URL decoded for a w x h box (an SVG is
+ * rasterized at that size), decoding it on first use; -1 on failure.
+ * Decoded bitmaps are found again through url_hash, which is distinct
+ * from the <img> sharing hash because it mixes in the size. */
+static int data_url_image(const char *url, int w, int h)
+{
+	unsigned int key, len;
+	unsigned char *data;
+	int i;
+
+	if (!starts_with(url, "data:") || w <= 0 || h <= 0)
+		return -1;
+	key = data_img_key(url, w, h);
+	for (i = 0; i < image_count; i++)
+		if (images[i].state == 1 && images[i].url_hash == key)
+			return i;
+	for (i = 0; i < data_img_nfail; i++)
+		if (data_img_fail[i] == key)
+			return -1;
+	if (image_count < BR_MAX_IMAGES &&
+	    data_url_decode(url, &data, &len) == 0) {
+		int r = decode_image_data(data, len, &images[image_count],
+					  w, h, w, h);
+
+		free(data);
+		if (r == 0) {
+			images[image_count].url_hash = key;
+			return image_count++;
+		}
+	}
+	if (data_img_nfail < DATA_IMG_FAIL_MAX)
+		data_img_fail[data_img_nfail++] = key;
+	return -1;
+}
+
 /* A local file image bypasses the helpers: count its decode so a page opened
  * from /share or /data still produces the same timing breakdown. */
 static void img_stat_local(int before, unsigned int bytes, unsigned int io_ms)
@@ -5081,8 +5233,24 @@ static int preload_fill_jobs(void)
 						preload_node++;
 						continue;
 					}
+					if (starts_with(bsrc, "data:")) {
+						/* Inline image: decode now,
+						 * no fetch helper. */
+						unsigned char *data;
+						unsigned int len;
+
+						preload_node = i + 1;
+						if (data_url_decode(bsrc, &data,
+								    &len) == 0) {
+							preload_decode(i, 1, data,
+								       len, 0);
+							free(data);
+						} else {
+							nd->bg_img_index = -2;
+						}
+						continue;
+					}
 					if (bsrc[0] &&
-					    !starts_with(bsrc, "data:") &&
 					    resolve_link(page_url, bsrc, url,
 							 sizeof(url)) == 0) {
 						if (image_count +
@@ -5150,7 +5318,20 @@ static int preload_fill_jobs(void)
 			continue;
 		}
 		src = br_src(&doc, i);
-		if (!src[0] || starts_with(src, "data:")) {
+		if (starts_with(src, "data:")) {
+			unsigned char *data;
+			unsigned int len;
+
+			preload_node = i + 1;
+			if (data_url_decode(src, &data, &len) == 0) {
+				preload_decode(i, 0, data, len, 0);
+				free(data);
+			} else {
+				doc.nodes[i].img_index = -2;
+			}
+			continue;
+		}
+		if (!src[0]) {
 			preload_node++;
 			continue;
 		}
@@ -6484,14 +6665,16 @@ static void svg_paint_box(struct ngui *ui, unsigned int node,
 			  int h);
 
 /* Run kinds besides text: img >= 0 an <img> bitmap, -2 a button label,
- * RUN_SVG an inline <svg> icon (its node in off). */
+ * RUN_SVG an inline <svg> icon (its node in off), RUN_PBOX an empty
+ * ::before/::after box with a background (Infima's menu chevrons). */
 #define RUN_SVG (-3)
+#define RUN_PBOX (-4)
 
 static int run_width(const struct lay_run *r)
 {
 	if (r->img >= 0)
 		return r->iw;
-	if (r->img == -2 || r->img == RUN_SVG)
+	if (r->img == -2 || r->img == RUN_SVG || r->img == RUN_PBOX)
 		return r->iw;
 	return (int)r->w;
 }
@@ -6690,6 +6873,29 @@ static void flush_line(struct lay *l)
 							     r->font_px, 0, 0, 0);
 					}
 				}
+			} else if (r->img == RUN_PBOX) {
+				struct ngui_rect bx = { x, ry, w, r->ih };
+
+				ngui_push_clip(l->ui, bx);
+				if (r->pb_has_bg)
+					ngui_fill_rect(l->ui, bx, r->pb_bg);
+				if (r->pb_img >= 0 && r->pb_img < image_count &&
+				    images[r->pb_img].state == 1) {
+					/* Rotate about the box centre: the
+					 * image is centred in it here. */
+					draw_image_transformed(l->ui,
+						&images[r->pb_img].img,
+						x + r->pb_x, ry + r->pb_y,
+						r->pb_w, r->pb_h,
+						r->transform_scale ?
+						r->scale_x : 100,
+						r->transform_scale ?
+						r->scale_y : 100,
+						r->transform_scale ?
+						r->rotate_x100 : 0,
+						50, 50, 1, 1);
+				}
+				ngui_pop_clip(l->ui);
 			} else if (r->img == RUN_SVG) {
 				struct br_style sst;
 
@@ -7637,6 +7843,10 @@ static void layout_children(struct lay *l, unsigned int parent,
 static void layout_flex_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 unsigned short link);
+static int pseudo_box_w(unsigned int node, const struct br_style *st, int pe,
+			struct br_style *ps, int base);
+static void emit_pseudo_box(struct lay *l, const struct br_style *parent,
+			    const struct br_style *ps, unsigned short link);
 static void layout_grid_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 unsigned short link);
@@ -8464,8 +8674,16 @@ static int svg_decode_image(const unsigned char *data, unsigned int len,
 	base.color = 0x0000;
 	br_style_for(&doc, svg, &base, &st);
 	svg_root_matrix(svg, w, h, m);
-	svg_vp_w = have_vb ? vb[2] : w;
-	svg_vp_h = have_vb ? vb[3] : h;
+	if (!have_vb && nat_w > 0 && nat_h > 0) {
+		/* No viewBox: user units are CSS pixels of the natural size,
+		 * so the up-to-4x raster (and a box size) must scale them.
+		 * A 40x20 icon used to fill only the top-left quarter of its
+		 * 160x80 bitmap and was drawn at a quarter of its size. */
+		m[0] = (double)w / nat_w;
+		m[3] = (double)h / nat_h;
+	}
+	svg_vp_w = have_vb ? vb[2] : nat_w;
+	svg_vp_h = have_vb ? vb[3] : nat_h;
 	svg_draw_children(&L, svg, m, &st, 255, 0);
 	bi->native_w = nat_w;
 	bi->native_h = nat_h;
@@ -9266,6 +9484,14 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 
 	if (depth > 32)
 		return 0;
+	if (doc.npe_rules) {
+		struct br_style ps;
+
+		/* Icon boxes (::before/::after with a size) are inline
+		 * content of the element. */
+		line_w += pseudo_box_w(node, pst, 1, &ps, base) +
+			  pseudo_box_w(node, pst, 2, &ps, base);
+	}
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
 		struct br_node *nd = &doc.nodes[c];
 		struct br_style cs;
@@ -9882,6 +10108,9 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	int avail = row_right - row_left;
 	int idx, row_top;
 	unsigned char row_align = l->align;
+	int box_left = l->left, box_right = l->right, row_top0 = l->y;
+	int bw = 0, aw = 0, first_h = -1;
+	struct br_style bps, aps;
 
 	if (avail < 8 || depth > 48) {
 		layout_children(l, parent, pst, depth, link);
@@ -9899,6 +10128,20 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 		}
 	}
 
+	/* ::before/::after icon boxes are the row's first and last items
+	 * (Docusaurus' sidebar chevron): reserve their columns. */
+	if (doc.npe_rules) {
+		bw = pseudo_box_w(parent, pst, 1, &bps, avail);
+		aw = pseudo_box_w(parent, pst, 2, &aps, avail);
+		if (bw + aw > avail - 8) {
+			bw = 0;
+			aw = 0;
+		}
+		row_left += bw;
+		row_right -= aw;
+		avail = row_right - row_left;
+	}
+
 	for (c = doc.nodes[parent].first;
 	     c && nitem < (int)(sizeof(it) / sizeof(it[0]));
 	     c = doc.nodes[c].next) {
@@ -10084,10 +10327,41 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 			if (k + 1 < count)
 				x += gap + extra;
 		}
+		if (first_h < 0)
+			first_h = row_h;
 		row_top += row_h;
 	}
-	l->left = row_left;
-	l->right = row_right;
+	if (bw || aw) {
+		int pass;
+
+		for (pass = 0; pass < 2; pass++) {
+			const struct br_style *ps = pass ? &aps : &bps;
+			int a = pass ? row_right : box_left;
+			int b = pass ? box_right : row_left;
+			int h = ps->height_kind ?
+				style_height_px(ps, box_right - box_left) : 0;
+
+			if (!(pass ? aw : bw))
+				continue;
+			l->left = l->line_left = l->line_x = l->run_x = a;
+			l->right = l->line_right = b;
+			l->y = row_top0;
+			if (pst->align_items == 1 && first_h > h)
+				l->y += (first_h - h) / 2;
+			l->line_y = l->y;
+			l->line_h = 0;
+			l->nruns = 0;
+			l->space_pending = 0;
+			emit_pseudo_box(l, pst, ps, link);
+			flush_line(l);
+			/* The box sits inside the first row; it only makes
+			 * the row taller when it is taller itself. */
+			if (row_top0 + h > row_top)
+				row_top = row_top0 + h;
+		}
+	}
+	l->left = box_left;
+	l->right = box_right;
 	l->align = row_align;
 	l->y = row_top;
 	l->line_x = l->left;
@@ -11056,6 +11330,119 @@ struct br_deferred {
 static struct br_deferred deferred[BR_MAX_DEFERRED];
 static int ndeferred;
 
+/* Outer width of an element's empty ::before (pe 1) / ::after (pe 2) icon
+ * box, 0 when it has none; *ps receives the pseudo-element's style. */
+static int pseudo_box_w(unsigned int node, const struct br_style *st, int pe,
+			struct br_style *ps, int base)
+{
+	char buf[8];
+	int w, h;
+
+	if (!doc.npe_rules ||
+	    br_pseudo(&doc, node, pe, st, ps, buf, sizeof(buf)) ||
+	    ps->content != BR_CONTENT_EMPTY || ps->display == BR_DISPLAY_NONE)
+		return 0;
+	w = ps->width_kind ? style_width_px(ps, base) : 0;
+	h = ps->height_kind ? style_height_px(ps, base) : 0;
+	if (ps->min_width_kind == 1 && w < (int)ps->min_width)
+		w = (int)ps->min_width;
+	if (w <= 0)
+		w = h;
+	if (w <= 0 || w > 512)
+		return 0;
+	return w + (ps->ml_auto ? 0 : mp_px(ps, 3, base)) + mp_px(ps, 1, base);
+}
+
+/* An empty ::before/::after with a size and a background: one run the
+ * size of the box.  Only data: background images are drawn (decoded at
+ * the background-size); `margin-left: auto` inside a flex parent pushes
+ * the box to the line's right edge like the flex row would. */
+static void emit_pseudo_box(struct lay *l, const struct br_style *parent,
+			    const struct br_style *ps, unsigned short link)
+{
+	struct lay_run *r;
+	int lw = l->line_right - l->line_left, w, h, ml, mr, tw, th;
+	const char *url = ps->bg_image ? br_bg_image_url(ps->bg_image - 1) : "";
+
+	if (lw < 8)
+		lw = 8;
+	w = ps->width_kind ? style_width_px(ps, lw) : 0;
+	h = ps->height_kind ? style_height_px(ps, lw) : 0;
+	if (ps->min_width_kind == 1 && w < (int)ps->min_width)
+		w = (int)ps->min_width;
+	if (w <= 0)
+		w = h;
+	if (h <= 0)
+		h = w;
+	if (w <= 0 || w > 512 || h > 512)
+		return;
+	ml = ps->ml_auto ? 0 : mp_px(ps, 3, lw);
+	mr = mp_px(ps, 1, lw);
+	if (ml < 0)
+		ml = 0;
+	if (mr < 0)
+		mr = 0;
+	if (l->line_x + ml + w > l->line_right &&
+	    (l->nruns > 0 || l->line_x > l->line_left))
+		flush_line(l);
+	if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
+		flush_line(l);
+	if (l->nruns == 0) {
+		l->line_y = l->y;
+		l->line_h = 0;
+		start_line(l, ps);
+	}
+	if (ps->ml_auto && parent->display == BR_DISPLAY_FLEX &&
+	    l->line_right - w - mr > l->line_x)
+		l->line_x = l->line_right - w - mr;
+	else
+		l->line_x += ml;
+	r = &l->runs[l->nruns++];
+	memset(r, 0, sizeof(*r));
+	r->gap = (unsigned short)(l->line_x > l->run_x ?
+				  l->line_x - l->run_x : 0);
+	r->img = RUN_PBOX;
+	r->href = link;
+	r->iw = (unsigned short)w;
+	r->ih = (unsigned short)h;
+	r->w = (unsigned short)w;
+	r->font_px = ps->font_px;
+	r->pb_has_bg = ps->has_bg;
+	r->pb_bg = ps->bg;
+	r->pb_img = -1;
+	if (ps->transform_set) {
+		r->transform_scale = 1;
+		r->scale_x = ps->scale_x;
+		r->scale_y = ps->scale_y;
+		r->rotate_x100 = ps->rotate_x100;
+	}
+	/* Background size and position, as draw_bg_image() resolves them. */
+	tw = w;
+	th = h;
+	if (ps->bg_size_kind == 1 || ps->bg_size_kind == 2) {
+		int bw = ps->bg_size_w, bh = ps->bg_size_h;
+
+		if (ps->bg_size_kind == 2) {
+			bw = bw ? w * bw / 100 : 0;
+			bh = bh ? h * bh / 100 : 0;
+		}
+		tw = bw ? bw : (bh ? bh : w);
+		th = bh ? bh : tw;
+	}
+	if (tw > 0 && th > 0 && tw <= 512 && th <= 512 && url[0])
+		r->pb_img = (short)data_url_image(url, tw, th);
+	r->pb_w = (short)tw;
+	r->pb_h = (short)th;
+	r->pb_x = (short)(ps->bg_pos_x_kind == 1 ?
+			  (w - tw) * ps->bg_pos_x / 100 : ps->bg_pos_x);
+	r->pb_y = (short)(ps->bg_pos_y_kind == 1 ?
+			  (h - th) * ps->bg_pos_y / 100 : ps->bg_pos_y);
+	l->line_x += w + mr;
+	l->run_x = l->line_x;
+	if (l->line_h < h)
+		l->line_h = h;
+}
+
 /* ::before/::after generated text (br_pseudo()) as literal runs in the
  * element's line.  A block-level pseudo-element gets its own line; its box
  * (margins, padding, background) is not drawn. */
@@ -11068,6 +11455,11 @@ static void emit_pseudo(struct lay *l, unsigned int node,
 	unsigned int i = 0;
 	int block, space_w;
 
+	if (!n && ps.content == BR_CONTENT_EMPTY &&
+	    ps.display != BR_DISPLAY_NONE) {
+		emit_pseudo_box(l, st, &ps, link);
+		return;
+	}
 	if (!n)
 		return;
 	block = ps.display == BR_DISPLAY_BLOCK || ps.display == BR_DISPLAY_FLEX;

+ 130 - 31
user/apps/browser/browser_html.c

@@ -21,7 +21,13 @@ static int root_font_px = 16;
 /* Distinct background-image URLs per page (app stores give every card an
  * icon through an inline style). */
 #define BR_BG_URLS 512
-static char bg_urls[BR_BG_URLS][192];
+/* Background URLs live in one pool: a data: URL (Infima's menu chevron is
+ * an inline SVG) is far longer than a fixed slot. */
+#define BR_BG_URL_POOL (128 * 1024)
+#define BR_BG_URL_MAX  8191
+static const char *bg_urls[BR_BG_URLS];
+static char bg_url_pool[BR_BG_URL_POOL];
+static unsigned int bg_url_pool_used;
 static unsigned int bg_url_count;
 
 /* @font-face definitions, one document at a time.  The URL is resolved by
@@ -673,7 +679,7 @@ static void css_var_set(struct br_doc *d, const char *name,
 		return;
 	}
 	noff = store_raw(d, name, nlen, 63);
-	voff = store_raw(d, val, vlen, 199);
+	voff = store_raw(d, val, vlen, BR_BG_URL_MAX);
 	if (!noff || !voff)
 		return;
 	if (i >= BR_MAX_VARS) {
@@ -928,6 +934,7 @@ void br_doc_init(struct br_doc *d)
 		d->var_slot[vi] = 0;
 	root_font_px = 16;
 	bg_url_count = 0;
+	bg_url_pool_used = 0;
 	font_reset();
 }
 
@@ -2687,7 +2694,9 @@ static int color_is_transparent(const char *s, unsigned int len)
 
 static int register_bg_url(const char *s, unsigned int len)
 {
-	unsigned int i, j, n;
+	char tmp[BR_BG_URL_MAX + 1];
+	unsigned int i, n = 0;
+	char q = 0;
 
 	while (len && is_space(*s)) {
 		s++;
@@ -2697,23 +2706,31 @@ static int register_bg_url(const char *s, unsigned int len)
 		len--;
 	if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
 	    s[len - 1] == s[0]) {
+		q = s[0];
 		s++;
 		len -= 2;
 	}
-	if (len > 191)
-		len = 191;
-	for (i = 0; i < bg_url_count; i++) {
-		for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
-			;
-		if (j == len && bg_urls[i][j] == '\0')
-			return (int)i + 1;
+	/* A quoted URL is a CSS string: `\"` inside a data: SVG is a quote. */
+	for (i = 0; i < len && n < BR_BG_URL_MAX; i++) {
+		if (q && s[i] == '\\' && i + 1 < len) {
+			i++;
+			if (s[i] == '\n')
+				continue;
+		}
+		tmp[n++] = s[i];
 	}
-	if (bg_url_count >= BR_BG_URLS || len == 0)
+	tmp[n] = '\0';
+	if (!n)
 		return 0;
-	n = len < 191 ? len : 191;
-	for (i = 0; i < n; i++)
-		bg_urls[bg_url_count][i] = s[i];
-	bg_urls[bg_url_count][n] = '\0';
+	for (i = 0; i < bg_url_count; i++)
+		if (!strcmp(bg_urls[i], tmp))
+			return (int)i + 1;
+	if (bg_url_count >= BR_BG_URLS ||
+	    bg_url_pool_used + n + 1 > BR_BG_URL_POOL)
+		return 0;
+	memcpy(bg_url_pool + bg_url_pool_used, tmp, n + 1);
+	bg_urls[bg_url_count] = bg_url_pool + bg_url_pool_used;
+	bg_url_pool_used += n + 1;
 	return (int)(++bg_url_count);
 }
 
@@ -3745,6 +3762,41 @@ static void add_transform_origin(struct br_decl *out, unsigned int *nout,
 	}
 }
 
+/* Copy a background value with every url(...) body blanked (kept as
+ * `url()`), for the keyword and colour scans of the shorthand. */
+static unsigned int bg_blank_urls(const char *s, unsigned int len, char *out,
+				  unsigned int cap)
+{
+	unsigned int i = 0, n = 0;
+
+	while (i < len && n + 1 < cap) {
+		if (i + 4 <= len && ci_eq_n(s + i, "url(", 4)) {
+			char q = 0;
+
+			memcpy(out + n, "url(", 4);
+			n += 4;
+			i += 4;
+			while (i < len) {
+				if (q) {
+					if (s[i] == '\\' && i + 1 < len)
+						i++;
+					else if (s[i] == q)
+						q = 0;
+				} else if (s[i] == '"' || s[i] == '\'') {
+					q = s[i];
+				} else if (s[i] == ')') {
+					break;
+				}
+				i++;
+			}
+			continue;       /* the ')' is copied below */
+		}
+		out[n++] = s[i++];
+	}
+	out[n] = '\0';
+	return n;
+}
+
 static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
 			      const char *v, unsigned int vl, int important)
 {
@@ -3753,10 +3805,24 @@ static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
 	for (i = 0; i + 4 <= vl; i++) {
 		if (ci_eq_n(v + i, "url(", 4)) {
 			unsigned int st = i + 4, en = st;
+			char q = 0;
 			int idx;
 
-			while (en < vl && v[en] != ')')
+			/* `)` inside a quoted data: URL (an SVG's
+			 * rgba(...)) does not end it. */
+			while (en < vl) {
+				if (q) {
+					if (v[en] == '\\' && en + 1 < vl)
+						en++;
+					else if (v[en] == q)
+						q = 0;
+				} else if (v[en] == '"' || v[en] == '\'') {
+					q = v[en];
+				} else if (v[en] == ')') {
+					break;
+				}
 				en++;
+			}
 			idx = register_bg_url(v + st, en - st);
 			if (idx)
 				decl_add(out, nout, PROP_BG_IMAGE, idx,
@@ -4918,7 +4984,16 @@ static int parse_content(struct br_doc *d, const char *s, unsigned int len)
 			i++;
 		}
 	}
-	if (!n || d->ncontent >= BR_MAX_CONTENT)
+	if (!n) {
+		/* `content: ""` still creates the box: icon
+		 * pseudo-elements are empty strings with a size and a
+		 * background image. */
+		for (i = 0; i < len; i++)
+			if (s[i] == '"' || s[i] == '\'')
+				return BR_CONTENT_EMPTY;
+		return 0;
+	}
+	if (d->ncontent >= BR_MAX_CONTENT)
 		return 0;
 	off = store_raw(d, out, n, sizeof(out));
 	if (!off)
@@ -5261,8 +5336,9 @@ static void parse_decls(struct br_doc *d, const char *s,
 			continue;
 		i++;
 		vstart = i;
-		while (i < len && s[i] != ';')
-			i++;
+		/* `;` inside a string or url() (data:image/svg+xml;utf8,...)
+		 * does not end the value. */
+		i = css_value_end(s, len, i);
 		vend = i;
 		while (pstart < pend && is_space(s[pstart]))
 			pstart++;
@@ -5352,24 +5428,30 @@ static void parse_decls(struct br_doc *d, const char *s,
 			unsigned short c;
 			int is_bg = str_eq(prop, "background");
 
+			static char bgv[1024];
+			const char *vs = s + vstart;
+			unsigned int vl = vend - vstart;
+
 			if (is_bg) {
 				add_bg_image_decl(out, nout, s + vstart,
 						  vend - vstart, important);
+				/* Keywords and colours are read with url()
+				 * contents blanked: a data: SVG carries its
+				 * own fill="rgba(...)". */
+				vl = bg_blank_urls(vs, vl, bgv, sizeof(bgv));
+				vs = bgv;
 				add_bg_shorthand_extra(out, nout,
-						       s + vstart,
-						       vend - vstart,
+						       vs, vl,
 						       important);
-				if (contains_ci_len(s + vstart, vend - vstart,
+				if (contains_ci_len(vs, vl,
 						    "no-repeat"))
 					decl_add(out, nout, PROP_BG_REPEAT, 1,
 						 important);
-				else if (contains_ci_len(s + vstart,
-							 vend - vstart,
+				else if (contains_ci_len(vs, vl,
 							 "repeat-x"))
 					decl_add(out, nout, PROP_BG_REPEAT, 2,
 						 important);
-				else if (contains_ci_len(s + vstart,
-							 vend - vstart,
+				else if (contains_ci_len(vs, vl,
 							 "repeat-y"))
 					decl_add(out, nout, PROP_BG_REPEAT, 3,
 						 important);
@@ -5377,14 +5459,14 @@ static void parse_decls(struct br_doc *d, const char *s,
 			/* `transparent`/`none` are real values: they must
 			 * beat a lower-specificity colour (and tell form
 			 * controls the author styled them). */
-			if (color_is_transparent(s + vstart, vend - vstart) ||
-			    (vend - vstart == 4 &&
-			     ci_eq_n(s + vstart, "none", 4)))
+			if (color_is_transparent(vs, vl) ||
+			    (vl == 4 &&
+			     ci_eq_n(vs, "none", 4)))
 				decl_add(out, nout, PROP_BG_NONE, 1, important);
-			else if (parse_color(s + vstart, vend - vstart, &c))
+			else if (parse_color(vs, vl, &c))
 				decl_add(out, nout, PROP_BG, c, important);
 			else if (is_bg &&
-				 find_color_last(s + vstart, vend - vstart, &c))
+				 find_color_last(vs, vl, &c))
 				decl_add(out, nout, PROP_BG, c, important);
 		} else if (str_eq(prop, "font-size")) {
 			unsigned short raw;
@@ -9189,6 +9271,10 @@ unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
 	const char *c;
 	unsigned int len = 0;
 
+	if (out) {
+		out->content = 0;
+		out->display = BR_DISPLAY_NONE;
+	}
 	if (!d || !d->npe_rules || node >= d->nnode || !cap ||
 	    (pe != 1 && pe != 2))
 		return 0;
@@ -9199,6 +9285,19 @@ unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
 	cascade_pe = pe;
 	br_style_for(d, node, elem, out);
 	cascade_pe = 0;
+	if (out->content == BR_CONTENT_EMPTY) {
+		/* No text; the caller may still draw the box (an icon
+		 * with a size and a background image). */
+		buf[0] = '\0';
+		n->pe_state |= chk;
+		if (out->display != BR_DISPLAY_NONE && out->pos != 1 &&
+		    out->pos != 2 && (out->width_kind || out->height_kind) &&
+		    (out->has_bg || out->bg_image))
+			n->pe_state |= has;
+		else
+			out->display = BR_DISPLAY_NONE;
+		return 0;
+	}
 	if (out->content && out->display != BR_DISPLAY_NONE &&
 	    out->pos != 1 && out->pos != 2) {
 		c = attr_ptr(d, d->content_off[out->content - 1]);

+ 5 - 1
user/apps/browser/browser_html.h

@@ -21,6 +21,8 @@
 #define BR_FACE_URL      192
 #define BR_MAX_VARS      512
 #define BR_MAX_LAYERS    48
+/* br_style.content for `content: ""`: a box without generated text. */
+#define BR_CONTENT_EMPTY 0x7FFF
 #define BR_RULE_USES_SCOPED    1
 #define BR_RULE_DEFINES_SCOPED 2
 #define BR_MAX_CONTENT   512
@@ -344,7 +346,9 @@ void br_doc_init(struct br_doc *d);
 /* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
  * style (inheriting from the element's style `elem`) and its generated
  * text.  Returns the text length; 0 when there is no content, or when the
- * pseudo-element is display:none or absolutely positioned. */
+ * pseudo-element is display:none or absolutely positioned.  `content: ""`
+ * returns 0 with out->content == BR_CONTENT_EMPTY and a visible display
+ * when the box has a size and a background (an icon to draw). */
 unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
 		       const struct br_style *elem, struct br_style *out,
 		       char *buf, unsigned int cap);

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