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

Render docs.cloudron.io: cascade layers, calc folding, scoped variables

Docusaurus ships Infima in @layer blocks, which the CSS parser skipped,
so the page came out nearly unstyled.  Fixes, in order of impact:

- @layer blocks cascade in layer order (unlayered above layers, the
  style attribute above all, !important reversed); @supports applies
  unless negated.
- A page starting `<!doctype html><html>` makes <html> node 0; layout,
  style_for_node(), selector combinators and the preloader's
  node_hidden() now treat it as the root element, so its colour, font
  and line-height reach the page.
- calc() is folded after var() expansion (*, /, nesting, rem/em, vw,
  unitless) and var() keeps token boundaries in minified shorthands.
- Rules using custom properties that components redefine are
  re-expanded per element against its matched rules (Infima's grid).
- Flex: definite flex-basis sets the item width, items honour
  min/max-width, narrow rows only fall back to block flow when the
  items do not fit.
- Floats: width is the content box (margins added to the column).
- `<a href=/x/>` no longer self-closes; `/>` is ignored on non-void
  HTML elements outside SVG.
- <svg> is inline by default; svg-only inline-blocks stay in the line;
  empty <button> has no label; font-weight 500 picks the regular face;
  rgba(...,.05) parses its alpha; percentage <img> heights use the
  containing block height; matchMedia() uses the CSS media evaluator.
- Fixed boxes paint their background over their own columns only and
  a box at left:100% keeps a one-line width; dark-text pages without a
  canvas colour get a white canvas.

The compat ladder still passes; its renders differ only by the
corrected float widths.
Johannes Zellner 3 дней назад
Родитель
Сommit
f3081de286

+ 49 - 6
AGENTS.md

@@ -1420,8 +1420,29 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   120-byte cap silently dropped every responsive variant past it.  A
   stylesheet whose helper fetch fails is retried once synchronously
   (`[css] retry` on the log) instead of leaving the page unstyled.
-  `@media (min-width/max-width)` blocks are evaluated against the browser
-  canvas width when the page is parsed; `@import`/`@keyframes` are skipped.
+  `@media` blocks (`min-width`/`max-width` and the `(width >= 997px)` range
+  form) are evaluated against the browser canvas width when the page is
+  parsed, and `window.matchMedia()` gives scripts the same answers
+  (`br_media_matches()`).  `@layer` blocks are parsed and cascade in layer
+  order (`br_rule.layer`, ranked by first appearance including `@layer a,
+  b;` statements): precedence is a band per layer above raw specificity
+  (`cascade_rank()`), unlayered rules above every layer, the `style`
+  attribute above all, and `!important` reverses the layer order.
+  Docusaurus/Infima ships its whole sheet in layers.  `@supports` blocks
+  apply unless negated or asking for `selector()`; `@import`/`@keyframes`
+  are skipped.  After `var()` expansion every `calc()` in a declaration
+  block is folded (`css_fold_calc()`: `*`, `/`, nested parentheses, %,
+  px, rem/em at the root size, vw, unitless) into a plain length or the
+  `calc(P% + Npx)` form the property parsers read; `var()` substitution
+  keeps token boundaries, so minified `font:var(--w)var(--size)` cannot
+  glue into one token.  A rule that uses or defines a custom property some
+  non-root rule also defines (`BR_VAR_SCOPED`) keeps its raw block and is
+  re-expanded per element in `br_style_for()` against the definitions of
+  the element's own matched rules (`css_env_build()`; Infima's `.col--3`
+  sets `--ifm-col-width`, `.col[class*=col--]` uses it).  The flex shorthand
+  and `flex-basis` map a definite basis onto the item width, and flex items
+  are clamped by `min-width`/`max-width` (a capped growing item leaves its
+  space to the others).
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   (the browser fetches remote faces before layout).  The compatibility
   ladder is `testdata/browser/compat/`
@@ -1735,7 +1756,24 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   is ~3.7 KB.  Inline `<style>` text is collected up to 256 KB
   (`BR_CSS_SIZE`; google.com inlines ~94 KB).  Media features with a known
   answer are evaluated (`forced-colors: none`, normal contrast,
-  `hover: hover`, `pointer: fine`); unknown ones stay true.  Flex/grid,
+  `hover: hover`, `pointer: fine`, `prefers-reduced-motion: no-preference`,
+  `display-mode: browser`); unknown ones stay true.  When the page starts
+  with `<html>` (no whitespace after the doctype) that element is node 0:
+  layout inherits from its cascade (`root_style()`), and selector
+  combinators, `style_for_node()` and the preloader's `node_hidden()` treat
+  node 0 as its children's parent (`sel_parent()`).  An unquoted attribute
+  value ending in `/` (`<a href=/x/>`) does not self-close the tag, and
+  `/>` is ignored on non-void HTML elements outside SVG.  `<svg>` is inline
+  by default (an inline-block holding only an `<svg>` icon stays in the
+  text line); an empty `<button>` has no label.  `font-weight` 500 picks
+  the regular face (CSS matching with only 400/700 faces), 600+ the bold
+  one.  Alpha without a leading zero (`rgba(0,0,0,.05)`) is parsed.  A
+  float's `width` is its content box (margins, and padding/border unless
+  `border-box`, are added to the column).  A fixed box's deferred
+  background fill covers only its own columns, and a positioned box pushed
+  past its containing block (`left: 100%`) keeps a one-line width instead
+  of collapsing to 8 px.  Percentage `<img>` heights resolve against the
+  containing block's definite height.  Flex/grid,
   basic tables, inline SVG and CSS
   variables are supported (see known issue 22 for the remaining CSS and DOM
   gaps).  `SYS_SPAWN`
@@ -2592,7 +2630,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     win, a component-level definition applies only while no other rule
     gives the name a different value, otherwise `var()` uses its fallback
     -- Font Awesome's competing `--fa-width` values made every icon 100%
-    wide), percentage heights (against the containing block's definite
+    wide; rules touching such names are re-expanded per element against
+    that element's own matched rules, but values are not inherited from
+    ancestors' rules), percentage heights (against the containing block's definite
     height, `lay.cb_h`, with the viewport for `html`/`body`; used as a
     minimum since there are no scrollable inner boxes, and auto when the
     containing height is indefinite -- they used to resolve against the
@@ -2670,8 +2710,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     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.  A page with
-    no `html`/`body` background still gets the dark default canvas, so dark
-    text can be hard to read; pages with their own background are unaffected.
+    no `html`/`body` background gets the dark default canvas unless its
+    computed text colour is dark, in which case the canvas is white like a
+    normal browser's.  `@layer` ranks nested layers flat by first
+    appearance (a parent layer's own rules lose to its sublayers, the spec
+    says the reverse).
     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

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/userland.html


+ 262 - 27
user/apps/browser/browser.c

@@ -992,7 +992,9 @@ static int js_host_computed(unsigned int node, const char *property,
 	struct br_style st;
 	char name[40];
 
-	if (!buf || cap <= 0 || node >= doc.nnode || node == 0)
+	/* Node 0 is a real element when the page starts with <html>. */
+	if (!buf || cap <= 0 || node >= doc.nnode ||
+	    (node == 0 && doc.nodes[0].type != BR_NODE_ELEM))
 		return 0;
 	buf[0] = '\0';
 	css_computed_name(property, name, sizeof(name));
@@ -4566,6 +4568,8 @@ static void preload_begin(void)
  * menu/carousel are never visited and keep img_top == 0.  Without this check
  * the preloader walks the DOM in node order and spends the whole image
  * budget on logos and off-canvas thumbnails the reader cannot see. */
+static struct br_style base_style(void);
+
 static int node_hidden(unsigned int node)
 {
 	unsigned int path[32], n = 0, p = node, i;
@@ -4575,9 +4579,17 @@ static int node_hidden(unsigned int node)
 		path[n++] = p;
 		p = doc.nodes[p].parent;
 	}
-	parent = (struct br_style){0};
-	parent.font_px = 16;
-	parent.display = BR_DISPLAY_BLOCK;
+	/* Node 0 is the <html> element when the page starts with one: it
+	 * is an ancestor too.  Styles are cached per node, so the chain
+	 * must start from the same base as style_for_node(); seeding it
+	 * with a zeroed style left <body> cached with black text and no
+	 * line-height once an image job ran (docs.cloudron.io). */
+	if (!p && n && n < sizeof(path) / sizeof(path[0]) &&
+	    doc.nodes[0].type == BR_NODE_ELEM &&
+	    doc.nodes[0].tag == BR_TAG_HTML &&
+	    !(doc.nodes[path[n - 1]].flags & BR_NODE_DETACHED))
+		path[n++] = 0;
+	parent = base_style();
 	for (i = n; i > 0; i--) {
 		br_style_for(&doc, path[i - 1], &parent, &st);
 		if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
@@ -4597,7 +4609,12 @@ static void style_for_node(unsigned int node, struct br_style *out)
 	unsigned int p = doc.nodes[node].parent;
 	struct br_style parent;
 
-	if (p) {
+	/* Parent index 0 is ambiguous: it is the <html> element when the page
+	 * starts with one (see root_style()), but also the parent field of a
+	 * detached subtree root. */
+	if (p || (node != 0 && doc.nodes[0].type == BR_NODE_ELEM &&
+		  doc.nodes[0].tag == BR_TAG_HTML &&
+		  !(doc.nodes[node].flags & BR_NODE_DETACHED))) {
 		style_for_node(p, &parent);
 		br_style_for(&doc, node, &parent, out);
 	} else {
@@ -5930,6 +5947,43 @@ static struct br_style base_style(void)
 	return s;
 }
 
+/* The style node 0's children inherit.  Node 0 is the first parsed node:
+ * a minified page that starts `<!doctype html><html ...>` makes it the
+ * <html> element itself, and the layout walks only its children, so its
+ * own cascade (html { color; font } -- Docusaurus/Infima put the page
+ * colour and font there) used to be skipped and the theme defaults showed
+ * through.  Only inherited properties are taken: the root's box (margins,
+ * display, background) is not a container style for its children here. */
+static struct br_style root_style(void)
+{
+	struct br_style base = base_style(), st;
+
+	if (!doc.nnode || doc.nodes[doc.root].type != BR_NODE_ELEM ||
+	    doc.nodes[doc.root].tag != BR_TAG_HTML)
+		return base;
+	br_style_for(&doc, doc.root, &base, &st);
+	base.color = st.color;
+	base.font_px = st.font_px;
+	base.bold = st.bold;
+	base.italic = st.italic;
+	base.mono = st.mono;
+	base.font_face = st.font_face;
+	base.align = st.align;
+	base.line_h_pct = st.line_h_pct;
+	base.line_h_px = st.line_h_px;
+	base.nowrap = st.nowrap;
+	base.uppercase = st.uppercase;
+	base.cursor = st.cursor;
+	base.vis_hidden = st.vis_hidden;
+	base.fill_mode = st.fill_mode;
+	base.fill = st.fill;
+	base.stroke_mode = st.stroke_mode;
+	base.stroke = st.stroke;
+	base.stroke_w = st.stroke_w;
+	base.fill_evenodd = st.fill_evenodd;
+	return base;
+}
+
 /* Resolve a margin/padding raw value.  Percentages use the containing
  * block width for all four sides, as CSS specifies. */
 static int css_mp_px(const struct br_style *st, int v, int unit, int base)
@@ -6920,14 +6974,20 @@ static void add_image_run(struct lay *l, unsigned int node,
 					maxw * (int)st->width / 100 :
 					(int)st->width;
 		}
-		if (st->height_kind) {
+		/* Percentage heights resolve against the containing block's
+		 * definite height (Docusaurus' logo: `.navbar__logo { height:
+		 * 2rem }` holding an `img { height: 100% }`), and are auto
+		 * when it has none; they used to use the width. */
+		if (st->height_kind == 1 ||
+		    ((st->height_kind == 2 || st->height_kind == 3) &&
+		     l->cb_h > 0)) {
 			css_h = 1;
 			if (st->height_kind == 3)
-				dh = maxw * st->height_calc_pct / 100 +
+				dh = l->cb_h * st->height_calc_pct / 100 +
 				     st->height_calc_px;
 			else
 				dh = st->height_kind == 2 ?
-					maxw * (int)st->height / 100 :
+					l->cb_h * (int)st->height / 100 :
 					(int)st->height;
 		}
 		if (css_w && !css_h)
@@ -7355,6 +7415,12 @@ static void add_form_run(struct lay *l, unsigned int node,
 		   kind == FORM_RESET) {
 		if (fs->len)
 			text = fs->value;
+		else if (doc.nodes[node].tag == BR_TAG_BUTTON)
+			/* A <button>'s label is its content: an empty one
+			 * (Docusaurus' sidebar caret, drawn by a CSS
+			 * ::before icon) shows none.  Only <input> buttons
+			 * get the UA's default label. */
+			text = "";
 		else
 			text = kind == FORM_SUBMIT ? "Submit" :
 			       kind == FORM_RESET ? "Reset" : "Button";
@@ -7611,7 +7677,7 @@ static int flex_wants_row(unsigned int parent, const struct br_style *pst)
 		    cs.width_kind || cs.has_bg ||
 		    cs.border_w[0] || cs.border_w[1] ||
 		    cs.border_w[2] || cs.border_w[3] ||
-		    nd->tag == BR_TAG_IMG)
+		    nd->tag == BR_TAG_IMG || nd->tag == BR_TAG_SVG)
 			return 1;
 	}
 	return 0;
@@ -8681,6 +8747,19 @@ static void layout_block(struct lay *l, unsigned int node,
 				l->y = flow_y + block_h[parent] - st->bottom -
 				      style_height_px(st, old_right - old_left);
 		}
+		/* `left: 100%` parks a box (Docusaurus' "Skip to main
+		 * content" link) past the containing block's right edge
+		 * until it is focused.  An auto width then has no room: it
+		 * used to collapse to 8 px and wrap one word per line into a
+		 * tall column painted over the page.  Lay it out at the
+		 * containing block's width so it stays one line and
+		 * overflows (off-screen) like in a real browser. */
+		if (def_w < 0 && l->right - l->left < 8) {
+			int avail = st->pos == 2 ? page_rect.w - 12 :
+				    flow_right - flow_left;
+
+			l->right = l->left + (avail > 8 ? avail : 8);
+		}
 		old_left = l->left;
 		old_right = l->right;
 	} else if (relative) {
@@ -9258,7 +9337,15 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			} else if (nd->height_attr && iw && ih) {
 				line_w += iw * (int)nd->height_attr / ih;
 			} else if (cs.height_kind && iw && ih) {
-				int hh = style_height_px(&cs, base);
+				/* A percentage height needs the parent's
+				 * definite height (the logo's 2rem box), as in
+				 * add_image_run(); otherwise it is auto. */
+				int hh = cs.height_kind == 1 ?
+					 style_height_px(&cs, base) :
+					 cs.height_kind == 2 &&
+					 pst->height_kind == 1 ?
+					 (int)pst->height * (int)cs.height / 100 :
+					 0;
 
 				line_w += hh > 0 ? iw * hh / ih : iw;
 			} else if (iw) {
@@ -9386,10 +9473,23 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 		if (nd->tag == BR_TAG_A && nd->href_off)
 			lk = (unsigned short)n;
 		right = cs.float_left == 2;
-		if (cs.width_kind == 2 || cs.width_kind == 3) {
-			colw = style_width_px(&cs, row_right - row_left);
-		} else if (cs.width_kind == 1) {
-			colw = (int)cs.width;
+		if (cs.width_kind == 1 || cs.width_kind == 2 ||
+		    cs.width_kind == 3) {
+			/* The column is the margin box: `width` is the
+			 * content (or, with border-box, the border) box,
+			 * and layout_block() takes margins, padding and
+			 * borders back out of the column.  A 200px float
+			 * with margin-right:20px used to get 180px of
+			 * content. */
+			int base = row_right - row_left;
+
+			colw = cs.width_kind == 1 ? (int)cs.width :
+			       style_width_px(&cs, base);
+			colw += mp_px(&cs, 3, base) + mp_px(&cs, 1, base);
+			if (!cs.box_sizing)
+				colw += mp_px(&cs, 7, base) +
+					mp_px(&cs, 5, base) +
+					cs.border_w[1] + cs.border_w[3];
 		} else {
 			int base = row_right - row_left;
 
@@ -9611,8 +9711,19 @@ static int inline_block_flows(unsigned int node, const struct br_style *cs,
 
 		if (nd->type != BR_NODE_ELEM)
 			continue;
-		if (nd->tag == BR_TAG_IMG || nd->tag == BR_TAG_SVG ||
-		    br_tag_is_block(nd->tag))
+		if (nd->tag == BR_TAG_SVG) {
+			/* An icon box (Docusaurus' admonition heading:
+			 * `<span inline-block><svg/></span>Note`): the SVG
+			 * is an inline replaced run, so the box can stay in
+			 * the text line. */
+			br_style_for(&doc, c, cs, &ch);
+			if (ch.display == BR_DISPLAY_NONE ||
+			    ch.display == BR_DISPLAY_INLINE ||
+			    ch.display == BR_DISPLAY_INLINE_BLOCK)
+				continue;
+			return 0;
+		}
+		if (nd->tag == BR_TAG_IMG || br_tag_is_block(nd->tag))
 			return 0;
 		br_style_for(&doc, c, cs, &ch);
 		if (ch.display == BR_DISPLAY_NONE)
@@ -9722,6 +9833,43 @@ struct flex_item {
 	int w;
 };
 
+/* An item's min-width/max-width as outer (margin-box) widths in the flex
+ * row, or -1 when unset.  Percentages and calc() resolve against the
+ * container width. */
+static int flex_limit_w(const struct br_style *cs, int which_max, int avail)
+{
+	unsigned char kind = which_max ? cs->max_width_kind :
+			     cs->min_width_kind;
+	int w;
+
+	if (kind == 3)
+		w = which_max ?
+		    avail * cs->max_width_calc_pct / 100 +
+		    cs->max_width_calc_px :
+		    avail * cs->min_width_calc_pct / 100 +
+		    cs->min_width_calc_px;
+	else
+		w = css_size(kind, which_max ? cs->max_width : cs->min_width,
+			     avail);
+	if (w < 0)
+		return -1;
+	if (!cs->box_sizing)
+		w += mp_px(cs, 7, avail) + mp_px(cs, 5, avail) +
+		     cs->border_w[1] + cs->border_w[3];
+	return w + mp_px(cs, 3, avail) + mp_px(cs, 1, avail);
+}
+
+static int flex_clamp_w(const struct br_style *cs, int w, int avail)
+{
+	int mx = flex_limit_w(cs, 1, avail), mn = flex_limit_w(cs, 0, avail);
+
+	if (mx >= 0 && w > mx)
+		w = mx;
+	if (mn >= 0 && w < mn)
+		w = mn;
+	return w;
+}
+
 static void layout_flex_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 unsigned short link)
@@ -9729,6 +9877,7 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	struct flex_item it[32];
 	unsigned int c;
 	int nitem = 0, grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
+	int grow_need = 0;      /* growing items at their content width */
 	int row_left = l->left, row_right = l->right;
 	int avail = row_right - row_left;
 	int idx, row_top;
@@ -9765,31 +9914,74 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 		it[nitem].link = (nd->tag == BR_TAG_A && nd->href_off) ?
 				 (unsigned short)c : link;
 		it[nitem].w = 0;
-		if (cs.flex_grow && !cs.width_kind)
+		if (cs.flex_grow && !cs.width_kind) {
 			grow_count++;
-		else
-			fixed += flex_item_width(l, c, &cs, avail);
+			grow_need += flex_item_width(l, c, &cs, avail);
+		} else
+			fixed += flex_clamp_w(&cs,
+					      flex_item_width(l, c, &cs, avail),
+					      avail);
 		nitem++;
 	}
 	/* A narrow container (an icon row sized to its content, Google's
 	 * mic + lens buttons) is still a row when its items fit; only a
 	 * narrow one that would overflow or squeeze growing items falls
 	 * back to block flow. */
-	if (avail < 160 && (grow_count || fixed > avail)) {
+	if (avail < 160 && fixed + grow_need > avail) {
 		layout_children(l, parent, pst, depth, link);
 		return;
 	}
 	grow_w = grow_count ? (avail - fixed) / grow_count : 0;
-	if (grow_w < 96)
+	/* A growing item capped by max-width (Docusaurus' doc column,
+	 * `max-width: 75%`; its main container, `calc(100% - 300px)`)
+	 * keeps its cap and leaves the rest of the free space to the other
+	 * growing items. */
+	if (grow_count) {
+		int pass, capped[32] = { 0 };
+
+		for (pass = 0; pass < nitem; pass++) {
+			int changed = 0;
+
+			for (idx = 0; idx < nitem; idx++) {
+				struct br_style cs;
+				int lim;
+
+				if (capped[idx])
+					continue;
+				br_style_for(&doc, it[idx].node, pst, &cs);
+				if (!cs.flex_grow || cs.width_kind)
+					continue;
+				lim = flex_clamp_w(&cs, grow_w, avail);
+				if (lim == grow_w)
+					continue;
+				capped[idx] = lim + 1;
+				fixed += lim;
+				grow_count--;
+				changed = 1;
+			}
+			if (!changed || !grow_count)
+				break;
+			grow_w = (avail - fixed) / grow_count;
+		}
+		for (idx = 0; idx < nitem; idx++)
+			it[idx].w = capped[idx] ? capped[idx] - 1 : 0;
+	}
+	if (grow_w < 96 && avail >= 160)
 		grow_w = 96;
 	for (idx = 0; idx < nitem; idx++) {
 		struct br_style cs;
 
+		if (it[idx].w)
+			continue;       /* capped growing item */
 		br_style_for(&doc, it[idx].node, pst, &cs);
 		if (cs.flex_grow && !cs.width_kind)
 			it[idx].w = grow_w;
 		else
-			it[idx].w = flex_item_width(l, it[idx].node, &cs, avail);
+			it[idx].w = flex_clamp_w(&cs,
+						 flex_item_width(l,
+								 it[idx].node,
+								 &cs, avail),
+						 avail);
 		if (it[idx].w > avail)
 			it[idx].w = avail;
 		if (it[idx].w < 8)
@@ -11150,6 +11342,7 @@ static int measure_page(int left, int right)
 	l->depth = 0;
 	l->nruns = 0;
 	l->space_pending = 0;
+	base = root_style();
 	layout_children(l, doc.root, &base, 0, 0);
 	return l->y + 8;
 }
@@ -11210,6 +11403,7 @@ static void render_page(int left, int right, int y0)
 	l->space_pending = 0;
 	nhits = 0;
 	ndeferred = 0;
+	base = root_style();
 	layout_children(l, doc.root, &base, 0, 0);
 	sort_deferred();
 	{
@@ -11236,10 +11430,32 @@ static void render_page(int left, int right, int y0)
 			if (st.pos == 2 && block_h[deferred[i].node]) {
 				struct ngui_rect clip;
 				int ch = block_h[deferred[i].node] + 24;
-
-				clip.x = page_rect.x;
+				int pr = page_rect.x + page_rect.w - 12;
+				int x0 = page_rect.x, x1 = pr;
+
+				/* The background pre-fill spans the box's own
+				 * columns, not the whole page: Docusaurus parks
+				 * a white "Skip to main content" link at
+				 * `left: 100%` with a z-index above the navbar,
+				 * and a full-width fill painted over the bar. */
+				if (st.left_set)
+					x0 = page_rect.x + st.left;
+				if (st.width_kind == 1) {
+					if (st.right_set && !st.left_set)
+						x0 = pr - st.right - st.width;
+					x1 = x0 + st.width;
+				} else if (st.right_set) {
+					x1 = pr - st.right;
+				}
+				if (x0 >= pr || x1 <= page_rect.x)
+					continue;       /* entirely off-screen */
+				if (x0 < page_rect.x)
+					x0 = page_rect.x;
+				if (x1 > pr)
+					x1 = pr;
+				clip.x = x0;
 				clip.y = page_rect.y;
-				clip.w = page_rect.w - 12;
+				clip.w = x1 - x0;
 				if (ch > page_rect.h)
 					ch = page_rect.h;
 				clip.h = ch;
@@ -11708,16 +11924,35 @@ static void draw_page(struct ngui_rect page)
 		unsigned int body = doc.nnode > 1 ?
 			br_find_tag(&doc, BR_TAG_BODY) : 0;
 		struct br_style bs;
+		int bg_found = 0;
 
 		if (root) {
 			style_for_node(root - 1, &bs);
-			if (bs.has_bg)
+			if (bs.has_bg) {
 				page_bg = bs.bg;
+				bg_found = 1;
+			}
 		}
 		if (body) {
 			style_for_node(body - 1, &bs);
-			if (bs.has_bg)
+			if (bs.has_bg) {
 				page_bg = bs.bg;
+				bg_found = 1;
+			}
+		}
+		/* No canvas colour, but the author made the text dark
+		 * (Infima's light theme: `--ifm-background-color:
+		 * transparent` on html, #1c1e21 text): the page expects the
+		 * white canvas of a normal browser, and dark text on the
+		 * dark default canvas is unreadable. */
+		if (!bg_found && (body || root) && bs.color != ui.theme.text) {
+			unsigned int c = bs.color;
+			unsigned int lum = ((c >> 11) & 31) * 8 * 299 +
+					   ((c >> 5) & 63) * 4 * 587 +
+					   (c & 31) * 8 * 114;
+
+			if (lum < 100 * 1000)
+				page_bg = 0xFFFF;
 		}
 	}
 	if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&

Разница между файлами не показана из-за своего большого размера
+ 762 - 21
user/apps/browser/browser_html.c


+ 17 - 0
user/apps/browser/browser_html.h

@@ -20,6 +20,9 @@
 #define BR_FACE_FAMILY   40
 #define BR_FACE_URL      192
 #define BR_MAX_VARS      512
+#define BR_MAX_LAYERS    48
+#define BR_RULE_USES_SCOPED    1
+#define BR_RULE_DEFINES_SCOPED 2
 #define BR_MAX_CONTENT   512
 #define BR_TAG_MAX       128
 #define BR_RULE_HASH     256
@@ -272,6 +275,13 @@ struct br_rule {
 	unsigned char nd;
 	unsigned char single;       /* one compound, no ancestor walk needed */
 	unsigned char pe;           /* 0 element, 1 ::before, 2 ::after */
+	unsigned char layer;        /* 0 unlayered, else @layer order (1-based) */
+	/* BR_RULE_USES_SCOPED / BR_RULE_DEFINES_SCOPED: the block uses or
+	 * defines a custom property whose value depends on the element (see
+	 * br_style_for); raw_off/raw_len keep the unexpanded block. */
+	unsigned char var_scope;
+	unsigned short raw_len;
+	unsigned int raw_off;
 	struct br_decl d[BR_DECLS];
 };
 
@@ -308,6 +318,10 @@ struct br_doc {
 	unsigned char var_flags[BR_MAX_VARS];
 	unsigned short var_slot[1024];
 	unsigned int nvars;
+	/* @layer names (hash of the full dotted name) in order of first
+	 * appearance; a rule's `layer` indexes this table + 1. */
+	unsigned int layer_hash[BR_MAX_LAYERS];
+	unsigned int nlayers;
 	/* Rule index: rightmost class/id/tag -> linked list of rule indices. */
 	unsigned short rule_next[BR_MAX_RULES];
 	unsigned short rule_bucket[BR_RULE_HASH];
@@ -343,6 +357,9 @@ 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_set_viewport(unsigned int width);
+/* A media query list (`screen and (min-width: 600px)`) against the
+ * viewport set above; window.matchMedia() uses the same answers as CSS. */
+int  br_media_matches(const char *query);
 void br_set_theme(unsigned short text, unsigned short bg,
 		  unsigned short panel, unsigned short link);
 void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);

+ 1 - 1
user/apps/browser/js_browser.c

@@ -3151,7 +3151,7 @@ static void win_matchmedia(js_State *state)
 	const char *query = js_tostring(state, 1);
 
 	js_newobject(state);
-	js_pushboolean(state, 0);
+	js_pushboolean(state, br_media_matches(query));
 	js_setproperty(state, -2, "matches");
 	js_pushstring(state, query);
 	js_setproperty(state, -2, "media");

Некоторые файлы не были показаны из-за большого количества измененных файлов